Android Dynamic View Positioning - android

I need view2 to be next to view1 if view1 is not filling the whole width.
If the content of view1 is full width (the content fills whole width of screen) then view2 should be under it.
For example if my TextView text is short a timestamp be next to it
If TextView text is multiline the time stamp be under it
Can I achieve this using ConstraintLayout?
When view1 is not full width
When view1 is full width

Yes, but, i recommend use the Constraint Layout in head but a other layout (Relative or Recycle) to organize the other elements from view.
You can use a custom width, if you are a sure the position.
Or, still, you can try use "parent", if you divide in fragments the wrap content works too.

Related

Android Row: How to make first and third views' widths equal to the widest of them, with the middle view expanding to fill the row

I am designing a title row that has the item title in the middle, with a status description on the left and expiration date description on the right. The title should be centered at all times and expand width until it runs into either of the side views, at which time it should get ellipsized. I'm having trouble figuring out how to keep the title view centered while allowing for the two side views to have different sizes, while also trying to show as much of the text as possible in cases where the side views have only two or three characters, or as many as 20.
This is NOT a basic question of having the center view fill the space. The cases will outline the additional requirements I have that make it more tricky.
I tried two main approaches:
LinearLayout
I started with a horizontal LinearLayout. The first and third TextViews had widths of WRAP_CONTENT while the center title view had a width of 0dp and weight of 1, allowing it to fill the middle section of the row. The issue here was that the first and third views wouldn't have the same width with WRAP_CONTENT, so the title view would be shifted one way or the other. See here how the title is shifted to the left towards the smaller text:
After that, I assigned a weightSum of 20 to the LinearLayout, gave weights of 4 to the first and third views, a weight of 12 to the middle view, and width of 0dp for all 3. This worked better but wouldn't allow for longer text in the end views, even when there was plenty of room in the row for the view to expand:
Changing the views to use layout_weight and widths of WRAP_CONTENT didn't work either.
ConstraintLayout
I switch the row to a ConstraintLayout to try that as well.
I started this route with the end views using WRAP_CONTENT as their width and the title view using 0dp. The title view had its start linked to the end of the first view, and its end linked to the start of the last view. Of course, this would shift the title view towards whichever end text was shorter. Changing the end views to a width of 0dp yielded the same result.
From here I got closer. I linked the middle view's start and end to the parent LinearLayout so that it stretched the entire width. Then I linked the first view's end to the middle view's start, and the last view's start to the middle view's end, and set both of their widths to 0dp. By setting the middle view's width to 0dp and adding a marginStart and marginEnd of 64dp, I allowed some space for the first and last views to be shown. Still, this was like the LinearLayout solution with weights, as the gaps on each side of the middle view was of the fixed 64dp size, and didn't allow for expansion.
My last effort was so close! Changing the middle view's width to WRAP_CONTENT, and start and end both linked to the parent, allowed the end views to be of equal widths, and fully expanded. That is, UNTIL the title view became very long. With a width of WRAP_CONTENT, the middle view would push the end views off the screen with a very long text, even with a min_width set on the end views.
Changing the end views to have widths of WRAP_CONTENT didn't work either.
What I want to accomplish is most like this last try, except that I want the middle view to stop expanding and become ellipsized instead of pushing the end views out. Any other ideas to try?
The problem is if you want the first and third views equal width I don't think it is possible with just standard xml layouts as it's too late once a view has been sized, it won't be resized again without extra code.
Something like TableLayout has the extra code to resize all the rows to match, one it's sized them individually.
But you can to this yourself programmatically
e.g
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout mainLayout = findViewById(R.id.content);
// Wait until mainLayout has it's size calculated
ViewTreeObserver.OnGlobalLayoutListener mGlobalLayoutListener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Don't listen anymore
mainLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
TextView col1 = new TextView(getApplicationContext());
TextView col2 = new TextView(getApplicationContext());
TextView col3 = new TextView(getApplicationContext());
col1.setText("Much Long");
col1.setBackgroundColor(Color.CYAN);
col3.setText("Long");
col3.setBackgroundColor(Color.CYAN);
col3.setGravity(Gravity.RIGHT);
col2.setText("Really Really Really Really Really Really Really Really Really Long");
col2.setGravity(Gravity.CENTER_HORIZONTAL);
col2.setSingleLine(true);
// Set it to have Ellipsizes if too long
TextUtils.TruncateAt truncate = TextUtils.TruncateAt.END;
col2.setEllipsize(truncate);
int measureSpec = View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED); // As big as the view wants to be
// Measure all the views
col1.measure(measureSpec, measureSpec);
col2.measure(measureSpec, measureSpec);
col3.measure(measureSpec, measureSpec);
// work which side is bigger
int maxWidth = Math.max(col1.getMeasuredWidth(), col3.getMeasuredWidth());
int parentWidth = mainLayout.getWidth();
// Probably should have some checks to make sure centre width does not go negative
int centreWidth = parentWidth - (2 * maxWidth);
col1.setWidth(maxWidth);
col2.setWidth(centreWidth);
col3.setWidth(maxWidth);
mainLayout.addView(col1);
mainLayout.addView(col2);
mainLayout.addView(col3);
}
};
mainLayout.getViewTreeObserver().addOnGlobalLayoutListener(mGlobalLayoutListener);
}
}
Produces some examples
The centre of the middle column is always at the centre of the screen

Constraint Layout - Move view below of the neighbour view if neighbour has reached a certain width

I have 2 views.
The view A is in wrap_content and with a max_width of 100dp. Then there's this view B next to him. I want view B to move below view A when view A reachs 100 dp width.
How can I accomplish this with Constraint Layout and xml? Or must be done programatically?
You can move change a view's constraint's programmatically. You could listen for the view's width and perform a change when it reaches >100dp.
Video on the subject:
https://www.youtube.com/watch?v=-sPOtGqd5OA

Set Padding of TextView depending on Content height of TextView

I currently have a textview with a paddingBottom of 20dp.
I want to reduce this padding if the text within the textView is long enough to otherwise get cut off at the bottom.
Here's a diagram illustrating the behavior I want:
However, I'm not sure where to call setPadding().
Can someone recommend me an approach for getting this behavior of a dynamic bottom padding?
I ended up creating a custom Layout that contained my textView, and overrode its onMeasure() method. The custom layout was set with height='wrap_content'.
The system passes in the available height to this layout's onMeasure() method. So here, I measured my child textView's height with
textView.onMeasure()
int preferredHeight = textView.getMeasuredHeight()
If its preferred height was larger than what my custom view had available to it (minus the bottom padding), I removed the bottom padding with setPadding(). Otherwise, I added the padding.

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

use center of view for alignment

I'd like to use the center of a View to align it in another View, let's say, a TextView in a RelativeLayout.
I don't want to have it centered inside the RelativeLayout but rather offset from the bottom left corner.
At the moment I align the TextView ParentLeft and ParentBottom and use margins on left and bottom to move it away from those sides.
However, because my TextView is set to wrap_content in width, it "moves" around when the text inside and with it its width changes (since the borders of the TextView are used for the offsets).
I want the center of the TextView to stay on my offsetted position.
Is this possible in the xml, without having to write code that manages the offset or to use another layout?
That's the unwanted behaviour when the text changes: Screen1
The desirable behaviour: Screen2
Note that the center of the TextView stays the same.
There is a simple logic here. If you want to align a view with respect to the parent then for relative layout you can use align_parent wherein in linear use android:layout_gravity. Here in you case make textview as match_parent and set gravity to centre.

Categories

Resources