Issue
When I click on a particular button, the button background's colour is suppose to change while the other buttons remain in its default colour. However, upon running the app, the button doesn't change its colour though the rest of the logic in the code works well. I still can't figure out what's wrong. Any help will be greatly appreciated. Thanks.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timer_test);
adult = (Button) findViewById(R.id.btnAdult);
peer = (Button) findViewById(R.id.btnPeer);
material = (Button) findViewById(R.id.btnMaterial);
physical = (Button) findViewById(R.id.btnPhysical);
//Set button disabled, when new activity is turned on
flag.setEnabled(false);
alarm = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
...
OnClickListener codes :
if (textDemo.getText().toString() == "Record") {
View.OnClickListener listener = new View.OnClickListener() {
@Override
public void onClick(View v) {
if (v.equals(physical)) {
// do something
physical.setText("PHYSICAL ON CLICK WORKS");
physicalPrompt = "Yes";
physical.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
adult.setBackgroundResource(R.drawable.button_selector);
peer.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(material)) {
// do something
material.setText("MATERIAL ON CLICK WORKS");
materials = "Yes";
material.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
peer.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
physical.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(peer)){
peer.setText("PEER ON CLICK WORKS");
peers = "Yes";
peer.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
physical.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else if (v.equals(adult)){
adult.setText("ADULT ON CLICK WORKS");
peers = "Yes";
peer.setBackgroundColor(getResources().getColor(R.color.buttonPressed));
physical.setBackgroundResource(R.drawable.button_selector);
adult.setBackgroundResource(R.drawable.button_selector);
material.setBackgroundResource(R.drawable.button_selector);
}
else {
// do something else
textDemo.setText("Error!");
}
}
};
physical.setOnClickListener(listener);
material.setOnClickListener(listener);
peer.setOnClickListener(listener);
adult.setOnClickListener(listener);
}
}
}
In value/colors.xml,
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="buttonNotPressed">#2196F3</color>
<color name="buttonPressed">#1976D2</color>
<!--<color name="colorHighlightFAB">#B6B6B6</color>-->
<color name="ColorPrimary">#E91E63</color>
</resources>
Extra Codes :
public class Timer_Test extends ActionBarActivity {
//Declare widgets
Button buttonStart, flag, back, timeButton, adult, peer, material, physical;
TextView timerTextView, tvTest, interval, child, status, id, session,
passName, tvStartTime, textDemo;
Solution
Try this solution first and let me know if its working -
private View.OnClickListener mFirstListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.red);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mSecondListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.blue);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mThirdListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.green);
((Button)v).setText("New Text");
}
};
private View.OnClickListener mFourthListener = new View.OnClickListener() {
@Override
public void onClick(View v) {
v.setBackgroundResource(R.color.light_blue);
((Button)v).setText("New Text");
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button);
Button button2 = (Button) findViewById(R.id.button2);
Button button3 = (Button) findViewById(R.id.button3);
Button button4 = (Button) findViewById(R.id.button4);
button1.setOnClickListener(mFirstListener);
button2.setOnClickListener(mSecondListener);
button3.setOnClickListener(mThirdListener);
button4.setOnClickListener(mFourthListener);
}
Recommended way (Using XML only) -
Create a file inside your drawable folder called button_states and put the following code in -
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="@color/buttonPressed" /> <!--Pressed State-->
<item android:state_focused="true" android:drawable="@color/buttonPressed" /> <!--Focused State-->
<item android:drawable="@color/grey" /> <!--Normal State-->
</selector>
Now in your xml set the button background like this -
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button"
android:padding="10dp"
**android:background="@drawable/button_states"**
android:layout_below="@+id/textView"
android:layout_toEndOf="@+id/textView"
android:layout_marginStart="42dp"/>
And then you don't need to change the colours in code at all and it will work fine for you.
If you use recommended way get rid of this line from all listeners - v.setBackgroundResource(R.color.blue);
but for text change you still need to keep the following line ((Button)v).setText("New Text");
.
Hope it helps.
Answered By - Varundroid
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.