What is the difference between the two syntaxes? - android

android:textAppearance="#android:style/TextAppearance.Small"
android:textAppearance="?android:textAppearanceSmall"
Both are giving the same result. Is there any difference in there compilation efficiency.

android:textAppearance="#android:style/TextAppearance.Small"
is text style setting directly.
android:textAppearance="?android:textAppearanceSmall"
is the refrence text style that set value on this Activity or this TextView's Theme . Like this:
<style name="MyStyle">
<item name="textAppearanceSmall">#style/textStyle</item>
</style>

Related

Color of the text is not changing when style including textColor is applied to textAppearance of textView

I want to reduce my xml code repetition. So I made some standard styles for text in textView. We can apply styles under 'style' attribute as well as 'android:textAppearance' attribute in textView.
Below are some styles I made for text appearance-
<style name="Grey">
<item name="android:textColor"> #333333 </item>
</style>
<style name="CodeFont" parent="#android:style/TextAppearance.Medium">
<item name="android:textColor"> #00FF00 </item>
<item name="android:typeface">monospace</item>
<item name="android:textSize">20sp</item>
</style>
When I apply these styles under 'textAppearance' attribute the color of the text is not changing in none of the above styles. It's working under 'style' attribute of textView.
//textColor not working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textAppearance="#style/CodeFont"/>
//textColor working
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
style="#style/CodeFont"/>
I want them to work under 'textAppearance' attribute so that I can apply some other style under 'style' attribute. And according to android documentation we can apply textColor styles under 'textAppearance' attribute.
Please suggest some solution to this.
Thanks
Try setting the text color in your widget as null like this:
<TextView
android:id="#+id/profile_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Full Name"
android:textColor="#null" //add this line
android:textAppearance="#style/CodeFont"/>
Also, I think you should try to Invalidate cache and Restart Android Studio. Import and linking issues can be solved like this sometimes.
This snippet works for me
<style name="fonts" parent="TextAppearance.AppCompat">
<item name="android:textColor">#245546</item>
<item name="android:textSize">30dp</item>
</style>
and textview is
<TextView
android:id="#+id/sample"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Welcome to stackoverflow"
android:textAppearance="#style/fonts"/>
Sometimes if you don't give android:textColor in styles you can have this issue that text color won't appear in your TextView, So the solution is to just give completely <item name="android:textColor">#color/yourColor</item> in styles rather than this <item name="textColor">#color/yourColor</item>.. your problem would be resolved :)

Changing android:layout_height and android:gravity in styles.xml

I'm new to android development and I've stumbled upon a problem I couldn't solve with the help of existing StackOverflow questions.
In the list of custom items I'm loading a TextView object which can use 2 different styles (both defined in styles.xml) - if there's a secondary TextView object, the style applied is itemHeadingText_Medium, and if not it's itemHeadingText_Large. I'm changing the style programmaticaly in the ListItemAdapter class.
<!-- Heading in each list item -->
<style name="itemHeadingText_Large" parent="#android:style/TextAppearance.Large">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/black</item>
<!-- Testing height and gravity -->
<item name="android:layout_height">100dp</item>
<item name="android:gravity">center_vertical</item>
</style>
<!-- Heading in each list item -->
<style name="itemHeadingText_Medium" parent="#android:style/TextAppearance.Medium">
<item name="android:textStyle">bold</item>
<item name="android:textColor">#android:color/black</item>
</style>
Sadly, the android:layout_height and android:gravity aren't applied to the view, and whole app looks like that. Here's how both TextViews are defined:
<TextView
android:id="#+id/heading"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/darker_gray"/>
<TextView
android:id="#+id/secondary"
style="#style/itemSecondaryText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"/>
I would like to center vertically the Lorem Ipsum heading of the second item provided in the screenshot.
EDIT: 25 June 2017
I managed to achieve what I wanted by changing the height programmatically using the getLayoutParams().height and adding the android:gravity attribute to the TextView xml definition - after all in both styles text can be centered vertically within its TextView.
Nevertheless, my question on how to do that in styles.xml is still open!
android:gravity will specify how the text should be laid out within the TextView, if the View is larger than the text
android:layout_gravity will specify how the View itself should be laid out in its ViewGroup.
I would try replacing gravity with layout_gravity
The issue that I can see in your code is:
<TextView
android:id="#+id/secondary"
style="#style/itemSecondaryText" // Look at this line
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_red_light"/>
You are using "itemSecondaryText" style for your textview, but in your styles.xml there isn't any style named "itemSecondaryText". You have "itemHeadingText_Medium" and "itemHeadingText_Large" in your style.xml but there isn't item named "itemSecondaryText".
I guess this is the issue, otherwise everything looks good.

android:lineSpacingMultiplier doesn't work in android:textAppearance

I'm trying to add android:lineSpacingMultiplier in my textAppearance style (android:textAppearance), and it's not working. Am I doing something wrong?
TextAppearance style:
<style name="TextAppearance.Body1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
Use of style:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
android:textAppearance="TextAppearance.Body1"
/>
For whatever reason android:lineSpacingMultiplier doesn't work as an item within your textAppearance. You'll either have to set it directly as a TextView attribute (using android:lineSpacingMultiplier), or create a regular style which "wraps" setting the textAppearance and lineSpacingMultiplier
Create a style
<style name="TextBody1">
<item name="android:textAppearance">#style/TextAppearance.AppCompat.Body1</item>
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
or
<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
and then apply via style instead of android:textAppearance
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
style="#style/TextBody1"
/>
lineSpacingMultiplier as well as lineSpacingExtra you can apply only in style.
But as an alternative, you can use lineHeight attribute, which can be applied to the MaterialTextView using textAppearance.
Interesting note, that if you use a fully Material theme (not Bridge) all your TextView will auto-inflate to MaterialTextView, otherwise, you will need to specify <com.google.android.material.textview in your xml.
I was able to solve the issue by simply applying the style to the TextView itself instead of the textAppearance (similar to the accepted answer, but with a bit less code).
Here is a sample:
Style:
<style name="TextBody1" parent="TextAppearance.AppCompat.Body1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
or simpler if you don't care about the parent:
<style name="TextBody1">
<item name="android:lineSpacingMultiplier">1.25</item>
</style>
Then in your View:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="This is body 1\nThis is body 1"
style="#style/TextBody1"
/>
I am not sure why applying android:textAppearance to a TextView with a given style that defines android:lineSpacingMultiplier does not work (I would speculate that it may be due to the fact that the line spacing is on the style View itself instead of the textAppearance of the View) but this is a bit simpler than the accepted answer if you don't care about the parent.

Referencing specific attribute in another style

I'm trying to find the best way to match the text size of a label next to an EditText using xml.
I would prefer not to inherit the EditText style directly, since that would bring in a bunch of attributes I'm not interested in.
<EditText
android:id="#+id/editQuantity"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textUnit"
style="#style/FontMatchingEditText"
android:text="units"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
Is there some way to refer to a specific attribute in another style?
Otherwise, would it make more sense make my own style for EditText to make sure it stays consistent with the label?
Edit
I was hoping there might be a theme independent way of doing it, semantically something like this:
<item name="android:textSize">#android:style/Widget.EditText.textAppearance.textSize</item>
But I'm probably overcomplicating it.
Thanks!
Assuming your parent theme is android:Theme.Holo.Light.
From \android-sdk\platforms\android-X\data\res\values\themes.xml:
<style name="Theme.Holo.Light" parent="Theme.Light">
...
<!-- Widget styles -->
<item name="editTextStyle">#android:style/Widget.Holo.EditText</item>
From \android-sdk\platforms\android-X\data\res\values\styles.xml:
<style name="Widget.Holo.EditText" parent="Widget.EditText">
</style>
<style name="Widget.EditText">
...
<item name="android:textAppearance">?android:attr/textAppearanceMediumInverse</item>
<item name="android:textColor">?android:attr/editTextColor</item>
</style>
Now you can just reuse theme attributes used for EditText for your TextView:
<TextView
android:id="#+id/textUnit"
android:textColor="?android:attr/editTextColor"
android:textAppearance="?android:attr/textAppearanceMediumInverse"
android:text="units"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />

How do I set EditText background and ellipsize attributes via #style?

I have EditText in my XML as
<!-- Inside RelativeLayout -->
<EditText
android:id="#+id/edtEmail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/edtPhone"
android:layout_alignLeft="#id/strut"
android:layout_alignBaseline="#+id/txtLabelEmail"
android:inputType="textEmailAddress"
android:singleLine="true"
android:imeOptions="actionDone"
android:hint="#string/profile_email_hint"
style="#style/listTextValue"
android:background="#drawable/edittext_bg"
android:ellipsize="end" />
Style
<style name="listTextValue" parent="android:Widget.EditText">
<item name="android:textSize">#dimen/listTextSize</item>
<item name="android:textStyle">bold</item>
<item name="android:textColor">#color/black</item>
<item name="android:background">#drawable/edittext_bg</item>
<item name="android:focusable">true</item>
<item name="android:focusableInTouchMode">true</item>
<item name="android:singleLine">true</item>
<item name="android:ellipsize">end</item>
</style>
If I remove background attribute from EditText in Layout xml, background image will be disappeared.
If I remove ellipsize attribute from EditText in Layout xml, hint text will be expand to two lines.
As this attribute is available in Style from Eclipse's suggestion, why don't they work when put in the style?
Edit: My bad, GUI editor didn't show the background. It was another screen.
I have another layout XML with the same attributes for EditText, the background was shown correctly.
Difference between the layout would be that the first one was used inside a Fragment while the latter used directly within Activity.
This is really strange :S
I think you should try android:textAppearance="#style/your_style". It works for TextView and TextView is parent of EditText. I think it should also works.

Categories

Resources