Search code examples
androidandroid-layouttextviewandroid-dialogfragmentandroid-dynamic-shortcuts

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


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.


Solution

  • 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();
    
        }