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.
Related
I am writing an app in android studio that all property of TextViews are same except id and text. For this reason I declare a style in style.xml as below:
<style name="thirdTextView" parent="TextAppearance.AppCompat">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:textColor">#color/black</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
In my activity_main.xml I used this style:
<TextView
style="#style/thirdTextView"
android:text="#string/questionWeight"/>
But I can not see the preview of layout and I got this error:
One or more layouts are missing the layout_width and layout_height attributes
How to solve this?
Change your TextViews to this;
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/thirdTextView"
android:text="#string/questionWeight"/>
Every widget in a layout file needs height and width attributes. Hope, this helps.
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.
I have two themes that I switch back and forth between in my Android application. In some places, I have TextViews that I want to remain the default color. In other places, I want them to be another color (let's say orange). When changing themes, I'd like only the TextViews that were previously orange to become blue.
Is there a way to do this simply in Android? I had something in mind such as a class tag in html/css but can't seem to find anything.
EDIT: To clarify what I was referring to with the html/css equivalent:
<div>This text is black</div>
<div class="redDiv">This text is red</div>
.redDiv {
color:red;
}
Let's say you have two themes in your application: MyFirstTheme and MySecondTheme:
<style name="MyFirstTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MyFirstTheme.TextView</item>
[...]
</style>
<style name="MySecondTheme" parent="android:Theme">
<item name="android:textViewStyle">#style/MySecondTheme.TextView</item>
[...]
</style>
The textview styles defined in your styles.xml could look like:
<style name="MyFirstTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#android:color/black</item>
[...]
</style>
<style name="MySecondTheme.TextView" parent="#android:style/Widget.TextView">
<item name="android:textColor">#color/orange</item>
[...]
</style>
<style name="PersistentTheme.TextView" parent="#style/MyFirstTheme.TextView">
<item name="android:textColor">#color/red</item>
</style>
So when you have a layout with two TextViews, you can have the one completely follow the active theme by not setting anything extra to it, and you can apply other appearance to your other TextView instance by specifying a style attribute for it:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- This textview will change text color when other style applied -->
<TextView
android:id="#+id/tv_styledependent"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!-- This textview will always have the same (red) text color -->
<TextView
android:id="#+id/tv_persistent"
style="#style/PersistentTheme.TextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/tv_styledependent" />
</RelativeLayout>
I am trying to fit TextViews in the screen, so that every textview contains 1 word of a long sentence.
This is a very long sentence, so long that it does not
fit in the screen.
Here fit in the screen are 4 textviews, which I need to move to the next line. Basically, this way I can get click events for each word (since I need to support Android 2.2, and Spannable needs getX and getY that 2.2 does not support).
Layout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:fitsSystemWindows="true"
android:orientation="horizontal" >
<TextView
android:id="#+id/word1"
style="#style/select_part_of_speech_word" />
<TextView
android:id="#+id/word2"
style="#style/select_part_of_speech_word" />
..... more TextViews.....
<TextView
android:id="#+id/word15"
style="#style/select_part_of_speech_word" />
</LinearLayout>
Style applied
<style name="select_part_of_speech_word" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_marginBottom">10dp</item>
<item name="android:layout_marginTop">10dp</item>
<item name="android:text">#string/randomTutorialText</item>
<item name="android:textColor">#android:color/black</item>
<item name="android:textSize">25sp</item>
<item name="android:visibility">visible</item>
<item name="android:lines">1</item>
</style>
With this, the textviews just seem to disappear. I guess if I use a horizontal scrollview, I will be able to view all 15 textviews, but I need them to overflow to the next available space below. Can this be done ?
So you want something equivalent to a FlowLayout.
You could try Romain Guy's implementation.
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.