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>
Related
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!
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:background="#drawable/ic_shade"
android:layout_height="match_parent" >
<TextView android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:gravity="center"
android:textSize="54sp"
android:layout_centerVertical="true"
android:textColor="#android:color/white"
android:text="مبروك \nلقد ربحت كوبون"/>
</RelativeLayout>
What happens, it doesnt take entire width/height and i dont see the background on entire page.I am using the activity to be transparent.
Below is code for activity, however if i add
< item name="android:windowFullscreen ">true</item>
to the below part it works.. but i dont want it to do it because its resizing my previous activity.
<style name="Theme.Transparent" parent="android:Theme">
<item name="android:windowIsTranslucent">true</item>
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowIsFloating">false</item>
<item name="android:backgroundDimEnabled">false</item>
</style>
<!-- Application t
your textview is filling the whole screen, change layout_height and layout_width attributes of your text view to wrap_content. then textview will be in center and background will appear on entire screen
I have an activity layout file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/compose_message_view"
style="#style/Container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout style="#style/SectionContainer" >
<TextView
style="#style/FieldHeader"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:focusable="true"
android:focusableInTouchMode="true" />
<Spinner
android:id="#+id/compose_message_department"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:prompt="#string/compose_message_department_prompt"
android:spinnerMode="dropdown" />
</LinearLayout>
<LinearLayout style="#style/SectionContainer" >
<TextView
style="#style/FieldHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawablePadding="8dp"
android:text="#string/message_account_label" />
<Spinner
android:id="#+id/compose_message_account"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="8dp"
android:prompt="#string/compose_message_account_prompt"
android:spinnerMode="dropdown" />
</LinearLayout>
<EditText
android:id="#+id/compose_message_subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="32dp"
android:layout_marginTop="16dp"
android:hint="#string/compose_message_subject_hint"
android:inputType="textCapSentences" />
<EditText
android:id="#+id/compose_message_body"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/compose_message_body_hint"
android:inputType="textMultiLine|textCapSentences" />
</LinearLayout>
</ScrollView>
The relevant styles look like this:
<style name="Container">
<item name="android:layout_marginRight">130dp</item>
<item name="android:layout_marginLeft">130dp</item>
<item name="android:paddingTop">32dp</item>
<item name="android:paddingLeft">32dp</item>
<item name="android:paddingRight">32dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:orientation">vertical</item>
</style>
<style name="SectionContainer">
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:orientation">vertical</item>
<item name="android:paddingBottom">16dp</item>
</style>
<style name="FieldHeader" parent="android:Widget.TextView">
<item name="android:textAllCaps">true</item>
<item name="android:paddingLeft">12dp</item>
<item name="android:paddingTop">24dp</item>
<item name="android:paddingRight">12dp</item>
</style>
Now when I open this activity on a Nexus 7 in landscape (haven't tried other tablets yet), the ScrollView and its contents extend beyond the right edge of the screen. When I type several lines into the compose_message_body EditText so that the UI extends below the bottom of the screen, the right edge of the UI comes in to where it belongs. See screenshots below.
Any ideas as to what is going on here?
This is a side effect of using the fillViewPort attribute on your ScrollView. If you have set fillViewPort to true, then this will only be taken into account if the measured height of your child is lower than the height of your ScrollView. In that case the ScrollView will stretch the content to fill the viewport and your margins won't be applied correctly. As soon as your child's content is greater than the height of your ScrollView, your margins will be applied correctly.
I suggest to not use margins on your child layout (LinearLayout) that you have placed in the ScrollView, but only use padding.
So your Container style could become
<style name="Container">
<item name="android:paddingTop">32dp</item>
<item name="android:paddingLeft">164dp</item>
<item name="android:paddingRight">164dp</item>
<item name="android:layout_width">match_parent</item>
<item name="android:layout_height">match_parent</item>
<item name="android:orientation">vertical</item>
</style>
In styles, your container has a margin left 130dp. Pushing your entire layout to the right by that much. If you set it to 0dp it should be in its correct position.
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>
I have a 'dialog' Activity based on the built-in Android theme Theme.Dialog:
<style name="Theme.Dialog">
<item name="android:windowFrame">#null</item>
<item name="android:windowTitleStyle">#android:style/DialogWindowTitle</item>
<item name="android:windowBackground">#android:drawable/panel_background</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">#null</item>
<item name="android:windowAnimationStyle">#android:style/Animation.Dialog</item>
<item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>
<item name="android:colorBackgroundCacheHint">#null</item>
<item name="textAppearance">#android:style/TextAppearance</item>
<item name="textAppearanceInverse">#android:style/TextAppearance.Inverse</item>
<item name="textColorPrimary">#android:color/primary_text_dark</item>
<item name="textColorSecondary">#android:color/secondary_text_dark</item>
<item name="textColorTertiary">#android:color/tertiary_text_dark</item>
<item name="textColorPrimaryInverse">#android:color/primary_text_light</item>
<item name="textColorSecondaryInverse">#android:color/secondary_text_light</item>
<item name="textColorTertiaryInverse">#android:color/tertiary_text_light</item>
<item name="textColorPrimaryDisableOnly">#android:color/primary_text_dark_disable_only</item>
<item name="textColorPrimaryInverseDisableOnly">#android:color/primary_text_light_disable_only</item>
<item name="textColorPrimaryNoDisable">#android:color/primary_text_dark_nodisable</item>
<item name="textColorSecondaryNoDisable">#android:color/secondary_text_dark_nodisable</item>
<item name="textColorPrimaryInverseNoDisable">#android:color/primary_text_light_nodisable</item>
<item name="textColorSecondaryInverseNoDisable">#android:color/secondary_text_light_nodisable</item>
<item name="textColorHint">#android:color/hint_foreground_dark</item>
<item name="textColorHintInverse">#android:color/hint_foreground_light</item>
<item name="textColorSearchUrl">#android:color/search_url_text</item>
<item name="textAppearanceLarge">#android:style/TextAppearance.Large</item>
<item name="textAppearanceMedium">#android:style/TextAppearance.Medium</item>
<item name="textAppearanceSmall">#android:style/TextAppearance.Small</item>
<item name="textAppearanceLargeInverse">#android:style/TextAppearance.Large.Inverse</item>
<item name="textAppearanceMediumInverse">#android:style/TextAppearance.Medium.Inverse</item>
<item name="textAppearanceSmallInverse">#android:style/TextAppearance.Small.Inverse</item>
</style>
My layout for the Activity, I have a single EditText, a single Button and an invisible TextView I use to display messages to the user based on the validity of the EditText content:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp">
<TextView
style="#style/DialogMessage"
android:id="#+id/error"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="4dp"
android:visiblity="gone" />
<EditText
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text|textCapWords"
android:maxLength="100"
android:maxLines="1"
android:ems="20"
android:scrollHorizontally="true" />
<Button
android:id="#+id/rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right" />
</LinearLayout>
When the user is in portrait mode, and they enter some 'bad' text in the EditText, I make the TextView visible, and populate it with an appropriate error message. This works fine, as everything shifts down, and the TextView is displayed great, even if the content spans multiple lines. Now, in landscape, when I do the same, the TextView displays fine, but the bottom element (in this case the Button) is compressed to about half it's normal height. Moving the elements around, if I put the EditText at the bottom, when the TextView is made visible, it too is compressed, and puttint the TextView at the bottom results in anything more than a single line of text being cut off.
I have tried forcing the issue after the TextView is populated by re-setting the sizes of the LinearLayout via:
LinearLayout dialog = (LinearLayout) findViewById(R.id.dialog);
dialog.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
And I have tried re-drawing the LinearLayout after the TextView is populated by calling:
LinearLayout dialog = (LinearLayout) findViewById(R.id.dialog);
dialog.invalidate();
Neither worked. Any ideas what is going on here?
Thanks,
Paul
Edit:
There is ample screen real-estate to show all three components fully... as a fully inflated TextView with the same content as I am dynamically inflating, and everything displayed fully.
Didn't figure out why this was happening, but figured out a work-around. Switching the layout to a RelativeLayout solved the issue, and everything is now displayed properly using the following:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="6dp"
android:paddingRight="6dp"
android:paddingBottom="6dp">
<EditText
android:id="#+id/inpt_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text|textCapWords"
android:maxLength="100"
android:maxLines="1"
android:ems="20"
android:scrollHorizontally="true" />
<Button
android:id="#+id/btn_rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#id/inpt_title"
android:layout_below="#id/inpt_title" />
<TextView
style="#style/DialogMessage"
android:id="#+id/error"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_below="#id/btn_rename"
android:layout_alignLeft="#id/inpt_title"
android:layout_alignRight="#id/inpt_title" />
</RelativeLayout>