10

I am creating button dynamically in linearlayout horizontalscrollview and on click i get selected button position.

I want to know how to change text color of selected button?

Here is my code.

String[] categories = {"SUN","MON", "TUS", "WED", "THU", "FRI", "SAT", "SUN","MON", "TUS", "WED", "THU", "FRI", "SAT"};
private LinearLayout ll;
Button btn;

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    ll = (LinearLayout) findViewById(R.id.hsvLinearLayout);

    for(int i = 0; i < categories.length; i++) {
        btn = new Button(this);
        btn.setText(categories[i]);
        btn.setBackgroundColor(Color.parseColor("#ffffff"));
        btn.setOnClickListener(buttonClick);
        ll.addView(btn);
        int idx = ll.indexOfChild(btn);
        btn.setTag(Integer.toString(idx));
       // btn.setId(idx);
    }
}

OnClickListener buttonClick = new OnClickListener() {
    public void onClick(View v) {
        String idxStr = Integer.toString(ll.indexOfChild(v));
        //(String)v.getTag();
        Toast.makeText(MainActivity.this, idxStr, 6000).show();
    }
};
user3555472
  • 836
  • 3
  • 11
  • 38
  • Are you OK with a solution that utilizes xml defined styles that you apply programmatically or did you need everything in the java code? – PaulR Jan 17 '15 at 17:58

6 Answers6

22

check the type and assign the text color

 OnClickListener buttonClick = new OnClickListener() {
        public void onClick(View v) {
            String idxStr = Integer.toString(ll.indexOfChild(v));
            if(v instanceof Button){
               ((Button)v).setTextColor(Color.parseColor("#000000"));
            }
            Toast.makeText(MainActivity.this, idxStr, 6000).show();
        }
    };
SorryForMyEnglish
  • 1,181
  • 7
  • 14
  • How can i again remove color from button which are not selected ? I just want to keep button text color changed for those which is selected at a moment. – Jay Rathod Nov 24 '16 at 07:28
  • 1
    Thanks You! `.setTextColor(Color.parseColor("#000000"));` does the trick. Note that `.setTextColor(R.color.colorname);` is not working at the moment (01/2019). – Kerim Yagmurcu Jan 20 '19 at 17:17
4

try this

Edited Answer

 ((Button)view).setTextColor(Color.parseColor("#000000"));
Moubeen Farooq Khan
  • 2,875
  • 1
  • 11
  • 26
3

please check the following answer here and here .

as you can see you can do it programmatically and through xml by creating a style file for all of the states of the button .

Hope that helps

Community
  • 1
  • 1
  • Thanks for adding styles to the answer. Using them makes the code much more maintainable. Also, great reuse of the existing answers. – PaulR Jan 20 '15 at 17:00
2

This works:

button.setTextColor(getColor(R.color.blue))
Damrad
  • 46
  • 1
  • 4
1

I just check all already posted solutions. No one works.

They also produce error like this

btnjava.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setTextColor(int)' on a null object reference

Real Solution :

Step-1: When you try to change setTextColor then always use try/catch, to prevent app from Crash.

Step-2: No matter you define your Button already, define(like R.id.btnId) again before setTextColor code line.

public class MainActivity extends AppCompatActivity {
        Button btn;

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

            btn=findViewById(R.id.btnId);
            btn.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    // use try/catch for handle any kind of error
                    try {
                        Button btnForTextColorChange= (Button) findViewById(R.id.btnId);
                        // must define Button again before setTexColor code line
                        btnForTextColorChange.setTextColor(getResources().getColor(R.color.white));
                    } catch (Exception e){
                        Log.e(TAG, "Error:"+e);
                    }
                    
                }
            });
    }

[sorry for bad english]

Happy Coding :)

AG-Developer
  • 361
  • 2
  • 10
1

This worked for me:

btnItem.setTextColor(ContextCompat.getColor(context, R.color.black))
Manish kumar
  • 113
  • 7