2

I'm trying to dynamically add a layout into MainActivity (default Android Application that Android Studio creates), but the new layout isn't being displayed. I've tried with a different layout, but that one was clipped after ~450px height.

I'm pretty new to Android UI programming and I have a feeling I'm doing something wrong in the adding function. Here's the adding code (which was inspired from this answer):

LayoutInflater vi = (LayoutInflater) getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout linearLayout = (LinearLayout) findViewById(R.id.linlayout);
View v = vi.inflate(R.layout.test_layout, null);

// insert into main view
ViewGroup.LayoutParams layoutParams = new ViewGroup.LayoutParams(
        1000,//ViewGroup.LayoutParams.FILL_PARENT,
        1000);//ViewGroup.LayoutParams.FILL_PARENT);

linearLayout.addView(v, 0, layoutParams);

This is the activity_main.xml:

<include
    android:id="@+id/include"
    layout="@layout/content_main" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|end"
    android:layout_margin="@dimen/fab_margin"
    app:srcCompat="@android:drawable/ic_dialog_email" />

<LinearLayout
    android:id="@+id/linlayout"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal">
    <!--<include layout="@layout/test_layout"/>-->
</LinearLayout>

And here's the layout I'm trying to add:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">

    <ImageView
        android:id="@+id/imageView2"
        android:layout_width="188dp"
        android:layout_height="191dp"
        app:srcCompat="@mipmap/ic_launcher" />
</RelativeLayout>

If I add the layout statically (adding <include layout="@layout/test_layout"/> in the xml), test_layout is shown properly.

What am I doing wrong here?

(Using Android 7.1.2 on Nexus 5X)

Nitay
  • 4,193
  • 6
  • 33
  • 42

2 Answers2

1

Use this method to add View Dynamically:

LinearLayout myLayout = (LinearLayout)findViewById(R.id.linlayout);
View testlin= getLayoutInflater().inflate(R.layout.test_layout, myLayout, false); 
myLayout.addView(testlin); 
Divyesh Patel
  • 2,576
  • 2
  • 15
  • 30
0
        ll_fovorite = (LinearLayout) view.findViewById(R.id.ll_fovorite);

        tv_no_fovorite = new TextView(getActivity());
        tv_no_fovorite.setText("There are no fovorite words");
        tv_no_fovorite.setTextColor(Color.parseColor("#8b8682"));
        tv_no_fovorite.setTextSize(24);
        tv_no_fovorite.setGravity(Gravity.CENTER);
        tv_no_fovorite.setTypeface(Typeface.MONOSPACE);
        ll_fovorite.addView(tv_no_fovorite, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
Fakhriddin Abdullaev
  • 4,169
  • 2
  • 35
  • 37