How to Add a Textview to Linearlayout in Android

Adding TextViews inside horizontal LinearLayout dynamically

The interestWidth and parentWidth are initially 0 because they have not been laid out when getWidth is called.

get width for dynamically created textViews

The above link helped me getting width of dynamically created textViews from interestList.

And by using ViewTreeObserver on interestLinearLayout I was able to get the width of LinearLayout after it was laid out.

Finally, the above code should be modified as below to add textViews from JAVA inside a LinearLayout.

       final LinearLayout interestLinearLayout = findViewById(R.id.interests);
interestLinearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
@Override
public void onGlobalLayout() {
interestLinearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
String interestList[]={"Travel","Music","Photography","Sports","Dance","Animals","SciFi Movies"};
int interestWidth =0;
int parentWidth = interestLinearLayout.getWidth(); // got width inside view tree observer for linearlayout
for(String interest: interestList) {
TextView textView = new TextView(MainActivity.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.setMargins(2,0,10,2);
textView.setLayoutParams(params);
textView.setPadding(2, 2, 2, 2);
textView.setText(interest);
textView.setIncludeFontPadding(true);
textView.measure(0,0); //using approach mentioned in link to get width of text views
interestWidth += textView.getMeasuredWidth();
if(interestWidth<parentWidth)
interestLinearLayout.addView(textView);
else
break;
}
}
});

How to set textview under each other in linearlayout

You can use something like below:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">

<ImageView
android:id="@+id/displayImage"
android:layout_width="67dp"
android:layout_height="100dp"
android:src="@color/colorPrimaryDark" />

<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:orientation="vertical">

<TextView
android:id="@+id/textview_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="3dp"
android:text="Left text"
android:textColor="@color/colorPrimaryDark"
android:textStyle="bold" />

<TextView
android:id="@+id/textview_description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingBottom="3dp"
android:text="Right text" />
</LinearLayout>
</LinearLayout>

You can nest containers like LinearLayout,RelativeLayout, FrameLayout, etc inside each other to create complex Layouts.

Also, I'd advise knowing what each tags do by Googling them before using them inappropriately anywhere inside your layout. It needed me solid 5 minutes to fix your layout because you added layout_weight basically anywhere you pleased!

Dynamically add textViews to a linearLayout

Something like the following should be what you need:

final int N = 10; // total number of textviews to add

final TextView[] myTextViews = new TextView[N]; // create an empty array;

for (int i = 0; i < N; i++) {
// create a new textview
final TextView rowTextView = new TextView(this);

// set some properties of rowTextView or something
rowTextView.setText("This is row #" + i);

// add the textview to the linearlayout
myLinearLayout.addView(rowTextView);

// save a reference to the textview for later
myTextViews[i] = rowTextView;
}

How to add image view and text view to a linear layout

Try this way,hope this will help you to solve your problem.

main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/outerLinearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="25dp" >

<TextView
android:id="@+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="12dp"
android:text="@string/app_name"
android:textSize="24.5sp" />

</LinearLayout>

MainActivity.java

public class MyActivity extends Activity {

private LinearLayout outerLinearLayout;
private TextView text1;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
outerLinearLayout = (LinearLayout) findViewById(R.id.outerLinearLayout);
text1 = (TextView) findViewById(R.id.text1);

LinearLayout innerLinearLayout = new LinearLayout(this);

ImageView imageView = new ImageView(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
params.bottomMargin=12;
imageView.setLayoutParams(params);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(R.drawable.ic_launcher);
innerLinearLayout.addView(imageView);
TextView textView = new TextView(this);
textView.setTextSize((float) 24.5);
textView.setText("New Text View");
textView.setLayoutParams(params);
outerLinearLayout.removeAllViews();
innerLinearLayout.addView(text1);
outerLinearLayout.addView(textView);
outerLinearLayout.addView(innerLinearLayout);
}
}

How to add TextView to custom LinearLayout view?

I'd personsally go down the inflation route, as have had headaches in the past caused by programmatically adding views. Here's a quick example of a custom view that's extended from LinearLayout, inflated from an XML layout file, with a public method for setting the value of the embedded textview.

The key bit is this:

private TextView embeddedTextView;

..

private void init() {

LayoutInflater.from(getContext()).inflate(
R.layout.linear_layout_with_textview_layout, this);

embeddedTextView = (TextView) findViewById(R.id.embedded_text_view);

}

public void setEmbeddedTextViewText(String text) {

embeddedTextView.setText(text);
}

I take this approach as you swap out different styled layouts in XML and use the same Custom View; proper code re-usability. Less work in the long-run.

Edit: Here's a way of hiding the textview by default, or an empty string "".



Related Topics



Leave a reply



Submit