In This Tutorial We Will See How To Check Whether Edittext Is Empty Or Not.
Create New Android Application Project And name It as EditTextEmpty.
Now Go To Your Xml Layout activity_main.xml file.
In Your Graphical Layout Add One EditText, You may Add Plain Text to check Empty Text.
Now Add One Button To Check Edittext Is Empty Or Not On its Click Event.
Your activity_main.xml will Now Look Like Below.
Your EditText's ID Is editText1.
So We Can Check EditText is Empty Or Not With Following Code.
(input.getText().toString().equals(""))
We have Given Two Condition Empty And Not Empty Using If Else Condition And The Message Is Shown Using Toast.
MainActivity.java file is As Below . Replace With Your MainActivity.java file.
MainActivity.Java
Create New Android Application Project And name It as EditTextEmpty.
Now Go To Your Xml Layout activity_main.xml file.
In Your Graphical Layout Add One EditText, You may Add Plain Text to check Empty Text.
Now Add One Button To Check Edittext Is Empty Or Not On its Click Event.
Your activity_main.xml will Now Look Like Below.
<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_marginTop="22dp"
android:ems="10" >
<requestFocus />
</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/editText1"
android:layout_marginTop="48dp"
android:text="Check" />
</RelativeLayout>
Your EditText's ID Is editText1.
So We Can Check EditText is Empty Or Not With Following Code.
(input.getText().toString().equals(""))
We have Given Two Condition Empty And Not Empty Using If Else Condition And The Message Is Shown Using Toast.
MainActivity.java file is As Below . Replace With Your MainActivity.java file.
MainActivity.Java
package
arshad.edittextempty;
import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import android.os.Bundle;
import
android.app.Activity;
import android.graphics.Color;
import android.view.Menu;
import android.view.View;
import
android.view.View.OnClickListener;
import
android.widget.Button;
import
android.widget.EditText;
import
android.widget.Toast;
public class MainActivity extends Activity {
EditText
input;
Button
check;
@Override
protected void onCreate(Bundle
savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
input = (EditText)
findViewById(R.id.editText1);
check = (Button) findViewById(R.id.button1);
check.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
if(input.getText().toString().equals(""))
{
Toast.makeText(MainActivity.this, "Input Text Is
Empty.. Please Enter Some Text", Toast.LENGTH_SHORT).show();
}
else
{
Toast.makeText(MainActivity.this, "Input Text is
Not Empty", Toast.LENGTH_SHORT).show();
}
}
});
}
@Override
public boolean
onCreateOptionsMenu(Menu menu) {
// Inflate the
menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
hi
ReplyDelete