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="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical"> <Spinner android:id="@+id/spinner" android:layout_width="match_parent" android:layout_height="wrap_content" > <span style="line-height: 1.5;"></LinearLayout></span>
Step3: Create and write following into raw.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:orientation="vertical" tools:context=".MainActivity"> <TextView android:id="@+id/text1" android:layout_width="wrap_content" android:layout_height="wrap_content"> <TextView android:id="@+id/text2" android:layout_width="match_parent" android:layout_height="wrap_content" android:singleLine="true" android:ellipsize="marquee" android:marqueeRepeatLimit ="marquee_forever" android:textStyle="bold"> </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<MyObject> myArrayList = new ArrayList<MyObject>(); myArrayList.add(new MyObject(0, "Lollipop 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0 5.0")); myArrayList.add(new MyObject(1, "Kitkat 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4 4.4")); myArrayList.add(new MyObject(2, "Jellybean 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3 4.3")); MyAdapter myAdapter = new MyAdapter(this, myArrayList); spinner.setAdapter(myAdapter); spinner.setOnItemSelectedListener(new OnItemSelectedListener(){ @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { MyObject clickedObj = (MyObject)parent.getItemAtPosition(position); Toast.makeText(MainActivity.this, "Clicked item:n" + clickedObj.getNumber() + ": " + clickedObj.getName(), Toast.LENGTH_LONG).show(); } @Override public void onNothingSelected(AdapterView<?> parent) { }}); } private class MyAdapter extends BaseAdapter { private ArrayList<MyObject> myList; private Activity parentActivity; private LayoutInflater inflater; public MyAdapter(Activity parent, ArrayList<MyObject> 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:
Checkout my last post published on SlidingPaneLayout Example
Top 10 Android App Development Trends | 2020 Guide
5 Best Resources to Get Started with Android Nougat
Android Studio Introduction
Services – An Android Component
Applying MediaCodec On An Open Source Android Audio Player
5 Most Used Android Testing Frameworks
Android Language Highlights A Developers Perspective
Android KitKat Development