- Home
- >> Nerd Digest
- >> Android
Your account has been flagged due to frequent spamming, you are not permitted to post comments. Contact admin@findnerd.com.
-
How to create Registration form in android ?
about 9 years ago
Here I have created a registration form in android. In my registration form i have used validation function to check inputs like email ,name,age etc. And I have also used check box , radio button and spinner function. Therefor below example will clearly described you to make Registration form.
Step(1)- create main.xml layout file. For (Inputs data)<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#9afff2"> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/NameLayout"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Name" android:id="@+id/textView" android:layout_alignParentTop="true" android:layout_alignParentStart="true"/> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textPersonName" android:layout_toRightOf="@+id/textView" android:id="@+id/name" android:layout_alignTop="@+id/textView" android:layout_centerHorizontal="true" android:hint="Enter your name.." /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/Email" android:layout_below="@+id/NameLayout"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Email" android:id="@+id/textView2" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="textEmailAddress" android:id="@+id/email" android:layout_toRightOf="@+id/textView2" android:layout_toEndOf="@+id/textView2" android:hint="enter your email address" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/AgeLayout" android:layout_below="@+id/Email"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Age" android:id="@+id/textView3" android:layout_alignParentStart="true" /> <EditText android:layout_width="wrap_content" android:layout_height="wrap_content" android:inputType="number" android:ems="10" android:id="@+id/age" android:layout_toRightOf="@+id/textView3" android:hint="Enter your age.." /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/GenderLayout" android:layout_below="@+id/AgeLayout"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Gender" android:id="@+id/textView4" android:layout_alignParentStart="true" /> <RadioGroup android:id="@+id/radioSex" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_toRightOf="@+id/textView4"> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Male" android:checked="true" android:id="@+id/male" /> <RadioButton android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Female" android:id="@+id/female"/> </RadioGroup> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/BranchLayout" android:layout_below="@+id/GenderLayout"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Branch" android:id="@+id/textView5" android:layout_alignParentStart="true" /> <Spinner android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="@+id/branch" android:layout_toRightOf="@+id/textView5" android:layout_marginStart="56dp" /> </RelativeLayout> <RelativeLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/SubmitLayout" android:layout_below="@+id/BranchLayout"> <Button android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Submit" android:id="@+id/send" android:layout_centerHorizontal="true" android:layout_marginTop="54dp" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Hindi" android:id="@+id/hindi" android:checked="false" android:layout_alignParentTop="true" android:layout_alignStart="@+id/send" /> <CheckBox android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="English" android:id="@+id/english" android:layout_alignBottom="@+id/hindi" android:layout_toEndOf="@+id/hindi" android:checked="false" /> </RelativeLayout> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Language" android:id="@+id/textView7" android:layout_below="@+id/BranchLayout" /> </RelativeLayout>
Step(2)- create main2.xml layout file. For(Displaying result)
<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:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity" android:background="#3e82ff"> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Name" android:id="@+id/textView6" android:layout_alignParentTop="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/name" android:layout_alignBottom="@+id/textView6" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:id="@+id/textView8" android:layout_marginTop="50dp" android:layout_below="@+id/textView6" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Email" android:id="@+id/textView9" android:layout_alignTop="@+id/textView8" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/email" android:layout_alignBottom="@+id/textView9" android:layout_alignStart="@+id/name" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Age" android:id="@+id/textView11" android:layout_marginTop="47dp" android:layout_below="@+id/textView8" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/age" android:layout_alignBottom="@+id/textView11" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Gender" android:id="@+id/textView13" android:layout_centerVertical="true" android:layout_alignParentStart="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Branch" android:id="@+id/textView15" android:layout_marginTop="61dp" android:layout_below="@+id/textView13" android:layout_alignEnd="@+id/textView13" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/gender" android:layout_alignTop="@+id/textView13" android:layout_alignStart="@+id/age" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/branch" android:layout_alignBottom="@+id/textView15" android:layout_centerHorizontal="true" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:textAppearance="?android:attr/textAppearanceLarge" android:text="Language" android:id="@+id/textView10" android:layout_below="@+id/branch" android:layout_alignParentStart="true" android:layout_marginTop="39dp" /> <TextView android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="New Text" android:id="@+id/language" android:layout_alignBottom="@+id/textView10" android:layout_alignStart="@+id/branch" /> </RelativeLayout>
Step(3)-MainActivity code.
package com.tiwari.rajshekhar.sampleretry; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.Button; import android.widget.CheckBox; import android.widget.EditText; import android.widget.RadioButton; import android.widget.RadioGroup; import android.widget.Spinner; import java.util.IllegalFormatCodePointException; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends Activity { EditText Name; EditText Email; EditText Age; RadioGroup radioSex; Spinner branch; ArrayAdapter<CharSequence>adapter; CheckBox hindi,english; Button Submit; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); //View form Button Button button =(Button)findViewById(R.id.send); button.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { if (Name.getText().toString().trim().isEmpty()){ Name.setError("Please enter your name"); Name.requestFocus(); } else if (!isValideEmail(Email.getText().toString())) { Email.setError("Please enter your Email"); Email.requestFocus(); } else if (Age.getText().toString().trim().isEmpty()){ Age.setError("Please enter your"); Age.requestFocus(); } else { sendMessage(); } } }); Name=(EditText)findViewById(R.id.name); Email=(EditText)findViewById(R.id.email); Age=(EditText)findViewById(R.id.age); radioSex=(RadioGroup)findViewById(R.id.radioSex); branch=(Spinner)findViewById(R.id.branch); adapter =ArrayAdapter.createFromResource(this,R.array.branch,android.R.layout.simple_spinner_item); adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item); hindi=(CheckBox)findViewById(R.id.hindi); english=(CheckBox)findViewById(R.id.english); branch.setAdapter(adapter); branch.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() { @Override public void onItemSelected(AdapterView<?> parent, View view, int position, long id) { } @Override public void onNothingSelected(AdapterView<?> parent) { } }); } private boolean isValideEmail(String Email){ String Email_PATTERN = "^[_A-Za-z0-9-\\+]+(\\.[_A-Za-z0-9-]+)*@" + "[A-Za-z0-9-]+(\\.[A-Za-z0-9]+)*(\\.[A-Za-z]{2,})$"; Pattern pattern = Pattern.compile(Email_PATTERN); Matcher matcher = pattern.matcher(Email); return matcher.matches(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } public void sendMessage(){ Intent intent =new Intent(MainActivity.this,MainActivity.class); intent.putExtra("name",Name.getText().toString()); intent.putExtra("email",Email.getText().toString()); intent.putExtra("age",Age.getText().toString()); //get selected radio button from radioGroup int selectedId =radioSex.getCheckedRadioButtonId(); //find the radiobutton by retunend id RadioButton radioSexButton = (RadioButton)findViewById(selectedId); intent.putExtra("radioSex", radioSexButton.getText()); intent.putExtra("branch",branch.getSelectedItem().toString()); if(hindi.isChecked()&&english.isChecked()) { intent.putExtra("language","Hindi,English"); } else if(hindi.isChecked()) { intent.putExtra("language","Hindi"); } else if(english.isChecked()) { intent.putExtra("language","English"); } else { intent.putExtra("language","None"); } startActivity(intent); } }
Step(4)-Main2Activity code.(Second activity)
package com.tiwari.rajshekhar.sampleretry; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuItem; import android.widget.TextView; import org.w3c.dom.Text; public class Main2Activity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main2); Intent intent=getIntent(); TextView name =(TextView)findViewById(R.id.name); name.setText(intent.getStringExtra("name")); TextView email =(TextView)findViewById(R.id.email); email.setText(intent.getStringExtra("email")); TextView age =(TextView)findViewById(R.id.age); age.setText(intent.getStringExtra("age")); TextView gender=(TextView)findViewById(R.id.gender); gender.setText(intent.getStringExtra("radioSex")); TextView branch=(TextView)findViewById(R.id.branch); branch.setText(intent.getStringExtra("branch")); TextView language=(TextView)findViewById(R.id.language); language.setText(intent.getStringExtra("language")); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main2, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }/ Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_main2, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
Step(5)Add item in string.xml(resoureces).For adding radio button and spinner.
<resources> <string name="app_name">sampleRetry</string> <string name="hello_world">Hello world!</string> <string name="action_settings">Settings</string> <string name="Male">male</string> <string name="Female">female</string> <string name="title_activity_main2">Main2Activity</string> <string name="select_branch">Select Trade</string> <string-array name="branch"> <item>CS</item> <item>EC</item> <item>IT</item> <item>EE</item> </string-array> </resources>
Comment on it
Insert an Image
To select an image, click on it.
Image path:
Example : https://wmd-editor.com/images/cloud1.jpg
0 Comment(s)