To Show Error,When Text is not entered in the Edit Text means,
EditTextError.java
package com.android.EditText;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextError extends Activity {
/** Called when the activity is first created. */
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(R.id.editText1);
final Button click = (Button) findViewById(R.id.button1);
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (edit.getText().length() == 0) {
edit.setError("Please enter the text");
}
}
});
}
}
The Layout Design : res/layout/main.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp">
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_width="fill_parent">
</EditText>
<Button
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="Test it!!"
android:layout_width="wrap_content"
android:onClick="btnClick">
</Button>
</LinearLayout>
The Output Shows like this:
If We click the button without Text,then this error will display:
EditTextError.java
package com.android.EditText;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class EditTextError extends Activity {
/** Called when the activity is first created. */
EditText edit;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
edit = (EditText) findViewById(R.id.editText1);
final Button click = (Button) findViewById(R.id.button1);
click.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (edit.getText().length() == 0) {
edit.setError("Please enter the text");
}
}
});
}
}
The Layout Design : res/layout/main.xml,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="@+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_margin="5dp">
<EditText
android:layout_height="wrap_content"
android:id="@+id/editText1"
android:layout_width="fill_parent">
</EditText>
<Button
android:layout_height="wrap_content"
android:id="@+id/button1"
android:text="Test it!!"
android:layout_width="wrap_content"
android:onClick="btnClick">
</Button>
</LinearLayout>
The Output Shows like this:
If We click the button without Text,then this error will display:


No comments:
Post a Comment