Reputation: 336
Hello again I have another question regarding my hello world app I want to change the background when A button is pressed so I did this:
public void onclick01(View View)
{
View.setBackgroundColor(Color.GREEN);
}
But that changes the background colour of the button and not the whole app.
Edit
I have two more questions.
1) How would I set
View.setBackgroundColor(Color.GREEN);
to something like:
View.setBackgroundColor(Color.RANDOM);
2) How would I do the same to change the text colour? something like:
View.setTextColor(Color.GREEN);?
Upvotes: 1
Views: 2074
Reputation: 3261
Use Selector
you will get button press action.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/button_background" android:state_pressed="false"/>
<!-- default -->
<item android:drawable="@drawable/button_pressed" android:state_pressed="true"/>
<!-- pressed -->
</selector>
Upvotes: 0
Reputation: 6715
You should have to use xml for this situation
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_pressed="true">
<shape android:shape="rectangle">
<solid android:color="yourColor"/>
</shape>
</item>
<item>
<shape android:shape="rectangle">
<solid android:color="yourColor"/>
</shape>
</item>
</selector>
Upvotes: 0
Reputation: 4567
If you want to set through xml, then you need to do as below:
android:background="@android:color/green"
in case if you decide to use android's default color code or if you have colors specified in colors.xml, then use
android:background="@colors/green"
If you want to do programmatically, then do:
LinearLayout linearlayout=(LinearLayout) findViewById(R.layout.yourlayout);
linearlayout.setBackgroundColor(Color.GREEN);
Upvotes: 0
Reputation: 3480
public void setActivityBackgroundColor(int color) {
view = this.getWindow().getDecorView();
view.setBackgroundColor(color);
}
Then call it from your OnClickListener passing in whatever color you want.
Upvotes: 0
Reputation: 38439
main_act.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<Button
android:id="@+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button" />
</LinearLayout>
activity
public class MainActivity extends Activity {
/** Called when the activity is first created. */
Button b1;
LinearLayout layout;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main_act);
layout=(LinearLayout)findViewById(R.id.layout);
blueButton=(Button)findViewById(R.id.b1);
b1.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// TODO Auto-generated method stub
layout.setBackgroundColor(Color.BLUE);
}
});
}
}
Upvotes: 1
Reputation: 9836
Here View reference your Button's View. So you need to create object for parent layout an then
layout.setBackgroundColor(Color.GREEN);
Upvotes: 0