I am working with list view in android.
I want to set background color for each item in list view.
If I use setbackgroudcolor function, it displays incorrect color. It seems there is the mix color of application background and new color.
If I use setbackgroundresource fund, it displays ok. However, when I click on an item, color is not change.
i need to set background for for each item on list view, and when click on an item, background is changed to other color or background
My code: OnView
// Set background
RelativeLayout root = (RelativeLayout) vi.findViewById(R.id.p60001_layout_info);
if (position % 2 == 0){
root.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
root.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
Row layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/p60001_layout_info"
android:layout_width="match_parent"
android:layout_height="30dp">
<TextView
android:id="#+id/p60001_item_info"
android:layout_width="wrap_content"
android:layout_height="30dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:textSize="15dp" android:layout_marginLeft="10dp" android:fadeScrollbars="false" android:textColor="#color/txt_normal" android:scrollHorizontally="true" android:gravity="center_vertical">
</TextView>
<ImageView
android:id="#+id/icon"
android:layout_width="22px"
android:layout_height="30dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:src="#drawable/arrow" android:layout_marginRight="15dp"/>
<ImageView
android:id="#+id/p60001_image_notice_num"
android:layout_width="wrap_content"
android:layout_height="25dp"
android:layout_alignParentTop="true"
android:layout_marginRight="30dp"
android:layout_toLeftOf="#+id/icon"
android:layout_marginTop="5dp" android:src="#drawable/fukidashi" android:visibility="invisible" android:clickable="false" android:focusable="false" android:focusableInTouchMode="false" android:longClickable="false"/>
<TextView
android:id="#+id/p60001_item_notices"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/icon"
android:ems="10" android:layout_marginRight="40dp" android:background="#android:color/transparent" android:freezesText="false" android:focusableInTouchMode="false" android:focusable="false" android:clickable="false" android:selectAllOnFocus="false" android:textSize="20px" android:layout_marginTop="4dp" android:visibility="invisible">
<requestFocus />
</TextView>
</RelativeLayout>
If you are using custom adapter concept and the above is your getView code.
you must a convert view(xml file of each row) change the background of it like below.
if (position % 2 == 0){
converterView.setBackgroundResource(R.drawable.cell_g_02);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}else{
converterView.setBackgroundResource(R.drawable.cell_g_01);
//root.setBackgroundColor(R.color.color_fist_in_pack_view);
}
if you need to change the color on focus or pressed you use the below xml as background.
<?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/red" />
<item android:state_selected="true"
android:drawable="#color/red" />
</selector>
also if need to set background for clicked items then a way to do this is::::
to have a bool variable(=false)in your pojo(bean) where you are storing the data of each item.
so when ever you click on item make that variable to true. And in getView() just check whether that variable is true or false and do the need full accordingly.
Try this
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<!-- When selected, use selected state color -->
<item android:drawable="#drawable/your_drawable_when_selected"
android:state_selected="true" />
<!-- When not selected, use default item color-->
<item android:drawable="#drawable/your_drawable_when_unselected" />
</selector>
Related
I have an arraylist named incorrectAnswers, I want to change the text color of those strings inside that arraylist.
Here's a snippet of my code:
ArrayList<String> incorrectAnswers = new ArrayList<>();
if(mItemArray.get(0).second.startsWith("In")){
numberOfCorrect++;
numberOfTries++;
} else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(0).second);
}
if(mItemArray.get(1).second.startsWith("If")){
numberOfCorrect++;
numberOfTries++;
}
else{
numberOfErrors++;
incorrectAnswers.add(mItemArray.get(1).second);
}
This is the layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/item_layout"
android:layout_width="match_parent"
android:layout_height="70dp"
android:orientation="horizontal"
android:gravity="center_vertical"
android:padding="10dp"
android:backgroundTint="#android:color/holo_green_light"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:background="#drawable/border_spinner">
<ImageView
android:id="#+id/image"
android:layout_width="1dp"
android:layout_height="1dp"
android:src="#drawable/ic_abrasion"
android:visibility="invisible"/>
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#color/textColor"
android:layout_height="wrap_content"/>
I want to change programmatically the text color of those items I've added inside incorrectAnswers. How can I achieve that?
First of all create a xml in drawables folder like this to setup the color states:
Lets call it text_color.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#android:color/holo_red_dark" android:state_selected="true" />
<item android:color="#android:color/holo_green_light" />
</selector>
Then, use this xml as your text color in your layout:
<TextView
android:id="#+id/list_text"
android:layout_width="wrap_content"
android:textSize="16sp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"/>
Finally, you have to set only the textviews that will have the incorrect answers as checked like this:
TextView textList = (TextView) findViewById(R.id.list_text);
textList.setSelected(true);
I am using RadioButton in my layout. Where i have provided background to the RadioButton. Below is the code
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_spain"
android:id="#+id/am_rb_spain" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_germany"
android:layout_marginLeft="#dimen/margin_15"
android:layout_marginRight="#dimen/margin_15"
android:id="#+id/am_rb_german" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:background="#drawable/rb_english"
android:id="#+id/am_rb_english" />
</RadioGroup>
RadioButton background drawable
<?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/ic_spain" />
<item android:state_checked="true" android:drawable="#drawable/ic_spain_pressed" /> //pressed image is Large in size
<item android:drawable="#drawable/ic_spain" /> <!-- default -->
</selector>
I have saved the selection in TinyDB so that app will remember my selection whenever i open my app. But whenever i open my app the default selected RadioButton background Appears Large.
I have taken all the images of same size in drawable's with same padding.
First flag is Default selected here. But it appear to be slightly large in size
Now i have pressed 2nd flag. But first flag is not coming to its normal state.
You should change your android:button="#null" to android:button="#drawable/rb_germany" and remove background from each RadioButton. It will run fine.
Your updated RadioButton should be like:
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/rb_spain"
android:id="#+id/am_rb_spain" />
I have a row of buttons and i am setting their selectors for background and text programatically. The reason i want to do this programmatically is because, I have a set of themes the user can choose from and depending upon the theme selected, i want to change the selector for the button.
For example, if the user selects a blue theme, when loaded, the background of the button is blue and text colour is white. When he presses the button, the background changes to white and the text colour changes to blue. When user removes the finger from button, the changes revert back to default blue for background and white for text colour. You can see the respective selectors for blue below.
This is similar to all other themes. I have separate XMLs for all the themes. The selector for text colour change works fine. The problem is with the background selector for button.
selector_background_blue.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/white" android:state_pressed="true"/>
<item android:drawable="#color/blue_500"/>
</selector>
color_selector.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/blue_500"/>
<item android:color="#android:color/white"/>
</selector>
I have a class that returns the drawable(selector) depending upon the theme selected. I am getting the selector as follows:
public Drawable getButtonBackgrounds(String theme) {
Drawable drawable = null;
if (theme.equalsIgnoreCase(Const.Theme.BLUE))
drawable = context.getResources().getDrawable(
R.drawable.selector_background_blue);
return drawable;
}
I'm setting these selector for button's background as follows:
private void setButtonBackgrounds(Drawable buttonDrawable) {
int sdk = android.os.Build.VERSION.SDK_INT;
if (sdk < android.os.Build.VERSION_CODES.JELLY_BEAN) {
btnA.setBackgroundDrawable(buttonDrawable);
btnT.setBackgroundDrawable(buttonDrawable);
.....
.....
btnVoice.setBackgroundDrawable(buttonDrawable);
} else {
btnA.setBackground(buttonDrawable);
btnT.setBackground(buttonDrawable);
.....
.....
btnVoice.setBackground(buttonDrawable);
}
}
button's xml:
<Button
android:id="#+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/button_t"
android:textSize="22sp" />
Total Row's XML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<Button
android:id="#+id/btnA"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/arithmetic_symbol"
android:textSize="16sp" />
<Button
android:id="#+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/trigonometric_symbol"
android:textSize="16sp" />
<Button
android:id="#+id/btnN"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/voice_calculator_symbol"
android:textSize="16sp"
android:visibility="gone" />
<ImageButton
android:id="#+id/btnVC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:contentDescription="#string/empty"
android:src="#drawable/ic_keyboard_voice_black"
android:text="" />
<Button
android:id="#+id/btnC"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/button_c"
android:textSize="16sp" />
<Button
android:id="#+id/btnD"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="?android:attr/selectableItemBackground"
android:text="#string/button_del"
android:textSize="16sp" />
</LinearLayout>
This is the same for all the buttons in the row.
The drawable is set just fine on the load. Please refer to the image below.
The problem is When I click on a button(for ex., A), the adjacent ImageButton(microphone) is also changing its state. Please look at the images below:
Why is this happening? Can someone help me with this. Please let me know if you need any other info.
I think that you are experiencing a mutate-related issue (please take a look here, it's extremly useful)
You need to call mutate() on your drawable before assingning it to the View if yout don't want to share the common state across the various instances:
Drawable buttonDrawable = context.getResources().getDrawable(R.drawable.btn);
buttonDrawable.mutate()
btnA.setBackgroundDrawable(buttonDrawable);
In your code you are using the same Drawable for more then one View, so you need to adopt the approach I've described above to avoid the state-sharing.
Dude You have to set the selector From The XML File See below:
<Button
android:id="#+id/btnT"
android:layout_width="0dip"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:background="#drawable/button_selector"
android:text="#string/button_t"
android:textSize="22sp" />
Here The property android:background="#drawable/you_drawable_selector" is where you have to set The Selector.
Hope I helped.
This question already has an answer here:
How to change color of the text in TextView on hover like in css?
(1 answer)
Closed 9 years ago.
I trying to change the color of by background on android:state_hovered.But it is not reflecting any changes on my application.Now what i want to when the relative layout get focused i want to change it's background and the that time i want to change the color of my Text View Text .But this code is not working for me please suggest me the changes
this is my XML file:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dip" >
<ImageView
android:id="#+id/imgLogo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:src="#drawable/logo_demo" />
<RelativeLayout
android:id="#+id/flight_relative"
android:layout_width="match_parent"
android:layout_height="70dp"
android:layout_below="#+id/imgLogo"
android:layout_marginTop="5dp"
android:background="#drawable/button_effect"
android:gravity="center_vertical" >
<ImageView
android:id="#+id/flight_list_image"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/flight_content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/flight_list_image"
android:layout_marginRight="5dp"
android:layout_marginTop="10dp"
android:layout_toLeftOf="#+id/flight_arrow"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_tittle"
android:textColor="#drawable/text_color"
android:textStyle="bold"
android:textSize="15dp"
/>
<TextView
android:id="#+id/content_flight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/flight_content"
android:layout_marginRight="5dp"
android:layout_toLeftOf="#+id/flight_arrow"
android:layout_toRightOf="#+id/flight_list_image"
android:text="#string/flight_content"
android:textColor="#2f2f2f"
android:textSize="10dp" />
<ImageView
android:id="#+id/flight_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/flight_content"
android:layout_alignParentRight="true"
android:src="#drawable/arrow" />
</RelativeLayout>
</RelativeLayout>
This is my style file that is button_effect.xml to change the background of my relative layout.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#android:color/darker_gray" android:state_hovered="true"></item>
<item android:drawable="#drawable/gray_bg"></item>
</selector>
This is my text_color.xml to change the color of the text.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#177f75"/>
<item android:color="#152b72"/>
</selector>
You can do something like this, for textview as well as with your layout.
Here I have used drawables for different state when - pressed, hovered and normal.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/book_press" android:state_pressed="true"/> <!-- pressed -->
<item android:drawable="#drawable/book_focused" android:state_focused="true"/> <!-- focused -->
<item android:drawable="#drawable/book_default"/> <!-- default -->
</selector>
Instead of drawables use your color. Hope this will help you.
And you can set layout focused using xml for using Java code.
use this for changing text color on hover
textview.setOnTouchListener(new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN){
//Button Pressed
}
if(event.getAction() == MotionEvent.ACTION_UP){
//finger was lifted
}
return false;
}
});
I have RelativeLayout with android:clickable="true" to change image color from grey to white when button stay pressed (otherwise if android:clickable="false" it doesn't work):
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/contacts"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_weight="0.2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:clickable="true" <!-- here clickable = true -->
android:contentDescription="#string/content_description_contacts"
android:scaleType="fitXY"
android:src="#drawable/contacts" />
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignBottom="#id/image"
android:paddingBottom="10dp"
android:textColor="#drawable/text_color"
android:text="#string/button_contacts"
android:textSize="12sp" />
</RelativeLayout>
and seems like:
My contacts Selector seems:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#drawable/contacts_over" />
<item android:state_selected="true"
android:drawable="#drawable/contacts_selected" />
<item
android:drawable="#drawable/contacts_default" />
</selector>
As you can see I have 3 images: by default, selected and pressed.
According to documentation, after setting android:clickable="true" to ImageView I stopped to get onClickListener event.
contacts = (RelativeLayout) findViewById(R.id.contacts);
contacts.setOnClickListener(this);
So I have
or set clickable="false" and "state_pressed" doesn't work
or set clickable="true" and onClickListener doesn't work (no event received)
What should I do, any workaround?
Thanks,
Try to set onClickListener to your ImageView instead of RelativeLayout.
And this may interest you too:
How to pass the onClick event to its parent on Android?
Work well, if you set Listner to your Imageview instead Layout.
Try using ImageButton widget instead of ImageView.