Set background checkbox android when dissable - android

to day i work with it and I get a problem.
When I set Checkbox like:
checkboxsend.setEnabled(false);
Here i want the background of checkbox not change to disable?
What can i do that..please help me! Thank
The my xml:
<CheckBox
android:id="#+id/checkboxsend"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="false"
android:enabled="false" />

You will want to use a state drawable as the background for the Checkbox, something like this should get you closer to your goal:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:state_enabled="true" android:state_checked="true" android:drawable="#android:drawable/checkbox_on_background"/>
<item android:drawable="#android:drawable/checkbox_off_background"/>
</selector>
You can make modifications to this in order to match your exact logic.
Your CheckBox will look like this:
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:enabled="false"
android:background="#drawable/custom_checkbox"
android:checked="true"/>

Try this...If you don't want to change the background Color of your check box than....
Instead:
checkboxsend.setEnabled(false);
Use:
checkboxsend.setClickable(false);

Related

android imageview android:state_selected="true" not working

I am using Image selector for changing my tab-widget image icons , when I selected any one of image view, image of that image view should change. For that i have used following code, but it is working good , but problem is android:state_pressed="true" is working but android:state_selected="true" not working in any image view , i am getting stuck from this issue , can anyone help me, answer will be appreciable.Thanks in advance.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_selected="true"/>
<item android:drawable="#drawable/ic_contacts_vippie"/>
Here is my Imageview:
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/flexi_contact_selector" />
Instead of image view use toggle button or check-box or radio button. For example I am using check box.
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/hiiMoblieContact"
android:button="#drawable/flexi_contact_selector"/>
flexi_contact_selector
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/ic_contacts_vippie_selected" android:state_checked="true" />
<item android:drawable="#drawable/ic_contacts_vippie" />
</selector>
Hope this will help you..
Imageview doesn't save states. But don't worry, there is a workaround. Either use toggleButtons and set background or use CheckableLinearlayout as parent of imageview. Xml of imageview should be like this-
<package_name.CheckableLinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/hiiMoblieContact"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:clickable="false"
android:duplicateParentState="true"
android:src="#drawable/flexi_contact_selector"/>
</package_name.CheckableLinearLayout>
The changes are highlighted in bold. Now you can handle the checkablelinearlayout's set checkedmethod. If for example you set the checkacle linearlayout's set checked true, the image will automatically change. You will have to make this CheckableLinearLayout class in your project and its link is - http://developer.android.com/intl/es/samples/CustomChoiceList/src/com.example.android.customchoicelist/CheckableLinearLayout.html

Android swtich compat icon?

I need to create the switch as the below image
I have both on and off images for it. I implemented them as
<android.support.v7.widget.SwitchCompat
android:id="#+id/swtichTournament"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:showText="false"
android:thumb="#drawable/switch_selector"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:checked="false" />
The selector XML
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:drawable="#drawable/switch_off" />
<item android:state_pressed="true" android:drawable="#drawable/switch_on" />
<item android:state_checked="true" android:drawable="#drawable/switch_on" />
<item android:drawable="#drawable/switch_off" />
This is what I get
As you can see there is some extra color which is not required. How do I remove that?
If you are using the AppCompat library, replace your switch element by a SwitchCompat element and you'll get what you want.
For more info about switchCompat, please see: https://developer.android.com/reference/android/support/v7/widget/SwitchCompat.html
You can set Switch's background to transparent in your XML:
android:background="#android:color/transparent"
i hope this link will be help you
https://androidician.wordpress.com/2014/09/24/android-custom-toggle-button-example-ios-like-toggle-buttons/

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>

Checkbox without the square android

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

Android checkbox is not visible well in black background prob

I want make a list of Checkbox with white text in black background. We are using following code:
CheckBox chkAdditionalPack = new CheckBox(MainActivity.this);
chkAdditionalPack.setTag(j);
chkAdditionalPack.setText(offerPackageListForAddl.get(j).getOfferPackageName().toString());
chkAdditionalPack.setTextColor(Color.WHITE);
It gives a view like below:
The problem is now boxes of checkboxes is not clearly visible. How can I make it clearly visible keeping intact other parts?
If you are using android-studio, select your checkbox, and in the properties, set 'buttonTint' property value to whatever color you want using the editor. Or, if you prefer the XML solution, use
android:buttonTint="#color/white"
Note: This assumes that the color white is defined.
try the below xml code ref from solution
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="true"
android:drawable="#drawable/cbchk_blue"
android:state_focused="true" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="false" >
</item>
<item
android:state_checked="false"
android:drawable="#drawable/cbunchk_blue"
android:state_focused="true" >
</item>
</selector>
One really simple way to do it, within the xml for the checkbox, simply set the android:background variable to the colour you wish.
Instead of having the text and check box created together like this, simply create a custom list item layout xml file.
e.g custom_list_item.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
/>
</LinearLayout>
Simply use this :
android:buttonTint="#FFFFFF"
** replace #FFFFFF with any color

Categories

Resources