0

I have a ListView with color names (array of strings stored in separate xml). How do I change background of my application depending on which color in the list I clicked? I have a function that displays a toast message depending on Item clicked but I don't know how to transform it into background color changing function.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
        public void onItemClick(AdapterView parent, View view, int position, long id) {

            Toast.makeText(getApplicationContext(), ((TextView) view).getText(), Toast.LENGTH_SHORT).show();

        }
    });
Yochert
  • 85
  • 9
  • possible duplicate of [Switching application-wide theme programmatically?](http://stackoverflow.com/questions/4663752/switching-application-wide-theme-programmatically) – tachyonflux Feb 19 '15 at 16:42

3 Answers3

0

Your activity has a layout.

Give it a name ( here is one named outer container. )

 <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
            android:id="@+id/outer_container"
            android:focusable="true"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">

Do a findViewById on it and then set the color on the view.

     View view = findViewById( R.id.outer_container );
     view.setBackground or view.setBackgroundColor
mjstam
  • 1,049
  • 1
  • 6
  • 5
  • The problem for me is to get value from ListView and then setting background color equal to this value – Yochert Feb 19 '15 at 16:54
0

Inside the OnItemClickListener you can change the color depending on the Text of the ListView Item.

To solve this problem, you just need to use an if-else checking the value of the TextView and then changing the color. I am assuming that you don't have color resources, so I use the ARGB values.

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        //Save the color name into a variable
        String colorName=((TextView) view).getText().toString();

        //Default is color White
        int color=Color.argb(255, 255, 255, 255);

        //Check the color name
        if(colorName.equals("black"))
        {
           color=Color.argb(255, 0, 0, 0);
        }
        else if (colorName.equals("red"))
        {
            color=Color.argb(255, 255, 0, 0);
        }
        //...and so on with other colors

        //Find the background view using the layout id. Then you will be able to change the background with the color
        findViewById(R.id.id_of_the_layout).setBackgroundColor(color);

    }
});
programmer23
  • 533
  • 4
  • 15
  • I actually have two arrays, one with names of colors and other with their hex value. How do I put them both together? – Yochert Feb 19 '15 at 17:01
  • If they are stored in the same order (for instance, Array1=[Black,Red...] and Array2=["#000000","#FF0000"...] then you need to do this: color=Color.parseColor(Array2[position]), so using this way even you won't need the if-else – programmer23 Feb 20 '15 at 08:36
0

ListViews has an adapter, a customized one or a standard like ArrayAdapter. Something like:

ArrayAdapter<String> itemsAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, items);

You can also create a custom adapter to store complex objects extending BaseAdapter. In your case, it might be enough with String. You can store hexadecimal codes for each different item in the list.

To get items from the adapter use getItem(int position) method.

String colorCode = itemsAdapter.getItem(position);

So, in your itemClickListener:

lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    public void onItemClick(AdapterView parent, View view, int position, long id) {
        String colorCode = itemsAdapter.getItem(position);
        setBackgroundColor(colorCode);
    }
});

Your set backgroundColor method will use a reference to the parent container that you must store at startup.

View parentContainer;

...
// at onCreate
parentContainer = findViewById(R.id.container_id);
...

void setBackgroundColor(String colorCode) {
    int color = Color.parseColor(colorCode);
    parentContainer.setBackgroundColor(color);
}
Juanjo Vega
  • 1,410
  • 1
  • 12
  • 20