applying style to textview drawable - android

I have a layout for a TextView and drawable as such...
<LinearLayout style="#style/dashboard_content_horizontal" >
<ImageView
style="#style/dashboard_imageview"
android:src="#drawable/menu6" />
<TextView
style="#style/dashboard_textview"
android:text="#string/menu6_text />
</LinearLayout>
as you can see the image uses a style attribute which looks like this
<style name="dashboard_imageview">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">1</item>
<item name="android:scaleType">fitCenter</item>
</style>
the linear layout's style surrounding it looks like this
<style name="dashboard_content_horizontal">
<item name="android:layout_width">0px</item>
<item name="android:layout_height">fill_parent</item>
<item name="android:layout_weight">3</item>
<item name="android:orientation">vertical</item>
<item name="android:layout_gravity">center</item>
<item name="android:gravity">center</item>
</style>
and TextView's style
<style name="dashboard_textview">
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:gravity">center</item>
<item name="android:textSize">20sp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
</style>
this all works and looks exactly how I want it to look. however, because of this eclipse warning
"This tag and its children can be replaced by one
and a compound drawable" I decide to try to do this with just a TextView and drawable
like such..
<TextView
android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Menu6"
android:drawableBottom="#drawable/my_drawable"
android:padding="5dp" />
this renders the image too small.. I don't see a way to apply a style to the image to get it like I want it.. anyone know the answer? is even possible?

Related

How to apply letter spacing in custom button?

I have one button and letterspacing is not working with it. Please check my following snippet code
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:letterSpacing">0.1</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>
You can't apply letter spacing in button/custom button because their is some limitation in button component in which you can't make text appearance and background customization and more limitation
the solution for this is instead of button you can use text view and make custom text view it will also work similar to button and customize it according to requirement.
for example :-
https://stackoverflow.com/questions/9477336/how-to-make-a-custom-textview#:~:text=Create%20a%20Custom%20View%20for%20Textview.%20Make%20the,values%20Make%20entries%20of%20all%20fonts%20in%20strings.xml
Add android:letterSpacing in PrimaryButton_v2 it will work perfectly.
<style name="ButtonText_v2" parent="TextAppearance.AppCompat">
<item name="fontFamily">#font/silka_regular</item>
<item name="android:textSize">#dimen/dimen_14dp</item>
</style>
<style name="PrimaryButton_v2" parent="Widget.AppCompat.Button">
<item name="android:background">#drawable/button_selector_primary</item>
<item name="android:textColor">#color/white</item>
<item name="textAllCaps">true</item>
<item name="textAppearance">#style/ButtonText_v2</item>
<item name="android:letterSpacing">0.1</item>
</style>
layout.xml File
<Button
android:text="Button"
android:layout_height="56dp"
android:layout_width="200dp"
style="#style/PrimaryButton_v2"
android:layout_margin="15dp"
/>

Can't inherit button color on style

I am trying to define button properties on style.
My code is
<style name="button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
And using as
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"
style="#style/button"/>
But the color of the button's normal state is not changing.
This button style is only for some buttons. Not for all butons.
What to do now?
edit after question clarification
Save you style.xml in res/values/
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="custom_button" parent="#android:style/Widget.Button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
Apply it like so:
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"
style="#style/custom_button"/>
first answer
You need to inherit the parent style.
<style name="Button" parent="#android:style/Widget.Button">
<item name="android:paddingTop">5dp</item>
<item name="android:paddingBottom">5dp</item>
<item name="android:textAllCaps">false</item>
<item name="colorButtonNormal">#f37022</item>
<item name="android:textColor">#ffffff</item>
</style>
That way all your Button's that are not styled will have your custom style. i.e. no need to put in the style value.
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Prepaid Package"/>

Android layout error: "Resource id <res_id> is not of type STYLE (instead attr)"

I'm seeing an error I've never seen before w/ Android that's preventing some of my layout files from rendering in Eclipse. They render fine at runtime.
The error that shows up in the graphical layout editor (and my Error log) is:
"Resouce id 0x1010081 is not of type STYLE (instead attr)"
I've searched for resource id 0x1010081 in my R files, and I can't find it so I'm guessing maybe somehow I have a conflict with a built in Android attr. I've also verified that all of my style attributes below point to actual styles and not attr. Any help appreciated.
Here's my full layout code:
<RelativeLayout 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:background="#drawable/bkg_light"
android:padding="#dimen/padding_med" >
<RelativeLayout
android:id="#+id/relativeLayout_activity"
android:layout_width="fill_parent"
android:layout_height="#dimen/button_height"
android:background="#drawable/xml_button"
android:padding="#dimen/padding_med" >
<TextView
style="#style/text_large_bold_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/activity" />
<TextView
android:id="#+id/textView_activity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/margin_med"
android:layout_toLeftOf="#+id/textView_arrow_right_start_date"
android:text="#string/none_selected"/>
<TextView
android:id="#+id/textView_arrow_right_start_date"
style="#style/arrow_right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true" />
</RelativeLayout>
<!-- kv Duration -->
<RelativeLayout
android:id="#+id/relativeLayout_duration"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/relativeLayout_activity"
android:layout_marginTop="#dimen/margin_large"
android:background="#drawable/xml_button">
<TextView
android:id="#+id/textView_duration"
style="#style/text_large_bold_white"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:text="#string/duration_lc"
android:paddingLeft="#dimen/padding_med"/>
<Spinner
android:id="#+id/spinner_duration"
android:layout_width="wrap_content"
android:layout_height="#dimen/button_height"
android:layout_alignParentRight="true"
android:entries="#array/array_durations"
android:prompt="#string/duration_lc"
android:spinnerMode="dropdown"
android:clickable="false"/>
</RelativeLayout>
</RelativeLayout>
And here's my styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="arrow_right">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_xxlarge</item>
<item name="android:text">#string/arrow_right</item>
</style>
<style name="button_blue">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#drawable/selector_button_blue</item>
<item name="android:padding">#dimen/padding_med</item>
</style>
<style name="button_blue_small">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#drawable/selector_button_blue_small</item>
<item name="android:padding">#dimen/padding_med</item>
</style>
<style name="button_dark">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/white</item>
<item name="android:background">#drawable/selector_button_dark</item>
<item name="android:padding">#dimen/padding_med</item>
</style>
<style name="button_light">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
<item name="android:background">#drawable/selector_button_light</item>
<item name="android:padding">#dimen/padding_med</item>
</style>
<style name="button_light_small">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
<item name="android:textSize">#dimen/text_small</item>
<item name="android:background">#drawable/selector_button_light</item>
<item name="android:padding">#dimen/padding_small</item>
</style>
<style name="text_small_gray_light">
<item name="android:textSize">#dimen/text_small</item>
<item name="android:textColor">#color/gray_light</item>
</style>
<style name="text_med_bold_gray_light">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_med</item>
<item name="android:textColor">#color/gray_light</item>
</style>
<style name="text_large_bold_white">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_large</item>
</style>
<style name="text_large_bold_black">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_large</item>
<item name="android:textColor">#color/black</item>
</style>
<style name="text_large_bold_gray_dark">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_large</item>
<item name="android:textColor">#color/gray_dark</item>
</style>
<style name="text_large_bold_gray_light">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_large</item>
<item name="android:textColor">#color/gray_light</item>
</style>
<style name="text_large_bold_white">
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/text_large</item>
<item name="android:textColor">#color/white</item>
</style>
<style name="text_large_white">
<item name="android:textSize">#dimen/text_large</item>
<item name="android:textColor">#color/white</item>
</style>
</resources>
And here's my custom theme.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources xmlns:android="http://schemas.android.com/apk/res/android">
<style name="Theme.P90X" parent="#style/Theme.Sherlock">
<item name="android:textColor">#ffffffff</item>
</style>
</resources>
I also have this problem, with the same resource ID showing up. It disappears when I change android:spinnerMode to "dialog" so I suspect it's not a problem with your code.
The id in question refers to the android attribute spinnerStyle. See the doc for R.attr
I found a workaround, namely replace
android:spinnerMode="dropdown"
with
android:spinnerStyle="#android:style/Widget.Spinner.DropDown".
Have you tried changing the each style property to something like this:
style="?attr/text_large_bold_white"
As JulianSymes says the problem is
android:spinnerMode="dropdown"
His solution (replace for android:spinnerStyle="#android:style/Widget.Spinner.DropDown") runs on my phone but not on my tablet
My solution is more simple: DELETE THE LINE.
spinnerMode="dropdown" is by default, therefore it's unnecesary
Delete all style elements and then add them one after another and always check if it still renders. So you can at least find out which one's to blame.
As Google points to this page for this error, and for the sake of posterity...
In my case, in a whim, I closed and reopened eclipse and boom the error was gone and eclipse had no problem rendering the preview.
Though I strongly believe this may not be the case for all.
If you feel you have done everything right, I guess, it won't hurt restarting eclipse.

Image in button with drawableTop can not be resized

I am having a simple dashboard layout with several Button's that show image and text.
<Button
android:id="#+id/some_id"
style="#style/DashboardButton"
android:drawableTop="#drawable/image"
android:text="#string/text" />
<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">20dp</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#000000</item>
<item name="android:background">#null</item>
</style>
However, I can't seem to influence the size of the image by modifying layout_width or layout_height. I need the images to be bigger on tablets, now they are too small there.
Is there a way to do this easily?

problem changing style in a Spinner

i need to implement an application in android using styles. I know that, difining an style for each object in styles.xml, it's possible to stablish diferent styles for: Textview, Buttons, editviews..but in the special case of spinner it doesn't work. this is my example:
this is the layout main.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
style="#style/TextviewVerde"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/escribenombre"
/>
<EditText
style="#style/EditviewAzul"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TxtNombre"
/>
<Spinner
style="#style/SpinnerRojo"
android:textColor="#FFFFFF"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/TxtSpinner"
android:prompt="#string/seleccionetexto"
/>
<Button
style="#style/BotonBlanco"
android:id="#+id/BtnHola"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Pulse aqui"
/>
</LinearLayout>
and this is the styles.xml:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="TextviewVerde" >
<item name="android:textColor">#00FF00</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">5pt</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="TextviewRosa" >
<item name="android:textColor">#5F04B4</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">15pt</item>
<item name="android:gravity">center</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
</style>
<style name="EditviewAzul" >
<item name="android:textColor">#0000FF</item>
<item name="android:textSize">8pt</item>
<item name="android:layout_width">fill_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:shadowColor">#0000FF</item>
</style>
<style name="SpinnerRojo" >
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#DF0101</item>
<item name="android:gravity">center</item>
<item name="android:typeface">normal</item>
<item name="android:textSize">11pt</item>
<item name="android:drawSelectorOnTop">true</item>
</style>
<style name="BotonBlanco">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">12pt</item>
<item name="android:gravity">center_vertical|center_horizontal</item>
<item name="android:textColor">#FFFFFF</item>
<item name="android:shadowColor">#FF000000</item>
<item name="android:clickable">true</item>
</style>
</resources>
every object works well, but the spinner doesn't change its appearance. My boss told me that i have to implement styles without changing the *.java file, what can i do?? i need to change the colour of the spinner options, the backgound, add some pics.. etc. thanks a lot for the help
http://www.mokasocial.com/2011/03/easily-create-a-default-custom-styled-spinner-android/
To make custom styled dropdown when the spinner is clicked, you should create a custom Adapter with a reference to your layout xml defining the rows of the list.
For example:
String[] elements = {"optionA", "optionB"};
Spinner s = (Spinner) findViewById(R.id.your_spinner_id);
ArrayAdapter adapter = new ArrayAdapter<String>(this,
R.id.textViewId, elements);
adapter.setDropDownViewResource(R.layout.your_dropdown_layout);
Where textViewId should be a child view in your_dropdown_layout.
Hope that helped.
Ripityom

Categories

Resources