This is my first time using Volley in Fragments.
Can anyone help me with this. I am trying to use the following code for the fragment that I am calling from the navigation box:
public class ChangePassword extends android.app.Fragment implements View.OnClickListener
{
EditText etOldSignUpPassword;
EditText etNewSignUpPassword;
EditText etNewSignUpConfirmPassword;
ImageButton imageChangePasswordButton;
ProgressBar pbWeb;
String old,npass,ncpass;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_change_password, container, false);
etOldSignUpPassword = (EditText)v.findViewById(R.id.etOldPassword);
etNewSignUpPassword = (EditText)v.findViewById(R.id.etNewSignUpPassword);
etNewSignUpConfirmPassword = (EditText)v.findViewById(R.id.etNewSignUpConfirmPassword);
imageChangePasswordButton = (ImageButton)v.findViewById(R.id.imageChangePasswordButton);
pbWeb = (ProgressBar)v.findViewById(R.id.pbWebC);
pbWeb.setVisibility(View.GONE);
imageChangePasswordButton.setOnClickListener(this);
return v;
}
@Override
public void onClick(View view) {
if(view.getId() == R.id.imageChangePasswordButton) {
old = etOldSignUpPassword.getText().toString();
npass = etNewSignUpPassword.getText().toString();
ncpass = etNewSignUpConfirmPassword.getText().toString();
if (!isNetworkAvailable()) {
Toast.makeText(getActivity(), "Your Wifi/Data is disabled. Please switch on and try again", Toast.LENGTH_SHORT).show();
} else {
pbWeb.setVisibility(View.VISIBLE);
String server_url = "url_working_fine_for_activity_so_no_issue_here";
StringRequest strReq = new StringRequest(Request.Method.POST, server_url, new Response.Listener<String>() {
@Override
public void onResponse(String response) {
if (response.equals("SUCCESS")) {
Toast.makeText(getActivity(), "Password Changed Successfully", Toast.LENGTH_SHORT).show();
Intent loginIntent = new Intent(getActivity(), LoginActivity.class);
startActivity(loginIntent);
pbWeb.setVisibility(View.GONE);
getActivity().finish();
} else {
pbWeb.setVisibility(View.GONE);
Toast.makeText(getActivity(), "Couldn't Change Password. Old Password Exists", Toast.LENGTH_SHORT).show();
}
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
pbWeb.setVisibility(View.GONE);
Toast.makeText(getActivity(), error.toString(), Toast.LENGTH_SHORT).show();
}
}) {
@Override
protected Map<String, String> getParams() throws AuthFailureError {
Map<String, String> map = new HashMap<String, String>();
map.put("old", old);
map.put("new", npass);
map.put("newCon", ncpass);
return map;
}
};
VolleySingleton.getInstance(getActivity().getApplicationContext()).addToReqQueue(strReq);
}
}
}
PREV: This code is currently crashing.
NOTE. . I tried using onClickListener () directly on imageChangePasswordButton inside the onActivityCreated () method, not using imClickListener in the class, but found in the stream that there was an error.
EDIT: The current code seems to be working fine.
Here is the xml for the query:
<?xml version="1.0" encoding="utf-8"?>
<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:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context=".mypackage.ChangePassword">
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/pbWebC"
style="@android:style/Widget.ProgressBar.Small"
android:layout_centerInParent="true" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:src="@drawable/image1"
android:id="@+id/logo"
android:layout_gravity="center_horizontal" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/prompt_old_email"
android:ems="10"
android:layout_marginTop="10dp"
android:id="@+id/etOldPassword" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/prompt_new_password"
android:ems="10"
android:layout_marginTop="10dp"
android:id="@+id/etNewSignUpPassword" />
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:hint="@string/prompt_new_confirm_password"
android:ems="10"
android:layout_marginTop="10dp"
android:id="@+id/etNewSignUpConfirmPassword" />
<ImageButton
android:layout_width="150dp"
android:layout_height="50dp"
android:layout_gravity="center"
android:id="@+id/imageChangePasswordButton"
android:layout_marginTop="20dp"
android:src="@drawable/reset" />
</LinearLayout>
</RelativeLayout>
EDIT: The code is working fine. Thanks for the help. @LongRanger @ i-Droid