-1

I have a TextView that displays an error message beside 2 Buttons. They are currently inside a horizontal LinearLayout. The problem is if the TextView is too wide, the 2 Buttons will be pushed off the screen. Is it possible to push the elements downwards in those cases?

If the text is short there are no problems:

(Textview text) (Button1) (Button2)|(Edge of screen) 

If the textview is long, I want to push the 2 buttons down a "row"

(Realllllllllllly long text that may|(Edge of screen) 
span 2 lines)

                 (Button1) (Button2)|(Edge of screen) 
Rgfvfk Iff
  • 1,549
  • 2
  • 23
  • 47

4 Answers4

2

I think you need to keep one more Linear layout below to your horizontal linear layout and need to check text size runtime if it's width is greater than required two button space then need to hide horizontal linear layout buttons and need to show below layout buttons

to refer how to check text size runtime refer below link :

Refer this link

Sameer Jani
  • 1,192
  • 9
  • 16
1

Yes you can do that, flexbox-layout is the solution.

How to use

Gradle dependency

dependencies {
    implementation 'com.google.android:flexbox:0.3.2'
}

And xml code

<com.google.android.flexbox.FlexboxLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    app:flexWrap="wrap"
    app:alignItems="stretch"
    app:alignContent="stretch" >

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        />

    <Button
        android:id="@+id/button2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_alignSelf="flex_end"
        />
</com.google.android.flexbox.FlexboxLayout>

There are few other attributes also [read documentation], which you can try and find what works more suitable in you case.

Pankaj Kumar
  • 81,967
  • 29
  • 167
  • 186
1

Try this way: Use FlowLayout

<org.apmem.tools.layouts.FlowLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content">

</org.apmem.tools.layouts.FlowLayout>

Inside FlowLayout you can put your view's and it will auto move to next line if not fit.

Vishal Yadav
  • 3,642
  • 3
  • 25
  • 42
0

you can use the TextView predefined method, to gave validation to end user like this

TextView textView=(TextView)findViewById(R.id.text_view);
textView.setError("Your Text is very wide please provide short text");

setError put red mark on textview view, with that we can tell the end user. provided text is wide

Mohd Qasim
  • 896
  • 9
  • 20