Here view validation for few edit text field all field validation and particular field.
main.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"
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" >
<TextView
android:id="@+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="Name" />
<TextView
android:id="@+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/textView1"
android:layout_marginTop="41dp"
android:text="FirstName" />
<TextView
android:id="@+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView2"
android:layout_below="@+id/textView2"
android:layout_marginTop="62dp"
android:text="Email" />
<TextView
android:id="@+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_centerVertical="true"
android:layout_marginLeft="14dp"
android:text="Phone" />
<TextView
android:id="@+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView3"
android:layout_below="@+id/textView4"
android:layout_marginTop="44dp"
android:text="Address" />
<EditText
android:id="@+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView1"
android:ems="10" >
<requestFocus />
</EditText>
<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText1"
android:layout_below="@+id/textView2"
android:ems="10"
android:inputType="textPersonName" />
<EditText
android:id="@+id/editText4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/editText3"
android:layout_below="@+id/textView4"
android:ems="10"
android:inputType="phone" />
<Button
android:id="@+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/editText5"
android:layout_marginTop="20dp"
android:text="Ok" />
<EditText
android:id="@+id/editText3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/textView4"
android:layout_below="@+id/textView3"
android:ems="10"
android:inputType="textEmailAddress" />
<EditText
android:id="@+id/editText5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="@+id/button1"
android:layout_below="@+id/textView5"
android:layout_marginTop="22dp"
android:ems="10"
android:inputType="textPostalAddress" />
</RelativeLayout>
Mainactivity.java
public class MainActivity extends Activity implements OnClickListener {
TextView t1, t2, t3, t4, t5;
EditText e1, e2, e3, e4, e5;
Button b;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t1 = (TextView) findViewById(R.id.textView1);
t2 = (TextView) findViewById(R.id.textView2);
t3 = (TextView) findViewById(R.id.textView3);
t4 = (TextView) findViewById(R.id.textView4);
t5 = (TextView) findViewById(R.id.textView5);
e1 = (EditText) findViewById(R.id.editText1);
e2 = (EditText) findViewById(R.id.editText2);
e3 = (EditText) findViewById(R.id.editText3);
e4 = (EditText) findViewById(R.id.editText4);
e5 = (EditText) findViewById(R.id.editText5);
b = (Button) findViewById(R.id.button1);
b.setOnClickListener(this);
}
@Override
public void onClick(View v) {
if (v == b) {
if (e1.getText().toString().equals("")
&& e2.getText().toString().equals("")
&& e3.getText().toString().equals("")
&& e4.getText().toString().equals("")
&& e5.getText().toString().equals("")) {
e1.setError("Fill It");
// e1.requestFocus();
e2.setError("Fill It");
e3.setError("Fill It");
e4.setError("Fill It");
e5.setError("Fill It");
Toast.makeText(getApplicationContext(), "name field empty",
Toast.LENGTH_SHORT).show();
} else if (e1.getText().toString().equalsIgnoreCase("") ) {
e1.setError("Fill It");
} else if (e2.getText().toString().equalsIgnoreCase("")) {
e2.setError("Fill It");
} else if (e3.getText().toString().equalsIgnoreCase("")) {
e3.setError("Fill It");
} else if (e4.getText().toString().equalsIgnoreCase("") ) {
e4.setError("Fill It");
} else if (e5.getText().toString().equalsIgnoreCase("")) {
e5.setError("Fill It");
}
}
}
OUTPUT:-
ENJOY CODE AND SHARE ON ARTICLE....
No comments :
Post a Comment