Android: Query regarding TableRow Selection - android

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.

Related

Change background color of dynamically add table row in Android

I am trying to make a table to show my database content and I want the user of my app can select an specific row of the table. and the selected row have a different background in comparison with other row.
I want to add all thing dynamically so I have following code for my TableView (this code is a test and not connected to database yet).
RelativeLayout container = (RelativeLayout) findViewById(R.id.container);
TableLayout MainLayout = new TableLayout(this);
MainLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT, TableRow.LayoutParams.FILL_PARENT));
MainLayout.setStretchAllColumns(true);
//Create the first row and add two text views
TableRow row1 = new TableRow(this);
TextView text1 = new TextView(this);
text1.setText("Test1\n");
text1.setGravity(android.view.Gravity.CENTER);
TextView text2 = new TextView(this);
text2.setText("Test2");
text2.setGravity(android.view.Gravity.CENTER);
row1.addView(text1);
row1.addView(text2);
row1.setBackgroundResource(R.drawable.row_selector);
MainLayout.addView(row1);
and my row_selector.xml is:
<!-- language: xml -->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#color/green" />
<item android:state_selected="true" android:drawable="#color/red" />
<item android:state_focused="true" android:drawable="#color/blue" />
<item android:state_selected="true" android:state_focused="false" android:state_pressed="false" android:drawable="#android:color/transparent" />
</selector>
and I have defined the green/red/blue color in colors.xml in values.
but when I run my app and click on the row, nothing changes and the background color remain transparent. I have changed the transparent to red and the color remains red even after clicking. So I know that table row is connected to the row_selector.xml properly but it does not work properly.
Why the color does not change?
Update:
I use the $row1.setClickable(true)$ and it works properly. (It makes blue when I click on the row). so how can I have a specific color for selected row until I select another row. I mean I want to only last selected row have a different color.
I do it myself by adding the following code:
row1.setBackgroundResource(R.color.blue);
row2.setBackgroundResource(R.color.blue);
row1.setId(10001);
row2.setId(10002);
View.OnTouchListener rowClick = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event)
{
if(lastRowId>0){
((TableRow)findViewById(lastRowId)).setBackgroundResource(R.color.blue);
}
lastRowId = v.getId();
v.setBackgroundResource(R.color.red);
return true;
}
};
row1.setOnTouchListener(rowClick);
row2.setOnTouchListener(rowClick);

How can I make a RadioButton with overlapping text?

I'm adding programmatically multiple answers to a questions as RadioButtons in a RadioGroup (see code and image below).
I almost achieved it but instead of having a radio button with a text on its side (see image below), I would like to have only the text and the background.
Can I get rid of the radio button?
Or can I set my current background as the radio button (with checked/unchecked states), and add a text overlapping it?
RadioGroup answer_container = (RadioGroup) findViewById(R.id.answer_container);
while (c.isAfterLast() == false) {
RadioButton answer = new RadioButton(PCItem.this);
answer.setText(c.getString(c.getColumnIndex(DBAdapter.KEY_ANS_TEXT)));
answer.setBackgroundResource(R.drawable.btn_ans);
answer.setPadding((int)(30 * density), 0, 0, 0);
answer.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams((int)(775 * density), (int)(81 * density));
params.setMargins(0, (int)(20*density), 0, 0);
answer.setLayoutParams(params);
answer_container.addView(answer);
c.moveToNext();
}
You can use radioButton.setButtonDrawable(). You can also specify different drawables for each button state (including checked/unchecked) using a StateListDrawable.
To completely remove the button you can simply set it as null:
<RadioButton
android:button="#null"
...
/>
Be aware that by code radioButton.setButtonDrawable(null); doesn't work! The correct way is to set an empty StateListDrawable:
radioButton.setButtonDrawable(new StateListDrawable());
If you have more items, you can create a custom style to override the default properties of RadioButton:
<style name="CustomRadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#null</item>
<item name="android:paddingLeft">0dp</item>
</style>
Ok, I made it using
answer.setButtonDrawable(android.R.color.transparent);
to remove the button, and using a selector as btn_ans:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/btn_choix_unsel" />
<item android:state_checked="true" android:drawable="#drawable/btn_choix_sel" />
</selector>

Android ListView text color change

I'm working with list view, and try to achive desire look. Long stroy short - I want to change list item text View color when item is pressed. Default text color for textView is White, and when I press item in listView text Color automaticly change to Black. It is default settings and it works pretty well. But when I set color in text View, for example in Red then nothing happen when item in list View is pressed. I try to set color selector for Text View but it doesn't work(even if I set duplicateParentState=true)
I Just want some default text color and diffrent text color when user press item.
Thx for help.
put this file in res/color folder
color_selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"
android:color="#color/skyblue" />
<item android:state_focused="true" android:state_pressed="true"
android:color="#color/white" />
<item android:state_focused="false" android:state_pressed="true"
android:color="#color/white" />
<item android:color="#color/skyblue" />
</selector>
now set this selector in your Textview's textcolor property
prepare a color.xml in "values" folder
color.xml
<?xml version="1.0" encoding="utf-8"?>
<resources>
<color name="white">#ffffffff</color>
<color name="blue">#39abdf</color>
</resources>
You have to override the baseadapter. It is not difficult. This will allow you to target each element in each row and even allow for clicking of buttons and such on each row.
Here is one link. I know there are more. Ill post them if I come across them again.
Android: ListView elements with multiple clickable buttons
Android : BaseAdapter how to?
You can override the onItemClick() event and use the View that is given to you in the method. Using that, you can call View.findViewById(R.id.textview) for each of the textviews to change their font and font color.

GridView children don't display focus

In an android app, I have a GridView holding TextView created by an adapter. In those TextView I add an icon which has three states (pressed, selected and default.)
I removed the default selector of the GridView with android:listSelector="#00000000" and I would like the selected state of the icon to display instead. But although the pressed state works (ie when the TextView is pressed, the pressed version of the icons is shown) the seleted doesn't.
I've tried those tricks (found at different places on the web) but it didn't work either:
setting
android:descendantFocusability="afterDescendants"
or
android:drawSelectorOnTop="true"
or (in the TextViews)
android:duplicateParentState="true" />
And if I set the TextView to be focusable, it gets focus independently of the GridView (ie, clicking on it doesn't call the GridView onClick method...)
The icon is defined in a xml file like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/ic_flag_bg_pressed"/>
<item android:state_focused="true"
android:drawable="#drawable/ic_flag_bg_selected"/>
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/ic_flag_bg_default"/>
</selector>
Is there a way to tell the GridView to pass the focused state to its children ?
Got stuck on this as well. An alternate approach that can look okay is to use a border style selector and draw it on top.

OnClick change tablerow background color

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.

Categories

Resources