On hover not working properly on Relative Layout Android [duplicate] - android

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;
}
});

Related

Abnormal Behavior with RadioButton

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" />

Custom Checkbox changes its size when isChecked is true Android?

I have an single choice ExpandableListView with a custom checkbox defined by a Drawable I created in the Android Asset Studio.
For some reason, when the checkbox is checked it looks smaller than when it is not. I already checked if I made a mistake with the size of the images and they all have the same size. (disabled or enabled).
Here's the code of my custom checkbox:
drawable/custom_checkbox.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_success_enabled" android:state_checked="true" android:state_focused="true"/>
<item android:drawable="#drawable/ic_accept_disabled" android:state_checked="false" android:state_focused="true"/>
<item android:drawable="#drawable/ic_accept_disabled" android:state_checked="false"/>
<item android:drawable="#drawable/ic_success_enabled" android:state_checked="true"/>
</selector>
My drawable/ic_success_enabled & disabled are:
The rows for each element in the ExpandableListView
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:padding="5dp" >
<TextView
android:id="#+id/title_one_item"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/one_item_checkbox"
android:layout_toRightOf="#+id/one_item_row_image"
android:padding="10dp"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/akdemia_0_black" />
<com.akdemia.application.CircularImageView
android:id="#+id/one_item_row_image"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentLeft="false"
android:layout_alignParentTop="false"
android:layout_centerVertical="true"
android:src="#drawable/placeholder_icon_iphone" />
<CheckBox
android:id="#+id/one_item_checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:background="#drawable/akdemia_checkbox"
android:button="#null"
android:focusable="false" />
</RelativeLayout>
And here is a screen shot of the ExpandableListView, where you guys can see the problem: (All the orange rectangles where added on purpose, just ignore them)
Hope anyone can help me with this, thanks in advance!
Give your checkbox a layout width and height like this:
android:layout_width="25dp"
android:layout_height="25dp"

Android: How to change the color of a button?

I'm using Eclipse ADT and I need to know how to change the default color of a form widgets button from the default gray to other color, of course using the xml.
Or you can use 9 patch images.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/patch_pressed" android:state_pressed="true"/>
<item android:drawable="#drawable/patch_normal" android:state_enabled="true"/>
<item android:drawable="#drawable/patchdisable" android:state_enabled="false"/>
And in Xml
<Button
android:id="#+id/button_register"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#drawable/patch"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:text="REGISTER"
android:textColor="#ffffff"
android:textStyle="bold" />
In your xml, you can set the background color by using android:background="#FF0000"
<Button
android:id="#+id/loadimage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Load Image"
android:background="#FFFF00"/>
you can set any background color of your choice...:)
Using code in you activity class:
Button btn = (Button)findViewById(R.id.button);
btn.setBackgroundColor(Color.BLUE);
btn.setBackgroundColor(Color.rgb(0, 0, 255));
btn.setBackgroundColor(Color.parseColor("blue"));
Doing it straight from your XML:
Add this property to your Button in your XML file:
android:background="#800080"
#800080 is purple.
Here is answer my friend
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/grey"/>

TextColor of button is not changing onclick of parent LinearLayout

I want to change the textcolor of button and modify image when I click on its parent linear layout.
Image is changing but textcolor is not getting changed on click of linear layout.
Here is my code in xml -
<LinearLayout
android:id="#+id/btn_layout"
android:layout_width="0.0dip"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1.0"
android:gravity="center"
android:clickable="true"
android:orientation="horizontal" >
<Button
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/done_with_accounts_action"
android:textColor="#drawable/done_button_text_selector"
android:focusable="false"
android:textSize="#dimen/done_button_text_size" />
<ImageView
android:id="#+id/done_btn_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingLeft="4dp"
android:focusable="false"
android:src="#drawable/done_btn_arrow_bg_selector" />
</LinearLayout>
**selector xml for button -**
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:color="#color/done_btn_pressed_color" />
<item android:state_pressed="true" android:color="#color/done_btn_pressed_color" />
<item android:color="#color/button_text_white_color" />
</selector>
If I replace button with TextView, everything works fine.
In this lines:
<item android:state_focused="true" android:color="#color/done_btn_pressed_color" />
<item android:state_pressed="true" android:color="#color/done_btn_pressed_color" />
You put the same color done_btn_pressed_color.
Maybe thats the problem?
I found the solution -
I have to set clickable as false for the done button.Now it also recognizes its parent onClick event.
Why the above code worked with textView because its onClick event is disabled byDefault, so it recognizes its parent onClick event.

Set background for item in list view

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>

Categories

Resources