Textview with background color - android

I am new to android. I got stuck while doing an application.I have taken 5 textviews with time slots,I would like to show that when I click one text view it should change its background color and when I click another textview the first text view's background color has to disappear and present text view's color has to be highlight.
Here I am posting my code:
public void onClick(View v) {
switch (v.getId()){
case R.id.time_slot_one:
setTimeSlotOne.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
case R.id.time_slot_two:
setTimeSlotTwo.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
case R.id.time_slot_three:
setTimeSlotThree.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
case R.id.time_slot_four:
setTimeSlotFour.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
case R.id.time_slot_five:
setTimeSlotFive.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
}

A simple way would be to create a disable Background function that will be called each time you click a new one.
public void removeBackgroundColors() {
setTimeSlotOne.setBackgroundColor(Color.WHITE);
setTimeSlotTwo.setBackgroundColor(Color.WHITE);
setTimeSlotThree.setBackgroundColor(Color.WHITE);
setTimeSlotFour.setBackgroundColor(Color.WHITE);
setTimeSlotFive.setBackgroundColor(Color.WHITE);
}
now simply change your code to:
case R.id.time_slot_two:
removeBackgroundColors();
setTimeSlotTwo.setBackgroundColor(Color.parseColor("#bdbdbd"));
break;
...
Even simpler is to call it before your case statement, depending on what actions you want to take.

Use if else to setBackground color, for example ; if onclick of firsttextview set its color and on click of second change firsttextview color and set seconds color

You have to change the color of all Textviews.
Example t1 is selectable change t1's color as selected and other textviews as unselected,so on for rest of the textviews

It would be better if you use radio group which handles all clicks and selection and highlighting if you have many textviews.
Please check below url.
Url
In that link use answer by Sanjeet Ajnabee. It is excellent. I have been using it.

Related

Changing background colour for the Selected Tab

This is my Screen layout, and i want to do this, when I clicked on Popular tab, then Popular block should turns Blue.
Same for the Price, Time and Duration tab.
I've tried to do with Linear Layout but it doesn't.
i have taken it in Text View, and the vertical and Horizontal Lines are used using View tag and I have not use TabLayout, just to make it simple I just used TextView With Background.
Please suggest me a proper solution with code if you can.
Any help would be Appreciated.
In your xml file add following line in your TabLayout,
app:tabBackground="#0000FA"
by this you can chage color of selected tab.
If you have used TextView then just simply create or add this method to you java file & call it from each textviews onclick event by passing the textview to it.
public void changeTabColor(TextView tvSelected){
tv1.setBackgroundColor(Color.White);
tv2.setBackgroundColor(Color.White);
tv3.setBackgroundColor(Color.White);
tvSelected.setBackgroundColor(Color.Blue);
}
try this :
public void onClick(View v) {
switch (v.getId()) {
case R.id.popular_tab:
ResetTabColor();
popular_tab.setBackgroundColor(Color.blue);
case //Do the rest with other tab
}
}
private void ResetTabColor(){
popular_tab.setBackgroundColor(Color.TRANSPARENT); // or white color
time_tab.setBackgroundColor(Color.TRANSPARENT);
duration_tab.setBackgroundColor(Color.TRANSPARENT);
price_tab.setBackgroundColor(Color.TRANSPARENT);

GridLayout get buttons to fit area

I am designing a game for android. The code already works, but I am having some issues with the UI side of things. All the Layout types are confusing and never seem to have their attributes helpfully defined. Essentially, I am looking to make my buttons, that are being added programmatically, fill out the entire space at the bottom of the screen cleanly. There could be between 2 and 6 buttons, depending on the difficulty setting the player chooses. But ultimately, I would even just settle for a layout that fits all 6 buttons evenly here. I am not exactly sure why this does not fill out the whole area indicated by the .xml designer. I have tried to provide the relevant parts below, but happy to add additional details on request.
This is the initial XML for the grid layout
<GridLayout
android:layout_width="match_parent"
android:layout_height="182dp"
android:rowCount="2"
android:columnCount="3"
android:id="#+id/buttonsLayout"
android:layout_gravity="center"></GridLayout>
And setting up the buttons here:
private static void setButtons() {
for (int i = 0; i < GlobalValues.numberOfColors; i++) {
Button b = new Button(mainActivity);
ImageView IV = new ImageView(mainActivity);
switch (i) {
case 0:
IV.setImageResource(R.drawable.red);
b.setId(i);
break;
case 1:
IV.setImageResource(R.drawable.green);
b.setId(i);
break;
case 2:
IV.setImageResource(R.drawable.blue);
b.setId(i);
break;
case 3:
IV.setImageResource(R.drawable.yellow);
b.setId(i);
break;
case 4:
IV.setImageResource(R.drawable.purple);
b.setId(i);
break;
case 5:
IV.setImageResource(R.drawable.cyan);
b.setId(i);
break;
default:
break;
}
b.setBackgroundDrawable(IV.getDrawable());
b.setOnClickListener(new Button.OnClickListener() {
public void onClick(View view) {
UI.UpdateColor(view);
}
});
buttonsLayout.addView(b);
}
}
And finally, here is what it currently looks like. It is clearly not making use of the whole area very well, and I am not sure why.
Align Buttons in GridLayout might help you. I know it's not exactly your case, but you stated that for now it's good enough to solve it for 6 buttons, so this might work. BTW if I were you I would have different layouts depending on the number of buttons to make them nice. But starting with 6 is the way.

Change background of all buttons

I would like to change the background of all the Buttons in a View.
android:background="#drawable/button_red"
And i would like to do this in an OnClick() event.
android:onClick="ChangeCouleur"
I would like to do this in a foreach loop, but i am not sure how to do this.
For example:
for( b in ... )
if (b.getid()!=idofthebutton)
b.setbackgroud(button_red)
Thanks for any help!
Put all your Buttons in an Array of Buttons, then cycle to it an change the background.
Button button1 = (Button)this.findViewById(...);
Button button2 = (Button)this.findViewById(...);
Button button3 = (Button)this.findViewById(...);
Button[] buttons={button1, button2, button3};
for (Button currentButton : buttons) {
currentButton.setBackgroundResource(R.drawable.my_new_background);
}
One thing you could do is subclass Button, then make all the buttons in you app instances of your new class. That way, if you decide that you want to adjust the color or change something else, you only have to do it once, and it will change all the buttons in your app. Here's a question that should give you some pointers on how to do that.
For changing the background of the clicked button
public void changeColor(View v) {
v.setBackground(btn_red);
}
You will have to get a reference to all the Buttons. Add them to an ArrayList or something similar. In your ChangeCouleur method use a loop to iterate through all the buttons changing the color of each one.
Create .XML file in your “res/drawable/” and use selector attribute in it.And use different images for the button.Please refer this link.
http://www.mkyong.com/android/android-imagebutton-selector-example/
This will clear your idea. :)

How can I change the name of buttons on click of a button?

In my calculator app, I want an Inverse button, which when clicked changes the text of other buttons. Like sin to sin inverse etc. Is this possible?
You can just change the text of a button in that button onclick event.
Say For example
Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(btn.getText().toString().trim().equals("sin")){
btn.setText("sin inverse");
}else if(btn.getText().toString().trim().equals("sin inverse")){
btn.setText("sin");
}
}
});
I think this will help you
While the other answers are completely right showing you to how rename the buttons, I suggest another solution with a cleaner design: Have different buttons for "sin" and "sin inverse", and just make them visible/invisible when clicking the "Inverse" button. That way you can write clean click handlers and don't have to use a lot of "if (isInverseMode()...)".
To make that work correctly, you just declare some additional buttons for the inverse operations in your XML layout file and set them to android:visibility="gone".
If you then set one the visible buttons to invisible and the next insivible button besides it to visible in the code, then the effect for the user looks like you exchanged one button by the other (so he only notices the text of the button changing).
It's possible.
Just re-set the button text in your onClick method for the button.
buttonId.setText("Your button text");
From what you're saying though it sounds like you want to change the buttons function as well as the text... in which case you need to put an if statement in your onClick method to handle the two button states.
Setting the text on a button would help anytime.
This code may help you..
final Button btn = (Button)findViewById(R.id.button1);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
btn.setText(new StringBuilder(btn.getText().toString().trim()).reverse());
}
});

Android onClick(View v) not working?

I'm new to Android. I'm stuck at a point and would really appreciate it if anyone could please help me. I'm developing an app which has a grid of colored rectangles. These are created by changing the background colors of a number of TextViews. There are 3 Buttons which cause the background color to change according to some algorithm. There are also 2 TextViews which show the current status of the game.
Now the problem is I have a button example (one of the three buttons) which is supposed to change the background color of the rectangles.
example.setOnClickListener(new Button.OnClickListener() {
public void onClick(View v) {
status_val.setText("true board-example working");
level_1_true();
}
});
The level_1_true() method sets the background color of the rectangles. The above code results in an "activity not responding" dialog being shown, and the OnClickListener does not change the view. Someone suggested I try the runOnUIThread method in activity, but I can't get it to do what I want it to do.
How do I change the view of the screen by clicking the button?
Maybe you can try adding in your button xml code android:onClick="onClickMethod" and adding in your activity
public void onClickMethod(View v) {
status_val.setText("true board-example working");
level_1_true();
}
How did you define status_val? You remeber to link it like this?
status_val=(TextView)findViewById(R.id.);
Please tell me if I can be of more help

Categories

Resources