Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android change text color of button programmatically

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();
    }
};
like image 463
user3555472 Avatar asked Jan 17 '15 17:01

user3555472


People also ask

How do I change text color in programmatically?

Android TextView – Text Color. TextView Text Color – To change the color of text in TextView, you can set the color in layout XML file using textColor attribute or change the color dynamically in Kotlin file using setTextColor() method.

How can I change background color of clicking button in Android?

Inside the function use setBackgroundResource(R. color. button_color) function, this will set the background with color button_color.


2 Answers

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 :)

like image 111
AG-Developer Avatar answered Oct 23 '22 14:10

AG-Developer


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();
        }
    };
like image 45
SorryForMyEnglish Avatar answered Oct 23 '22 14:10

SorryForMyEnglish