Home » Android

A general Form design with specific fields in Android

In this article, we are going to learn how to create a general form with specific fields in Android?
Submitted by Manu Jemini, on December 10, 2017

Somehow we all end up creating a form this way or the other. This is a complete example of should you make a form in the layout file and give them different id’s and then in JAVA file declare variables and link them with the reference of the ids of layout objects.

Here we have the following type of WIDGETS:

  • EditText- This is used take the inputs from the user in the form of Text. There are different types EditText with different nature of their own. Like EditText of type, PASSWORD will not show the characters that you have typed. Another type which is very interesting and useful is EditText of type Email, now this type would restrict the user to type ‘@’. What good about EditText is that you can make your own checks and implement them on these wonderful widgets.
  • Radio Button - This is used take the inputs from the user in the form of true/false or on/off. When the Radio Button is selected it gives true and false otherwise.

Note: You can add as many fields as you need, this article is only for a referential purpose.

After doing everything up, your code should look like this. Connect an Android device or run an ADV and then build your project to see the result.

Java file:

package com.example.hp.demo;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

import android.widget.Button;
import android.widget.EditText;
import android.widget.RadioButton;

public class MainActivity extends AppCompatActivity {
EditText id,name,fname,address,email,password,dob;
    RadioButton male,female;
    Button submit;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

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

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

        fname = (EditText) findViewById(R.id.fname);

        address = (EditText) findViewById(R.id.address);

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

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

        dob = (EditText) findViewById(R.id.dob);

        male = (RadioButton) findViewById(R.id.male);

        female = (RadioButton) findViewById(R.id.female);

        submit = (Button) findViewById(R.id.Submit);
    }
}

XML file:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:weightSum="1">


    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/id"
        android:hint="Id" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/name"
        android:hint="Name" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPersonName"
        android:ems="10"
        android:id="@+id/fname"
        android:hint="Father&apos;s name" />

    <RadioGroup
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:orientation="horizontal">
        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Male"
            android:id="@+id/male"
            android:checked="false" />

        <RadioButton
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Female"
            android:id="@+id/female"
            android:checked="false" />
    </RadioGroup>

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/address"
        android:hint="Address" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textEmailAddress"
        android:ems="10"
        android:id="@+id/email"
        android:hint="Email" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPassword"
        android:ems="10"
        android:id="@+id/password"
        android:hint="Password" />

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="date"
        android:ems="10"
        android:id="@+id/dob"
        android:hint="Date of Birth" />

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/Submit"
        android:hint="Submit" />


</LinearLayout>

Manifest file:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="com.example.hp.demo">

    <application
        android:allowBackup="true"
        android:icon="@mipmap/ic_launcher"
        android:label="@string/app_name"
        android:supportsRtl="true"
        android:theme="@style/AppTheme">
        <activity android:name=".MainActivity">
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
    </application>

</manifest>

Output

form layout design in Android 1 Example

form layout design in Android 1 Example





Comments and Discussions

Ad: Are you a blogger? Join our Blogging forum.




Languages: » C » C++ » C++ STL » Java » Data Structure » C#.Net » Android » Kotlin » SQL
Web Technologies: » PHP » Python » JavaScript » CSS » Ajax » Node.js » Web programming/HTML
Solved programs: » C » C++ » DS » Java » C#
Aptitude que. & ans.: » C » C++ » Java » DBMS
Interview que. & ans.: » C » Embedded C » Java » SEO » HR
CS Subjects: » CS Basics » O.S. » Networks » DBMS » Embedded Systems » Cloud Computing
» Machine learning » CS Organizations » Linux » DOS
More: » Articles » Puzzles » News/Updates


© https://www.includehelp.com some rights reserved.