Android Widget Backgroundcolor when focused - android

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

Related

Why Button with Background gets no focus when pressed in android?

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>

How can I change highlight (focused) color of the triangle in a Spinner

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>

Change Color onClick and background on Focus

i have a ListView in my Navigation Drawer and want it looks like the Playstore App or Youtube App.
Means onClick changing FontColor for Example to red and when i just hold it the background should turn grey.
Do i have to use 2 onitemclicklistener?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="48dp">
<ImageView
android:id="#+id/icon"
android:layout_width="25dp"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
android:contentDescription="#string/DrawerListIcon"
android:src="#drawable/ic_home"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_toRightOf="#id/icon"
android:textSize="14sp"
android:textStyle="bold"
android:minHeight="?android:attr/listPreferredItemHeightSmall"
android:textAppearance="?android:attr/textAppearanceListItemSmall"
android:textColor="#color/DrawerListFontColor"
android:gravity="center_vertical"
android:paddingRight="40dp"
android:paddingLeft="0dp"/>
</RelativeLayout>
Tried with Item and Selector. First thing Font Color don't change when i pressed the Button. Second the background of the Relative Layout turns blue insead of Grey.
drawer_list_font.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListFontColorPressed"
/>
<item android:color="#color/DrawerListFontColor"/></selector>
drawer_list_background.xml
<?xml version="1.0" encoding="utf-8"?><selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_focused="true"
android:color="#color/DrawerListBackgroundFocused"
/>
<item
android:color="#color/DrawerBackground"
/></selector>
I had to use android:drawable instead of background on the relative Layout. On the Textview Android Stuio accepted android:color.
This is changed by changing view from ur java file at every click event u can change the view. U have to make multiple view ie xml files for that. Which are dynamically changed or u can also send parameters to a function and create an xml at runtime but that will be too deep for u
No need for listeners at all.
A better approach here is to create drawable XML's with definitions for every state ("pressed", "focused", etc).
Check out Color State List resources
For example:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_focused="true" android:state_pressed="false"android:color="#80000" />
<item android:state_focused="true" android:state_pressed="true" android:color="#ff0000" />
<item android:state_focused="false" android:state_pressed="true" android:color="#ff0000" />
<item android:color="#color/DrawerListFontColor" />
</selector>

How can I have an ImageView selector when using Picasso (Square) on Android?

I know that Picasso sets the "src" therefore leaving the "image.background" in peace, but I cannot seem to make the ImageView respond to a selector (as in when it's touched to show the standard holo selector or any custom one).
Does anybody have a working sample? I must be missing something. I've tried different selectors but I don't see the touched state.
I've tried doing a setselected(true) in onClick() but I know that this is something that the View handles automatically if there's a drawable with states like:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:drawable="#android:color/transparent" />
<!-- Even though these two point to the same resource, have two states so the drawable will invalidate itself when coming out of pressed state. -->
<item android:state_focused="true" android:state_enabled="false" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_enabled="false" android:drawable="#drawable/abc_list_selector_disabled_holo_light" />
<item android:state_focused="true" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="false" android:state_pressed="true" android:drawable="#drawable/abc_list_selector_background_transition_holo_light" />
<item android:state_focused="true" android:drawable="#drawable/abc_list_focused_holo" />
</selector>
The Java code to load the image is:
Picasso.with(mContext).load(url).error(R.drawable.error).fit().into(holder.someIV);
And the XML for that someIV looks like:
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:contentDescription="#string/mixes"
android:scaleType="centerCrop"
android:background="#drawable/mix_list_art_selector"
android:cropToPadding="false" />
The background is set to the above selector.
Any ideas? Thanks.
Here's what I did:
what I wanted to highlight was an ImageView that was part of the layout of a list_item (technically speaking, a row).
But I didn't want to highlight the whole row, just an image inside it.
I had to wrap the ImageView in a FrameLayout.
<FrameLayout
android:id="#+id/list_item_image_wrapper"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:clickable="true"
android:foreground="#drawable/mix_list_art_selector">
<ImageView
android:id="#+id/list_item_image"
android:layout_width="match_parent"
android:layout_height="#dimen/mixset_list_image_size"
android:layout_marginTop="1dp"
android:layout_marginLeft="1dp"
android:layout_marginRight="1dp"
android:contentDescription="#string/mixes"
android:scaleType="centerCrop"
android:clickable="false"
android:focusable="false"
android:cropToPadding="false" />
</FrameLayout>
… and also move the Click Listeners to the FrameLayout.
Thanks to #jason_t for the pointer to the solution.
Please note that for other widgets like TextView, the above is likely not needed.

How can I make a home widget behave like a launcher icon?

I have a widget that acts as a launcher on the home screen.
How can I make it behave like a launcher icon?
I use this layout for portrait:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:background="#drawable/widget_background_selector"
android:focusable="true"
android:layout_width="74dip"
android:layout_height="79dip"
android:layout_marginLeft="3dip"
android:layout_marginTop="14dip">
<ImageView android:id="#android:id/background"
android:layout_width="72dip"
android:layout_height="72dip"
android:layout_marginLeft="1dip"
android:layout_marginTop="4dip" />
</RelativeLayout>
</RelativeLayout>
And this is the background selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:drawable="#drawable/widget_background_pressed" />
<item android:state_window_focused="true" android:state_focused="true" android:drawable="#drawable/widget_background_focused" />
<item android:state_window_focused="false" android:state_focused="true" android:drawable="#android:color/transparent" />
</selector>
This way if I use the DPAD the widget is focusable but the click doesn't work.
The touch still works but the widget is not displayed as focused.
Any idea what I do wrong?
What about the code that registers your click events? Can you show that?
I don't see any id attribute for your RelativeLayout, so my stab is that you haven't even registered an OnClickPendingIntent for the RelativeLayout for which you want to handle click events. And perhaps you have created an intent for the ImageView, which is receiving the touch events, but not the dpad click events, because it's parent (the RelativeLayout) has requested focus (and has no click event-handler).

Categories

Resources