How to add a TextView to LinearLayout in Android

301,518

Solution 1

try using

LinearLayout linearLayout = (LinearLayout)findViewById(R.id.info);
...
linearLayout.addView(valueTV);

also make sure that the layout params you're creating are LinearLayout.LayoutParams...

Solution 2

Hey i have checked your code, there is no serious error in your code. this is complete code:

main.xml:-

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:id="@+id/info"
android:layout_height="wrap_content" 
android:orientation="vertical">
</LinearLayout>

this is Stackoverflow.java

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.TextView;

public class Stackoverflow extends Activity {
    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        View linearLayout =  findViewById(R.id.info);
        //LinearLayout layout = (LinearLayout) findViewById(R.id.info);

        TextView valueTV = new TextView(this);
        valueTV.setText("hallo hallo");
        valueTV.setId(5);
        valueTV.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));

        ((LinearLayout) linearLayout).addView(valueTV);
    }
}

copy this code, and run it. it is completely error free. take care...

Solution 3

You can add a TextView to your linear layout programmatically like this:

LinearLayout linearLayout = (LinearLayout) findViewById(R.id.mylayout);
TextView txt1 = new TextView(MyClass.this);
linearLayout.setBackgroundColor(Color.TRANSPARENT);
linearLayout.addView(txt1);

Solution 4

for(int j=0;j<30;j++) {
    LinearLayout childLayout = new LinearLayout(MainActivity.this);
    LinearLayout.LayoutParams linearParams = new LinearLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT);
    childLayout.setLayoutParams(linearParams);

    TextView mType = new TextView(MainActivity.this);
    TextView mValue = new TextView(MainActivity.this);

    mType.setLayoutParams(new TableLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1f));
    mValue.setLayoutParams(new TableLayout.LayoutParams(
        LayoutParams.WRAP_CONTENT,
        LayoutParams.WRAP_CONTENT, 1f));

    mType.setTextSize(17);
    mType.setPadding(5, 3, 0, 3);
    mType.setTypeface(Typeface.DEFAULT_BOLD);
    mType.setGravity(Gravity.LEFT | Gravity.CENTER);

    mValue.setTextSize(16);
    mValue.setPadding(5, 3, 0, 3);
    mValue.setTypeface(null, Typeface.ITALIC);
    mValue.setGravity(Gravity.LEFT | Gravity.CENTER);

    mType.setText("111");
    mValue.setText("111");

    childLayout.addView(mValue, 0);
    childLayout.addView(mType, 0);

    linear.addView(childLayout);
}

Solution 5

You should use something similar to this for adding TextView to LinearLayout dynamically:

LinearLayout linearLayout = getActivity().findViewById(R.id.infoLayout);

TextView valueTV = new TextView(context);
valueTV.setText("hallo hallo");
valueTV.setId(Integer.parseInt("5"));
valueTV.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.FILL_PARENT,
       LinearLayout.LayoutParams.WRAP_CONTENT));

linearLayout.addView(valueTV);

getActivity() is used for inside Fragments, you can use context or anything similar per each instance you are inside.

Share:
301,518
Martin
Author by

Martin

Updated on July 08, 2022

Comments

  • Martin
    Martin almost 2 years

    I am trying to add TextViews to my xml-defined layout in code. I have a xml-sheet, where a lot of Views are defined. But I have to add some views in code, so a create a LinearLayout in the xml-sheet:

    <LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:id="@+id/info"
    android:layout_height="wrap_content" 
    android:orientation="vertical">
    </LinearLayout>
    

    And in this layout, I like to add my TextView:

        View linearLayout =  findViewById(R.id.info);
        //LinearLayout layout = (LinearLayout) findViewById(R.id.info);
    
    
        TextView valueTV = new TextView(this);
        valueTV.setText("hallo hallo");
        valueTV.setId(5);
        valueTV.setLayoutParams(new LayoutParams(
                LayoutParams.FILL_PARENT,
                LayoutParams.WRAP_CONTENT));
    
        ((LinearLayout) linearLayout).addView(valueTV);
    

    But I only get the following error message:

    : java.lang.ClassCastException: android.widget.TextView
    

    How can I do it?

    Thanks for you help. Martin

    • Robby Pond
      Robby Pond almost 14 years
      Which line is that exception for? It must be from the LinearLayout cast, are you sure the linearLayout variable is a LinearLayout and not a TextView? Also you shouldn't be specifying the Id since you can't guarantee it will be unique.
    • Martin
      Martin almost 14 years
      You are right, linearLayout is a TextView, but why? I have defined it in the xml-file as a LinearLayout ...
    • Rodja
      Rodja over 12 years
      Make sure you are really operating on the xml shown above. Is setContentView(R.layout.your_xml_layout); really loading the right xml? Do you have other xml layouts where you use android:id="@+id/info" which happen to be a TextView?
    • Talha
      Talha over 7 years
      Is this issue resolved? Kindly accept as answer or post one.
  • Martin
    Martin almost 14 years
    there is again an exception, because findViewById returns an TextView. But why? I fetch it with the LinearLayout id.... what do you mean with: also make sure that the layout params you're creating are LinearLayout.LayoutParams... ???
  • drusepth
    drusepth almost 12 years
    What is MyClass.this? I'm new to Android development; am I supposed to substitute the name of my fragment class in for "MyClass"?
  • Mihai Bratulescu
    Mihai Bratulescu over 10 years
    MyClass.this is a context and a text view takes in a context
  • Si8
    Si8 over 10 years
    What if I wanted to have the following TextView: <TextView android:id="@+id/tvDInfo3" android:layout_width="0dp" android:layout_height="wrap_content" android:textStyle="bold" android:text="Release Date" android:gravity="center" android:padding="@dimen/dyk_text_pad" android:textColor="#000000" android:textSize="@dimen/info_text_size" android:layout_weight="1" />
  • Subby
    Subby almost 10 years
    +1. This answer is much better than @Ben's as it contains an example of LayoutParams and how the rest of the properties of the TextView is initialised. This should be marked as an answer.
  • drigoangelo
    drigoangelo over 9 years
    Just for clarification: MyClass.this, in most cases, is the same as this. You need to specify, however, the name of the class, if you are in a nested class and want to access the instance of the "outer" class, which is very common when defining callbacks for the events in android.
  • drigoangelo
    drigoangelo over 9 years
    I guess some android developers got used to putting the name of the class where it's needed and started putting it everywhere. Also, MyClass.this is an instance of MyClass, and it will only be a context if MyClass implements Context (e.g. extends Activity)
  • mrres1
    mrres1 over 9 years
    Have you tested this?
  • user3509153
    user3509153 over 9 years
    Yes, I have. And it works perfectly! Have you tried it?
  • yesennes
    yesennes almost 8 years
    Why would that change anything? All it does is cast sooner rather than later, which should have the same effect.
  • Noor Hossain
    Noor Hossain over 3 years
    has to define, linear.addView(childLayout);, linear as Linear Layout at first