Android - Multi-Line Linear Layout

Android - multi-line linear layout

Check the comments: this will do the job

/*
* Copyright 2011 Sherif
*/

private void populateText(LinearLayout ll, View[] views , Context mContext) {
Display display = getWindowManager().getDefaultDisplay();
ll.removeAllViews();
int maxWidth = display.getWidth() - 20;

LinearLayout.LayoutParams params;
LinearLayout newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setGravity(Gravity.LEFT);
newLL.setOrientation(LinearLayout.HORIZONTAL);

int widthSoFar = 0;

for (int i = 0 ; i < views.length ; i++ ){
LinearLayout LL = new LinearLayout(mContext);
LL.setOrientation(LinearLayout.HORIZONTAL);
LL.setGravity(Gravity.CENTER_HORIZONTAL|Gravity.BOTTOM);
LL.setLayoutParams(new ListView.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
//my old code
//TV = new TextView(mContext);
//TV.setText(textArray[i]);
//TV.setTextSize(size); <<<< SET TEXT SIZE
//TV.measure(0, 0);
views[i].measure(0,0);
params = new LinearLayout.LayoutParams(views[i].getMeasuredWidth(),
LayoutParams.WRAP_CONTENT);
//params.setMargins(5, 0, 5, 0); // YOU CAN USE THIS
//LL.addView(TV, params);
LL.addView(views[i], params);
LL.measure(0, 0);
widthSoFar += views[i].getMeasuredWidth();// YOU MAY NEED TO ADD THE MARGINS
if (widthSoFar >= maxWidth) {
ll.addView(newLL);

newLL = new LinearLayout(mContext);
newLL.setLayoutParams(new LayoutParams(
LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
newLL.setOrientation(LinearLayout.HORIZONTAL);
newLL.setGravity(Gravity.LEFT);
params = new LinearLayout.LayoutParams(LL
.getMeasuredWidth(), LL.getMeasuredHeight());
newLL.addView(LL, params);
widthSoFar = LL.getMeasuredWidth();
} else {
newLL.addView(LL);
}
}
ll.addView(newLL);
}

Multiple lines in a android linear layout

I'm not sure if these are cut-and-past errors, but I noticed a few things in the xml. The first nested linear layout is missing a '>' before the EditText and doesn't specify a layout_height. And the first button also doesn't have a layout_height.

How do I alignBaseline to bottom line of multi-line TextView?

You could accomplish this with RelativeLayout if you implement a custom TextView. Specifically, you could override TextView.getBaseline like so:

package mypackage.name;

// TODO: imports, etc.

public class BaselineLastLineTextView extends TextView {
// TODO: constructors, etc.

@Override
public int getBaseline() {
Layout layout = getLayout();
if (layout == null) {
return super.getBaseline();
}
int baselineOffset = super.getBaseline() - layout.getLineBaseline(0);
return baselineOffset + layout.getLineBaseline(layout.getLineCount()-1);
}
}

Then you would use your custom TextView inside a RelativeLayout as follows:

<mypackage.name.BaselineLastLineTextView
android:id="@+id/text_view_1"
<!-- TODO: other TextView properties -->
/>

<TextView
android:id="@+id/text_view_2"
android:layout_alignBaseline="@id/text_view_1"
<!-- TODO: other TextView properties -->
/>

Android: Multiline TextView in Horizontal LinearLayout

It worked fine for me, try to put more characters in the text like:

android:text="Multiline text here with more characters uhuuuuu"

One TextView per line in LinearLayout

So it turns the Android Studio default FragmentList code will create a GridLayout if the list count is bigger than 1:

class TweetsFragment : Fragment() {

override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
val view = inflater.inflate(R.layout.fragment_tweets_list, container, false)

// Set the adapter
if (view is RecyclerView) {
with(view) {
layoutManager = when {
columnCount <= 1 -> LinearLayoutManager(context)
else -> GridLayoutManager(context, columnCount)
}
adapter = TweetsViewAdapter(TwitterPlaceholderContent.ITEMS)
}
}
return view
}
}

The solution is to always create a LinearLayout inside the onCreateView method:

if (view is RecyclerView) {
with(view) {
layoutManager = LinearLayoutManager(context)
adapter = TweetsViewAdapter(TwitterPlaceholderContent.ITEMS)
}
}

Horizontal LinearLayout with Multiple Children, Move Children Below on New Line When No More Horizontal Space

I would like to have a horizontal LinearLayout that is as wide as the
screen and as high as its children, but the trick is that its children
will have dynamic width each and I don't want them going off screen
(cut out).

A LinearLayout can't do that(any default layout from the SDK can't do that) because the LinearLayout is set to place all the children in one horizontal or vertical line. Also, any type of conditional layout rules based on dimensions not yet available(like in your case the available width for the LinearLayout) are not possible in the xml anyway.

What you need is a custom layout which measures the children to use the available space moving any non fitting children on a new line below(a so called FlowLayout).

Edit:

Google now provides the flexbox library which implements the web's flexbox layout on Android. Using that library, the children of a FlexboxLayout will be placed on multiple lines based on their widths by using the flexDirection(with a value of row) and flexWrap(with a value of wrap) attributes.

Cannot get the EditText to have multiple lines

I finally was able to resolve my issue by overriding the onViewCreated of the fragment and putting these line inside:

mAboutMeBox.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_FLAG_MULTI_LINE);
mAboutMeBox.setSingleLine(false);
mAboutMeBox.setImeOptions(EditorInfo.IME_FLAG_NO_ENTER_ACTION);

Forcing a TextView to be with a multiple line text

Use the below layout I have mentioned in comments too.

<LinearLayout
android:id="@+id/linearLayoutBottomData"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/linearLayoutTopData"
android:layout_marginBottom="5dp"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="1">

<TextView
android:id="@+id/myTextView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="left|start"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="@dimen/large_font_size"
android:singleLine="false"/>

<TextView
android:id="@+id/myTextView2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.4"
android:gravity="left|start"
android:text="TextView"
android:textColor="@android:color/black"
android:textSize="@dimen/large_font_size"/>



Related Topics



Leave a reply



Submit