Thursday, June 5, 2014

Dyanmic List using code

package com.example.dynamicview;

import java.util.ArrayList;

import android.app.Activity;
import android.app.ActionBar;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.os.Build;

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

       
//        String[] StringArray = { "one", "two", "three", "four", "five" };
    /*    ArrayList<String> name = new ArrayList<String>();
        name.add("Google");
        name.add("Microsoft");
        name.add("Apple");
        name.add("Oracle");
        int topval=100;
       
        RelativeLayout layout = new RelativeLayout(this);
        RelativeLayout.LayoutParams relativeParams = new RelativeLayout.LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        for (int i = 0; i < name.size(); i++) {
            TextView tv = new TextView(getApplicationContext());
            tv.setText(name.get(i));
            relativeParams.addRule(RelativeLayout.BELOW, tv.getId());
            relativeParams.topMargin=topval+100;
            layout.addView(tv, relativeParams);
        }       
       
        setContentView(layout);*/
       
        ArrayList<Person> namelist = new ArrayList<Person>();
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
        namelist.add(new Person("Ram","Bagbazar","98011234534"));
       
        LinearLayout layout;
        layout = (LinearLayout) findViewById(R.id.wrapper);
       
        for(int i=0;i<namelist.size();i++){
       
    View row=    getLayoutInflater().inflate(R.layout.list_row, null);
       
        TextView list_name = (TextView) row.findViewById(R.id.list_text);
        TextView list_address = (TextView) row.findViewById(R.id.list_address);
        TextView list_phone = (TextView) row.findViewById(R.id.list_phone);
   
        list_name.setText(namelist.get(i).getName());
        list_address.setText(namelist.get(i).getAddress());
        list_phone.setText(namelist.get(i).getPhone());
       
        layout.addView(row);
        }
        //setContentView(layout);
}
}











//list_view.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical" >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:paddingLeft="10dp" >
        <TextView
            android:id="@+id/tvName"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_alignParentTop="true"
            android:hint="Amir Shrestha"
            android:textSize="20sp" />
        <TextView
            android:id="@+id/tvPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentLeft="true"
            android:layout_below="@+id/tvName"
            android:hint="9801165310" />
        <TextView
            android:id="@+id/tvAddress"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_above="@+id/tvPhone"
            android:layout_marginLeft="20dp"
            android:layout_toRightOf="@+id/tvName"
            android:hint="Dhapasi" />
        <View
            android:id="@+id/view1"
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:layout_alignRight="@+id/imPhone"
            android:layout_below="@+id/tvPhone"
            android:background="#B2BEB5"
            android:paddingBottom="10dp"
            android:paddingTop="10dp" />
        <ImageView
            android:id="@+id/imPhone"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_alignParentRight="true"
            android:layout_alignParentTop="true"
            android:layout_marginRight="20dp"
            android:src="@android:drawable/ic_menu_call" />
    </RelativeLayout>
</LinearLayout>


//activity_main.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
   
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    >
   
   
    <LinearLayout android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:orientation="vertical"
    android:id="@+id/wrapper">
   
   

</LinearLayout>
   
   

</RelativeLayout>
</ScrollView>

No comments :

Post a Comment