Android Program : Create an Android App, it reads the Students Details (Name, Surname, Class, Gender, Hobbies, Marks) and Display the all information in another activity in table format on click of Submit button
strings.xml
Open res/values/string.xml and replace it with following content.
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
<
resources
>
<
string
name
=
"hello"
>Form</
string
>
<
string
name
=
"display"
>Display</
string
>
<
string
name
=
"app_name"
>ActivityDemo</
string
>
<
string
name
=
"name"
>Name</
string
>
<
string
name
=
"age"
>Age</
string
>
<
string
name
=
"female"
>Female</
string
>
<
string
name
=
"male"
>Male</
string
>
<
string
name
=
"gender"
>Gender</
string
>
<
string
name
=
"submit"
>Submit</
string
>
</
resources
>
form.xml
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
>
<
TextView
android:id
=
"@+id/nameText"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentLeft
=
"true"
android:layout_alignParentTop
=
"true"
android:layout_marginLeft
=
"18dp"
android:layout_marginTop
=
"24dp"
android:text
=
"@string/name"
/>
<
EditText
android:id
=
"@+id/name"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentRight
=
"true"
android:layout_alignTop
=
"@+id/nameText"
android:ems
=
"10"
android:inputType
=
"text"
/>
<
TextView
android:id
=
"@+id/ageText"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/nameText"
android:layout_below
=
"@+id/name"
android:layout_marginTop
=
"26dp"
android:text
=
"@string/age"
/>
<
EditText
android:id
=
"@+id/age"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignParentRight
=
"true"
android:layout_alignTop
=
"@+id/ageText"
android:ems
=
"10"
android:inputType
=
"text"
/>
<
TextView
android:id
=
"@+id/genderText"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/gridLayout1"
android:layout_below
=
"@+id/age"
android:layout_marginTop
=
"19dp"
android:text
=
"@string/gender"
/>
<
GridLayout
android:id
=
"@+id/gridLayout1"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignLeft
=
"@+id/ageText"
android:layout_below
=
"@+id/age"
android:layout_marginTop
=
"44dp"
>
<
RadioGroup
android:id
=
"@+id/gender"
android:layout_width
=
"fill_parent"
android:layout_height
=
"wrap_content"
android:orientation
=
"horizontal"
>
<
RadioButton
android:id
=
"@+id/femaleRadio"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:checked
=
"true"
android:text
=
"@string/female"
/>
<
RadioButton
android:id
=
"@+id/maleRadio"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:checked
=
"true"
android:text
=
"@string/male"
/>
</
RadioGroup
>
</
GridLayout
>
<
Button
android:id
=
"@+id/submit"
android:layout_width
=
"wrap_content"
android:layout_height
=
"wrap_content"
android:layout_alignRight
=
"@+id/gridLayout1"
android:layout_below
=
"@+id/gridLayout1"
android:layout_marginTop
=
"21dp"
android:text
=
"@string/submit"
/>
</
RelativeLayout
>
<?
xml
version
=
"1.0"
encoding
=
"utf-8"
?>
android:layout_width
=
"fill_parent"
android:layout_height
=
"fill_parent"
android:columnCount
=
"3"
>
<
TextView
android:id
=
"@+id/nameText"
android:layout_column
=
"1"
android:layout_gravity
=
"left"
android:layout_row
=
"1"
android:text
=
"@string/name"
/>
<
TextView
android:id
=
"@+id/nameValue"
android:layout_column
=
"2"
android:layout_gravity
=
"left"
android:layout_row
=
"1"
android:text
=
""
android:textAppearance
=
"?android:attr/textAppearanceMedium"
/>
<
TextView
android:id
=
"@+id/ageText"
android:layout_column
=
"1"
android:layout_gravity
=
"left"
android:layout_row
=
"3"
android:text
=
"@string/age"
/>
<
TextView
android:id
=
"@+id/ageValue"
android:layout_column
=
"2"
android:layout_gravity
=
"left"
android:layout_row
=
"3"
android:text
=
""
android:textAppearance
=
"?android:attr/textAppearanceMedium"
/>
<
TextView
android:id
=
"@+id/genderText"
android:layout_column
=
"1"
android:layout_gravity
=
"left"
android:layout_row
=
"5"
android:text
=
"@string/gender"
/>
<
TextView
android:id
=
"@+id/genderValue"
android:layout_column
=
"2"
android:layout_gravity
=
"left"
android:layout_row
=
"5"
android:text
=
""
android:textAppearance
=
"?android:attr/textAppearanceMedium"
/>
<
Space
android:layout_width
=
"21dp"
android:layout_height
=
"1dp"
android:layout_column
=
"0"
android:layout_gravity
=
"fill_horizontal"
android:layout_row
=
"0"
/>
<
Space
android:layout_width
=
"1dp"
android:layout_height
=
"21dp"
android:layout_column
=
"0"
android:layout_gravity
=
"fill_horizontal"
android:layout_row
=
"0"
/>
<
Space
android:layout_width
=
"64dp"
android:layout_height
=
"1dp"
android:layout_column
=
"1"
android:layout_gravity
=
"fill_horizontal"
android:layout_row
=
"0"
/>
<
Space
android:layout_width
=
"1dp"
android:layout_height
=
"10dp"
android:layout_column
=
"0"
android:layout_gravity
=
"fill_horizontal"
android:layout_row
=
"2"
/>
<
Space
android:layout_width
=
"1dp"
android:layout_height
=
"10dp"
android:layout_column
=
"0"
android:layout_gravity
=
"fill_horizontal"
android:layout_row
=
"4"
/>
</
GridLayout
>
FormActivity.java
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;
import
android.widget.RadioButton;
import
android.widget.RadioGroup;
public
class
FormActivity
extends
Activity
implements
OnClickListener {
Button button;
RadioGroup genderRadioGroup;
EditText name;
EditText age;
/** Called when the activity is first created. */
@Override
public
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.form);
//Get the ids of view objects
findAllViewsId();
button.setOnClickListener(
this
);
}
private
void
findAllViewsId() {
button = (Button) findViewById(R.id.submit);
name = (EditText) findViewById(R.id.name);
age = (EditText) findViewById(R.id.age);
genderRadioGroup = (RadioGroup) findViewById(R.id.gender);
}
@Override
public
void
onClick(View v) {
Intent intent =
new
Intent(getApplicationContext(), DisplayActivity.
class
);
//Create a bundle object
Bundle b =
new
Bundle();
//Inserts a String value into the mapping of this Bundle
b.putString(
"name"
, name.getText().toString());
b.putString(
"age"
, age.getText().toString());
int
id = genderRadioGroup.getCheckedRadioButtonId();
RadioButton radioButton = (RadioButton) findViewById(id);
b.putString(
"gender"
, radioButton.getText().toString());
//Add the bundle to the intent.
intent.putExtras(b);
//start the DisplayActivity
startActivity(intent);
}
}
DisplayActivity.java
import
android.app.Activity;
import
android.os.Bundle;
import
android.widget.TextView;
public
class
DisplayActivity
extends
Activity {
@Override
protected
void
onCreate(Bundle savedInstanceState) {
super
.onCreate(savedInstanceState);
setContentView(R.layout.display);
Bundle b = getIntent().getExtras();
TextView name = (TextView) findViewById(R.id.nameValue);
TextView age = (TextView) findViewById(R.id.ageValue);
TextView gender = (TextView) findViewById(R.id.genderValue);
name.setText(b.getCharSequence(
"name"
));
age.setText(b.getCharSequence(
"age"
));
gender.setText(b.getCharSequence(
"gender"
));
}
}
0 Comments