Lampione
Lampione

Reputation: 1608

Change values (colors) of TextView with a button click

So I'm trying to make this EditText to go from a textColor to another when I hit that specific button (for instance: button1 change the textcolor to BLUE, button2 change it to GREEN etc.)

 <EditText
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/test"
    android:id="@+id/test"
    android:layout_alignParentTop="true"
    android:layout_alignParentLeft="true"
    android:layout_alignParentStart="true"
    android:textColor="@color/orange_test"
    android:layout_toLeftOf="@+id/colorBtn"
    android:layout_alignBottom="@+id/colorBtn"
    android:textSize="25sp"
    android:gravity="center_vertical|center_horizontal" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtn"
    android:id="@+id/colorBtn"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"/>

How should I have to proceed? :\

Upvotes: 0

Views: 839

Answers (2)

Demigod
Demigod

Reputation: 5635

You mean something like this? -

button1.setOnClickListener(new OnClickListener() {
                        @Override
                        public void onClick(View v) {
                            editText1.setTextColor(Color.GREEN);
                        }
                    }););

Upvotes: 0

Phant&#244;maxx
Phant&#244;maxx

Reputation: 38098

Duplicate your button to set more than one color

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtn"
    android:id="@+id/colorBtn"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnRed"
    android:id="@+id/colorBtnRed"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnGreen"
    android:id="@+id/colorBtnGreen"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/colorBtnBlue"
    android:id="@+id/colorBtnBlue"
    android:layout_alignParentTop="true"
    android:layout_alignParentRight="true"
    android:layout_alignParentEnd="true"
    android:onClick="changeColor"
/>

In your changeColor, check the button id. If it is the colorButtonBlue id, then set the text color of your EditText to blue. If it is colorButtonRed, ther set to red abd so on...

public void changeColor(View v)
{
    switch(v.getId())
    {
        case R.id.colorBtnRed:
            edtTest.setTextColor(Color.RED);
            break;
        case R.id.colorBtnGreen:
            edtTest.setTextColor(Color.GREEN);
            break;
        case R.id.colorBtnBlue:
            edtTest.setTextColor(Color.BLUE);
            break;
        default:
            edtTest.setTextColor(Color.BLACK);
    }
}

[EDIT]

Of course, in your Declaration section, do this:

EditText edtTest = null;

And in your onCreate do something like this (after the super and setContentVieew lines):

edtTest = (EditText) findViewById(R.id.test);

Upvotes: 1

Related Questions