My Life with Android
Introduction Android Componets UserInterface Advanced UI Data Storage Advanced Concepts Others New Studio

Layout Inflator

a_inflatedemo.xml


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

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent" >

        <LinearLayout
            android:id="@+id/parent_linear_layout"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical" >
        </LinearLayout>
    </ScrollView>

</LinearLayout>

a_child_layout_demo.xml


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="horizontal" >
 <ImageView
        android:id="@+id/imageView1"
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:src="@drawable/ic_launcher" />
    <TextView
        android:id="@+id/textView1"
        android:layout_width="match_parent"
        android:layout_marginLeft="10dp"
        android:layout_height="wrap_content"
        android:textColor="#F00"
        android:layout_gravity="center_vertical"
        android:text="TextView" />

</LinearLayout>

File : MainActivity.java

package com.example.androidcollegeppt;

import com.squareup.picasso.Picasso;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class InflateViewEx extends Activity{
	LinearLayout parent_linear_layout;

	public  String [] prgmNameList={"C","C++","Java","Android"};
	public  String [] prgmImageUrl={"https://i.ytimg.com/vi/MBSTuYfD7oM/hqdefault.jpg","http://mekonah.com/wp-content/uploads/2015/11/C-logo.png","http://tarnaeluin.houseofbeor.net/wp-content/uploads/2014/08/Java-Logo.jpg","https://www.seeklogo.net/wp-content/uploads/2015/07/android-vector-logo.png"};
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.a_inflatedemo);//Step-1
		parent_linear_layout=(LinearLayout)findViewById(R.id.parent_linear_layout);//Step-2
		
		LayoutInflater inflater = (LayoutInflater) InflateViewEx.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);//Step-3
		
		//Step-4
		for(int i=0;i<prgmNameList.length;i++){
			
		View child = inflater.inflate(R.layout.a_child_layout_demo, null);
		TextView tv=(TextView)child.findViewById(R.id.textView1);
		ImageView imageView1=(ImageView)child.findViewById(R.id.imageView1);
		tv.setText(prgmNameList[i]);
		Picasso.with(InflateViewEx.this).load(prgmImageUrl[i]).into(imageView1);
		parent_linear_layout.addView(child);//Step-5
		
		}
		
	}
}


Android - Layout