Android unwanted padding on top and bottom with TextView - android

I am trying to put two text view within the circular progress bar as shown in the picture below. These two text should be aligned with center of the circular progress bar.
But the unwanted padding in text view which prevents me to align the text into the center of the circular progress bar. This is what i get.
here is my layout file:
<FrameLayout
android:id="#+id/onboarding_activity_progress_layout"
android:layout_gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_marginTop="10dp">
<com.locationlabs.finder.android.core.ui.CircularProgressBar
android:id="#+id/onboarding_activity_progress_bar"
android:layout_gravity="center"
android:padding="10dp"
android:layout_width="120dp"
android:layout_height="120dp"
app:progressColor="#color/progress_bar_color"
app:backgroundColor="#color/progress_bar_background_color"
app:progressBarWidth="10dp"
tools:progress="60"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:includeFontPadding="false"
android:layout_gravity="center">
<TextView
android:id="#+id/number_of_remaining_steps"
style="#style/OnboardingNumberOfRemainingStepText"
tools:text="2"/>
<TextView
android:id="#+id/remaining_number_of_steps"
style="#style/OnboardingRemainingStepText"
tools:text="steps left"/>
</LinearLayout>
</FrameLayout>
Here is the styling:
<style name="OnboardingNumberOfRemainingStepText">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:fontFamily">sans-serif</item>
<item name="android:textSize">40sp</item>
<item name="android:textColor">#color/dark_grey</item>
<item name="android:includeFontPadding">false</item>
</style>
<style name="OnboardingRemainingStepText">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_gravity">center_horizontal</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/dark_grey</item>
<item name="android:textStyle">normal</item>
</style>
I was told android:includeFontPadding can help reduce the padding but it doesn't help that much. I also tried with the single textView with Spannable but I get almost the similar result. I tried with negative margin but that didn't work and it is also not reliable.

When you transfer styling attributes of a TextView to XML, TextView might render in an unexpected way. This is because the default styling for TextView is applied in a layout file which is overridden when you use style attribute in it.
You can try using parent="android:TextAppearance" in the style defined for a TextView to ensure you inherit the pre-defined styles for the TextView.
Example:
Let me know if it worked for you!

Related

Center text with the icon in textButton

Well the question is probably dumb AF, but I can't figure it out. How can i center this button? Tried gravity and paddings, not with a success
Button style:
<style name="Theme.Base.Jabotmobile.TextBT"
parent="Widget.MaterialComponents.Button.TextButton">
<item name="icon">#drawable/ic_baseline_add_24</item>
<item name="iconPadding">4dp</item>
<item name="android:textSize">14sp</item>
<item name="fontFamily">#font/open_sans_regular</item>
<item name="android:maxLines">1</item>
<item name="android:padding">0dp</item>
<item name="textAllCaps">false</item>
<item name="android:layout_gravity">center_vertical</item>
<item name="iconGravity">start</item>
</style>
Button in layout:
<Button
android:id="#+id/cat_req_BT1"
style="#style/Theme.JaBotMobile.TextBT"
android:layout_width="155dp"
android:layout_height="42dp"
android:layout_marginStart="18dp"
android:layout_marginTop="16dp"
android:text="#string/category_req"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textInputLayout4" />
Try to add this constraint to, it should be centered with this way
app:layout_constraintEnd_toEndOf="parent"
PS
I understand the problem now, Basically you want to center the button items (icon and text) vertically. Unfortunately I don't have solution for this case, instead of using style you can create a custom view component, with custom view component you can have more flexibility for customizations.

Setting EditText's minLines

I have a simple layout:
and I want to make the EditTexts(the Views below) the same height as TextViews above. I want to avoid hardcoding height. For the TextViews I did android:minLines = 2. Unfortunately, this does not work for EditTexts. Is there a way to do this without hardcoding the height?
I am aware that this can be easily done if the EditText is to the side of the TextView in a wrap_content LinearLayout and then setting the layout_height of EditText to match_parent but, my views are in a vertical layout.
EDIT: Here is the layout of one of those columns
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_marginRight="#dimen/item_spacing"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
style="#style/TextView"
android:layout_width="match_parent"
android:layout_marginBottom="#dimen/item_spacing"
android:text="#string/xdim" />
<EditText
android:id="#+id/xdim"
style="#style/EditText"
android:layout_width="match_parent"
android:inputType="number" />
</LinearLayout>
And the styles:
<style name="TextView">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:minLines">2</item>
<item name="android:gravity">center</item>
<item name="android:background">#drawable/back</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
<style name="EditText" parent="#style/TextView">
<item name="android:layout_height">match_parent</item>
<item name="android:background">#drawable/item_border</item>
</style>

Custom preference layout widget not visible

edit:
for anyone coming across this, the way I got it to work is to either:
Set the layout directly for each preference item, as in:
<CheckboxPreference
android:layout="#layout/checkbox_preference_item"/>
OR set the style via a theme as I was doing before, but define a widgetlayout and set it instead of an actual layout, like so, where preferenceStyle is set to #style/Preference and checkBoxPreferenceStyle is set to #style/Preference.Checkbox:
<Style name="Preference">
<item name="android:layout">#layout/checkbox_preference_item</item>
</Style>
<Style name="Preference.Checkbox">
<item name="android:widgetLayout">#layout/your_custom_widget</item>
</Style>
I'm trying to customize the layout of a Checkbox Preference item and the checkbox itself is not showing up. I'm modeling after android code and not sure where I'm going wrong. The text views are working fine as well as the rest of the layout but the checkbox is not showing up. Would appreciate any help with this.
Relevant code:
theme.xml:
<style name="Theme.Settings" parent="android:Theme.Holo.Light">
<item name="android:actionBarStyle">#style/SettingsActionBarStyle</item>
<item name="android:windowActionBarOverlay">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:preferenceCategoryStyle">#style/PreferenceCategory</item>
<item name="android:preferenceStyle">#style/Preference</item>
<item name="android:checkBoxPreferenceStyle">#style/Checkbox</item>
</style>
style.xml
<style name="Checkbox">
<item name="android:layout">#layout/checkbox_preference_item</item>
</style>
<style name="Preference.Text">
<item name="android:layout_height">wrap_content</item>
<item name="android:layout_width">wrap_content</item>
<item name="android:fontFamily">sans-serif-light</item>
<item name="android:textSize">#dimen/preferences_item_font</item>
<item name="android:textColor">#color/preference_text</item>
</style>
<style name="Preference.Text.Extra" parent="Preference.Text">
<item name="android:textSize">#dimen/preferences_header_font</item>
</style>
checkbox_preference_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="#dimen/preferences_item_height"
android:background="#color/white"
android:paddingLeft="#dimen/preferences_item_left">
<LinearLayout android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:gravity="center_vertical">
<TextView android:id="#android:id/title"
style="#style/Preference.Text"/>
<TextView android:id="#android:id/summary"
style="#style/Preference.Text.Extra"/>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:id="#android:id/widget_frame"
android:layout_alignParentRight="true"/>
</RelativeLayout>
I am not seeing a CheckBox in the layout xml. Am I missing something? You need to specify something like:
<CheckBox android:id="..." ... />
Then you may need to specify something like
<CheckBoxPreference android:key="..." ... />
in the preference xml file. Setting should be android:widgetLayout to the custom CheckBox.
See this post may help you more customizing checkbox preference

Percentage widths AND overlapping with xml

I'm trying to replicate this:
in my android app. I would like the three textviews to have a percentage based width (33%) across the screen. The triangle is a separate drawable I've created.
The issue is, I can create the percentage width in a LinearLayout, but I can't have nicely overlapping view in a LinearLayout- the Basic Info textview should be behind the triangle.
Below is the code I've got working without the triangle:
Styles:
<style name="status_selected_textview">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">#dimen/standard_line_height</item>
<item name="android:layout_weight">0.33</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/font_size_medium</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:padding">5dp</item>
<item name="android:background">#color/green</item>
</style>
<style name="status_unselected_textview">
<item name="android:layout_width">0dp</item>
<item name="android:layout_height">#dimen/standard_line_height</item>
<item name="android:layout_weight">0.33</item>
<item name="android:gravity">center</item>
<item name="android:textStyle">bold</item>
<item name="android:textSize">#dimen/font_size_medium</item>
<item name="android:textColor">#color/gray</item>
<item name="android:padding">5dp</item>
<item name="android:background">#color/light_header</item>
</style>
Layout:
<LinearLayout
android:id="#+id/activate_key_status_header"
android:layout_below="#+id/activate_key_header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backround="#color/light_header">
<TextView
android:id="#+id/activate_key_textview"
android:text="Activate Key"
style="#style/status_selected_textview"/>
<TextView
style="#style/status_unselected_textview"
android:text="Basic Info" />
<TextView
android:text="Edit Profile"
style="#style/status_unselected_textview"/>
</LinearLayout>
Here is the code for my triangle that I'm trying to add somehow:
<View
android:layout_toRightOf="#+id/activate_key_textview"
android:layout_width="#dimen/standard_line_height"
android:layout_height="#dimen/standard_line_height"
android:background="#drawable/arrow_right" />
It seems like the solution is somehow a combo of a RelativeLayout and LinearLayout, but I can't seem to find the right way.
Any thoughts?
Can you wrap the 2nd TextView in a FrameLayout, and add the triangle view to it 2nd? That plus an android:gravity left would do it, I would think.
Something like this:
<FrameLayout
<!-- Whatever styles you need to make it fit 33%-->
>
<TextView
style="#style/status_unselected_textview"
android:text="Basic Info" />
<View
android:gravity="left"
android:layout_width="#dimen/standard_line_height"
android:layout_height="#dimen/standard_line_height"
android:background="#drawable/arrow_right" />
</FrameLayout>

android: set textColor with textAppearance attribute

I have created several styles for font in my app.
How can I add these styles to views? - 1) Using style attribute 2) Using textAppearance.
To use style is not an option because views may have other attributes (margins, paddings, min width, etc - I cant specify 2 styles for a view), so I need to specify text-related attributes separately, but android:textColor doesnt work here:
styles.xml:
<style name="TextAppearance.baseText" parent="#android:style/TextAppearance">
<item name="android:textAppearance">?android:textAppearanceSmall</item>
<item name="android:typeface">sans</item>
<item name="android:textColor">#android:color/white</item>
<item name="android:textSize">15sp</item>
</style>
layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="65dp"
android:orientation="horizontal" >
<Button
android:id="#+id/backButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/sometext"
android:textAppearance="#style/TextAppearance.baseText"/>
</RelativeLayout>
Why doesnt it work? How can I specify textcolor in textappearance?
As Oderik has mentioned in the comments, the Button view has a default value for it's textColor attribute. This textColor value overrides whatever value is set through the textAppearance attribute. Therefore you have two options:
Set the textColor to #null in the style:
<style name="Application.Button">
<item name="android:textAppearance">#style/Application.Text.Medium.White</item>
<item name="android:textColor">#null</item>
</style>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button" />
Set the textColor to #null in the Button XML:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/Application.Button"
android:textColor="#null" />
It works. Your text color is white - the same as default color. Put there any other color like <item name="android:textColor">#AA6699</item> and you will see difference.
Second thing is that you are trying to set text appearance in text appearance - doen's make sense. If you want to set small letters do this using parent parent="#android:style/TextAppearance.Small and delete line <item name="android:textAppearance">?android:textAppearanceSmall</item>
Third thing is that you want to have small letters and put additional textSize - doesn't make sense.
Try this, works for me:
<style name="BaseText" parent="#android:style/TextAppearance.Small">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
If you want to set everything in style:
<style name="BaseText" parent="#android:style/TextAppearance.Large">
<item name="android:typeface">sans</item>
<item name="android:textColor">#AA7755</item>
</style>
<style name="BaseText.Margins">
<item name="android:layout_margin">10dip</item>
</style>
Where BaseText.Margins inherits from BaseText.
Then you can just use:
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
style="#style/BaseText.Margins" />
If you want to use android:TextAppearance instead of style: to set your android:textColor, you need to set android:textColor=#null on your TextView.
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:textAppearance="#style/TextAppearance.AppCompat.Medium.MySubheader"
android:textColor="#null"
tools:text="Section" />
Then it will inherit correctly from your android:TextAppearance
<style name="TextAppearance.AppCompat.Medium.MySubheader">
<item name="android:fontFamily">sans-serif-medium</item>
<item name="android:textSize">16sp</item>
<item name="android:textColor">#color/black_54</item>
</style>

Categories

Resources