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).
Related
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.
I have an ImageButton and I wanted to be able to see a color when I clicked on it. I added a background, and used the same listselector I use for all my listviews, but it doesn't show anything when I click the ImageButton.
Here is the ImageButton xml
<ImageButton
android:id="#+id/button_biomes"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/biome"
android:background="#drawable/listselector"/>
And this is the listselector drawable:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:state_enabled="true"
android:state_pressed="true" android:drawable="#color/actionbar" />
<item android:state_enabled="true"
android:state_focused="true" android:drawable="#color/actionbar" />
<item
android:drawable="#color/android:transparent" />
</selector>
If anyone could help me It'd be greatly appreciated. Thanks!
It is possible that your image takes up entire space and doesn't show the background. Here, image and background are two different things, so do not expect the src image to be transparent.
I can take action when a view (button) is clicked on home screen widget. The thing is, I also want to show an effect so user will feel that button is alive. (Like the blue glow when you press on start/stop button on Music Player widget.)
Is this possible? If so, how? If not, what would be the alternative?
Here is a sample code for widget selector..
buttonimg.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector
xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="false"
android:drawable="#drawable/button_off" />
<item
android:state_pressed="true"
android:drawable="#drawable/button_on" />
<item
android:drawable="#drawable/button_off" />
</selector>
widget_layout.xml
<ImageButton
android:id="#+id/buttonimg"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_x="0px"
android:layout_y="2px"
android:clickable="true"
android:src="#drawable/button"
>
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);
i implemented a custom background for my EditText fields because i think the defaults for the EditTexts are too tall & have too much padding. anyway, the new EditText drawable backgrounds are displaying fine, BUT the fields shrink slightly when i open the keyboard and expand to their original (custom) size when i close the keyboard. this behavior must be overridable in some way that i just haven't stumbled across. can anyone help, please? I've read the IMEOptions article at the dev android site and nothing there really jumps out at me at as a possible solution to this problem. thanks!
here is my EditText - each EditText is in its own layout because i needed to add padding BETWEEN the fields:
<LinearLayout android:layout_height="wrap_content"
android:layout_width="fill_parent" android:orientation="vertical" android:paddingBottom="5dip" android:paddingLeft="3dip" android:paddingRight="3dip">
<EditText android:id="#+id/join_firstName"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:background="#drawable/joinedittextselector" android:padding="5dip" /> <!-- android:layout_weight="1" -->
</LinearLayout>
here is my selector:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_window_focused="false" android:state_enabled="true"
android:drawable="#drawable/join_field_bg_default" />
<item android:state_window_focused="false" android:state_enabled="false"
android:drawable="#drawable/join_field_bg_default" />
<item android:state_pressed="true" android:drawable="#drawable/join_field_bg_default" />
<item android:state_enabled="true" android:state_focused="true"
android:drawable="#drawable/join_field_bg_selected" />
<item android:state_enabled="true" android:drawable="#drawable/join_field_bg_default" />
<item android:state_focused="true" android:drawable="#drawable/join_field_bg_selected" />
<item android:drawable="#drawable/join_field_bg_default" />
i've tried adjustResize and adjustPan for the android:windowSoftInputMode setting of the activity that the EditText is used in and neither have worked. thanks again for any advice on this!
You would probably have to provide images for the various states defining them in an xml resource. People do this a lot for buttons.