Reputation: 11
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" />
Upvotes: 0
Views: 1546
Reputation: 33
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;
}
}
Upvotes: 1
Reputation: 11130
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
Upvotes: 0
Reputation: 1048
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.
Upvotes: 0
Reputation: 376
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
Upvotes: 0
Reputation: 3219
From:
view.setBackgroundResource(R.color.red);
To:
view.setBackgroundColor(Color.RED);
Upvotes: 0