How to insert the mobile number on an android application using EditText
Number Insertion in android application:
Sometimes while dealing with numbers, we don't want to insert any string inside an number so, we premiliary define an EditText so we externally specify an EditText attribute as android:inputType="number" After specifying this attribute in the EditText, it will be used only to insert a number.
Step 1: Create a brand new android project.
Step 2: Go to the XML file and select the design.
Step 3: Under the text option, find an option named as number.
Step 4: Drag and drop the number to your preview.
Step 5: After dragging the Number text, your XML part will look like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="@+id/layout">
<EditText
android:id="@+id/editText9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="102dp"
android:ems="10"
android:inputType="number" />
</RelativeLayout>
After step 5, your front end designing is completed.
Step 6: Insert the following code in you Main Activity file:
import android.widget.EditText;
Step 7: Insert the following code in your MainActivity class:
private EditText number;
Step 8: Inside the onCreate method of your MainActivity class, insert the following code:
number=findViewById(R.id.editText);
String num=number.getText().toString();
This will initialize the number instance.
Step 9: In the final step, you can use the String number to get the number inserted by the user.
Developer may check the number entered by the user against the number in his database via num String.
I would suggest you to take a button to insert the number from the user.
Insert the following code on your onCreate method inside the
MainActivity.java file:
if(number==9560146699){
Toast.makeText(MainActivity.this,"Welcome 9560146699",Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(MainActivity.this,"Not a valid user",Toast.LENGTH_SHORT).show();
}