I'm developing an Android application and I use this to customize RadioButtons:
<!-- Radio button style -->
<style name="RadioButton" parent="#android:style/Widget.CompoundButton.RadioButton">
<item name="android:button">#drawable/checkbox_unchecked</item>
</style>
But the image #drawable/checkbox_unchecked is very big.
Do you know how can I make it smaller?
I know you can achieve what you want by reducing the size of your image. But I would recommend using the following code. This will help you set images for both the unselected and selected modes.
This is how you can achieve it, in a few steps:
-Create Image Drawables for their two states (checked/unchecked).
-Create selector for this two drawables. The sample content should be something like this:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:drawable="#drawable/unchecked" />
<item android:state_checked="false" android:drawable="#drawable/checked" />
</selector>
-Add this selector as a android:button attribute to RadioButton
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#drawable/background">
<RadioButton
android:id="#+id/radioButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector"
android:text="Custom RadioButton" />
</LinearLayout>
Related
I want to create a control panel at the bottom of my screen that looks something like this.
Here is the image
And I want to simulate button press by having the clicked on item be blacked, and everything that isn't clicked be greyed out. As shown in the image, the picture icon had been clicked on.
At the moment, my solution is to overlay two rows of identical ImageViews. One layer consist of all black icons, and one layer consist of all grey icons. When I click on an image, I run a code that looks like this:
private void galleryClicked() {
mGalleryImageView_unclicked.setVisibility(View.INVISIBLE);
mPersonalImageView_unclicked.setVisibility(View.VISIBLE);
mExploreImageView_unclicked.setVisibility(View.VISIBLE);
mMenuImageView_unclicked.setVisibility(View.VISIBLE);
mGalleryImageView_clicked.setVisibility(View.VISIBLE);
mPersonalImageView_clicked.setVisibility(View.INVISIBLE);
mExploreImageView_clicked.setVisibility(View.INVISIBLE);
mMenuImageView_clicked.setVisibility(View.INVISIBLE);
}
I'm just wondering if there's a more efficient or better way of doing this. When I run this on my phone, and can feel some lag in the response. Thank you!
I have better solution, that is using selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/clicked" android:state_enabled="true" />
<item android:drawable="#drawable/unclicked" android:state_enabled="false" />
</selector>
so now you just set mGalleryImageView.setEnabled(true); or mGalleryImageView.setEnabled(false)
You can use Radiobuttons with RadioGroup, and each RadioButton has its own selector Drawable. Then the RadioGroup will take care of the selection for you (check one will uncheck all others).
Example code:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RadioGroup
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="horizontal">
<RadioButton
android:id="#+id/item_one"
style="#style/TabItem"
android:checked="true"
android:drawableTop="#drawable/selector_tab_item_one" />
<RadioButton
android:id="#+id/item_two"
style="#style/TabItem"
android:drawableTop="#drawable/selector_tab_item_two" />
<RadioButton
android:id="#+id/item_three"
style="#style/TabItem"
android:drawableTop="#drawable/selector_tab_item_three" />
<RadioButton
android:id="#+id/item_four"
style="#style/TabItem"
android:drawableTop="#drawable/selector_tab_item_four"/>
</RadioGroup>
</RelativeLayout>
Declare #style/TabItem in res/values/styles.xml:
<style name="TabItem" parent="android:style/Widget.Holo.Light.TextView">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_weight">1</item>
<item name="android:paddingTop">15dp</item>
<item name="android:drawablePadding">1dp</item>
<item name="android:button">#null</item>
<item name="android:background">#null</item>
<item name="android:gravity">bottom|center_horizontal</item>
</style>
The selector Drawable is like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/item_one_highlighted" android:state_checked="true"/>
<item android:drawable="#drawable/item_one_highlighted" android:state_pressed="true"/>
<item android:drawable="#drawable/item_one_default" />
</selector>
Results:
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>
I have an ImageButton:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:background="#null" /* Sets transparent BG for button image(PNG) */
/>
The image is "btn_70s", how do I set a "pressed" version of the image for when the ImageButton is tapped?
The documentation states to create an XML file and place it in the drawable directory:
<?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/button_pressed" /> <!-- pressed -->
<item android:state_focused="true"
android:drawable="#drawable/button_focused" /> <!-- focused -->
<item android:drawable="#drawable/button_normal" /> <!-- default -->
</selector>
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
<ImageButton
android:layout_width="60dp"
android:layout_height="60dp"
android:id="#+id/btn_70s"
android:src="#drawable/btn_70s"
android:android:background="#drawable/btn_70s_custom"
/>
Only issue with this, I can't use #null to allow transparency. Any assistance is appreciate.
Have a look over there for a (possible) solution to your problem : Make button background transparent using selector
And for the record,
This to me says it would affect all the ImageButtons rather than an individual one. Would I save it as an individual XML for each button for example (btn_70s_custom.xml), and then use it like:
No, it won't set this theme for all the buttons, but only for the ones where you specify to use this selector.
Hope this helps!
You can try selectableItemBackground
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="?attr/selectableItemBackground"/>
I would like to simply have the "tick" without the square. Also only as ImageView would be fine. Anyone knows the name of the resource in Android?
Just set selector to drawable left to Checkbox in whatever shape you desire.
Do like this.
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#null"
android:drawableLeft="#drawable/checkImageSelector" />
android:button="#null" will remove the default image of square with tick and drawableLeft will place your image in place of that.
checkImageSelector.xml will be like this.
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/check" android:state_selected="true"/>
<item android:drawable="#drawable/unchecked"/>
</selector>
You need two images for checked and unchecked state.
Create a selector with same resources :
checkbox_selector.xml :
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/checked_image" android:state_checked="true"/>
<item android:drawable="#drawable/unchecked_image"/>
</selector>
Then set this selector as button :
<CheckBox
android:id="#+id/checkbox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:button="#drawable/checkbox_selector" />
Activity:
CheckBox.setButtonDrawable((int)getResources().getColor(R.color.transparent));
Fragment:
CheckBox.setButtonDrawable((int)getActivity.getResources().getColor(R.color.transparent));
I'm trying to apply custom image to checkbox in android, for this I create an check_custom.xml file in which I define custom image for different states of check box like:
<item android:state_checked="true"
android:drawable="#drawable/btn_check_on" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#drawable/btn_check_off" /> <!-- unchecked -->
<item android:state_focused="true"
android:drawable="#drawable/btn_check_onfocus" /> <!--on focus-->
<item android:drawable="#drawable/btn_check_off" /> <!-- default -->
Three different images on three states on checked,on focus and on unchecked,
and then I assign this xml file to background attribute of check boxes,but I'm not getting required result, this technique apply the custom image as well as default image both.
Salman, set android:button instead of android:background. See also Custom checkbox image android
I didnĀ“t like the appearance of the checkbox default image when the background layout was dark, so I changed it using a xml selector in order to have a white background in the button.
The selector is needed in android:button="#drawable/selector_check"
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:text="Show password"
android:textColor="#color/white"
android:button="#drawable/selector_check"
>
And the following is the content of "drawable/selector_check.xml":
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true"
android:drawable="#android:drawable/checkbox_on_background" /> <!-- checked -->
<item android:state_checked="false"
android:drawable="#android:drawable/checkbox_off_background" /> <!-- unchecked-->
</selector>
And this is the result:
Copy the btn_check.xml from android-sdk/platforms/android-#/data/res/drawable to your project's drawable folder and change the 'on' and 'off' image states to your custom images.
Then your xml will just need android:button="#drawable/btn_check"
<CheckBox
android:button="#drawable/btn_check"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
If you want to use different default android icons, you can use android:button="#android:drawable/..."
<item android:drawable="#drawable/ON" android:state_checked="false"/>
<item android:drawable="#drawable/OFF" android:state_checked="true"/>
<item android:drawable="#drawable/ON"/>