signup using retrofit and handle radiobutton & spinner

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP



signup using retrofit and handle radiobutton & spinner



How to handle RadioButton and Spinner in user registration using retrofit.


RadioButton


Spinner



RadioButton is user type (professional, Visitor) and Spinner is list having user type category how to pick data from both using retrofit for user registration .... need help


RadioButton


Spinner



Design Screen
Design Screen


import android.content.Intent;
import android.os.Build;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Patterns;
import android.view.View;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.Spinner;
import android.widget.Toast;

import com.example.saadhashmi.docdirect.Interface.RetrofitClient;
import com.example.saadhashmi.docdirect.R;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;


public class Signup_Activity extends AppCompatActivity

private Spinner spinner;
private EditText edit_username , edit_email , edit_firstname ,
edit_lastname , edit_Phone , edit_password , edit_retypePassword;
private RadioGroup radiogroup;
private RadioButton professional_btn , visitor_btn;

@Override
protected void onCreate(Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_signup_);
Window window = getWindow();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M)
window.setStatusBarColor(getColor(R.color.login_statusbar_color));

else
window.setStatusBarColor(getResources().getColor(R.color.login_statusbar_color));


spinner = (Spinner) findViewById(R.id.ui_user_type);
edit_username = (EditText) findViewById(R.id.ui_username_signup);
edit_email = (EditText) findViewById(R.id.ui_email_signup);
edit_firstname = (EditText) findViewById(R.id.ui_firstname_signup);
edit_lastname = (EditText) findViewById(R.id.ui_lastname_signup);
edit_password = (EditText) findViewById(R.id.ui_password_signup);
edit_Phone = (EditText) findViewById(R.id.ui_phone_signup);
edit_retypePassword = (EditText) findViewById(R.id.ui_retypePassword_signup);
radiogroup = (RadioGroup) findViewById(R.id.ui_radiogroup_signup);
professional_btn = (RadioButton) findViewById(R.id.ui_professional_radiobtn);
visitor_btn = (RadioButton) findViewById(R.id.ui_visitor_radiobtn);


radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
int option = radiogroup.getCheckedRadioButtonId();
switch (option)

case R.id.ui_professional_radiobtn:
if (professional_btn.isChecked())
spinner.setVisibility(View.VISIBLE);


case R.id.ui_visitor_radiobtn:
if (visitor_btn.isChecked())
spinner.setVisibility(View.GONE);






);






findViewById(R.id.ui_signinaccount_text).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
Intent move_login_screen = new Intent(Signup_Activity.this , Login_Activity.class);
startActivity(move_login_screen);

);
findViewById(R.id.ui_signup).setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View v)
userSignup();

);

List<String> select_user_type = new ArrayList<>();
select_user_type.add(0 , "Select User Type");
select_user_type.add("Doctor");
select_user_type.add("Hospital");
select_user_type.add("Clinic");
select_user_type.add("Pharmacy");
select_user_type.add("Blood Bank");
select_user_type.add("Fitness Center");


ArrayAdapter<String> dataAdapter;
dataAdapter = new ArrayAdapter<String>(this , android.R.layout.simple_spinner_item , select_user_type);

dataAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
spinner.setAdapter(dataAdapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener()
@Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id)
if (parent.getItemAtPosition(position).equals("Select User Type"))

else
String item = parent.getItemAtPosition(position).toString();


@Override
public void onNothingSelected(AdapterView<?> parent)

);





private void userSignup()


String username = edit_username.getText().toString().trim();
String email = edit_email.getText().toString().trim();
String first_name = edit_firstname.getText().toString().trim();
String last_name = edit_lastname.getText().toString().trim();
String phone = edit_Phone.getText().toString().trim();
String password = edit_password.getText().toString().trim();
String confirm_password = edit_retypePassword.getText().toString().trim();



if (username.isEmpty())
edit_username.setError("User Name is Required");
edit_username.requestFocus();
return;

if (!Patterns.EMAIL_ADDRESS.matcher(email).matches())
edit_email.setError("Enter a valid Email");
edit_email.requestFocus();
return;

if (first_name.isEmpty())
edit_firstname.setError("First Name is Required");
edit_firstname.requestFocus();
return;

if (last_name.isEmpty())
edit_lastname.setError("Last Name is Required");
edit_lastname.requestFocus();
return;

if (!Patterns.PHONE.matcher(phone).matches())
edit_Phone.setError("Enter a valid Phone NO.");
edit_Phone.requestFocus();
return;

if (password.isEmpty())
edit_password.setError("Password required");
edit_password.requestFocus();
return;

if (password.length() < 6)
edit_password.setError("Password should be atleast 6 characters");
edit_password.requestFocus();
return;

if (!confirm_password.equals(password))
edit_retypePassword.setError("Error in Password matching please check");
edit_retypePassword.requestFocus();
return;

if (confirm_password.isEmpty())
edit_retypePassword.setError("Retype password is required");
edit_retypePassword.requestFocus();
return;

retrofit2.Call<ResponseBody> call = RetrofitClient.getInstance()
.getApi().createUser(username , email , first_name , last_name , phone , password , confirm_password);
call.enqueue(new Callback<ResponseBody>()
@Override
public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response)
try
String s = response.body().string();
Toast.makeText(Signup_Activity.this , s , Toast.LENGTH_SHORT).show();
catch (IOException e)
e.printStackTrace();




@Override
public void onFailure(Call<ResponseBody> call, Throwable t)
Toast.makeText(Signup_Activity.this , t.getMessage() , Toast.LENGTH_SHORT).show();

);






which api calling to registration.
– Android Team
Aug 10 at 11:55




2 Answers
2



To get data from spinner:


String text = spinner.getSelectedItem().toString();



To get data from Radio Button:


radiogroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener()
@Override
public void onCheckedChanged(RadioGroup group, int checkedId)
int option = radiogroup.getCheckedRadioButtonId();
switch (option)

case R.id.ui_professional_radiobtn:
if (professional_btn.isChecked())
String value = professional_btn.getText();
spinner.setVisibility(View.VISIBLE);


case R.id.ui_visitor_radiobtn:
if (visitor_btn.isChecked())
String value = visitor_btn.getText();
spinner.setVisibility(View.GONE);



);



Pass these values to your retrofit call.





thanks ... and what to do with radio button
– s.has
Aug 10 at 12:05





I've edited for the same. Please check and let me know.
– Jacob Celestine
Aug 10 at 12:13





how to pass these values to retrofit call.
– s.has
Aug 10 at 12:29





pass it along with the other values you pass
– Jacob Celestine
Aug 10 at 13:50





@s.has has your question been solved?
– Jacob Celestine
Aug 14 at 17:41



Try this code for retrofit post api..
make pojo class for user ..


public class User


@SerializedName("firstName")
private String firstName;

@SerializedName("password")
private String password;


@SerializedName("email")
private String email;
public void setFirstName(String firstName)
this.firstName = firstName;


public String getFirstName()
return firstName;


public void setPassword(String password)
this.password = password;


public String getPassword()
return password;

public void setEmail(String email)
this.email = email;


public String getEmail()
return email;





make respone class for given by server it make pojo class..


public class ResponseData

@SerializedName("response")
private String response;

@SerializedName("details")
private String details;

public void setResponse(String response)
this.response = response;


public String getResponse()
return response;


public void setDetails(String details)
this.details = details;


public String getDetails()
return details;





after define retrofit ..


public class ApiClient
private final static String BASE_URL = "https://api.github.com"; // define your url..

public static ApiClient apiClient;
private Retrofit retrofit = null;

public static ApiClient getInstance()
if (apiClient == null)
apiClient = new ApiClient();

return apiClient;


//private static Retrofit storeRetrofit = null;

public Retrofit getClient()
return getClient(null);



private Retrofit getClient(final Context context)

HttpLoggingInterceptor interceptor = new HttpLoggingInterceptor();
interceptor.setLevel(HttpLoggingInterceptor.Level.BODY);
OkHttpClient.Builder client = new OkHttpClient.Builder();
client.readTimeout(60, TimeUnit.SECONDS);
client.writeTimeout(60, TimeUnit.SECONDS);
client.connectTimeout(60, TimeUnit.SECONDS);
client.addInterceptor(interceptor);
client.addInterceptor(new Interceptor()
@Override
public okhttp3.Response intercept(Chain chain) throws IOException
Request request = chain.request();

return chain.proceed(request);

);

retrofit = new Retrofit.Builder()
.baseUrl(BASE_URL)
.client(client.build())
.addConverterFactory(GsonConverterFactory.create())
.build();


return retrofit;





make interface for api calling..


public interface ApiInterface
@POST("registrion")
Call<ResponseData> userReg(@Body User user);





api calling into activity or fragmen..


ApiInterface apiInterface = ApiClient.getInstance().getClient().create(ApiInterface.class);
User user=new User();
user.setEmail("abcd@gmail.com");
user.setPassword("123");
user.setFirstName("Vikas");
Call<ResponseData> dataCall=apiInterface.userReg(user);
dataCall.enqueue(new Callback<ResponseData>()
@Override
public void onResponse(Call<ResponseData> call, Response<ResponseData> response)
if (response!=null && response.isSuccessful() && response.body()!=null)
// show message



@Override
public void onFailure(Call<ResponseData> call, Throwable t)
Log.d("Error",t.getMessage());

);



add below dependecy into app level gradle file..


implementation 'com.squareup.okhttp3:logging-interceptor:3.4.1'
implementation 'com.squareup.retrofit2:retrofit:2.3.0'
implementation 'com.squareup.retrofit2:converter-gson:2.3.0'






By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

Popular posts from this blog

Firebase Auth - with Email and Password - Check user already registered

Dynamically update html content plain JS

How to determine optimal route across keyboard