Android Edit Text

Android Development

 Android Edittext :-



⇛ layout > .xml file :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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=".First"
android:orientation="vertical"
android:gravity="center"
android:background="@drawable/gradient"
android:padding="20dp">

<EditText

android:id="@+id/Idedittext1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="text"
android:textStyle="bold"
android:padding="10dp"
android:hint="Name :"
android:maxLines="1"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:textSize="20sp"/>

<EditText

android:id="@+id/Idedittext2"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:maxLines="1"
android:inputType="number"
android:padding="10dp"
android:textStyle="bold"
android:hint="Age :"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:textSize="20sp"/>

<EditText

android:id="@+id/Idedittext3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="text"
android:textStyle="bold"
android:padding="10dp"
android:maxLines="1"
android:hint="Village :"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:textSize="20sp"/>

<EditText

android:id="@+id/Idedittext4"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="text"
android:textStyle="bold"
android:padding="10dp"
android:maxLines="1"
android:hint="Country :"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:textSize="20sp"/>

<EditText

android:id="@+id/Idedittext5"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:inputType="date"
android:textStyle="bold"
android:hint="Born :"
android:padding="10dp"
android:maxLines="1"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:textSize="20sp"/>

<Button

android:id="@+id/Idbutton1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textSize="20sp"
android:text="Click Display in Textview"
android:textStyle="italic"
android:textColorHint="#ffff"
android:textColor="#cac7c7"
android:layout_marginTop="20dp"
android:background="@drawable/button"
android:padding="10dp"/>

<TextView

android:id="@+id/Idtextview3"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:textColor="#cac7c7"
android:textColorHint="#ffff"
android:textSize="20sp"
android:textStyle="bold"
android:textAlignment="center"
android:padding="10dp"/>

</LinearLayout>

⇛ drawable> button.xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape

xmlns:android="http://schemas.android.com/apk/res/android">

<stroke

android:width="18dp"

android:color="#350bef"/>

<corners android:radius="18dp"/>
<padding

android:right="2dp"

android:left="2dp"

android:top="2dp"

android:bottom="2dp"/>
<solid android:color="#350bef"/>
</shape>

⇛ drawable > gradient.xml file :

<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<gradient

android:startColor="#8bef08"

android:centerColor="#000"

android:endColor="#8bef08"

android:angle="90"/>
</shape>

⇛ java file :

package com.example.dell.edittext;

import android.support.v7.app.AppCompatActivity;

import android.os.Bundle;

import android.view.View;

import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
public class First extends AppCompatActivity {

private TextView txt1;

private EditText edt1, edt2, edt3, edt4, edt5;

private Button btn1;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_first);

edt1 = (EditText) findViewById(R.id.Idedittext1);

edt2 = (EditText) findViewById(R.id.Idedittext2);

edt3 = (EditText) findViewById(R.id.Idedittext3);

edt4 = (EditText) findViewById(R.id.Idedittext4);

edt5 = (EditText) findViewById(R.id.Idedittext5);

btn1 = (Button) findViewById(R.id.Idbutton1);

txt1 = (TextView) findViewById(R.id.Idtextview3);

/*When click on display button then the Name and Age will be displayed in Textview bellow button*/

btn1.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

/*Check if Name and Age field is empty or not (If empty then shown Tost else displayed in TextView)*/

if (edt1.getText().toString().isEmpty() || edt2.getText().toString().isEmpty()

|| edt3.getText().toString().isEmpty() || edt4.getText().toString().isEmpty()
|| edt5.getText().toString().isEmpty()){
Toast.makeText(getApplicationContext(),"Field can't be Blank !",Toast.LENGTH_SHORT).show();
}

/*Store Name and Age in String after click on button*/

else {

String name = edt1.getText().toString();
String age = edt2.getText().toString();
String village = edt3.getText().toString();
String country = edt4.getText().toString();
String born = edt5.getText().toString();

txt1.setText(name+"\n"+age+"\n"+village+"\n"+country+"\n"+born);/*Display the Name and age in TextView*/

}

}
});
}
}

No comments

Powered by Blogger.