-1

I have the following layout:

<!-- GROUPED BUTTONS -->
        <LinearLayout
            android:id="@+id/group_button_layout"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_alignParentBottom="true"
            android:gravity="center"
            android:layout_centerHorizontal="true">

            <Button
                android:id="@+id/group_button_week"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/white"
                android:background="@color/app_blue"
                android:textSize="12sp"
                android:text="Week"/>

            <Button
                android:id="@+id/group_button_month"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:textSize="12sp"
                android:padding="0dp"
                android:text="Month"/>

            <Button
                android:id="@+id/group_button_quarter"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:background="@color/white"
                android:textSize="12sp"
                android:text="Quarter"/>

            <Button
                android:id="@+id/group_button_half_year"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="@color/app_blue"
                android:background="@color/white"
                android:textSize="12sp"
                android:text="Half Year"/>
        </LinearLayout>
        <!-- //GROUPED BUTTONS -->

I would like to change style (background and text color) to clicked button and all other buttons (except clicked one) set the default (unclicked) style.

How can i do it in the right way please?

Many thanks for any advice.

redrom
  • 11,502
  • 31
  • 157
  • 264
  • You should use setTextAppearance() to change the style of your views, was that your question? – Nanoc Nov 24 '15 at 09:23
  • Take a look at this: [android button selector](http://stackoverflow.com/a/14024007/3110234). Play with some properties. and you are done. – activesince93 Nov 24 '15 at 09:28

2 Answers2

1

OnClickListener:

       final Button b1= (Button) findViewById(R.id.group_button_week);
       final Button b2= (Button) findViewById(R.id.group_button_month);
       final Button b3= (Button) findViewById(R.id.group_button_quarter);
       final Button b4= (Button) findViewById(R.id.group_button_half_year);

        View.OnClickListener listener= new View.OnClickListener() {
            @Override
            public void onClick(View v) {

                Button[] buttons=new Button[]{b1,b2,b3,b4};

                for (Button b:
                  buttons   ) {

                    if(b.equals(v)) // the clicked one
                    {
                        b.setBackgroundResource(android.R.color.darker_gray);
                        b.setTextColor(Color.WHITE);
                    }
                    else  // the others
                    {
                        b.setBackgroundResource(android.R.color.white);
                        b.setTextColor(Color.RED);
                    }
                }
            }
        };
        b1.setOnClickListener(listener);
        b2.setOnClickListener(listener);
        b3.setOnClickListener(listener);
        b4.setOnClickListener(listener);
csenga
  • 3,819
  • 1
  • 21
  • 31
0

Use setOnTouchListener on your button and check for ACTION_DOWN,ACTION_CANCEL,ACTION_UP MotionEvent

button1.setOnTouchListener(new OnTouchListener() {

        @Override
        public boolean onTouch(View v, MotionEvent event) {

            // when you touch button for click 
            if(event.getAction() == MotionEvent.ACTION_DOWN){
                 button.setBackgroundResource(R.color.onClickColor);   
                 text.setBackgroundResource(R.color.onClickColor);
                 // set background of button and text       
            }
            // when you remove touch from button 
            if(event.getAction() == MotionEvent.ACTION_CANCEL){
                button1.setBackgroundResource(R.color.onClickColor);  
                text.setBackgroundResource(R.color.onClickColor);
                // set background of button and text    
            }
            // when you release touch from button 
            if(event.getAction() == MotionEvent.ACTION_UP){
                button1.setBackgroundResource(R.color.onClickColor);  
                text.setBackgroundResource(R.color.onClickColor);
                // set background of button and text    
            }
            return true;
        }
    });
Iamat8
  • 3,888
  • 9
  • 25
  • 35