-3

This is my TextView

<TextView
    android:id="@+id/home_text"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingLeft="10dp"
    android:text="Home"
    android:textColor="@drawable/color_state"/>

This is my color_state.xml code in drawable folder

<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
    <item
        android:state_pressed="false"
        android:color="#fff"/>
    <item
        android:state_pressed="true"
        android:state_selected="true"
        android:color="#ff0000"/>
</selector>

I have searched similar questions but what I want is a different thing, all the questions which stack suggested "possible duplicate of", I already tried but that is not what I want, that's why I asked this question with all the explanation that I need.Existing question only changes the color when TexttView is clicked I have two TextView in a LinearLayout, I just want to change the color of my text when I click on it, with this code I can change color but when I release click the color goes back to the original.All I want is to change the color when I click on TextView and the change should be permanent until another view is not clicked Please any help will be appreciated

Pokemon
  • 3
  • 3
  • You will have to do that through code to maintain the state of the text color. Once you have selected any other view, manually reset the others. – Kapil G Aug 02 '17 at 05:09
  • 2
    Possible duplicate of [Change clickable TextView's color on focus and click?](https://stackoverflow.com/questions/5371719/change-clickable-textviews-color-on-focus-and-click) – AskNilesh Aug 02 '17 at 05:10
  • Possible duplicate of [android TextView : Change Text Color on click](https://stackoverflow.com/questions/4468380/android-textview-change-text-color-on-click) – Mahesh Gawhane Aug 02 '17 at 06:09

2 Answers2

0

If you want to make the textview color change permanently when clicked and back to normal when another textview clicked. You need to set on java code like this.

TextView home = (TextView) findViewById(R.id.home_text);
TextView another = (TextView) findViewById(R.id.another_text);
home.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            home.setTextColor(Color.GREEN);
            another.setTextColor(Color.BLACK);
        }
    });
another.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            home.setTextColor(Color.BLACK);
            another.setTextColor(Color.RED);
        }
    });
Iwan
  • 25
  • 7
0

You can make use of Enum class to easily define the colors wanted, and this way, you'll be able to increase the amount of supported colors without too much changes in your code.

Enum class

public enum ColorFactory {

    BLACK(R.id.textView1, R.color.black),
    WHITE(R.id.textView2, R.color.white),
    BLUE(R.id.textView2, R.color.blue),
    RED(R.id.textView2, R.color.red),
    CYAN(R.id.textView2, R.color.cyan),
    GREEN(R.id.textView2, R.color.green),
    GREY(R.id.textView2, R.color.grey),
    YELLOW(R.id.textView2, R.color.yellow),
    PINK(R.id.textView2, R.color.pink),
    PURPLE(R.id.textView2, R.color.purple),
    BROWN(R.id.textView2, R.color.brown),
    CUSTOM(R.id.textView2, R.color.custom);

    int id;
    int color;

    ColorFactory(int id, int color) {
        this.id = id;
        this.color = color;
    }

    public int getId() {
        return id;
    }

    public int getColor() {
        return color;
    }


    public static int getColorById(int id) {
        for (ColorFactory currentEnumElement : values()) {
            if (currentEnumElement.getId() == id) {
                return currentEnumElement.getColor();
            }
        }
        return -1;
    }
}

And you'll have to have those colors defined in your colors.xml file as following

Colors.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <color name="colorPrimary">#3F51B5</color>
    <color name="colorPrimaryDark">#303F9F</color>
    <color name="colorAccent">#FF4081</color>
    <color name="black">#000000</color>
    <color name="white">#ffffff</color>
    <color name="blue">#0000ff</color>
    <color name="red">#ff0000</color>
    <color name="cyan">#00f2ff</color>
    <color name="green">#00ff00</color>
    <color name="grey">#8b8378</color>
    <color name="yellow">#ffd700</color>
    <color name="pink">#e8a4a3</color>
    <color name="purple">#b72bef</color>
    <color name="brown">#a7823e</color>
    <color name="custom">#990000</color>
</resources>

And to the final, the code to use it...(simplified)

Code

public class MainActivity extends AppCompatActivity implements View.OnClickListener{

    TextView textView1;
    TextView textView2;
    .
    .
    .

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

        textView1 = (TextView) findViewById(R.id.textView1);
        textView2 = . . . .
        .
        .
        .
        .

    }

    @Override
    public void onClick(View view) {
        switch (view.getId()) {
            case R.id.textView1:
                textView1.setTextColor(ColorFactory.getColorById(view.getId()));
                break;
            case R.id.textView2:
                .
                .
                .
        }
    }
}

It should allow you to set the colors as a result of a click. NOTE: you can also use ButterKnife to bind the views to improve the readability of your code and so on.. Hope it helps.

BNAndroid
  • 158
  • 4