Jan 28, 2014

Implement auto scroll marquee TextView in ListView

This example implement auto scroll TextView in custom ListView to "List System Properties of Android system".

Implement auto scroll marquee TextView in ListView
Implement auto scroll marquee TextView in ListView
Reference:
To make a TextView auto-run, if the text is longer than display area, add the line to <TextView> in layout file:
        android:singleLine="true"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit ="marquee_forever"


And call textview.setSelected(true) in Java code.

MainActivity.java
package com.example.androidcustomlistview;

import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Properties;

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

public class MainActivity extends Activity {

 private class SystemProperties{
  private String propertiesName;
  private String properties;
  
  SystemProperties(String pName, String p){
   propertiesName = pName;
   properties = p; 
  }
  
  public String getPropertiesName(){
   return propertiesName;
  }
  
  public String getpPoperties(){
   return properties;
  }
 } 

 @Override
 protected void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);
  ListView listView = (ListView)findViewById(R.id.listview);
  
  //Init ArrayList of MyObject
  ArrayList<SystemProperties> myPropertyList = new ArrayList<SystemProperties>();
  
  //get System Properties
  Properties properties = System.getProperties();
  Enumeration<String> propEnum = 
                (Enumeration<String>)properties.propertyNames();
         
        while(propEnum.hasMoreElements()){
            String propName = propEnum.nextElement();
            String prop = System.getProperty(propName);
            SystemProperties nexProp = new SystemProperties(propName, prop);
            myPropertyList.add(nexProp);
        }
  
  
  MyAdapter myAdapter = new MyAdapter(this, myPropertyList);
  listView.setAdapter(myAdapter);
  
  listView.setOnItemClickListener(new OnItemClickListener(){

   @Override
   public void onItemClick(AdapterView<?> parent, 
     View view, int position, long id) {
    SystemProperties clickedObj = (SystemProperties)parent.getItemAtPosition(position);
    Toast.makeText(MainActivity.this, 
      clickedObj.getPropertiesName() + ":\n" +
      clickedObj.getpPoperties(), 
      Toast.LENGTH_LONG).show();
   }});
  
 }

 private class MyAdapter extends BaseAdapter {
  
  private ArrayList<SystemProperties> myList;
  
  private Activity parentActivity;
     private LayoutInflater inflater; 
  
     public MyAdapter(Activity parent, ArrayList<SystemProperties> 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);
         SystemProperties myObj = myList.get(position);
         text1.setText(String.valueOf(myObj.getPropertiesName()));
         text2.setText(myObj.getpPoperties());
         text2.setSelected(true);
         return view;
  }
 }
}

row.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>

activity_main.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:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="android-coding.blogspot.com" />
    <ListView
        android:id="@+id/listview"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" />

</LinearLayout>

Also read: Implement auto scroll marquee TextView in Spinner

1 comment:

Infolinks In Text Ads