0

Actually I have multiple rectangular buttons when i click on button i will get list view.now what the issue when i clicked on the button i need it highlighted with red color and other all buttons must be in green. Plzz help mee.

Activity:

if(name.isEmpty()||name==null){
    billnumber  +=name;
}
else{
    if(count==3){
        billnumber  +="  , "+name;
    }
    else if(count==2){
        billnumber  +="  , "+name;
    } 
    else if(count==1){
        billnumber  +=name;
    }
}


 if (count == 3) {
    id++;
    final Button dynamicTextView = new Button(this);
    dynamicTextView.setLayoutParams(new LayoutParams(350,
            LayoutParams.MATCH_PARENT));
    dynamicTextView.setBackgroundColor(Color.parseColor("#1c7900"));
    dynamicTextView.setText("Bill Numbers\n"+billnumber);
    dynamicTextView.setId(id);
    final Button dynamicinvnumber=new Button(this);


    if(id==(clickedid-1)){
        dynamicTextView.setBackgroundColor(Color
                        .parseColor("#cf0000"));
    }
    if(istouched){
    if (id == clickedid) {
        touchedlist=dynamicTextView.getText().toString();
        if(touchedlist.contains("Bill Numbers"))
        {
            touchedlist=touchedlist.replace("Bill Numbers","");
        }
        text=touchedlist;

        if(bumpedbillnumber!="0")
        {


            if(touchedlist.contains(bumpedbillnumber))
            {
                touchedlist=touchedlist.replace(bumpedbillnumber,"");
            }
            if(text.contains(bumpedbillnumber))
            {
                text=text.replace(bumpedbillnumber,"");
            }
        }
        dynamicTextView.setBackgroundColor(Color
                        .parseColor("#cf0000"));
    }
}
if(refreshlist!=1)
{
    if(id==1)
    {
        dynamicTextView.setBackgroundColor(Color
                        .parseColor("#cf0000"));
    }
}
if (invoice1 == null) {
    if (id ==1) {
        dynamicTextView.setBackgroundColor(Color
                .parseColor("#cf0000"));
    }

}
dynamicTextView.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 30);
dynamicTextView.setOnClickListener(new OnClickListener() {

    @SuppressWarnings("deprecation")
    @Override
    public void onClick(View v)
    {           
        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, headerButton

Drawable Xml:

<corners android:radius="3dp" />
  <solid android:color="#124a01" />

<stroke
    android:width="2px"
    android:color="#c8ea32" />

Lehue
  • 425
  • 8
  • 26
mallika
  • 1
  • 6
  • Check this http://stackoverflow.com/questions/3882064/how-to-change-color-of-button-in-android-when-clicked] Button color changes when clicked – Sagar Gangawane Nov 18 '16 at 11:04
  • Just pass your clicked postionto adapter and set condilike below code if (pos == position) { mListview.setSelection(pos); mCardView.setCardBackgroundColor(getResources().getColor(android.R.color.white)); mAdapter.notifyDataSetChanged(); } else { mCardView.setCardBackgroundColor(getResources().getColor(R.color.blue_trans)); } – Vadivel Nov 18 '16 at 11:19
  • @sagar that ques is different i want clicked button being red and other to be green what ever button i clicked i want red color and remaining green – mallika Nov 18 '16 at 11:20
  • @vadivel I want to change color on cliked button not in list view buttons wont get position – mallika Nov 18 '16 at 11:23
  • Declare Array of button then assign id to every button,then get clicked position id and use for loop to check conditio – Vadivel Nov 18 '16 at 11:25
  • yaa i did like this before – mallika Nov 18 '16 at 11:30
  • 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")); } } } – mallika Nov 18 '16 at 11:30
  • and calle this method in code – mallika Nov 18 '16 at 11:30
  • suppose if i have 3 buttons and cliked frwd and if i come back to 1 or 2 then the 3 button remain in red after count id becomes 3 and it get struck at 3 button i meand 3 and other button showing red color – mallika Nov 18 '16 at 11:33

3 Answers3

0

btn_selected.xml

   <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle">
     <solid android:color="red_color"/>
 </shape>

btn_un_selected.xml

  <?xml version="1.0" encoding="utf-8"?>
  <shape xmlns:android="http://schemas.android.com/apk/res/android" 
  android:shape="rectangle">
     <solid android:color="ordinary_color"/>
 </shape>

btn_selector

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >

 <item android:drawable="@drawable/btn_selected"  android:state_selected="true"></item>

 <item android:drawable="@drawable/btn_selected"  android:state_pressed="true"/>
 <item android:drawable="@drawable/btn_un_selected"/>
</selector>

In xml change button background of button

<Button 
........
android:background="@drawable/btn_selector/>

in code change

btn.setSelected(!btn.isSelected);
noobEinstien
  • 3,147
  • 4
  • 24
  • 42
0

Instead of creating your buttons programatically, create them on xml, then retrieve their ids on your activity so you can access them; after setUp onTouchListener for them and inside those you should do this.

Define Rect rect and shouldPerformClick as global variables of your activity.

button1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getActionMasked()){
                case MotionEvent.ACTION_DOWN:
                    changeButtonBackgrounds(v.getId());
                    shouldPerformClick = true;
                    break;
                case MotionEvent.ACTION_MOVE:
                    rect = new Rect();
                    v.getDrawingRect(rect);
                    if(!(rect.left < event.getX() && event.getX() < rect.right)){
                        shouldPerformClick = false;
                        resetButtonBackgrounds();
                    }
                    if(!(rect.bottom < event.getY() && event.getY() < rect.top)){
                        shouldPerformClick = false;
                        resetButtonBackgrounds();
                    }
                    break;
                case MotionEvent.ACTION_UP:
                    resetButtonBackgrounds();
                    if(shouldPerformClick){
                        button1.performClick;
                    }
                    break;
            }
            return true;
        }
    });

I would use a switch(viewID) on the changeButtonBackgrounds(), i think my whole approach requires a more boilerplate but its more optimal.

marcos E.
  • 477
  • 4
  • 11
0

Define button array buttons which are buttons from your XML file and a global variable int selectedIndex=0.

Make sure in your XML file that all buttons are green.

selectedIndex will be the index in the array of the last selected button.

Then, on every button in the array

    for (int i = 0 ; i < buttons.length ; i++)
        buttons[i].setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                buttons[selectedIndex]
                        .setBackgroundTintList(ColorStateList.valueOf(getResources()
                                .getColor(R.color.green, getTheme())));


                switch (view.getId()) {

                    case R.id.button1:
                        buttons[0].setBackgroundTintList(ColorStateList.valueOf(getResources()
                                .getColor(R.color.red)));
                        selectedIndex = 0;
                        break;

                    case R.id.button2:
                        buttons[1].setBackgroundTintList(ColorStateList.valueOf(getResources()
                                .getColor(R.color.red)));
                        selectedIndex = 1;
                        break;

                    .
                    .
                    .
                }

            }
        });
bluesummers
  • 11,365
  • 8
  • 72
  • 108