Here is a button with background that gets no focus when touch it.
Here is the xml:
<Button
android:id="#+id/continueID"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#drawable/buttonbg"
android:focusableInTouchMode="true"
android:focusable="true"
android:onClick="invokeLogin"
android:text="Login"
android:textColor="#F7F9FA" />
'buttonbg' background must handle the focused state so you can see changes when you touch it.
For example:-
// buttonbg.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:drawable="#drawable/your_imagename_while_focused"/>
<item android:state_pressed="true" android:drawable="#drawable/your_imagename_while_pressed" />
<item android:drawable="#drawable/image_name_while_notpressed" /> //means normal
</selector>
Related
I am currently working on an Android TV app, so the focus color is very important.
I have added some spinners in my layout and I am trying to change the selector color.
Primary/secondary/Accentcolor are not doing anything.
First thing I have is to put the spinners in layout. The top one is the one I am working on, and the bottom one is the default Appcompat
On the top one, I have added a color selector:
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:background="#drawable/selector"
android:layout_height="wrap_content"
android:focusable="true"
android:padding="0dp" />
<Spinner
android:id="#+id/spinner2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:padding="0dp" />
selector.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true"
android:drawable="#color/pressed" />
<item android:state_focused="true"
android:drawable="#color/focused" />
<item android:state_selected="true"
android:drawable="#color/selected" />
<item android:state_hovered="true"
android:drawable="#color/hovered" />
<item android:drawable="#color/transpa" />
</selector>
This is obvious, but the triangle has been removed due to the new background which is simply a rectangle.
What I would like to achieve is this selection but with my higlight color, and not the default grey:
And this is current result:
Just found the answer :-p
<item name="colorControlActivated">#android:color/holo_blue_dark</item>
<item name="colorControlHighlight">#android:color/holo_blue_dark</item>
I am creating a simple button as follows :
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/edSearch"
android:id="#+id/btnSearch"
android:layout_marginTop="30dp"
android:drawableTop="#drawable/ic_action_search"
android:textSize="16sp"
android:text="Search"
android:background="#color/md_green_500"
android:textColor="#fff"
/>
Now, when i try add any image in background like :android:background="#drawable/img123" then button raise effect does not work. is there any way i can achieve raise effect with background image ?
You can create a selector drawable for this. You need to provide 3 different images for this so that the button can toggle those images as per the state.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="false"
android:state_pressed="false"
android:drawable="#drawable/image_normal" />
<item android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/image_pressed" />
<item android:state_focused="true"
android:state_pressed="false"
android:drawable="#drawable/image_focused" />
</selector>
Also inspite of that you can use ImageButton which will keep your image in center of button and the effects will be default as per operating system.
I create button with background color but when i click on it, it's not show anything.
I need to show different color on button after click because user need to know button is
Click.
I don't understand how to do this?
Give me suggestion.
here is my button code.
<Button android:textSize="15px"
android:id="#+id/button9"
android:gravity="center|bottom"
android:textColor="#color/myWhiteColor"
android:drawableTop="#drawable/math"
android:text="#string/HomePage_Math"
android:background="#color/myMaroonColor"
android:layout_width="54dp"
android:layout_height="wrap_content" ></Button>
//XML file saved at res/drawable/button_bg.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="#ffff0000"/> <!-- pressed -->
<item android:state_focused="true"
android:color="#ff0000ff"/> <!-- focused -->
<item android:color="#ff000000"/> <!-- default -->
</selector>
//This layout XML will apply the color list to a View:
<Button android:textSize="15px"
android:id="#+id/button9"
android:gravity="center|bottom"
android:textColor="#color/myWhiteColor"
android:drawableTop="#drawable/math"
android:text="#string/HomePage_Math"
android:background="#drawable/button_bg"
android:layout_width="54dp"
android:layout_height="wrap_content" ></Button>
<?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/colorPrimaryDark" />
<item android:state_focused="true" android:drawable="#android:color/holo_green_dark" />
<item android:drawable="#color/colorCartButton" />
</selector>
This will work
I'm creating a ListView with buttons and having some issues.
My activity need to do 2 different actions for each action (ItemClick and buttonClick).
I assumed that:
1 – Because I has button on list items, I cant use OnItemClickListener(). Right?
So, I create layout for list items and make it clickable.
listitem_textview_button.xml:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:background="#drawable/selector_list_item">
<Button
android:id="#+id/listitem_textview_button_btn"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:text="#string/edit" />
<TextView
android:id="#+id/listitem_textview_button_txv"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical"
android:paddingLeft="6dip"
android:textSize="14sp"
android:textColor="#drawable/selector_textview"
android:minHeight="?android:attr/listPreferredItemHeight"
android:layout_toRightOf="#+id/listitem_single_line_w_button_btn" />
</RelativeLayout>
Notice that I've created a selector for the layout and a stateColorList for Textview.
selector_list_item.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_window_focused="false"
android:drawable="#color/transparent" />
<item
android:state_focused="true"
android:state_enabled="false"
android:state_pressed="true"
android:drawable="#drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_enabled="false"
android:drawable="#drawable/shape_list_item_disabled" />
<item
android:state_focused="true"
android:state_pressed="true"
android:drawable="#drawable/shape_list_item_transition" />
<item
android:state_focused="false"
android:state_pressed="true"
android:drawable="#drawable/shape_list_item_transition" />
<item
android:state_focused="true"
android:drawable="#drawable/shape_list_item_focus" />
</selector>
selector_textview.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_selected="true" android:color="#color/black" />
<item android:state_focused="true" android:color="#color/black" />
<item android:state_pressed="true" android:color="#color/black" />
<item android:color="#color/red" />
</selector>
2 – This is the best way to implement a ListView with custom items (included TextView Colors)?
The above code does not change the color of textViews when I click on items.
In some tests I saw that the text color changes when:
1. Using arrows of emulator.
2. Removing the button of ListView item.
Where is the problem?
prinscreens:
listitem selected by arrow device (text black, ok!)
listitem clicked by finger (text red, should be black, fail)
answer:
add android:duplicateParentState="true" to TextView.
I don't get it, your layout is clickable but you have buttons? Is the button for selecting a row?
If I were you I would not make anything clickable except the button. android:focusable="false" android:clickable="false"
Then you can in the button listener set the selection on a row manually:
getListView().setSelection(position);
Let me know if this works.
[EDIT] The real problem is that the TextView needs to be clickable, not the layout.
I am creating a homescreen widget. The widget should get the "normal" background colors when pressed and focussed. Transparent in the other cases.
Therefore I have defined a selector
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="true"
android:drawable="#drawable/widget_back_pressed" />
<item android:state_focused="false" android:state_pressed="true"
android:drawable="#drawable/widget_back_pressed" />
<item android:state_focused="true" android:drawable="#drawable/widget_back_selected" />
<item android:state_focused="false" android:state_pressed="false"
android:drawable="#drawable/transparent" />
Mostly everything works fine except the focused state. If the widget is on the homescreen and focused with the d-pad or the keyboard (in the emulator) it will not get my chosen color. Pressed color works fine.
Whats wrong?
Thanks and best regards,
Till
I had the same problem. I fixed it by making my ImageView focusable and making sure its parent layouts were not.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widget"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/transparent" >
<RelativeLayout
android:id="#+id/relativeLayoutWidget1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/logo"
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:maxHeight="72dp"
android:maxWidth="72dp"
android:scaleType="fitXY"
android:background="#drawable/btn_widget_background"
android:src="#drawable/widget_icon" />
</RelativeLayout>
</LinearLayout>
Also make sure you set your onClickPendingIntent correctly to the focusable item, otherwise your widget won't be clickable with the hardware button. So for this case, in your AppWidgetProvider:
views.setOnClickPendingIntent(R.id.logo, pendingIntent);