0

I'm trying to change the background color on activity with a listview in android studio, but nothing happens when i click on the items in the listview. Can someone help me?

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));

    view = this.getWindow().getDecorView();
    view.setBackgroundResource(R.color.white);
}


public void onListItemClick(ListView parent, View v, char position, long id) {
    String s = label.getText().toString();
    double l = Double.parseDouble(s);
    double o;
    switch (position) {
        case 0:
            view.setBackgroundResource(R.color.red);
            break;
        case 1:
            view.setBackgroundResource(R.color.green);
            break;
        case 2:
            view.setBackgroundResource(R.color.white);
            break;
        case 3:
            view.setBackgroundResource(R.color.orange);
            break;

    }
}

activity_main.xml

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />
Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
wanjari
  • 11
  • 3

5 Answers5

1

Update your activity_main.xml as below

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/background_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:background="@drawable/background_screen"
    android:focusable="true"
    android:focusableInTouchMode="true"
    android:orientation="vertical">

<ListView
    android:id="@android:id/list"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content" />

</LinearLayout>

And cast layout in your activity and apply background colour to the layout.This will change the background colour of the screen

private LinearLayout mLayout=null;

     @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        setListAdapter(new ArrayAdapter<String>(this, R.layout.row,R.id.label,items));
        mLayout = (LinearLayout) findViewById(R.id.background_layout);
        view = this.getWindow().getDecorView();
        view.setBackgroundResource(R.color.white);
    }


    public void onListItemClick(ListView parent, View v, char position, long id) {
        String s = label.getText().toString();
        double l = Double.parseDouble(s);
        double o;
        switch (position) {
            case 0:
                mLayout .setBackgroundColor(Color.RED);
                break;
            case 1:
                 mLayout .setBackgroundColor(Color.GREEN);
                break;
            case 2:
                  mLayout.setBackgroundColor(Color.WHITE);
                break;
            case 3:
                 mLayout.setBackgroundColor(Color.parseColor("#FFA500"));
                break;
        }
    }
0

From:

view.setBackgroundResource(R.color.red);

To:

view.setBackgroundColor(Color.RED);
Anjal Saneen
  • 3,109
  • 23
  • 38
0

The recommended way to do this is that in your project files you go to app/res/values and open colors.xml In there add colors like this : <color name="darkGreen">#669900</color>

Then in your mainActivity.java write
Case0: view.setBackgroundColor(getResources().getColor(R.color.darkGreen)); break;

If you need to find the hexidecimals for the colors you want you can use a website color picker like this one : https://www.w3schools.com/colors/colors_picker.asp

Faris Kapo
  • 346
  • 9
  • 30
0

Within your XML, provide the parent layout with an android:ID attribute and wire it in your java class. Then onClick change the colour of the layout and it should change the background colour of the parent layout.

Nero
  • 1,058
  • 1
  • 9
  • 25
0

This should work

first add id to your layout in activity_main layout like

 <LinearLayout
       android:id="@+id/LinearLayout1"
   ........

Then in your Activity

 final LinearLayout layout = (LinearLayout) findViewById(R.id.LinearLayout1);

   public void onListItemClick(ListView parent, View v, char position, long id) {        
         if(iscolor)
         {
             layout.setBackgroundColor(Color.BLUE);
             iscolor = false;
         }
         else
         {
             layout.setBackgroundColor(Color.WHITE);
             iscolor = true;
         }

        }

Choose whatever layout type you're using

Rainmaker
  • 10,294
  • 9
  • 54
  • 89
  • That would work if i just wanted the background color on the selected item to change in the listview, but i'm trying to change the background color on the activity – wanjari Nov 23 '17 at 15:57
  • why do you use position of items then if you want just change the background color of layout and not the row? – Rainmaker Nov 23 '17 at 16:15
  • because i tried to follow an example from my teacher, and i'm not good in java... it's an error on "iscolor", how do i fix that? – wanjari Nov 23 '17 at 16:16
  • you first make a boolean( Boolean iscolor;) before onCreate and then set it to true in your onCreate.Then change colors as you wish (your teacher gave you example to change row color it seems to me) – Rainmaker Nov 23 '17 at 16:20
  • by the way , in this answer you can do what you want just from layout .See the accepted answer but fist try the one i gave you https://stackoverflow.com/questions/5789661/android-relativelayout-change-color-onclick – Rainmaker Nov 23 '17 at 16:22