Syeda Naqvi
Syeda Naqvi

Reputation: 49

Change Button color and its Text Color in android

My activity consist of 10 buttons each given a unique background color. When a button is pressed the background of activity should change according to the color of pressed button. And the color of clicked button should change to white and it's text color should change to black. I have accomplished the task till change of background color of activity. I can't figure out how to change button color and its text color using View v. As I don't know which button will be pressed so I can't use the button id directly. Here is the code.

Java File:

    @Override

    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    btn_red=(Button)findViewById(R.id.red_btn);
    btn_pink=(Button)findViewById(R.id.pink_btn);
    btn_blue=(Button)findViewById(R.id.blue_btn);
    btn_sblue=(Button)findViewById(R.id.sblue_btn);
    btn_orange=(Button)findViewById(R.id.orng_btn);
    btn_purple=(Button)findViewById(R.id.purple_btn);
    btn_green=(Button)findViewById(R.id.grn_btn);
    btn_yellow=(Button)findViewById(R.id.yellow_btn);
    btn_brown=(Button)findViewById(R.id.brown_btn);
    btn_black=(Button)findViewById(R.id.black_btn);
    rl=(RelativeLayout)findViewById(R.id.rel_layout);

    btn_red.setOnClickListener(this);
    btn_pink.setOnClickListener(this);
    btn_blue.setOnClickListener(this);
    btn_sblue.setOnClickListener(this);
    btn_orange.setOnClickListener(this);
    btn_purple.setOnClickListener(this);
    btn_green.setOnClickListener(this);
    btn_yellow.setOnClickListener(this);
    btn_brown.setOnClickListener(this);
    btn_black.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
    // Change Background Color
    btn_clr=(ColorDrawable)v.getBackground();
    rl.setBackground(btn_clr);

    // Change Button Color
    v.setBackgroundColor(Color.WHITE);

    //Change Text Color
    //Text Color should change to black


    }
    }

XML File:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.bsse.asiya.colorchanger.MainActivity"
android:id="@+id/rel_layout">


<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Red"
    android:id="@+id/red_btn"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:layout_marginLeft="48dp"
    android:layout_marginStart="48dp"
    android:layout_marginTop="40dp"
    android:textColor="@color/white"
    android:background="@color/red"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Pink"
    android:id="@+id/pink_btn"
    android:layout_alignBottom="@+id/red_btn"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:layout_marginRight="40dp"
    android:layout_marginEnd="40dp"
    android:textColor="@color/white"
    android:background="@color/pink"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Blue"
    android:id="@+id/blue_btn"
    android:layout_below="@+id/red_btn"
    android:layout_alignRight="@+id/red_btn"
    android:layout_alignEnd="@+id/red_btn"
    android:layout_marginTop="40dp"
    android:textColor="@color/white"
    android:background="@color/blue"/>

<Button
    style="?android:attr/buttonStyleSmall"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Sky Blue"
    android:id="@+id/sblue_btn"
    android:layout_above="@+id/orng_btn"
    android:layout_alignLeft="@+id/pink_btn"
    android:layout_alignStart="@+id/pink_btn"
    android:textColor="@color/white"
    android:background="@color/sky_blue"
    android:layout_alignRight="@+id/pink_btn"
    android:layout_alignEnd="@+id/pink_btn" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Orange"
    android:id="@+id/orng_btn"
    android:layout_below="@+id/blue_btn"
    android:layout_alignLeft="@+id/blue_btn"
    android:layout_alignStart="@+id/blue_btn"
    android:layout_marginTop="40dp"
    android:textColor="@color/white"
    android:background="@color/orange"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Purple"
    android:id="@+id/purple_btn"
    android:layout_alignBottom="@+id/orng_btn"
    android:layout_alignRight="@+id/pink_btn"
    android:layout_alignEnd="@+id/pink_btn"
    android:textColor="@color/white"
    android:background="@color/purple"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Green"
    android:id="@+id/grn_btn"
    android:layout_below="@+id/orng_btn"
    android:layout_alignLeft="@+id/orng_btn"
    android:layout_alignStart="@+id/orng_btn"
    android:layout_marginTop="41dp"
    android:textColor="@color/white"
    android:background="@color/green"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Yellow"
    android:id="@+id/yellow_btn"
    android:layout_alignTop="@+id/grn_btn"
    android:layout_alignLeft="@+id/purple_btn"
    android:layout_alignStart="@+id/purple_btn"
    android:textColor="@color/white"
    android:background="@color/yellow"
    android:outlineProvider="bounds"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Brown"
    android:id="@+id/brown_btn"
    android:layout_alignParentBottom="true"
    android:layout_alignLeft="@+id/grn_btn"
    android:layout_alignStart="@+id/grn_btn"
    android:layout_marginBottom="41dp"
    android:textColor="@color/white"
    android:background="@color/brown"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Black"
    android:id="@+id/black_btn"
    android:layout_alignTop="@+id/brown_btn"
    android:layout_alignLeft="@+id/yellow_btn"
    android:layout_alignStart="@+id/yellow_btn"
    android:textColor="@color/white"
    android:background="@color/black"/>
</RelativeLayout>

Upvotes: 3

Views: 8388

Answers (2)

bitbybit
bitbybit

Reputation: 162

If changing the background color, button text color and background is all you need, there is no need to keep so many (10!) references in your code to every button. Simply add onClick attribute to all your buttons in xml android:onClick="changeColors". And declare changeColors method in your activity.

public void changeColors(View view) {
    // this method is invoked with the parameter "view" which is the button that was clicked
    // change activity background to view(button)'s current color etc.
    ((Button)view).setBackgroundColor(Color.WHITE);
    ((Button)view).setTextColor(Color.BLACK);
}

The correct view(button) is passed as the parameter so you don't have to worry about updating the wrong button.

Upvotes: 1

gus27
gus27

Reputation: 2666

What about this?

@Override
public void onClick(View v) {
    ...
    if (v instanceof Button) {
        ((Button) v).setBackgroundColor(Color.WHITE);
        ((Button) v).setTextColor(Color.BLACK);
    }
}

Upvotes: 4

Related Questions