Reputation: 1
I am making an app with 3 buttons.
I have the following .xml file that has rectangular button.
Now I want to to make them so if one is selected then that button turns red while the other two remain green.
On forward action it was working properly: if i click c button and come back and click A button both are showing same red color and if i click B and after A button then also showing same color after some delay it is appearing .
How can I solve this?
XML:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<corners android:radius="3dp" />
<solid android:color="#124a01" />
<stroke
android:width="2px"
android:color="#c8ea32" />
</shape>
Activity:
public void onClick(View v) {
DeselectButtons();
EnabledButton = dynamicTextView.getId();
clickedid=dynamicTextView.getId();
dynamicTextView.setBackgroundColor(Color
.parseColor("#cf0000"));
dynamicTextView.setSelected(true);
invoiceToDisplay = null;
invoiceToDisplay = new ArrayList<String>();
text = dynamicTextView.getText().toString();
if(text.contains("Bill Numbers"))
{
text=text.replace("Bill Numbers","");
}
String s[] = text.split(" , ");
invoice = text.split(" , ");
System.out.println("s" + s[0]);
istouched=true;
refreshlist=1;
if (s.length == 1) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
tv1.setVisibility(View.GONE);
tv2.setVisibility(View.GONE);
tv.setVisibility(View.VISIBLE);
footerText3.setVisibility(View.GONE);
footerText2.setVisibility(View.GONE);
loadListViews(invoiceselected, listView1, headerButton1);
invoice1 = invoiceselected;
invoice2 = "";
invoice3 = "";
headerButton2.setText("");
headerButton3.setText("");
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
listView2.setAdapter(adapter);
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
//adapter = new CustomAdapter(getBaseContext(),itemsList2,PendingOrdersActitvity.this);
listView3.setAdapter(adapter);
invnumber = "";
} else if (s.length == 2) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
int invoice12=receiptlist.indexOf(s[1].trim());
String invoiceselected1=invoiceList.get(invoice12);
invoice1 = invoiceselected;
invoice2 = invoiceselected1;
invoice3 = "";
loadListViews(invoiceselected, listView1, headerButton1);
loadListViews(invoiceselected1, listView2, headerButton2);
adapter = new CustomAdapter(
PendingOrdersActitvity.this, itemsList2);
listView3.setAdapter(adapter);
headerButton3.setText(""); footerText3.setVisibility(View.GONE); tv2.setVisibility(View.GONE);
invnumber = "";
} else if (s.length == 3) {
if(s[0].contains("\n"))
{
s[0]=s[0].replace("\n","");
}
int invoice11=receiptlist.indexOf(s[0].trim());
String invoiceselected=invoiceList.get(invoice11);
int invoice12=receiptlist.indexOf(s[1].trim());
String invoiceselected1=invoiceList.get(invoice12);
int invoice13=receiptlist.indexOf(s[2].trim());
String invoiceselected2=invoiceList.get(invoice13);
tv.setVisibility(View.VISIBLE);
tv1.setVisibility(View.VISIBLE);
tv2.setVisibility(View.VISIBLE);
invoice1 = invoiceselected;
invoice2 = invoiceselected1;
invoice3 = invoiceselected2;
loadListViews(invoiceselected, listView1, headerButton1);
loadListViews(invoiceselected1, listView2, headerButton2);
loadListViews(invoiceselected2, listView3, headerButton3);
invnumber = "";
} //thread.start();
}
});
Button button = new Button(this);
button.setLayoutParams(new LayoutParams(10,
LayoutParams.MATCH_PARENT));
linear.addView(dynamicTextView);
linear.addView(button);
count = 0;
invoiceNumber = "";
// invoice=null;
billnumber = "";
}
}
}
Method:
public void DeselectButtons() {
for (int i = 1; i < id; i++) {
if (EnabledButton != i) {
if (this.findViewById(i) == null) {
} else {
this.findViewById(i).setBackgroundColor(
Color.parseColor("#1c7900"));
}
} else {
this.findViewById(i).setBackgroundColor(
Color.parseColor("#1c7900"));
}
}
}
Upvotes: 0
Views: 445
Reputation: 1872
Initially keep all the buttons green.Keep track of previous clicked button.
int previousbutton = 0;
then in onclick:
public void onClick(View v){
if(previousbutton != 0){
findViewById(previousbutton).setBackgroundColor(//set green color);
}
v.setBackgroundColor(//set red color);
previousbutton = v.getId();
}
Upvotes: 0
Reputation: 3903
Take three RadioButtons
and put them inside a RadioGroup
and make them look like a button by setting a selector as background. So at a time only one will be selected and get highlighted.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="@drawable/btn_radio_off" />
<item android:state_checked="true" android:drawable="@drawable/btn_radio_on" />
</selector>
This way you will not need to handle the change and color manually.
Upvotes: 2