Android Set ImageView image src in Style - android

I need to set the ImageView's src within my style, but I cant find a good example of anyone doing this and when I try the below code, it crashes the app.
<ImageView android:src="#style/TitleBarImage"></ImageView>
<style name="TitleBarImage">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">#drawable/whiteButton</item>
</style>
This should be simple. What gives?

Your xml is incorrect:
<ImageView style="#style/TitleBarImage" />
And styles.xml :
<style name="TitleBarImage">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:src">#drawable/whiteButton</item>
</style>

Related

Is it possible to set constraint layout properties in a style?

When I set the below constraint layout properties within the activity's xml source I get the expected outcome, but when I set them within a style, the set values don't get processed at all.
<style name="vrm_entry_number_button" parent="#android:style/Widget.Button">
<item name="android:layout_margin">7dp</item>
<item name="android:background">#496587</item>
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">0dp</item>
<item name="app:layout_constraintHeight_default">percent</item>
<item name="app:layout_constraintHeight_percent">0.90</item>
<item name="app:layout_constraintWidth_default">percent</item>
<item name="app:layout_constraintWidth_percent">0.10</item>
</style>
What could this be down to and is there an alternative?
Do as below and it should work.
<style name="vrm_entry_number_button" parent="#android:style/Widget.Button">
<item name="layout_constraintHeight_default">percent</item>
<item name="layout_constraintHeight_percent">0.90</item>
<item name="layout_constraintWidth_default">percent</item>
<item name="layout_constraintWidth_percent">0.10</item>
</style>

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"/>

Can use xmlns on styles to define cardView corner radius?

I wanna make something like this:
<style name="cardViewInfo">
<item name="android:layout_marginLeft">#dimen/small_padding_card_view</item>
<item name="android:layout_marginRight">#dimen/small_padding_card_view</item>
<item name="android:layout_marginBottom">#dimen/small_padding_card_view</item>
<item name="android:layout_marginTop">#dimen/large_padding_card_view</item>
<item name="card_view:cardCornerRadius">2dp</item>
</style>
But I have problems with the name card_view thats is not definded, my question is if I can make something like on the layout's to use this namespace:
xmlns:card_view="http://schemas.android.com/apk/res-auto"
Best regards.
So, although this isn't exactly a custom style, it IS a property outside of the android xmlns (XML Namespace).
I found that this works for me.
<resources>
<style name="StyleName" parent="CardView.Light">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:elevation">50dp</item>
<item name="android:paddingLeft">6dip</item>
<item name="android:paddingRight">6dip</item>
<item name="cardUseCompatPadding">true</item>
<item name="cardBackgroundColor">#fff</item>
<item name="cardCornerRadius">6dp</item>
</style>
</resources>
If you have any other errors, can you please post them as an edit to your original question.

textAppearance not work in TextView

I show linked text in TextView.
At beginning , My code like this:
<TextView
style="#style/informed_consent_check_TextView"
android:id="#+id/accept_content_1"
android:text="#string/informed_consent_accept"
/>
<style name="informed_consent_check_TextView" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textSize">13dp</item>
<item name="android:textColor">#fff</item>
<item name="android:textColorLink">#fff000</item>
</style>
It works alright.The link text display like this:
That's what I want.
However , after I move attributes about text into a style . It works wrong.
<style name="informed_consent_content_textAppearance">
<item name="android:textSize">13dp</item>
<item name="android:textColor">#fff</item>
<item name="android:textColorLink">#fff000</item>
</style>
<style name="informed_consent_check_TextView" >
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textAppearance">#style/informed_consent_content_textAppearance</item>
</style>
The link text display the wrong color:
And then , I move the textAppearance attribute into TextView. But it still wrong.
<TextView
style="#style/informed_consent_check_TextView"
android:id="#+id/accept_content_1"
android:text="#string/informed_consent_accept"
android:textAppearance="#style/#style/informed_consent_content_textAppearance"/>
My device is 4.1.2 .
Who can help me . Thanks a lot.
make change in textview attribute
android:textAppearance = "#style/informed_consent_content_textAppearance"

applying style to textview drawable

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?

Categories

Resources