Autosizing TextView in ConstraintLayout - android

I have a chain in a ConstraintLayout which consists in a TextView, an ImageView and another TextView (doesn't need autosizing). I'd like them three to be vertically centered in the parent container (i.e. same distance to left/right).
I want the TextView to autosize so the text doesn't overflow. I can't use autosize with a layout_width set to wrap_content (nothing happens and the text doesn't resize). I can't set a fixed layout_width either since the extra space when the text isn't very long throws off the alignment.
Is there anyway to do this via XML or should I implement my own autosizing behavior?
This is a picture of what I'm trying to achieve (a center-aligned chain):

For TextView Auto-sizing, use android:layout_width="0dp".
But make sure you apply the ViewComponents Left/Start and Right/End Constraints.
This trick is basically used instead of android:layout_width="fill_parent" in ConstraintLayout.
Using this, you'll achieve Auto-sizing and it will consume only the space falling under the specified constraints and no over-lapping and no spilling-out.

Related

Layout design in android having TextView with image

how can i mange Textview in layout.? please help me..
how can i set textview and border then again textview.
Just create a horizontal LinearLayout, and add an ImageView, a TextView, a View with width 2dp and height match_parent, and a TextView as children.
Either repeat that for every row, or use the recommended approach of using a ListView with the above mentioned layout as rows.
I'd use two compound TextViews: Just create a horizontal LinearLayout, and add a TextView with drawableLeft="#drawable/icon_location" and another one with drawableLeft="#drawable/line_vertical" as children.
For both, a drawablePadding="8dp" or whatever you like best

Android XML: Good examples to use gravity rather than layout_gravity

I have done some basic xml layout from the Internet and I am confused as to what android:gravity would be used for vs android:layout-gravity. The UI designer seems to respond well when I change the layout_gravity, but I get no response to the regular gravity? What is up with this?
Is this similar to layout parameters with fill-parent and match-parent? one is gone now.
android:gravity is a statement from a parent to its content, indicating where the content should slide within the available parent space (if there is room for such sliding).
android:layout_gravity is a request from a child to its parent, asking that it be slid in a certain direction (if there is room for such sliding).
layout_gravity - It contains and it belongs to component's parent layout. Like if you set one TextView in LinearLayout, LinearLayout's width is fill_parent, but TextView basically will set at left-top. So now if you would give layout_gravity=right then your TextView will be shifted to top-right corner. But the text will not change its place. i.e. you had been wrote "Hello world" then "hello world" will not be changed to right, but its parent textView will be changed to right. So here in this case it will belongs to LinearLayout, means parent Layout.
gravity- It contains and it belongs to TextView as per above explanation. if you will give gravity="right" then "Hello-world" text will go to right-top corner of TextView not LinearLayout.

How to keep EditText from breaking to the next line when width does not match_parent?

I am trying to fit two EditTexts horizontally. What I am trying to accomplish is make the text keep scrolling right without the EditText extending in anyway when there is no more space in that specific EditText. This happens when the EditText is set to match_parent (width), but when I set an actually width or wrap_content, it breaks to the next line. Not the behavior I am hoping for. Any ideas?
If I understood your question correctly, you're saying you want the edit text to be a single line each? If so, have you tried using the xml attribute
android:singleLine
Or you can accomplish the same thing with:
setSingleLine() //called on the edit text
I just tested it out with 2 edit texts in a horizontal linear layout set to 100dp width each and I think it accomplishes the behavior you want.
You can try this:
Set both to fill_parent and layout_weight=1. This will align them side by side.

Android: My text is being displayed vertically in a programmatically set TextView

I'm adding a TextView to a parent LinearLayout RelativeLayout programmtically. There is enough space for the text to be displayed horizontally but for some reason the text is displayed vertically. Does anyone know what's going on here?
It's really hard to say what is the problem without looking at your XML and layout code. In my experience, sometimes it happens when the parent layout has height set as WRAP_CONTENT and the views inside it have wrong weight config, for example one has weight but WRAP_CONTENT and the other MATCH_PARENT (Sorry, I don't remember the case exactly). I suggest you check your LayoutParams carefully or set a fixed width and height for the TextView to see what is the problem.
You can also post your code here so we can have a look at it

How to prevent TextView from resizing its container

Take a look at my layout
http://pastebin.com/6tQVm3Rk
My problem is that the textviews (named header1 to 5) are resizing its containers when a certain amount of letters are written into it, although there is still some space left.
What changes do I have to make that the layout stays in its original state independent from the amount of text located in the headers?
This might be because of this attribute.
android:inputType="textMultiLine"
If you want your textview to have only one line you can use android:singleline="true"
Add the attribute
android:maxLength="2"
to the textview. This way you can limit the number of characters.
I think its better to use Relative layout instead Linear layout in the XML so that objects in Relative layout are easy to manage dynamically, all you need to do is that put all those text views in the Relative layout and set these parameters:
layout align left:
layout align right:
And other solution which is not appropriate is that fix the size of text view then it will not expand.
And please see the Documentation for further details of Relative Layout.
Set the width and heigth of your textview to a fixed amount of dp. This will prevent the textview from streching beyond the width and height you declared. Like this:
android:layout_width="50dp"
android:layout_heigth="50dp"
For me the solution was to use
android:layout_width="0dp"
together with
android:maxLines="1"

Categories

Resources