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
Related
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);
I want to change drawable of predefined checked in
android.R.layout.simple_list_item_checked it is possible?
From Checked
and Unchecked
To Checked
and Unchecked
And I don't want to create custom list item.
This is possible but only by using a custom layout with a standard adapter, you can do this by defining the same ids as the predefined layout with your custom layout but only changing the ones you want. Unfortunately in this case, the source code for android.R.layout.simple_list_item_checked doesn't have a drawable by default. (https://github.com/android/platform_frameworks_base/blob/master/core/res/res/layout/simple_list_item_checked.xml) it uses the checkmark attribute
android:checkMark="?android:attr/textCheckMark"
I'm unsure whether you can change this, but i still reccomend using a custom layout with a standard adapter, just compare the original with your current layout and define all the same attributes with a drawable instead of a checkmark, like in the example below: It's up to you though.
<CheckBox android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="new checkbox"
android:background="#drawable/checkbox_background"
android:button="#drawable/checkbox" />
This is how you assign custom images saved in the #drawable directly depending on the state of the checkbox.
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="true" android:state_focused="true"
android:drawable="#drawable/first" />
<item android:state_checked="false" android:state_focused="true"
android:drawable="#drawable/second" />
<item android:state_checked="false"
android:drawable="#drawable/third" />
<item android:state_checked="true"
android:drawable="#drawable/fourth" />
</selector>
further reading: http://developer.android.com/reference/android/widget/CompoundButton.html#setChecked%28boolean%29
more here: http://www.android-ios-tutorials.com/android/custom-android-checkbox-radiobutton/
I am trying to customize a Button UI in Android.
I tried the following things:
btn.setBackgroundColor
btn.setBackgroundResource
btn.setBackgroundColor
But all of these are increasing the size of the Button, and because of that the Buttons near by can not be segregated (??).
Please suggest something.
If you want to apply a Hover effect then you have to do this in your XML where button layout is like
Button
android:id="#+id/xyz"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginTop="15dp"
android:background="#drawable/general_btn_hover_effect"
android:onClick="somefunction"
android:text="#string/search_number"
android:textColor="#ffffff"
android:textSize="20sp"
/>
Notice android:background="#drawable/general_btn_hover_effect" there and then in #drawable folder make a general_btn_hover_effect.xml and write this into it
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/blank_normal_bg" android:state_focused="true" android:state_pressed="false"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="true" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_hover_bg" android:state_focused="false" android:state_pressed="true"/>
<item android:drawable="#drawable/blank_normal_bg"/>
</selector>
Changing background color can not change size of button. Maybe you changed size in xml layout. Check again
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>
Is it possible to define different style for menu item depending on its enabled/disabled status?
For example, I want the text color of menu item to be gray in disabled mode and white in enabled mode.
I didn't have success with changing color dinamycally just as many people didn't on stackoverflow.
It really depends on the item you wish to customize.
Basically you can create a custom color that changes depending on its state:
colors/custom_color.xml:
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:color="#FF0000" android:state_enabled="false" />
<item android:color="#CCCCCC"/>
</selector>
Then set it to your menu item like this:
menu.findItem(R.id.action_search).getActionView().
setBackgroundResource(R.colors/custom_color.xml);
Or perhaps in the xml if available:
android:textColor="#color/custom_color"
You can use a drawable as the text colour, and in drawable you can use selector to select the colour according to enabled status. Using following drawable definition as colour will make your disabled menu items grey and the rest black.
In e.g. res/drawable/default_text_colour.xml:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_enabled="false" android:color="#android:color/darker_gray"/>
<item android:color="#android:color/white"/>
</selector>
Then, using the drawable:
<item name="android:textColor">#drawable/default_text_colour</item>
For some one else may be needed: use ToolBar instead of ActionBar, add TextView and set style as MenuItem:
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_explorer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="?attr/actionBarSize"
android:theme="#style/CustomActionBar.Theme"
android:background="#mipmap/bg_naviber">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:text="#string/title_activity_explorer2"
android:layout_gravity="start"
android:padding="#dimen/dimen_8"
style="#style/CustomActionBar.Tittle"
/>
<TextView
android:id="#+id/toolbar_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="#string/action_delete"
android:textColor="#drawable/selector_text_view"
android:padding="#dimen/dimen_16"
style="#style/CustomActionBar.Menu"
/>
<TextView
android:id="#+id/toolbar_do_delete"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="end"
android:text="#string/action_do_delete"
android:textColor="#drawable/selector_text_view"
android:padding="#dimen/dimen_16"
style="#style/CustomActionBar.Menu"
android:visibility="gone"
/>
</android.support.v7.widget.Toolbar>
set textColor by selector:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android"
>
<!-- order is important -->
<item android:state_enabled="false" android:color="#color/white_disabled"/>
<item android:state_pressed="true" android:color="#color/white"/>
<item android:color="#color/white"/>
</selector>
Using:
<style name="Theme.WordsTrainer" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
I've added the line:
<item name="android:textColor">?android:attr/textColorPrimary</item>
to themes.xml and night\themes.xml, and this works fine with day and nigth mode both.
I've spend a day diggin it, and finally found the answer in obvious place:
https://developer.android.com/guide/topics/ui/look-and-feel/darktheme