raghu_3
raghu_3

Reputation: 1909

Giving an Element in a List a different Color

I am using a List as a sidebar and launching different activites in Main Window depending on the List Item. Now how can i change the color of a particular list item in layout or in my code .SO whenever i load an new activity i want a particular list item to be of different color. I cannot use Event Click here to trigger it.

This is my code -

protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.layout6);
        listView = (ListView) findViewById(R.id.listView);

        String[] values = getResources().getStringArray(R.array.menuList);

        ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
                android.R.layout.simple_list_item_1, android.R.id.text1, values);


        // Assign adapter to ListView
        listView.setAdapter(adapter);

        listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                                    int position, long id) {

                // ListView Clicked item index
                int itemPosition     = position;

                // ListView Clicked item value
                String  itemValue    = (String) listView.getItemAtPosition(position);


                // Show Alert

                switch( position )
                {
                    case 0:  Intent newActivity = new Intent(Activity6.this,MainActivity.class);
                        startActivity(newActivity);
                        break;

                    case 1:  Intent newActivity1 = new Intent(Activity6.this,Activity2.class);
                        startActivity(newActivity1);
                        break;
                    case 2:  Intent newActivity2 = new Intent(Activity6.this,Activity3.class);
                        startActivity(newActivity2);
                        break;
                    case 3:  Intent newActivity3 = new Intent(Activity6.this,Activity4.class);
                        startActivity(newActivity3);
                        break;
                    case 4:  Intent newActivity4 = new Intent(Activity6.this,Activity5.class);
                        startActivity(newActivity4);
                        break;

                }
                Toast.makeText(getApplicationContext(),
                        "Position :" + itemPosition + "  ListItem : " + itemValue, Toast.LENGTH_LONG)
                        .show();

            }

        });

    }

Upvotes: 2

Views: 63

Answers (2)

Pulkit Sethi
Pulkit Sethi

Reputation: 1325

On your item click listener save the position and in your getview function check if the position is this one. Then set the background color to a different color. You also need to call notifydatasetchanged at the end of onItemClick

Upvotes: 1

Majid Daeinejad
Majid Daeinejad

Reputation: 1037

You have to create a CustomAdapter and customize your row_item on getView overrided method. No way in xml file. creating a CustomAdapter depends on how you set the adapater for your listview. paste the part of your code that you are setting the adapter.

Upvotes: 1

Related Questions