CustomView Styling - android

I have a problem with styling customview.
I'd like to use different styles in different layouts. But my styles aren't applied.
CustomView layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android"
<ImageView
android:id="#+id/img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
</merge>
Definition in another layout:
<com.buka.customviews.Bukaview
android:id="#+id/bukaId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/BukaStyle1">
styles.xml
<style name="BukaStyle1">
<item name="android:textSize">10sp</item>
</style>
<style name="BukaStyle2">
<item name="android:textSize">20sp</item>
</style>
Thanks

You should create a custom attribute :
<attr name="BukaStyleTextSize" format="dimensions" />
Then in the TextView tag of your custom view layout, put :
android:textSize="?attr:BukaStyleTextSize"
And finally, in your styles in styles.xml put :
<item name="BukaStyleTextSize>10sp</item>

Related

Using <include> in android.support.v7.widget.GridLayout not displaying correctly

I'm trying to use <include> to include ImageButtons inside a android.support.v7.widget.GridLayout. The problem is, if I don't specify all the attributes explicitly on my <include> elements, they don't display properly. In other words, the include is pointless since I have to redeclare all the attributes on every <include> element.
dialpad_button.xml
<?xml version="1.0" encoding="utf-8"?>
<ImageButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:layout_width="0dp"
android:layout_height="0dp"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_gravity="fill"
android:layout_margin="5dp"
android:scaleType="centerInside"/>
dialpad_view.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.GridLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:grid="http://schemas.android.com/apk/res-auto"
android:id="#+id/dialpad_grid_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
grid:alignmentMode="alignBounds"
grid:columnCount="2"
grid:rowCount="1">
<include
android:id="#+id/dialpad_view_btn1"
android:src="#drawable/dialpad_1"
layout="#layout/dialpad_button" />
<include
android:id="#+id/dialpad_view_btn2"
android:src="#drawable/dialpad_2"
layout="#layout/dialpad_button" />
</android.support.v7.widget.GridLayout>
If I declare all the attributes directly on the <include>s, here's what it looks like in XML:
<include
layout="#layout/dialpad_button"
android:id="#+id/dialpad_view_btn1"
android:src="#drawable/dialpad_1"
android:layout_width="0dp"
android:layout_height="0dp"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_gravity="fill"
android:layout_margin="5dp"
android:scaleType="centerInside" />
As you can see in the comparison image below, the buttons now scale properly but the images are nowhere to be seen.
And if I change the <include>s to ImageButton, things work as expected.
<ImageButton
android:id="#+id/dialpad_view_btn1"
android:src="#drawable/dialpad_1"
android:layout_width="0dp"
android:layout_height="0dp"
grid:layout_columnWeight="1"
grid:layout_rowWeight="1"
grid:layout_gravity="fill"
android:layout_margin="5dp"
android:scaleType="centerInside" />
Is there a workaround to this?
Since I'm going to have 12 near-identical buttons in my dialpad, I'd really like to clean the XML up by including a "template" and only modifying the necessary attributes for each button (i.e. src and id).
EDIT
Trying to use styles, as suggested in one answer, did not work. None of the views that I applied the styles to get displayed. What's even more strange, even if I just apply the style to one of the views in the GridLayout, only the last view gets displayed (I've shortened down the sample code here to only two views for readability, in reality I have twelve).
Here's the style I tried using:
<?xml version="1.0" encoding="utf-8"?>
<resources
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:grid="http://schemas.android.com/apk/res-auto" >
<style name="dialPadButtonStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="grid:layout_columnWeight">1</item>
<item name="grid:layout_rowWeight">1</item>
<item name="grid:layout_gravity">fill</item>
<item name="android:layout_margin">5dp</item>
<item name="android:scaleType">centerInside</item>
<item name="android:background">#android:color/transparent</item>
</style>
</resources>
Maybe you can use a style to declare the common attributes and then set it for every button.
<style name="dialPadButtonStyle">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="android:layout_columnWeight">1</item>
<item name="android:layout_rowWeight">1</item>
<item name="android:layout_margin">5dp</item>
<item name="android:src">#android:drawable/ic_menu_camera</item>
</style>
I also added: columnCount to 3 and rowCount to 4 for GridLayout and added 12 ImageButtons with that style. The final result is this:

Use custom button as parent in custom style

In my project I use special fonts for my buttons. So I've added the PixlUI library so I can set the font in xml.
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_login"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/login"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btn_broker_register"
style="#style/custom_button_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/make_new_profile_broker"
pixlui:typeface="AvenirNextCondensed-Regular.ttf" />
These are my buttons, since they all have the same typeface I want to include the typeface in the 'custom_button_style'
This my custom style:
<style name="custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
</style>
How do I include
pixlui:typeface
in this style?
It´s not clear if we can´t see the complete xml layout file, but things that often happens is that the poeple forget to add the xmlns attribute to the parent. You parent layout should look something like this. It´s part of this library and the important thing is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui" <----thats the important attribute
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
You don´t need to add something in Your style.xml. So if this does not fix Your problem, come back.
EDIT
It is not tested yet, but for the style it could be:
<?xml version="1.0" encoding="utf-8" ?>
<resources xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui">
<style "custom_button_style" parent="#android:style/Widget.Button">
<item name="android:textSize">#dimen/hello_medium_fontsize</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:background">#drawable/custom_btn_background</item>
<item name="android:layout_marginBottom">#dimen/login_button_margin</item>
<item name="pixlui:typeface">AvenirNextCondensed-Regular.ttf</item>
</style>

Creating style for checkbox

I'm putting some settings to the checkbox, and it works. But when I take them into a style and apply them, it doesn't. Per below, first button uses the style (NOT working), second one direct settings (works). What's wrong with my style file, per below? Tks
<CheckBox android:id="#+id/mon"
android:text ="Mon"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="Monday"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
style="#style/FilledCheckButton"
/>
<CheckBox android:id="#+id/tue"
android:text="Tue"
android:layout_width="80dp"
android:layout_height="80dp"
android:contentDescription="Tuesday"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/mon"
android:gravity="center"
android:background="#drawable/v4_btn_weekday"
android:button="#drawable/v4_btn_weekday"
/>
My style file:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="FilledCheckButton" parent="android:style/Widget.CompoundButton.CheckBox"
android:background="#drawable/v4_btn_weekday"
android:button="#drawable/v4_btn_weekday"
android:gravity="center">
</style>
</resources>
Try this way
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="FilledCheckButton" parent="#android:style/Widget.CompoundButton.CheckBox">
<item name="android:background">#drawable/v4_btn_weekday</item>
<item name="android:button">#drawable/v4_btn_weekday</item>
<item name="android:gravity">center</item>
</style>
For more information go to http://aproblemlikemaria.wordpress.com/2011/09/26/theming-and-styling-in-android/

Refrencing a selector drawable from a style results blank imagebutton

I want to have a ImageButtons changing the image based on its state. (same as "like" button in nine gag and facebook apps).
I have defined a style for each of them that references a selector as a drawable but when I run the program, images doesn't appear. Can you tell me what's wrong with this approach? (sorry for my bad English)
comment_actions_style in values folder:
<style name="LikeStyle">
<item name="android:src">#drawable/like</item>
</style>
<style name="DislikeStyle">
<item name="android:src">#drawable/dislike_normal</item>
</style>
<style name="ReplyStyle">
<item name="android:src">#drawable/reply_normal</item>
</style>
<style name="ShareStyle">
<item name="android:src">#drawable/share</item>
</style>
</resources>
and like drawable in drawable folder: (other buttons are the same)
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/like_clicked" android:state_pressed="true"/>
<!-- pressed -->
<item android:drawable="#drawable/like_focused" android:state_focused="true"/>
<!-- focused -->
<item android:drawable="#drawable/like_normal"/>
<!-- default -->
</selector>
EDIT:
I forgot to mention, I have a list view that its items are user comments and comments have buttons described above.
here is my items layout (part of it)
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" />
</LinearLayout>
and comment_actions layout:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comment_action"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="5dip">
<ImageButton
android:id="#+id/comment_action_img"
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#android:color/transparent"
/>
<TextView
android:id="#+id/comment_action_num"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/comment_action_img"
android:gravity="center"
android:layout_marginLeft="5dip"/>
</RelativeLayout>
These buttons also inherit from a layout if that's matter.
Here, your are setting style for comment_actions layout, and it is a Relative Layout, which won't be abled to response to your "android:src" attribute.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/comment_content"
android:orientation="horizontal"
>
<include
android:id="#+id/like"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
layout="#layout/comment_actions"
style="#style/LikeStyle" /> <!--here,you are setting a style for the RelativeLayout -->
</LinearLayout>

How to set the textsize, textcolor and other parameters for the imagebutton in android?

How to set the text size, text color and other parameters in one xml and than assign them to the imagebutton in android?
I can set the parameters manually for each button, but if there are more than 3 buttons it becomes a nightmare. What is the best practices for doing this?
You can use styles for that
http://developer.android.com/guide/topics/ui/themes.html
Create a style depending on your requirement's add that to your button like this style="#style/your_style". look at this tutorial Styles and Themes
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="CustomText" parent="#style/Text">
<item name="android:textSize">20sp</item>
<item name="android:textColor">#008</item>
</style>
</resources>
<?xml version="1.0" encoding="utf-8"?>
<EditText
style="#style/CustomText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World!" />
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#00FF00"
android:typeface="monospace"
android:text="#string/hello" />

Categories

Resources