I have button style:
<style name="NavDrawerButton" parent="android:Widget.Button">
<item name="android:background">#drawable/navdrawer_item</item>
<item name="android:textColor">#color/navdrawer_item_text</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingLeft">16dp</item>
<item name="android:paddingRight">16dp</item>
<item name="android:textSize">16sp</item>
<item name="android:clickable">true</item>
</style>
In /drawable:
<!--Navdrawer_item.xml-->
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:state_pressed="true"
android:state_enabled="true"
android:drawable="#drawable/navdrawer_item_pressed"/>
<item
android:state_enabled="true"
android:drawable="#drawable/navdrawer_item_enabled" />
</selector>
<!--navdrawer_item_enabled-->
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/backrepeat">
</item>
</layer-list>
My button:
<Button
android:id="#+id/category1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/NavDrawerButton"
android:text="#string/category1"/>
Problem is that padding doesn't work. There is no padding between left edge of button and text inside it. I can't understard why it is so.
Could you help me please?
I have test your code there is no error...might be in your parent layout...
string.xml
<style name="NavDrawerButton" parent="android:Widget.Button">
<item name="android:background">#drawable/image1</item>
<item name="android:layout_marginTop">1dp</item>
<item name="android:gravity">start|center_vertical</item>
<item name="android:paddingLeft">10dp</item>
<item name="android:textSize">16sp</item>
<item name="android:clickable">true</item>
</style>
Layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.MainActivity"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="sdfg"
style="#style/NavDrawerButton" >
</Button>
</LinearLayout>
output
Related
I have created following style for custom AppCompatButton.
<style name="AppTheme.secondary_button" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/secondary_button_color</item>
<item name="colorControlActivated">#color/menu_blue</item>
<item name="android:colorButtonNormal">#color/secondary_button_color</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#color/gray</item>
</style>
What is the attribute to change text color on button pressed?
To do this, you just need to replace the textColor you specified with a selector:
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#color/white" />
<item android:state_focused="true" android:color="#color/grey" /> <!-- (for accessibility) -->
<item android:color="#color/black" />
</selector>
and then assign it like this:
android:textColor="#drawable/button_foreground_selector"
<android.support.v7.widget.AppCompatButton
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
style="#style/CustomAppCompatButtonStyle"
android:text="Hi, Neo"/>
Not clicked button:
Clicked button:
Style I used:
<style name="CustomAppCompatButtonStyle" parent="Base.Widget.AppCompat.Button">
<item name="colorButtonNormal">#color/red</item>
<item name="colorControlActivated">#color/blue</item>
<item name="android:colorButtonNormal">#color/green</item>
<item name="android:textColorPrimary">#color/gray</item>
<item name="android:textColor">#drawable/button_foreground_selector</item>
</style>
I have check box in my contacts list to select that. But check box is in white color,it is not visible...So i need to change that border to black color...Please help me to change the code
This is my layout code
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="?android:dividerVertical"
android:showDividers="middle"
android:dividerPadding="5dp"
tools:context=".MainActivity" >
<ImageButton
android:id="#+id/image"
android:layout_width="#dimen/contact_image"
android:layout_height="50dp"
android:src="#drawable/contacts_image" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="false"
android:focusable="true"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="0dp"
android:paddingTop="1dp"
android:textColor="#color/black"
android:textSize="18sp"
/>
<TextView
android:id="#+id/mobile_number"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/black"
android:textSize="15sp"
android:paddingLeft="5dp"
android:paddingRight="0dp"
/>
</LinearLayout>
<CheckBox
android:id="#+id/check_box"
android:layout_width="#dimen/check_box_width"
android:layout_height="50dp"
android:visibility="visible"
android:clickable="true"/>
</LinearLayout>
This is my style.xml code
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="AppTheme" parent="Theme.AppCompat.Light">
<item name="actionBarStyle">#style/MyActionBar</item>
</style>
<style name="MyActionBar" parent="#style/Widget.AppCompat.Light.ActionBar.Solid.Inverse">
<item name="background">#color/scandal</item>
<item name="titleTextStyle">#style/MyActionBarTitleText</item>
</style>
<style name="MyActionBarTitleText" parent="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
<item name="android:textColor">#color/white</item>
</style>
<style name="ActionBarCompat">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">50dp</item>
<item name="android:orientation">horizontal</item>
</style>
<style name="DashboardButton">
<item name="android:layout_gravity">center_vertical</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center_horizontal</item>
<item name="android:drawablePadding">2dp</item>
<item name="android:textSize">16dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#ff29549f</item>
<item name="android:background">#null</item>
</style>
<style name="FooterBar">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">40dp</item>
<item name="android:orientation">horizontal</item>
<item name="android:background">#dedede</item>
</style>
<style name="Divider">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">1dp</item>
<item name="android:background">?android:attr/listDivider</item>
</style>
</resources>
instead changing the default in style xml file. do this.
create this your_custom.xml file and and save it in drawable folder
your_custom.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/unselected" android:state_checked="false"><color android:textColor="#your_color" />
</item>
<item android:drawable="#drawable/selected" android:state_checked="true"><color android:textColor="#your_color" />
</item>
<item android:drawable="#drawable/uncheck"></item>
<!-- default -->
</selector>
you will have to create three images for checkbox display
1.selected
2.unselect
3.uncheck
after all this done set attribute to your checkbox in layout
android:button="#drawable/your_custom"
After specifying a custom style for some buttons that I am using for an Android app, I noticed that a mysterious black border appearing around the button when clicked. I have tried overriding the border in my style, but nothing seems to work. How can I get rid of it?
orange_button.xml
<item android:state_pressed="true" android:drawable="#color/OrangePastel"/>
<item android:state_pressed="false" android:drawable="#color/OrangePastel"/>
<item android:state_focused="true" android:drawable="#color/GreenPastel"/>
<item android:state_focused="false" android:drawable="#color/GreyPastel"/>
<item android:state_selected="true" android:drawable="#color/YellowPastel"/>
<item android:state_selected="true" android:drawable="#color/PurplePastel"/>
<item android:state_window_focused="true" android:drawable="#color/LavenderPastel"/>
<item android:state_window_focused="false" android:drawable="#color/WhitePastel"/>
<item android:state_checkable="false" android:drawable="#color/RedPastel"/>
<item android:state_checkable="true" android:drawable="#color/DarkGreenPastel"/>
<item android:state_hovered="true"
android:drawable="#color/OrangePastel"/>
<item android:state_checked="false"
android:drawable="#color/OrangePastel"/>
<item android:state_checked="true" android:drawable="#color/OrangePastel"/>
activity_button_test.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="ButtonTestActivity">
<Button
android:id="#+id/testButton2"
android:text="#string/hello_world"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/orange_button"/>
</LinearLayout>
Unpressed
Pressed
EDIT: try adding this in your styles.xml:
<resources>
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyle">android:borderlessButtonStyle</item>
</style>
</resources>
I have follow the answer in Android: Create a toggle button with image and no text everything was fine but when I load my application and when I click on my toggle button, nothing happen like it didn't change from off state to on state. So what do i have to do to make the sliding works? Do i need to code anything if yes what should i code?
Thanks
btn_toggle_bg.xml
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+android:id/background"
android:drawable="#android:color/transparent" />
<item android:id="#+android:id/toggle"
android:drawable="#drawable/btn_toggle" />
</layer-list>
btn_toggle.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false"
android:drawable="#drawable/close" />
<item android:state_checked="true"
android:drawable="#drawable/open" />
</selector>
layout.xml
<ToggleButton
android:id="#+id/toggleButton"
style="#style/toggleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn_toggle_bg"
android:onClick="onToggleClicked"
android:layout_marginLeft="150dp" />
style.xml
<style name="Widget.Button.Toggle" parent="android:Widget">
<item name="android:background">#drawable/btn_toggle_bg</item>
<item name="android:disabledAlpha">?android:attr/disabledAlpha</item>
<style name="toggleButton" parent="#android:Theme.Black">
<item name="android:buttonStyleToggle">#style/Widget.Button.Toggle</item>
</style>
Follow the three steps and after it all your app's togglebutton got the new style!
toggle.xml: (drawable-folder)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_checked="false" android:drawable="#drawable/toggle_off" />
<item android:state_checked="true" android:drawable="#drawable/toggle_on" />
</selector>
styles.xml: (values-folder)
<style name="toggle" parent="#android:style/Widget.Button.Toggle">
<item name="android:background">#drawable/toggle</item>
<item name="android:textOn"> </item>
<item name="android:textOff"> </item>
</style>
and last step you have to teach the current AppTheme that you have a custom toggle-button:
<style name="AppTheme" parent="android:Theme.Holo.Light">
<item name="android:buttonStyleToggle">#style/toggle</item>
</style>
I have styles
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="TextStyle" >
<item name="android:textSize">8sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
</style>
<style name="TextStylePressed" >
<item name="android:textSize">18sp</item>
<item name="android:textColor">#0A0A0A</item>
<item name="android:typeface">serif</item>
<item name="android:textStyle">bold</item>
<item name="android:background">#C2C2C2</item>
</style>
</resources>
and I use selector arrow.xml
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#style/TextStylePressed" android:state_selected="true"/>
<item android:drawable="#style/TextStylePressed" android:state_pressed="true"/>
<item android:drawable="#style/TextStylePressed" android:state_focused="true"/>
<item android:drawable="#style/TextStyle" />
</selector>
The problem is that I can not understand - if it works?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#drawable/arrow" // <---- Framed here
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/arrow" // <---- then here
android:orientation="vertical" >
</LinearLayout>
Consequently, after all attempts nothing happens
AFAIK selectors can only be defined to select a drawable and not any other attribute. If you check the doc , it compiles to StatelistDrawable, So i believe it can only be used for drawables