GTU DE Sem6 – 3360704 – 5 – Create an 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 DE Sem6 – 3360704 – 5 – Create an 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 DE Sem6 - 3360704 - 5 - Create an 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

This Android program explain how we can move from one activity to anther activity along with few data. Generally every web application development platform provides different mechanism to pass data from one page to another like session, cookie, etc. Here in Android if you want to move to another Activity with few data you can refer following example.

Jump to Android Program 6

FileName: MainActivity.java

/*
 * 
Mobile Computing and Application Development : 3360704

GTU DE Sem6 - 3360704 - 5 - Create an 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
*/

package gtu.de.mcad.ex05;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
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 {

	EditText et1;
	Button b1;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		et1 = (EditText) findViewById(R.id.editText1);
		b1 = (Button) findViewById(R.id.button1);
		b1.setOnClickListener(new View.OnClickListener() {
			
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
			
				String s1 = et1.getText().toString();
				
				Intent i = new Intent(getApplicationContext(),MainActivity2.class);
				i.putExtra("par1", s1);
				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.
		
		return super.onOptionsItemSelected(item);
	}
}

 

FileName: activity_main.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Enter Number 1"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="number" >

        <requestFocus />
    </EditText>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="Submit" />

</LinearLayout>

 

FileName: MainActivity2.java

/*
 * 
Mobile Computing and Application Development : 3360704

DE Sem6 - 3360704 - 5 - Create an 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


*/

package gtu.de.mcad.ex05;


import java.util.ArrayList;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;

public class MainActivity2 extends Activity {

	private TextView tv1;
	private ListView lv1;
	private ArrayAdapter<String> adapter;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main_activity2);
		
		String s1 = this.getIntent().getStringExtra("par1");
		
		tv1 = (TextView) findViewById(R.id.textView1);
		lv1 = (ListView) findViewById(R.id.listView1);
		
		ArrayList<String> myarray = new ArrayList<String>();
		
		for(int i=1;i<= Integer.parseInt(s1);i++)
		{
			myarray.add("Item : " + i);
		}
		
		
		adapter = new ArrayAdapter<>(getApplicationContext(), android.R.layout.test_list_item,myarray);
		
		lv1.setAdapter(adapter);
	}

	@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.

		return super.onOptionsItemSelected(item);
	}
}

 

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.de.mcad.ex05.MainActivity2" >


    <ListView
        android:id="@+id/listView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true" >

    </ListView>

</RelativeLayout>

 

Output of MainActivity

GTU DE Sem6 - 3360704 - 5 - Create an 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

 

Output of Activity2

GTU DE Sem6 - 3360704 - 5 - Create an 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

Leave a Reply