gravity bottom not working with textview inside relative layout - android

My RelativeLayout has a 25sp image and 25sp text on left side and then 12sp text on right side:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/omgIcon"
android:layout_width="25sp"
android:layout_height="25sp"
android:src="#drawable/omg_face_emoji"
android:layout_alignParentLeft="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OMG"
android:layout_toRightOf="#+id/omgIcon"
android:textAppearance="#style/HeaderFont" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="See All"
android:layout_alignParentRight="true"
android:gravity="bottom"
android:textAppearance="#style/LinkFont" />
</RelativeLayout>
styles.xml:
<style name="HeaderFont" parent="#android:style/TextAppearance.Large">
<item name="android:textColor">#000</item>
<item name="android:textSize">25sp</item>
<item name="android:fontFamily">arial</item>
</style>
<style name="LinkFont" parent="#android:style/TextAppearance.Small">
<item name="android:textColor">#FF00FF</item>
<item name="android:textSize">12sp</item>
<item name="android:fontFamily">arial</item>
</style>
Unfortunately, the right floating text does not align to bottom of relative layout:
The baseline of right text should be same as left text. What am I doing wrong?

yes, it does not align to the bottom of the relative layout. Why? Because the android:gravity="bottom" means that gravity/align inside itself and it means the text See All will be at the bottom of your TextView. You have to use the layout_gravity instead (but it works on LinearLayout only).
If you want to align bottom, just add android:layout_alignParentBottom="true" to your TextView. However, if you want to align bottom of OMG text, you should add android:layout_alignBottom="OMG_ID" instead

Related

Android MaterialButton padding between drawableLeft icon and text

As the topic said, with materialButton, I want to set up the distance between drawableLeft icon and text, I do not want to use icon, just drawableLeft
By default, the drawableleft icon is far away from text, I want to make them closer, but I cannot use icon and icon gravity. If I use icon and icon gravity, it will not show because of the style I use, but if I change the style, then there will be another problem: there will be shadow below the button. I do not want the shadow, so I have to use the style.
What I want is a MaterialButton, grey background, left icon, centre is text, icon and text is closed, MaterialButton is round corner, no shadow below the MaterialButton. Here is the code so far:
<?xml version="1.0" encoding="utf-8"?>
<com.google.android.material.button.MaterialButton
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/btnShortCut"
style="#style/Button"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:orientation="vertical"
android:layout_marginRight="10dp"
app:backgroundTint="#color/background"
app:cornerRadius="#dimen/cornerM"
app:strokeColor="#color/primaryGrey"
app:strokeWidth="0.5dp"
android:padding="5dp"
android:textColor="#color/secondaryText"
android:textSize="#dimen/fontBody" >
</com.google.android.material.button.MaterialButton>
<style name="Button" parent="Widget.MaterialComponents.Button.UnelevatedButton">
<item name="android:textAllCaps">false</item>
<item name="android:insetTop">0dp</item>
<item name="android:insetBottom">0dp</item>
<item name="android:gravity">center</item>
<item name="android:padding">0dp</item>
</style>
Is it necessary for you to use a material button? Have you considered using a FrameLayout with a textview overlapped on it?
In your example it would look something like this, but with some edits since I'm not sure exactly how you want it to look.
<FrameLayout
android:id="#+id/btnShortCut"
android:layout_width="wrap_content"
android:layout_height="36dp"
android:layout_marginRight="10dp"
android:orientation="vertical"
android:gravity="center"
app:cornerRadius="#dimen/cornerM"
app:strokeColor="#color/primaryGrey"
app:strokeWidth="0.5dp"
android:padding="5dp">
<TextView
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="15dp"
android:layout_marginEnd="15dp"
android:drawableStart="#drawable/<your icon here>"
android:drawablePadding="15dp"
android:textColor="#color/secondaryText"
android:textSize="#dimen/fontBody"/>
</FrameLayout>

Why is my ScrollView too wide only when it doesn't need to scroll?

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.

EditText not scrolling horizontally

I have created an EditText using:
<style name="foredits">
<item name="android:layout_width">wrap_content</item>
<item name="android:layout_height">wrap_content</item>
<item name="android:singleLine">true</item>
<item name="android:maxLines">1</item>
<item name="android:scrollHorizontally">true</item>
<item name="android:typeface">monospace</item>
<item name="android:imeOptions">actionNext</item>
</style>
When creating the EditText,I put in a layout_weight,an id and a id as other attributes.I would like the EditText to scroll horizontally when:
a.hint does not fit within the bounds of the edittext
b.text is being entered and the text goes past the right edge of the edittext's bounds
Because,this has not been scrolling horizontally,I have also tried with a ScrollingMovementMethod
edit_mul.setHorizontalScrollBarEnabled(true);
edit_mul.setMovementMethod(new ScrollingMovementMethod());
This does not seem to work either.How do I get this working?
Your code seems good i did the following for vertical Scroll Bar
xml
<TextView
android:id="#+id/response_sms_textView3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp"
android:background="#color/White"
android:gravity="center"
android:lines="8"
android:maxLines="8"
android:scrollbars="vertical" <!-- include -->
android:text="#string/without_account_response_sms_tv3"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/Black" />
code
tv3.setMovementMethod(new ScrollingMovementMethod()); // include
but if you put this inside ScrollView layout might not work. Thanks
You can use the following parameters in the edittext.
edittext.setFocusable(false);
edittext.setClickable(false);
edittext.setFocusableInTouchMode(false);
edittext.setEnabled(true);
edittext.setKeyListener(null);
edittext.setHorizontallyScrolling(true);
So it will work as the desired behaviour.

dynamic layout with two buttons problem

how can I set my buttons in a linearLayout dynamically? So I want that all android mobiles with different display sizes can use my app.
Code
<LinearLayout android:id="#+id/linear_layout_main2" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:orientation="horizontal">
<Button android:id="#+id/bt1" android:text="GO"
style="#style/button_font" android:clickable="false"
android:background="#drawable/box1"
android:layout_marginLeft="6px" />
<Button android:id="#+id/bt2" android:text="ON"
style="#style/button_font" android:clickable="false"
android:background="#drawable/box1"
android:layout_marginLeft="6px" />
</LinearLayout>
Style-Code
<style name="button_font" parent="#android:style/TextAppearance.Medium">
<item name="android:layout_width">44px</item>
<item name="android:layout_height">39px</item>
<item name="android:textColor">#ffffffff</item>
<item name="android:textSize">18px</item>
<item name="android:layout_gravity">center_vertical</item>
</style>
You can use android:layout_weight = "1" for two button, then it take same screen ratio.
Means 1:1 ratio is put for the two buttons. So it works in all screen sizes.

Android: Theme.Dialog layout - dynamically visible content being cut-off

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>

Categories

Resources