MOBILE APPLICATION DEVELOPMENT BASIC WIDGETS by Hiralal Falgun

MOBILE APPLICATION DEVELOPMENT BASIC WIDGETS by Hiralal Falgun

Author:Hiralal, Falgun [Hiralal, Falgun]
Language: eng
Format: epub, mobi, azw3
Publisher: UNKNOWN
Published: 2021-07-24T00:00:00+00:00


Example I – EditText in Android Studio

Below is the example of edit text in which we get the value from multiple edittexts and on button click event the Toast will show the data defined in Edittext. Step 1: Create a new project in Android Studio and name it EditTextExample. Step 2: Now Open res -> layout -> xml (or) activity_main.xml and add following code. In this code we have added multiple edittext and a button with onclick functionality.

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

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

xmlns:tools="http://schemas.android.com/tools"

android:id="@+id/activity_main"

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="com.example.edittextexample.MainActivity">

<EditText

android:id="@+id/editText1"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_alignParentTop="true"

android:layout_marginLeft="50dp"

android:layout_marginStart="50dp"

android:layout_marginTop="24dp"

android:ems="10"

android:hint="@string/name"

android:inputType="textPersonName"

android:selectAllOnFocus="true" />

<EditText

android:id="@+id/editText2"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/editText1"

android:layout_alignStart="@+id/editText1"

android:layout_below="@+id/editText1"

android:layout_marginTop="19dp"

android:ems="10"

android:hint="@string/password_0_9"

android:inputType="numberPassword" />

<EditText

android:id="@+id/editText3"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/editText2"

android:layout_alignStart="@+id/editText2"

android:layout_below="@+id/editText2" android:layout_marginTop="12dp"

android:ems="10"

android:hint="@string/e_mail"

android:inputType="textEmailAddress" />

<EditText

android:id="@+id/editText4"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/editText3"

android:layout_alignStart="@+id/editText3"

android:layout_below="@+id/editText3"

android:layout_marginTop="18dp"

android:ems="10"

android:hint="@string/date"

android:inputType="date" />

<EditText

android:id="@+id/editText5"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_alignLeft="@+id/editText4"

android:layout_alignStart="@+id/editText4"

android:layout_below="@+id/editText4"

android:layout_marginTop="18dp"

android:ems="10"

android:hint="@string/contact_number"

android:inputType="phone" />

<Button android:id="@+id/button"

style="@android:style/Widget.Button"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_alignParentLeft="true"

android:layout_alignParentStart="true"

android:layout_below="@+id/editText5"

android:layout_marginTop="62dp"

android:text="@string/submit"

android:textSize="16sp"

android:textStyle="normal|bold" />

</RelativeLayout> Step 3: Now open app -> java -> In this we just fetch the text from the edittext, further with the button click event a toast will show the text fetched before.

package com.example.edittextexample;

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.Toast;

public class MainActivity extends AppCompatActivity {

Button submit;

EditText name, password, email, contact, date;

package -> MainActivity.java and add the below code.

@Override

protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState);

setContentView(R.layout.activity_main);

name = (EditText) findViewById(R.id.editText1);

password = (EditText) findViewById(R.id.editText2);

email = (EditText) findViewById(R.id.editText3);

date = (EditText) findViewById(R.id.editText4);

contact = (EditText) findViewById(R.id.editText5);

submit = (Button) findViewById(R.id.button);

submit.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View v) {

if (name.getText().toString().isEmpty() || password.getText().toString().isEmpty() || email.getText().toString().isEmpty() || date.getText().toString().isEmpty()

|| contact.getText().toString().isEmpty()) {

Toast.makeText(getApplicationContext(), "Enter the Data", Toast.LENGTH_SHORT).show();

} else {

Toast.makeText(getApplicationContext(), "Name - " + name.getText().toString() + "
" + "Password - " + password.getText().toString()

+ "
" + "E-Mail - " + email.getText().toString() + "
" + "Date - " + date.getText().toString()

+ "
" + "Contact - " + contact.getText().toString(), Toast.LENGTH_SHORT).show();

}

}

});

}

}

Output: Now start the AVD in Emulator and run the App. You will see screen asking you to fill the data in required fields like name, password(numeric), email, date, contact number. Enter data and click on button. You will see the data entered will be displayed as Toast on screen.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.