Search code examples
androidxmltextviewhorizontalscrollview

android:state_activated for textviews in horizontalscrollview


I have a horizontalscrollview containing many textviews that are added dynamically. When I press on a textview, I would like its background to stay blue, until I click on another textview. In a listview, you would use android:state_activated, but Android wont let me use it for a textview. I have also tried setting the background in the onclicklistener, but it didnt work either.

How could I do this?

activity

public class HorizontaltextscrollerActivity extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout menu = (LinearLayout) findViewById(R.id.menu);

        for (int i = 0; i < 20; i++) {
            TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
            t.setId(i);
            t.setText("item" + i);
            t.setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
                    v.setBackgroundColor(R.color.background_selected);
                }
            });

            menu.addView(t);
        }

    }
}

main.xml

<?xml version="1.0" encoding="utf-8"?>
<HorizontalScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:background="#000000" >

    <LinearLayout
        android:id="@+id/menu"
        android:layout_width="wrap_content"
        android:layout_height="fill_parent"

        android:orientation="horizontal" >
    </LinearLayout>

</HorizontalScrollView>

<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:background="@drawable/text_selector"
    android:textSize="24sp"
    android:padding="30dp"
    android:clickable="true" >

</TextView>

text_selector.xml

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">

    <item android:drawable="@color/selected" android:state_pressed="true"/>

</selector>

EDIT1

public class HorizontaltextscrollerActivity extends Activity {
    TextView[] t = new TextView[20];

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        LinearLayout menu = (LinearLayout) findViewById(R.id.menu);

        for (int i = 0; i < 20; i++) {
            t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);

            t[i].setId(i);
            t[i].setText("item" + i);
            t[i].setOnClickListener(new OnClickListener() {

                @Override
                public void onClick(View v) {
                    Toast.makeText(getApplicationContext(), "hi " + v.getId(), Toast.LENGTH_SHORT).show();
                    t[v.getId()].setBackgroundColor(R.color.background_selected);
                }
            });

            menu.addView(t[i]);
        }
    }
}

Solution

  • create an array of TextViews...

      TextView[] t=new TextView[20];
    

    instead of this..

     TextView t = (TextView) getLayoutInflater().inflate(R.layout.text, null);
    

    use

     t[i] = (TextView) getLayoutInflater().inflate(R.layout.text, null);
      t[i].setId(i);
     //similarly..
      t[i].setOnClickListener(new OnClickListener() {
         for(int m=0;m<20;m++){
       if(m!=i){
        t[m].setBackgroundColor(R.color.putdefaultcolor here)
        }
    
         }
        t[i].setBackgroundColor(R.color.background_selected);
    
     .....
    

    I think then it will work fine..