Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Subject : Android app development 9. A publishing house provides content through

ID: 3742089 • Letter: S

Question

Subject : Android app development

9. A publishing house provides content through a mobile app. App provides users with registration ail address, country, publication Users need to enter their First name, Last name, gender, e-m types namely Arts, Sports, Science, Political. Provide atleast 6 countries. Users can select multiple publication types. Also provide two buttons namely Register and Cancel. On clicking Register button display all user's entry and selection. On clicking Cancel button, all entries should be blank. Write the XML and Java code.

Explanation / Answer

activity_register.xml :

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

<LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"

android:layout_width="fill_parent"

android:layout_height="fill_parent"

android:gravity="center"

android:orientation="vertical"

android:padding="10dp" >

<LinearLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_gravity="center"

android:orientation="vertical"

android:paddingLeft="20dp"

android:paddingRight="20dp" >

<android.support.design.widget.TextInputLayout

android:id="@+id/signup_input_layout_name"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp">

<EditText

android:id="@+id/signup_input_firstName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="Name"/>

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout

android:id="@+id/signup_input_layout_email"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp">

<EditText

android:id="@+id/signup_input_email"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:inputType="textEmailAddress"

android:hint="Email" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout

android:id="@+id/signup_input_layout_lastName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp">

<EditText

android:id="@+id/signup_input_lastName"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:inputType="textlastName"

android:hint="@string/hint_lastName" />

</android.support.design.widget.TextInputLayout>

<android.support.design.widget.TextInputLayout

android:id="@+id/signup_input_layout_country"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:layout_centerHorizontal="true"

android:layout_marginTop="10dp">

<EditText

android:id="@+id/signup_input_country"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:ems="10"

android:hint="@string/hint_country"/>

</android.support.design.widget.TextInputLayout>

<RelativeLayout

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:paddingTop="20dp">

<TextView

android:id="@+id/gender_textview"

android:paddingRight="15dp"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/hint_gender"

android:fontFeatureSettings="@string/hint_lastName"

android:textSize="20dp"

android:fontFamily="@string/hint_lastName"/>

<RadioGroup

android:id="@+id/gender_radio_group"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:layout_toRightOf="@+id/gender_textview"

android:orientation="horizontal">

<RadioButton

android:id="@+id/male_radio_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text="@string/male"

android:checked="true"

/>

<RadioButton

android:id="@+id/female_radio_btn"

android:layout_width="wrap_content"

android:layout_height="wrap_content"

android:text = "@string/female"

/>

</RadioGroup>

</RelativeLayout>

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/btn_sign_up"

android:background="@color/login_button_background"

android:layout_marginTop="40dp"

android:textColor="@android:color/white"/>

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

android:layout_width="fill_parent"

android:layout_height="wrap_content"

android:text="@string/btn_link_login"

android:background="@null"

android:layout_marginTop="3dp"

android:textColor="@android:color/black"/>

<Spinner

android:id="@+id/spinner1"

android:layout_width="match_parent"

android:layout_height="wrap_content"

android:background="@android:drawable/btn_dropdown"

android:spinnerMode="dropdown"/>

</LinearLayout>

</LinearLayout>

**********************************************************************************************************

RegisterActivity.java:

packcountry com.androidtutorialpoint.androidlogin;

import android.app.ProgressDialog;

import android.content.Intent;

import android.os.Bundle;

import android.support.v7.app.AppCompatActivity;

import android.util.Log;

import android.view.View;

import android.widget.Button;

import android.widget.EditText;

import android.widget.RadioGroup;

import android.widget.Toast;

import com.android.volley.Request;

import com.android.volley.Response;

import com.android.volley.VolleyError;

import com.android.volley.toolbox.StringRequest;

import org.json.JSONException;

import org.json.JSONObject;

import java.util.HashMap;

import java.util.Map;

public class RegisterActivity extends AppCompatActivity {

private static final String TAG = "RegisterActivity";

private static final String URL_FOR_REGISTRATION = "https://XXX.XXX.X.XX/android_login_example/register.php";

ProgressDialog progressDialog;

private EditText signupInputfirstName, signupInputlastName, signupInputEmail, signupInputcountry;

private Button btnSignUp;

private Button btnLinkLogin;

private RadioGroup genderRadioGroup;

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.activity_register);

// Progress dialog

progressDialog = new ProgressDialog(this);

progressDialog.setCancelable(false);

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

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

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

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

btnSignUp = (Button) findViewById(R.id.btn_signup);

btnLinkLogin = (Button) findViewById(R.id.btn_link_login);

genderRadioGroup = (RadioGroup) findViewById(R.id.gender_radio_group);

btnSignUp.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

submitForm();

}

});

btnLinkLogin.setOnClickListener(new View.OnClickListener() {

@Override

public void onClick(View view) {

Intent i = new Intent(getApplicationContext(),LoginActivity.class);

startActivity(i);

}

});

}

private void submitForm() {

int selectedId = genderRadioGroup.getCheckedRadioButtonId();

String gender;

if(selectedId == R.id.female_radio_btn)

gender = "Female";

else

gender = "Male";

registerUser(signupInputfirstName.getText().toString(),

signupInputEmail.getText().toString(),

signupInputlastName.getText().toString(),

gender,

signupInputcountry.getText().toString());

}

private void registerUser(final String firstName, final String email, final String lastName,

final String gender, final String country) {

// Tag used to cancel the request

String cancel_req_tag = "register";

progressDialog.setMesscountry("Adding you ...");

showDialog();

StringRequest strReq = new StringRequest(Request.Method.POST,

URL_FOR_REGISTRATION, new Response.Listener<String>() {

@Override

public void onResponse(String response) {

Log.d(TAG, "Register Response: " + response.toString());

hideDialog();

try {

JSONObject jObj = new JSONObject(response);

boolean error = jObj.getBoolean("error");

if (!error) {

String user = jObj.getJSONObject("user").getString("firstfirstName");

Toast.makeText(getApplicationContext(), "Hi " + user +", You are successfully Added!", Toast.LENGTH_SHORT).show();

// Launch login activity

Intent intent = new Intent(

RegisterActivity.this,

LoginActivity.class);

startActivity(intent);

finish();

} else {

String errorMsg = jObj.getString("error_msg");

Toast.makeText(getApplicationContext(),

errorMsg, Toast.LENGTH_LONG).show();

}

} catch (JSONException e) {

e.printStackTrace();

}

}

}, new Response.ErrorListener() {

@Override

public void onErrorResponse(VolleyError error) {

Log.e(TAG, "Registration Error: " + error.getMesscountry());

Toast.makeText(getApplicationContext(),

error.getMesscountry(), Toast.LENGTH_LONG).show();

hideDialog();

}

}) {

@Override

protected Map<String, String> getParams() {

// Posting params to register url

Map<String, String> params = new HashMap<String, String>();

params.put("firstName", firstName);

params.put("lastName", lastName);

params.put("email", email);   

params.put("gender", gender);

params.put("country", country);

Spinner dropdown = findViewById(R.id.spinner1);

String[] items = new String[]{"Arts", "Sports", "Science","Political"};

ArrayAdapter<String> adapter = new ArrayAdapter<>(this, android.R.layout.simple_spinner_dropdown_item, items);

//set the spinners adapter to the previously created one.

dropdown.setAdapter(adapter);

return params;

}

};

// Adding request to request queue

AppSingleton.getInstance(getApplicationContext()).addToRequestQueue(strReq, cancel_req_tag);

}

private void showDialog() {

if (!progressDialog.isShowing())

progressDialog.show();

}

private void hideDialog() {

if (progressDialog.isShowing())

progressDialog.dismiss();

}

}