Adding textview inside a linearlayout of a dialog fragment inside a class in android

927 Views Asked by At

I have a layout with a scrollview. Inside it, I have a LinearLayout and Button.

<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:scrollbars="none">

<LinearLayout
    android:id="@+id/ll_details"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:background="@color/white"
    android:orientation="vertical"
    android:padding="10dp">




    <Button
        android:id="@+id/ok"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:text="OK" />
</LinearLayout>

I am using a dialogfragment to view this layout. Also I am trying to add a textview dynamically. Below is my class

public class SecondLevelAdapter extends BaseExpandableListAdapter {
    private void showPreFilledData(String string) {
    final Dialog dialog = new Dialog(context);
    dialog.setContentView(R.layout.cust_data_layout);
    dialog.setTitle("Customer Info");

    final LinearLayout linearLayout = new LinearLayout(context);
    linearLayout.findViewById(R.id.ll_details);

    TextView textView1 = new TextView(context);
    textView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
            ViewGroup.LayoutParams.WRAP_CONTENT));
    textView1.setGravity(Gravity.CENTER);
    textView1.setText("programmatically created TextView1");
    linearLayout.addView(textView1);


    
    Button ok;

    ok = (Button) dialog.findViewById(R.id.ok);

    ok.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            dialog.dismiss();
        }
    });
    Window window = dialog.getWindow();
    window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
    dialog.show();

}
}

But when I launch the app I am unable to see the textview.

enter image description here

I must be missing something that I don't know.

Any help would be highly appreciated.

5

There are 5 best solutions below

0
On BEST ANSWER

You are creating a new LinearLayout instead of referencing the one in the xml file. Change your showPrefilledData function to,

private void showPreFilledData(String string) {
        final Dialog dialog = new Dialog(context);
        dialog.setContentView(R.layout.cust_data_layout);
        dialog.setTitle("Customer Info");

        LinearLayout linearLayout = dialog.findViewById(R.id.ll_details);

        TextView textView1 = new TextView(context);
        textView1.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
                ViewGroup.LayoutParams.WRAP_CONTENT));
        textView1.setGravity(Gravity.CENTER);
        textView1.setText("programmatically created TextView1");
        linearLayout.addView(textView1);



        Button ok;

        ok = (Button) dialog.findViewById(R.id.ok);

        ok.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        Window window = dialog.getWindow();
        window.setLayout(LinearLayout.LayoutParams.MATCH_PARENT, TableLayout.LayoutParams.MATCH_PARENT);
        dialog.show();

    }
6
On

Your way of getting the LinearLayout from the xml file is incorrect.

final LinearLayout linearLayout = new LinearLayout(context);
linearLayout.findViewById(R.id.ll_details);

In my understanding, this creates a new LinearLayout an attempts to find a child view with id ll_details under it. I think you want

final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll_details);

Then, adding the view to that LinearLayout should work.

0
On

You need to call addView to add a view programatically, but you need to do a little more than that to get it to work.

If you create a View via a constructor (eg: TextView textView = new TextView();), you will need to call setLayoutParams on the newly constructed view, passing in an instance of the parent view's LayoutParams inner class, before you add your newly constructed child to the parent view.

For example, you might have the following code in your onCreate() function assuming your LinearLayout has id R.id.main_layout:

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

 TextView textView = new TextView(this);
 textView.setLayoutParams(new LinearLayout.LayoutParams(
                                 LinearLayout.LayoutParams.MATCH_PARENT,
                                 LinearLayout.LayoutParams.MATCH_PARENT));

 myLayout.addView(textView);

Making sure to set the LayoutParams is important. Every view needs at least a layout_width and a layout_height parameter. Also getting the right inner class is important.

Also, I am not sure of your use case, but alternatively you can add a view in the xml itself, with a visibility of GONE, and later when you need it you can change it's visibility to VISIBLE. When visibility is set to GONE, it does not take any space until the visibility is made VISIBLE.

0
On

Forgot Everything removes redundant code. Use this code and set your custom dialog UI layout to replace with R.layout.my_dilog_ui and put it in your code.

 try {
        LayoutInflater factory = LayoutInflater.from(context);
        View views = factory.inflate(R.layout.my_dilog_ui, null);

        AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(context);
        AlertDialog alertDialog = alertDialogBuilder.create();

        alertDialogBuilder.setView(views);

        TextView tv_title = (TextView) views.findViewById(R.id.title);
        TextView title = views.findViewById(R.id.my_title);
        Button close = views.findViewById(R.id.close);

        tv_title.setText("Are you sure do you want to return your Order?");
        title.setText("Return request can not be revoked ");

        close.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                alertDialog.dismiss();
            }
        });

    
        alertDialog.setView(views);
   
        alertDialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
        alertDialog.show();
    } catch (Exception e) {

    }
0
On

dialog.setContentView(R.layout.cust_data_layout);

final LinearLayout linearLayout = new LinearLayout(context);

Haha, Look like you dialog content view isn't relate to your LinearLayout. Let use similar view in both.