AutoScroll TextView With Spinner

AutoScroll TextView can be added to Spinner, ListView or any other component as required or as needed. This tutorial will allow us to scroll text inside a TextView in a spinner.

Generally, textview will marque the text only when it is focused. By using the below code, the textview will automatically scroll even without focus to textview with using Spinner.

Check out my previous post on AutoScroll Text View here. Follow the steps below to get this working with a spinner:

Step1: Create a new android project in your android IDE.

Step2: Write following code into main layout:

 

<LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
android:orientation=&quot;vertical&quot;>
<Spinner
android:id=&quot;@+id/spinner&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot; >
<span style="line-height: 1.5;"></LinearLayout></span>

 

Step3: Create and write following into raw.xml:

 

<LinearLayout xmlns:android=&quot;http://schemas.android.com/apk/res/android&quot;
xmlns:tools=&quot;http://schemas.android.com/tools&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;match_parent&quot;
android:paddingBottom=&quot;@dimen/activity_vertical_margin&quot;
android:paddingLeft=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingRight=&quot;@dimen/activity_horizontal_margin&quot;
android:paddingTop=&quot;@dimen/activity_vertical_margin&quot;
android:orientation=&quot;vertical&quot;
tools:context=&quot;.MainActivity&quot;>

<TextView
android:id=&quot;@+id/text1&quot;
android:layout_width=&quot;wrap_content&quot;
android:layout_height=&quot;wrap_content&quot;>
<TextView
android:id=&quot;@+id/text2&quot;
android:layout_width=&quot;match_parent&quot;
android:layout_height=&quot;wrap_content&quot;
android:singleLine=&quot;true&quot;
android:ellipsize=&quot;marquee&quot;
android:marqueeRepeatLimit =&quot;marquee_forever&quot;
android:textStyle=&quot;bold&quot>

</LinearLayout>

 

Step4: Write following into main activity file:

 

package com.example.autoscrolltextwithspinner;

import java.util.ArrayList;

import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.BaseAdapter;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class MainActivity extends Activity {

private class MyObject {
private int number;
private String name;

MyObject(int num, String nam) {
number = num;
name = nam;
}

public int getNumber() {
return number;
}

public String getName() {
return name;
}
}

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner = (Spinner) findViewById(R.id.spinner);

// Init ArrayList of MyObject
ArrayList&lt;MyObject&gt; myArrayList = new ArrayList&lt;MyObject&gt;();
myArrayList.add(new MyObject(0, &quot;Lollipop 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0&quot;));
myArrayList.add(new MyObject(1, &quot;Kitkat 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4&quot;));
myArrayList.add(new MyObject(2, &quot;Jellybean 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3&quot;));

MyAdapter myAdapter = new MyAdapter(this, myArrayList);
spinner.setAdapter(myAdapter);

spinner.setOnItemSelectedListener(new OnItemSelectedListener(){

@Override
public void onItemSelected(AdapterView&lt;?&gt; parent, View view,
int position, long id) {
MyObject clickedObj = (MyObject)parent.getItemAtPosition(position);
Toast.makeText(MainActivity.this,
&quot;Clicked item:n&quot; +
clickedObj.getNumber() + &quot;: &quot; +
clickedObj.getName(),
Toast.LENGTH_LONG).show();
}

@Override
public void onNothingSelected(AdapterView&lt;?&gt; parent) {
}});

}

private class MyAdapter extends BaseAdapter {

private ArrayList&lt;MyObject&gt; myList;

private Activity parentActivity;
private LayoutInflater inflater;

public MyAdapter(Activity parent, ArrayList&lt;MyObject&gt; l) {
parentActivity = parent;
myList = l;
inflater = (LayoutInflater) parentActivity
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}

@Override
public int getCount() {
return myList.size();
}

@Override
public Object getItem(int position) {
return myList.get(position);
}

@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
if (convertView == null)
view = inflater.inflate(R.layout.row, null);

TextView text1 = (TextView) view.findViewById(R.id.text1);
TextView text2 = (TextView) view.findViewById(R.id.text2);
MyObject myObj = myList.get(position);
text1.setText(String.valueOf(myObj.getNumber()));
text2.setText(myObj.getName());
text2.setSelected(true);
return view;
}

}
}

 

 

Step5: Run for the output below:

AutoScrollTextWithSpinner1

 

 

AutoScrollTextWithSpinner12

Checkout my last post published on SlidingPaneLayout Example

Leave a Comment: