Logan
Logan

Reputation: 53132

Problems Adding TextView Programmatically Without Erasing Views in XML - Android

I'm trying to add a TextView programmatically, and it has proved immensely difficult. I have found code that works here (added to onCreate):

LinearLayout thisLayout = new LinearLayout(this);
setContentView(thisLayout);
TextView dynamicTextView = new TextView(this);
dynamicTextView.setText("Dynamically Added TextView");
thisLayout.addView(dynamicTextView);

The problem is that this erases all of the views I had created in the XML. There is a popular question with a highly upvoted answer HERE. Based on that, I created this:

LinearLayout myLayout = findViewById(R.layout.activity_main);

TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.FILL_PARENT,
        LinearLayout.LayoutParams.FILL_PARENT));
tv.setText("Trying Again");
myLayout.addView(tv);

But there is an error on this line:

LinearLayout myLayout = findViewById(R.layout.activity_main);

I try to cast it like this, which silences the error, but immediately crashes:

LinearLayout myLayout = (LinearLayout) findViewById(R.layout.activity_main);

I'm curious what I'm doing incorrectly. I realize that there are many similar questions out there, but none of them seem to work and many point to the answer I connected here. The problem I'm having is that the answer just doesn't work at all. Also, I'm in the process of familiarizing myself with the Android environment so if there's anything I'm doing that seems "off", please point it out.

Here's my XML:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin"
    tools:context="lowri.morepractice.app.MainActivity">

    <EditText android:id="@+id/edit_message"
        android:layout_weight="1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:hint="@string/edit_message" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/button_send"
        android:onClick="sendMessage"/>

</LinearLayout>

For anyone else who comes here, I also had to change the layout params to this:

tv.setLayoutParams(new LinearLayout.LayoutParams(
        LinearLayout.LayoutParams.MATCH_PARENT,
        LinearLayout.LayoutParams.WRAP_CONTENT));

Upvotes: 0

Views: 488

Answers (3)

luxer
luxer

Reputation: 660

You can't request the layout by id. It doesn't point to the LinearLayout.

To actually get the LinearLayout you have to add an id to it

<LinearLayout
    android:id="@+id/edit_linearlayout"
    ....

and request that like:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.edit_linearlayout);

To add the the TextView you have to set the LayoutParams like in the question you posted. Doesn't have to be FILL_PARENT of course:

TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.FILL_PARENT));
tv.setText("Trying Again");
myLayout.addView(tv);

Upvotes: 1

eluleci
eluleci

Reputation: 3529

You are using the xml and the LinearLayout wrong.

1- call this to set your xml as content view

setContentView(R.layout.activity_main);

2- give an id to your linear layout

android:id="@+id/myLinearLayout"

3- find your linear layout like this

LinearLayout myLayout = (LinearLayout) findViewById(R.id.myLinearLayout);

4- then add your view

TextView tv = new TextView(this);
tv.setLayoutParams(new LinearLayout.LayoutParams(
    LinearLayout.LayoutParams.FILL_PARENT,
    LinearLayout.LayoutParams.FILL_PARENT));
tv.setText("Trying Again");
myLayout.addView(tv);

I didn't try but this should work.

The mistakes you're doing are

1- SetContentView method overrides all the views in the content view. so it'll remove everything when you call

LinearLayout thisLayout = new LinearLayout(this);
setContentView(thisLayout);

2- You need to use id of an item while trying to find it. Not the name of the layout file like you did here

// findViewById gets the id of the view that you want to find
LinearLayout myLayout = (LinearLayout) findViewById(R.layout.activity_main);

Upvotes: 1

Chefes
Chefes

Reputation: 1942

The error is because you are looking for an XML file in your resources.

Try this,

In your code:

LinearLayout myLayout = (LinearLayout) findViewById(R.id.layout_main);

In your XML

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/layout_main"
android:layout_width="match_parent"

Upvotes: 1

Related Questions