Amandeep Singh February 2016

Dynamic view getting inserted with whole screen android

I am trying to create an app where parent layout is a scrollable view and I add a view dynamically to this parent layout using following code:

    LinearLayout ll = (LinearLayout) findViewById(R.id.parent);

    for(int i=0;i<5;i++){
        LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );
        View format = inflater.inflate(R.layout.student_present_absent,null);
        ll.addView(format);
    }

Here is the view where I want to insert the view:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:layout_width="match_parent" android:layout_height="match_parent"
 android:orientation="vertical"
 android:background="@drawable/backgroundf"
 android:weightSum="8"
 android:id="@+id/takeattendanceinterim">

        <LinearLayout
            android:orientation="vertical"
            android:layout_width="fill_parent"
            android:layout_height="0dp"
            android:layout_weight="1.1">
            <LinearLayout
                android:orientation="horizontal"
                android:layout_width="match_parent"
                android:layout_height="match_parent"
                android:weightSum="10">

                <LinearLayout
                    android:orientation="vertical"
                    android:layout_width="0dp"
                    android:layout_height="match_parent"
                    android:layout_weight="2">

                    <ImageButton
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:id="@+id/bLogoImage"
                        android:src="@drawable/icwhitehouse"
                        android:layout_gravity="right"
                        android:layout_margin="5dp"
                        android:background="@null"/>
                </LinearLayout>

                   

Answers


Vasant February 2016

You can add 5 view static as like below.

LayoutInflater inflater = (LayoutInflater) getSystemService( Context.LAYOUT_INFLATER_SERVICE );

 View format = inflater.inflate(R.layout.student_present_absent,null);
 View format1 = inflater.inflate(R.layout.student_present_absent,null);
 View format2 = inflater.inflate(R.layout.student_present_absent,null);
 View format3 = inflater.inflate(R.layout.student_present_absent,null);
 View format4 = inflater.inflate(R.layout.student_present_absent,null);
  ll.addView(format);
  ll.addView(format1);
  ll.addView(format2);
  ll.addView(format3);
  ll.addView(format4);


LuXoR February 2016

This sounds like a usecase for a Listview/Recyclerview with multiple layouts.

Using google I found the following Link... I didn't test it but it looks quite good at first sight:

http://www.survivingwithandroid.com/2014/08/android-listview-with-multiple-row.html

Post Status

Asked in February 2016
Viewed 3,444 times
Voted 6
Answered 2 times

Search




Leave an answer