SameerKhan1406 SameerKhan1406 - 5 days ago 6
Android Question

Showing two screen on single page using ViewPager

I am trying to show two screen using single page by using ViewPager

Here is my xml Part named as login:

<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:padding="0dip"
tools:context=".LoginActivity">

<com.medmainfomatix.VoIPvoiceapp.MyViewPager
android:id="@+id/viewPagerVertical"
android:layout_width="match_parent"
android:layout_height="match_parent">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_sms"
android:gravity="center_horizontal"
android:orientation="vertical">


<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:layout_gravity="center"
>

<EditText
android:id="@+id/loginNo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minWidth="200dp"
android:layout_gravity="center_horizontal"
android:digits="1234567890">

<requestFocus />

</EditText>

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

<Button
android:id="@+id/btn_request_sms"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />


<CheckBox
android:id="@+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"/>

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/layout_otp"
android:gravity="center_horizontal"
android:orientation="vertical">

<TextView
android:id="@+id/otptext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
<TextView
android:id="@+id/otptext1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>

<android.support.design.widget.TextInputLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
>



<EditText
android:id="@+id/inputOtp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>

<requestFocus />

</EditText>

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

<Button
android:id="@+id/loginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
</com.medmainfomatix.VoIPvoiceapp.MyViewPager>
</RelativeLayout>


Here is my java Activity file named as LoginActivity :

public class abc extends BaseActivity implements SinchService.StartFailedListener,View.OnClickListener {

private static String TAG = LoginActivity.class.getSimpleName();

private PrefManager pref;
private Button mLoginButton,btnRequestSms;
private EditText mLoginNo,inputOtp;
private ProgressDialog mSpinner;
private ViewPagerAdapter adapter;
private CheckBox mBox;
private SQLiteDatabase db;
private TextView otptext;
private ViewPager viewPager;


@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.login);

createDatabase();
viewPager = (ViewPager) findViewById(R.id.viewPagerVertical);
mBox = (CheckBox) findViewById(R.id.checkBox1);
otptext= (TextView) findViewById(R.id.otptext);
inputOtp=(EditText) findViewById(R.id.inputOtp);
String checkBoxText = "I agree to all the <a href='http://www.redbus.in/mob/mTerms.aspx' > Terms and Conditions</a>";

mBox.setText(Html.fromHtml(checkBoxText));
pref = new PrefManager(this);
mBox.setMovementMethod(LinkMovementMethod.getInstance());
btnRequestSms = (Button) findViewById(R.id.btn_request_sms);
mLoginButton = (Button) findViewById(R.id.loginButton);
mBox.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View view) {
if(mBox.isChecked()){
btnRequestSms.setVisibility(View.VISIBLE);
}
else{

btnRequestSms.setVisibility(View.GONE);
}
}
});
adapter = new ViewPagerAdapter();
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}

@Override
public void onPageSelected(int position) {
}

@Override
public void onPageScrollStateChanged(int state) {

}
});

btnRequestSms.setOnClickListener(this);
mLoginNo = (EditText) findViewById(R.id.loginNo);
mLoginButton.setEnabled(false);
mLoginButton.setOnClickListener(this);

// Checking for user session
// if user is already logged in, take him to main activity
if (pref.isLoggedIn()) {
Intent intent = new Intent(LoginActivity.this, PlaceCallActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);

finish();
}
if (pref.isWaitingForSms()) {
viewPager.setCurrentItem(1);

}

}

public void onClick(View view) {
switch (view.getId()) {
case R.id.btn_request_sms:
validateForm();

break;

case R.id.loginButton:
verifyOtp();
loginClicked();
break;

}
}

private void loginClicked() {
String userName = mLoginNo.getText().toString();

if (userName.isEmpty()) {
Toast.makeText(this, "Please enter your number", Toast.LENGTH_LONG).show();
return;
}


}

private void openPlaceCallActivity() {
Intent mainActivity = new Intent(this, abc2.class);
startActivity(mainActivity);
}
private void requestForSMS(final String mobile) {
StringRequest strReq = new StringRequest(Request.Method.POST,
Config.URL_REQUEST_SMS, new Response.Listener<String>() {

@Override
public void onResponse(String response) {
Log.d(TAG, response.toString());

try {
JSONObject responseObj = new JSONObject(response);

// Parsing json object response
// response will be a json object
boolean error = responseObj.getBoolean("error");
String message = responseObj.getString("message");

// checking for error, if not error SMS is initiated
// device should receive it shortly
if (!error) {
// boolean flag saying device is waiting for sms
pref.setIsWaitingForSms(true);
viewPager.setCurrentItem(1);
Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

} else {
Toast.makeText(getApplicationContext(),
"Error: " + message,
Toast.LENGTH_LONG).show();
}

// hiding the progress bar


} catch (JSONException e) {
Toast.makeText(getApplicationContext(),
"Error: " + e.getMessage(),
Toast.LENGTH_LONG).show();


}

}
}, new Response.ErrorListener() {

@Override
public void onErrorResponse(VolleyError error) {
Log.e(TAG, "Error: " + error.getMessage());
Toast.makeText(getApplicationContext(),
error.getMessage(), Toast.LENGTH_SHORT).show();
}
}) {

/**
* Passing user parameters to our server
* @return
*/
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("mobile", mobile);

Log.e(TAG, "Posting params: " + params.toString());

return params;
}

};

// Adding request to request queue
MyApplication.getInstance().addToRequestQueue(strReq);
}

class ViewPagerAdapter extends PagerAdapter {

@Override
public int getCount() {
return 2;
}

@Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}

public Object instantiateItem(View collection, int position) {

int resId = 0;
switch (position) {
case 0:
resId = R.id.layout_sms;
break;
case 1:
resId = R.id.layout_otp;
break;
}
return findViewById(resId);
}
}
}


Here is my MyViewPager class :

public class MyViewPager extends ViewPager {

public MyViewPager(Context context) {
super(context);
}

public MyViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}

@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}

@Override
public boolean onTouchEvent(MotionEvent event) {
// Never allow swiping to switch between pages
return false;
}
}


If you are not getting anything please comment because i have cut most of my code and maybe by mistake I mistakenly cutted something related to MyViewPager so please feel free to comment so that i could edit my code and you could get more clear idea else if you are already getting then you can understand what I am trying to achieve here

GOAL : To request otp to mobile and the verify otp should come to next screen while request part is gone

Answer

once you send an OTP to device you will receive a callback stating 'success',then yourViewPager.setCurrentItem(1); this will move to next screen

if (!error) {
                    // boolean flag saying device is waiting for sms
                    pref.setIsWaitingForSms(true);
                    viewPager.setCurrentItem(1);//this will move to next screen.
                    Toast.makeText(getApplicationContext(), message, Toast.LENGTH_SHORT).show();

                } else {
                    Toast.makeText(getApplicationContext(),
                            "Error: " + message,
                            Toast.LENGTH_LONG).show();
                }
Comments