How to carry the values of the activity
Carrying valeues from one class to another calss:
write the following code on the main activity
package com.example.carryingvalues;
import java.util.ArrayList;
import com.example.codingonandroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Carryingvalues extends Activity implements OnClickListener {
Button navi;
EditText pass,username;
ArrayList<String> ad=new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass=(EditText)findViewById(R.id.editText1);
username=(EditText)findViewById(R.id.editText2);
navi=(Button)findViewById(R.id.button1);
navi.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String name=pass.getText().toString();
String user=username.getText().toString();
ad.add(user);
ad.add(name);
System.out.println("alist"+ad);
Intent i=new Intent(Carryingvalues.this,Second.class);
i.putStringArrayListExtra("cred", ad);
startActivity(i);
}
}
In Secon class write the following code:
package com.example.carryingvalues;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Second extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
// String ss = b.getString("ed_name");
ArrayList<String> ad=b.getStringArrayList("cred");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, ad));
}
}
main.xml should be the following way:
<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=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/username"
android:inputType="textPersonName" />
<requestFocus />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="42dp"
android:ems="10"
android:inputType="textPassword"
android:hint="@string/password" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText2"
android:layout_marginLeft="42dp"
android:layout_marginTop="22dp"
android:text="@string/button" />
</RelativeLayout>
output :
Carrying valeues from one class to another calss:
write the following code on the main activity
package com.example.carryingvalues;
import java.util.ArrayList;
import com.example.codingonandroid.R;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class Carryingvalues extends Activity implements OnClickListener {
Button navi;
EditText pass,username;
ArrayList<String> ad=new ArrayList<String>();
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
pass=(EditText)findViewById(R.id.editText1);
username=(EditText)findViewById(R.id.editText2);
navi=(Button)findViewById(R.id.button1);
navi.setOnClickListener(this);
}
public void onClick(View v) {
// TODO Auto-generated method stub
String name=pass.getText().toString();
String user=username.getText().toString();
ad.add(user);
ad.add(name);
System.out.println("alist"+ad);
Intent i=new Intent(Carryingvalues.this,Second.class);
i.putStringArrayListExtra("cred", ad);
startActivity(i);
}
}
In Secon class write the following code:
package com.example.carryingvalues;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
public class Second extends ListActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
Bundle b = getIntent().getExtras();
// String ss = b.getString("ed_name");
ArrayList<String> ad=b.getStringArrayList("cred");
setListAdapter(new ArrayAdapter<String>(this, android.R.layout.simple_expandable_list_item_1, ad));
}
}
main.xml should be the following way:
<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=".MainActivity" >
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:ems="10"
android:hint="@string/username"
android:inputType="textPersonName" />
<requestFocus />
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="@+id/editText1"
android:layout_below="@+id/editText1"
android:layout_marginTop="42dp"
android:ems="10"
android:inputType="textPassword"
android:hint="@string/password" >
</EditText>
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/editText2"
android:layout_marginLeft="42dp"
android:layout_marginTop="22dp"
android:text="@string/button" />
</RelativeLayout>
output :
when you press enter it shows :
No comments:
Post a Comment