To day we will explore some more form stuffs viz.CheckBox, RadioButton and Spinner.
RelativeLayout and Toast will also be introduced.
Form stuff become much important when you move towards enterprise development.In previous example simple widget textview buttons and edit text were explained but the complete setup of a form requares lots of widgets like progress bar rating bar switch etc.Gradually we will learn all stuff.
This tutorial website is full focused on enterprise development so we will be less concern about graphics like stuff.
So now you are familiar with project and class set up.
Just put this code in your xml file.
<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"
tools:context=".FormStuffs" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Complete The Details:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="16dp"
android:text="Nationality" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spnNationality"
android:layout_toRightOf="@+id/textView1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Female" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/radioGroup1"
android:text="I am" />
<Spinner
android:id="@+id/spnNationality"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" />
<CheckBox
android:id="@+id/cbTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup1"
android:layout_toRightOf="@+id/textView1"
android:text="Accept Terms and conditions" />
<Button
android:id="@+id/bSubmit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/cbTerms"
android:text="Submit" />
<TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/bSubmit"
android:text="" />
</RelativeLayout>
And in class file copy and paste this code-
package com.thenewtechnoworld.android;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class FormStuffs extends Activity {
Spinner nationality;
RadioButton iamMale;
CheckBox tnc;
Button submit;
String[] nat = { "Indian", "American", "Other" };
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// seting reference to the layout stuffs
setContentView(R.layout.activity_form_stuffs);
nationality = (Spinner) findViewById(R.id.spnNationality);
iamMale = (RadioButton) findViewById(R.id.radMale);
tnc = (CheckBox) findViewById(R.id.cbTerms);
submit = (Button) findViewById(R.id.bSubmit);
show = (TextView) findViewById(R.id.tvShow);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, nat);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
nationality.setAdapter(adapter);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String nation = nationality.getSelectedItem().toString();
String sex = iamMale.isChecked() ? "Male" : "Female";
// String accTC=tnc.isChecked()?"accept":"not accept";
if (tnc.isChecked()) {
show.setText("Nationality: " + nation + "\n" + "Sex: "
+ sex);
Toast.makeText(FormStuffs.this, "Successfully submited",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(FormStuffs.this,
"Form cannot be submitted.", Toast.LENGTH_LONG)
.show();
show.setText("Please Fill Mondatory Fields");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form_stuffs, menu);
return true;
}
}
Thank you for reading this post..
RelativeLayout and Toast will also be introduced.
Form stuff become much important when you move towards enterprise development.In previous example simple widget textview buttons and edit text were explained but the complete setup of a form requares lots of widgets like progress bar rating bar switch etc.Gradually we will learn all stuff.
This tutorial website is full focused on enterprise development so we will be less concern about graphics like stuff.
So now you are familiar with project and class set up.
Just put this code in your xml file.
<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"
tools:context=".FormStuffs" >
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Complete The Details:" />
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="@+id/textView2"
android:layout_marginTop="16dp"
android:text="Nationality" />
<RadioGroup
android:id="@+id/radioGroup1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/spnNationality"
android:layout_toRightOf="@+id/textView1"
android:orientation="horizontal" >
<RadioButton
android:id="@+id/radMale"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Male" />
<RadioButton
android:id="@+id/radio1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true"
android:text="Female" />
</RadioGroup>
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="@+id/radioGroup1"
android:text="I am" />
<Spinner
android:id="@+id/spnNationality"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/textView1" />
<CheckBox
android:id="@+id/cbTerms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/radioGroup1"
android:layout_toRightOf="@+id/textView1"
android:text="Accept Terms and conditions" />
<Button
android:id="@+id/bSubmit"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/cbTerms"
android:text="Submit" />
<TextView
android:id="@+id/tvShow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_below="@+id/bSubmit"
android:text="" />
</RelativeLayout>
And in class file copy and paste this code-
package com.thenewtechnoworld.android;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.RadioButton;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
public class FormStuffs extends Activity {
Spinner nationality;
RadioButton iamMale;
CheckBox tnc;
Button submit;
String[] nat = { "Indian", "American", "Other" };
TextView show;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// seting reference to the layout stuffs
setContentView(R.layout.activity_form_stuffs);
nationality = (Spinner) findViewById(R.id.spnNationality);
iamMale = (RadioButton) findViewById(R.id.radMale);
tnc = (CheckBox) findViewById(R.id.cbTerms);
submit = (Button) findViewById(R.id.bSubmit);
show = (TextView) findViewById(R.id.tvShow);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_spinner_item, nat);
adapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
nationality.setAdapter(adapter);
submit.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
String nation = nationality.getSelectedItem().toString();
String sex = iamMale.isChecked() ? "Male" : "Female";
// String accTC=tnc.isChecked()?"accept":"not accept";
if (tnc.isChecked()) {
show.setText("Nationality: " + nation + "\n" + "Sex: "
+ sex);
Toast.makeText(FormStuffs.this, "Successfully submited",
Toast.LENGTH_LONG).show();
} else {
Toast.makeText(FormStuffs.this,
"Form cannot be submitted.", Toast.LENGTH_LONG)
.show();
show.setText("Please Fill Mondatory Fields");
}
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_form_stuffs, menu);
return true;
}
}
When you compile and run on ADV the output will look like--
You can see the toast appearing at the end of the emulator.
Again you can download complete source code here.But you will have to be logged in to facebook.
Thank you for reading this post..
0 comments:
Post a Comment