GTU Android Program 5 Android List Example

GTU Android Program 5 Create an android application that will pass some number to the next screen , and on the next screen that number of items should be display in the list

GTU Android Program 5

Hope you have understand the Android Browser Intent example. Now i am going to explain how to add Listview in to Android and add items programmatically based on user request. for example user ask to add 5 items then we will add 5 items to listview using adapter. Here is the complete working code for listview in android.

 

Location : res\layout

FileName: activity_main.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.moc.prog05.MainActivity" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:text="Enter Number (1-10)"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/textView1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="22dp"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/editText1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="17dp"
        android:text="Next" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_below="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="20dp"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceLarge" />

</RelativeLayout>

 

Location : res\layout

FileName: activity_main2.xml

<RelativeLayout 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"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="gtu.moc.prog05.MainActivity2" >

    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_above="@+id/textView2"
        android:layout_alignParentLeft="true"
        android:layout_below="@+id/textView1"
        android:layout_marginTop="24dp" >

    </ListView>

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="18dp"
        android:text="List Items" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:text="Developed By: Adarsh Patel"
        android:textAppearance="?android:attr/textAppearanceMedium" />

</RelativeLayout>

 

Location : src\package\

FileName : MainActivity.java

package gtu.moc.prog05;

/* 
*  GTU Android Program 5 Create an android application that will pass some number to the next screen , and on the next screen that number of items should be display in the list
*/

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class MainActivity extends Activity {
	
	Button b1;
	EditText et1;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		b1 = (Button) findViewById(R.id.button1);
		et1 = (EditText) findViewById(R.id.editText1);
		
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View arg0) {
			
				Intent i = new Intent(getApplicationContext(),MainActivity2.class);
				i.putExtra("parameter1", et1.getText().toString());
				startActivity(i);
			}
		});
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Location : src\package\

FileName : MainActivity2.java

package gtu.moc.prog05;

/* 
*  GTU Android Program 5 Create an android application that will pass some number to the next screen , and on the next screen that number of items should be display in the list
*/


import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;

public class MainActivity2 extends Activity {

	ListView lv1;
	String[] arrayOfItems;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity2);
		
		lv1 = (ListView) findViewById(R.id.listView1);
		
		//Receive Parameter Value Sent by Previous Activity.
		String receiveValue = getIntent().getStringExtra("parameter1");
		
		//Covnert Value from String to Integer
		int n = Integer.parseInt(receiveValue);
		
		//Generated 1 CSV String Items values from 1 upto ParameterValue
		String generatedCsvString="";
		for(int i = 1;i<=n;i++)
		{
			generatedCsvString += "Item : " + i + ",";
		}
		
		
		//Convert Items into Array sothat lateron we can use it with adapter
		arrayOfItems = generatedCsvString.split(",");
		
		//Create Array Adapter and Pass ArrayOfValues to it.
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1,android.R.id.text1,arrayOfItems);
		
		//BindAdpater with our Actual ListView
		lv1.setAdapter(adapter);
		
		//Do something on click on ListView Click on Items
		lv1.setOnItemClickListener(new OnItemClickListener() {

			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,long arg3) {
				String msg = arrayOfItems[arg2];
				Toast.makeText(getApplicationContext(), msg, Toast.LENGTH_LONG).show();
			}
		});
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main_activity2, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}
}

 

Output 1

GTU Android Program 5

Output 2

GTU Android Program 5

Output 3

GTU Android Program 5

Download Few Popular Android Apps

  1. Bank IFSC Code : Now easily get bank ifsc code which you need while online transfer.
  2. All India PIN Code : Get all india PIN Code Numbers on 1 Android App.
  3. RTO Code : Get all India RTO Code on 1 Android App.
  4. Google 2 Step Code Speak : Now app will speak your google 2 step code, so no need to check mobile, code will reach you.
  5. All Gujarat ST Bus Stand Helpline Numbers (GSRTC Helpline Numbers)
  6. Mumbai City Bus Numbers (MSRTC Helpline Numbers)

  7. Andhra Pradesh City Bus Helpline Numbers (APSRTC Helpline Numbers)

  8. Bhavnagar Diary (Bhavnagar Business Directory)

  9. Baroda Diary (Baroda Business Directory)
  10. Surat Diary (Surat Business Directory)
  11. Ahmedabad Diary (Ahmedabad Business Directory)
  12. Rajkot Diary (Rajkot Business Directory)

1 thought on “GTU Android Program 5 Android List Example

  1. Pingback: Android Quiz 1 : Android Test : Test Your Android Knowledge - Adarsh Patel (MCA)

Comments are closed.