OnClick change tablerow background color - android

So I'm trying to find an easy way to get the background color or a table row to change when its clicked on. I've been trying to find a way to call what the background color is and check it but I haven't found a way to call the color. Here is what I have right now.
RowName = (TableRow) findViewById(R.id.RowName);
RowName.setBackgroundColor(Color.TRANSPARENT);
RowName.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (RowName.equals(Color.TRANSPARENT))
RowName.setBackgroundColor(Color.YELLOW);
else if (RowName.equals(Color.YELLOW))
RowName.setBackgroundColor(Color.TRANSPARENT);
}
});
I know that its wrong. Hopefully you can see what I'm trying to accomplish. If not, what I want to do is have the table row start of transparent. When someone clicks on the table row I want it to change to yellow. Then, if they click it again, I want it to change back to transparent. Thanks.

You need to set the background color of your row to a state list drawable (that handles select, pressed, active, non-active).
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
The XML should look something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active state -->
<item android:state_selected="true" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<!-- Inactive state-->
<item android:state_selected="false" android:state_focused="false"
android:state_pressed="false" android:drawable="#android:color/transparent" />
<!-- Pressed state-->
<item android:state_pressed="true" android:drawable="#android:color/yellow" />
<!-- Selected state (using d-pad) -->
<item android:state_focused="true" android:state_selected="true"
android:state_pressed="false" android:drawable="#android:color/yellow" />
</selector>

So here is what wound up working. Make sure you have your TableRows named. Before my on create I have
private TableRow RowName;
I also have
int state = 0;
. I then add the code
public void RowName(View view) {
switch (state) {
case 0:
RowName.setBackgroundColor(Color.YELLOW);
state = 1;
break;
case 1:
RowName.setBackgroundColor(Color.TRANSPARENT);
state = 0;
break;
}
}
To get it to work, go into your xml and in the OnClick property add RowName or the name of the public void that you are working with.
Enjoy.

Related

Android. How do I keep a button displayed as PRESSED until the action created by that button is finished? Activated/Selected not usable

I want to keep a ImageButton with a state-list drawable in a "Wait-for-Action-Finished" state as long the action was not done.
I've seen the other topics around this problem. But in my case the other solutions are not working for me because:
I'm using the layout (which contains the buttons) in a RemoteView too (but not only there)
I'm using the same layout in a recyclerview with Multi-Selection capabilities. Therefore the activated/selected state is used by the multi-selector implementation and not possible for the state-list drawable except it was not propagated from selected-item to child views => my butotns
using custom state not working with RemoteViews
Using ToogleButton not possible, because setEnabled is not available for Buttons/ToggleButtons/CompoundButtons in RemoteViews
It's not important that the "Wait-for-Action-Finished" drawable was shown in the RemoteView (but it would be nice...), but I don't want to duplicate the layout. It's okay if the Wait-State was shown in the RecyclerView items only.
But there it should work along with the Multiselector function.
I'm using an ImageButton because it can be disabled in a RemoteView (in opposite to Button/ToggleButton/CompoundButton in general).
any hints how I could solve this?
You will have to manually set the state of the button and keep it there. This solution will give you some insight link
After reading out your question The thing what I understand the solution will be like below.
First set the button.xml as below.Use it as drawable for that button.Each of the state btn_disabled or the btn_pressed is the drawable state of your button which is defined by you.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/btn_disabled" android:state_enabled="false"/>
<item android:drawable="#drawable/btn_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/btn_pressed" android:state_selected="true"/>
<item android:drawable="#drawable/btn_default"/>
</selector>
Next use the code snippet below.
boolean checkActionOpen = false;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
if(!checkActionOpen) {
checkActionOpen = true;
button.setSelected(true);
}
}
});
Then again where the work will go on after the finish you will set checkActionOpen as false and do the button.setSelected(false); action.
I've got it working by combining the selected and activated states.
My button looks like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/progress_small" android:state_activated="true" android:state_selected="true"/>
<item android:drawable="#drawable/btn_bookmark_add_pressed" android:state_focused="true" android:state_pressed="true" />
<item android:drawable="#drawable/btn_bookmark_add_pressed" android:state_focused="false" android:state_pressed="true" />
<item android:drawable="#drawable/btn_bookmark_add_pressed" android:state_focused="true" />
<item android:drawable="#drawable/btn_bookmark_add_normal" android:state_focused="false" android:state_pressed="false" />
</selector>
And my button.onClicklistener looks like this:
public void onClick(View v) {
if (v.isActivated() && v.isSelected()) {
return;
}
v.setActivated(true);
v.setSelected(true);
AppController.getJobManager().addJob(new Job()));
}
This way it won't collide neither with "click the itemView", where selected state will be propagated to all child views (inclusive my buttons) nor with activate the "multi-selection" feature where the activated state will be propagated to all child views.
After the job is finished it will notify (EventBus) the RecyclerView where the ViewHolder can reset both activated and selected state back to false.

Android selector. How to go to normal state after press?

I've got a list of cards, and when the user presses on them, it goes to different fragments.
I'm using a selector on a card to have a "pressed state". I use the following XML:
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_enabled="true"
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:state_enabled="true"
android:state_focused="true"
android:drawable="#color/transparent" />
<item
android:drawable="#color/transparent" />
</selector>
When I press on a card, if the next fragment takes 1 second to load, the card stays in the "pressed" state, until the fragment is displayed. Does someone know how to do like in recent Google apps (Messenger, Gmail, ...), ie after the press, the card goes to "normal" state, and then after the fragment is ready, the fragment is displayed.
Thanks.
UPDATE: The problem doesn't seem to be in the selector. If instead of going to another fragment I go to display another activity, the card goes to normal state. It's only when going to a fragment. It's like if the fragment takes all the resources and prevent the normal state to be displayed.
Yet, if we go to the onTouchEvent() method of the View class, there's this code:
// Use a Runnable and post this rather than calling
// performClick directly. This lets other visual state
// of the view update before click actions start.
if (mPerformClick == null) {
mPerformClick = new PerformClick();
}
if (!post(mPerformClick)) {
performClick();
}
So I don't know why the normal state is not displayed...
Try with the following Selector for CardView
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/card_on_press"/>
<item android:state_focused="true" android:state_enabled="true"
android:drawable="#drawable/card_on_focus"/>
<item android:state_enabled="true"
android:drawable="#drawable/card_default"/>
<item android:state_enabled="false"
android:drawable="#drawable/card_on_press"/>
</selector>
Note: Drawable default will be fine as a transparent item as CardView provides a default background for all android versions
Try this...
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:state_pressed="true"
android:drawable="#drawable/hatching" />
<item
android:drawable="#color/transparent" />

listview selection color in android

in my listview, when user long press on a item, i draw a custom actionbar and provide user option to delete multiple items at a time.
by default if i perform long press action, i will get selection color as blue and it gets disappear.
To overcome i tried adding a selector like this.
listviewselector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/BLUE" />
</selector>
If i set this selector, when user performs long press i can see red color, but after wards if user performs selection, no color is getting retained on item. By default it looks white.
I tried setting background color based on condition like below
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(REDCOLOR);
}
else
{
convertView.setBackgroundColor(WHITE);
}
In this case, if user performs long press and then if user select multiple items i could see red color and by default all the items color will be white. But if user touches any item the default color will be nothing i.e no color appears on selection.
How to get default white color, on tap blue color and upon multiple selection red color?
i tried like this
New selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Selected -->
<item
android:state_focused="true"
android:state_selected="false"
android:drawable="#color/RED"/>
<!-- Pressed -->
<item
android:state_selected="true"
android:state_focused="false"
android:drawable="#color/WHITE" />
</selector>
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.RED));
}
else
{
convertView.setBackgroundColor(R.drawable.listviewselector);
}
In this i get by default all items blue color. why?
i tried like this, it worked.
if(mSelectedItemsIds.get(key))
{
convertView.setBackgroundColor(mContext.getResources().getColor(R.color.BLUE));
}
else
{
convertView.setBackgroundColor(android.R.drawable.list_selector_background);
}

Android Selectors Text Color

I've been over way too many StackOverflow questions and still can't get it working.
I dynamically (programmatically) create a number of buttons on my activity. I need to use selectors to change the background and text color depending on if the button is pressed or not.
I've got the background to change from black to white when its pressed, but I can't seem to get the text color to change (or set it for that matter ) - it just defaults to black.
Here's what I've got.
Java file:
monthButtons[i].setBackgroundResource(R.drawable.button_background);
monthButtons[i].setTextColor(R.color.text_color);
button_background.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/white"/>
<item android:drawable="#color/black"/>
</selector>
text_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:textColor="#color/black" />
<item
android:textColor="#color/white" />
</selector>
Can someone point me to a method to change the text color when the button is pressed? Please bear in mind I don't have anything defined in a layout file for these buttons.
Thanks
Why don't you just use one background xml? Like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/white"
android:textColor="#color/black" />
<item android:drawable="#color/black"
android:textColor="#color/white"/>
</selector>//Untested
And set it to be background for the button. If it doesn't work you can always do it programmatically. Hope this helps.
How about this:
button.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN )
{
button.setBackground("#000");
edittext.setTextColor("#dedede");
return true;
}
else
{
button.setBackground("#dedede");
edittext.setTextColor("#000");
return true;
}
return false;
})); // Untested. Sorry i'm away from pc :(
You can use Color State List Resource to change the text color dynamically:
https://developer.android.com/guide/topics/resources/color-list-resource.html

Android: Query regarding TableRow Selection

In my XML, I have a TableLayout with only 1 TableRow i.e. the heading. Other all rows I add dynamically setting BackgroundColor (LTGray) for TableRow & TextColor for TextViews in it . I also handle click event on each row.
private void createView(TableRow tr, TextView tv, String data, int rowId) {
tv.setText(data);
//tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
tv.setTextColor(Color.BLACK);
tv.setPadding(20, 0, 0, 0);
tr.setPadding(0, 1, 0, 1);
tr.setBackgroundColor(Color.LTGRAY);
tr.setId(rowId);
tr.setClickable(true);
tr.setOnClickListener(this);
tr.addView(tv);
}
Reg selection :
I want to change the BackgroundColor of TableRow lets say Yellow. So if 1st row is selected it bgColor should be Yellow. Then if 3rd row is selected the 1st row's color should turn to LTGray.
And if anywhere out of the Rows is clicked, then the selected Row (if at all) should also be de-selected. For this do I have to add the main layout clickListener OR make the row select again and it turns deselected ?
Can selector (state list drawable) work for both the ways or I got to handle it programmatically. What sort of Drawable should I use to setBackgroundDrawable in my Java Code to se the statelist drawable ?
I believe like other components for TableRow also onClick will also take care of onTouch. Please correct me if am wrong. As want to handle the same feature with touching the row also.
What is the best way to achieve the goal ? Any help is highly appreciated.
Do not change that in code! Use selector instead.
Taken from here:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Active state -->
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<!-- Inactive state-->
<item android:state_selected="false" android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
<!-- Pressed state-->
<item android:state_pressed="true" android:drawable="#android:color/yellow" />
<!-- Selected state (using d-pad) -->
<item android:state_focused="true" android:state_selected="true" android:state_pressed="false" android:drawable="#android:color/yellow" />
</selector>
Not so thorough, but more useful answer is here
changing the selector dynamically here
Ok, than use OnFocusChangeListener. It catches obtaining and losing the focus.
onFocusChange(View v, boolean hasFocus)
Called when the focus state of a view has changed.
Thanks Friends,
I managed it handling in the code itself. Added click listener to each row addd and handle the colors of selected and non-selected row accordingly.

Categories

Resources