I am currently setting up 3 textviews that go into a LinearLayout(code below). However the last color that is assigned using the setColor method only seems to apply to the first textview and in fact overwrites the color that i had initially set for it.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
TextView view1 = new TextView(this);
view1.setText("I am view one");
view1.setTextColor(ColorStateList.valueOf(Color.RED));
view1.setTextSize(25);
view1.setGravity(Gravity.CENTER);
TextView view2 = new TextView(this);
view2.setText("I am view two");
view1.setTextColor(ColorStateList.valueOf(Color.BLUE));
view2.setTextSize(30);
view2.setGravity(Gravity.CENTER);
TextView view3 = new TextView(this);
view3.setText("I am view three");
view1.setTextColor(ColorStateList.valueOf(Color.YELLOW));
view3.setTextSize(50);
view3.setGravity(Gravity.CENTER);
LinearLayout myLinearLayout = new LinearLayout(this);
myLinearLayout.setOrientation(LinearLayout.VERTICAL);
myLinearLayout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
myLinearLayout.setGravity(Gravity.CENTER);
myLinearLayout.addView(view1);
myLinearLayout.addView(view2);
myLinearLayout.addView(view3);
setContentView(myLinearLayout);
In this case view1 is yellow and the other views are grey…red and blue aren’t being applied to the correct views. As a note i initially had just tried to use setColor and as you can see i was trying to use ColorStateList per the documentation but the same results apply. Anyhow, how would i go about forcing the color i want on the different textviews? I know this should be easy, perhaps i am just missing something here. thanks all in advance.
Note the objects you are setting the colors on – it seems like there’s a whole bunch of copy-paste left-overs. Every color assignment is done on
view1, whereas what you really want is to set the color in the second assignment toview2, and in the third toview3:By the way, simple calling
.setColor(Color.RED)(or a different color) on the views should suffice.