0

I write simple ListView but I have problem, I want to marquee text when I select some item from my listview but it not working I don't know why I think I write code correctly when I copy my code from listView.setOnItemSelectedListener(new OnItemSelectedListener() to listView.setOnItemClickListener(new OnItemClickListener() that's work correctly when I click, but I want this only for select. That's my code :

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_settings);
    List<HashMap<String, String>> aList = new ArrayList<HashMap<String, String>>();
    for (int i = 0; i < 7; ++i) {
        HashMap<String, String> hm = new HashMap<String, String>();
        hm.put("firstLine", menu[i]);
        hm.put("secondLine", submenu[i]);
        hm.put("icon", Integer.toString(ikons[i]));
        aList.add(hm);
    }
    String[] from = { "icon", "firstLine", "secondLine" };
    int[] to = { R.id.icon, R.id.firstLine, R.id.secondLine };
    SimpleAdapter adapter = new SimpleAdapter(getBaseContext(), aList,
            R.layout.alarm_row, from, to);
    ListView listView = (ListView) findViewById(R.id.list);
    listView.setAdapter(adapter);

    listView.setOnItemSelectedListener(new OnItemSelectedListener() {

        @Override
        public void onItemSelected(AdapterView<?> arg0, View view,
                int arg2, long arg3) {
            TextView t = (TextView) view.findViewById(R.id.secondLine);
            t.setSelected(true);
        }

        @Override
        public void onNothingSelected(AdapterView<?> arg0) {
            // TODO Auto-generated method stub

        }
    });

    listView.setOnItemClickListener(new OnItemClickListener() {

        @Override
        public void onItemClick(AdapterView<?> arg0, View view, int arg2,
                long arg3) {
            // TODO Auto-generated method stub
            /*
             * TextView t = (TextView) view.findViewById(R.id.secondLine);
             * t.setFocusable(true); t.setSelected(true);
             */
        }
    });
}

XML :

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/panel1"
    android:layout_width="fill_parent"
    android:layout_height="?android:attr/listPreferredItemHeight"
    android:background="@drawable/listview_bg"
    android:padding="6dip" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"
        android:layout_alignBottom="@+id/secondLine"
        android:layout_alignParentTop="true"
        android:scaleType="center"
        android:src="@drawable/alarm_icon" />

    <TextView
        android:id="@+id/secondLine"
        android:layout_width="fill_parent"
        android:layout_height="26dip"
        android:layout_alignParentBottom="true"
        android:layout_alignParentRight="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Alarm" />

    <TextView
        android:id="@+id/firstLine"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_above="@id/secondLine"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:layout_alignWithParentIfMissing="true"
        android:layout_toRightOf="@id/icon"
        android:ellipsize="marquee"
        android:marqueeRepeatLimit="marquee_forever"
        android:paddingLeft="15dip"
        android:paddingRight="15dip"
        android:scrollHorizontally="true"
        android:singleLine="true"
        android:text="Ustawienia Alarmu" />

</RelativeLayout>

I think that should work I don't have any idea what is wrong, I will be very gratefull for any help.

pkruk
  • 809
  • 1
  • 7
  • 24

2 Answers2

1

Try adding

t.requestFocus();

after

t.setSelected(true);

in onItemSelected

Vrashabh Irde
  • 14,129
  • 6
  • 51
  • 103
  • still not working :/ I don't know what is wrong because everything looks fine for me... – pkruk Jul 22 '13 at 16:23
  • Take a look at the answers here- http://stackoverflow.com/questions/1827751/is-there-a-way-to-make-ellipsize-marquee-always-scroll, particularly last one I think that suits your list view requirement – Vrashabh Irde Jul 22 '13 at 16:46
  • any answer from this topic didn't help me I'm still in dead point :/ I don't know what is wrong :/ – pkruk Jul 23 '13 at 08:33
0

you should define a customized adapter, which extends e.g. BaseAdapter or which adapter you like and then add t.setSelected(true) in the getView method of that:

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

   TextView t = (TextView) view.findViewById(R.id.secondLine);
   text2.setSelected(true);
   return view;
}
Parissa Kalaee
  • 606
  • 1
  • 9
  • 17