I have a login page on fragment connected to BackgroundWorker class which has database connectivity.
my login page sends texts from EditTexts to BackgroundWorker Class, then all the database queries(by accessing PHP files from WAMP) are done in the BackgroundWorker class.
When i click the login button on my fragment it opens the dialog box which tells me if login was successful or not. (the code of dialog box exists in BackgroundWorker class (Please refer the code if i am not being clear) )
but the thing is, i don't want dialog box, i want to go from one fragment to another from that BackgroundWorker.java file.
i have the code of how can i go from one activity to another, but not of fragment
fragment XML FILE
<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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.admin.blingiton.Client_login">
<!-- TODO: Update blank fragment layout -->
<RelativeLayout
android:id="@+id/ClientLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/back"
android:layout_width="wrap_content"
android:layout_height="600dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:scaleType="fitXY"
app:srcCompat="@drawable/back" />
<ImageView
android:id="@+id/mirae"
android:layout_width="300dp"
android:layout_height="250dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="12dp"
android:layout_marginStart="32dp"
android:scaleType="centerInside"
app:srcCompat="@drawable/mirae" />
<ImageView
android:id="@+id/header"
android:layout_width="wrap_content"
android:layout_height="600dp"
android:layout_alignBottom="@+id/mirae"
android:layout_alignParentStart="true"
android:layout_marginBottom="20dp"
android:scaleType="fitStart"
app:srcCompat="@drawable/header" />
<EditText
android:id="@+id/cusername"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_alignTop="@+id/mirae"
android:layout_centerHorizontal="true"
android:layout_marginTop="13dp"
android:background="@drawable/username"
android:ems="10"
android:hint="username"
android:inputType="textPersonName"
android:textAlignment="center" />
<EditText
android:id="@+id/cpassword"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_alignStart="@+id/cusername"
android:layout_below="@+id/cusername"
android:layout_marginTop="18dp"
android:background="@drawable/password"
android:ems="10"
android:hint="password"
android:inputType="textPassword"
android:textAlignment="center" />
<Button
android:id="@+id/clogin"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_above="@+id/signupbtn"
android:layout_alignStart="@+id/cpassword"
android:layout_marginBottom="11dp"
android:background="@drawable/login" />
<Button
android:id="@+id/signupbtn"
android:layout_width="200dp"
android:layout_height="35dp"
android:layout_alignBottom="@+id/mirae"
android:layout_alignStart="@+id/clogin"
android:background="@drawable/signup"
/>
<ImageView
android:id="@+id/imageView"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:src="@drawable/log" />
</RelativeLayout>
<RelativeLayout
android:id="@+id/afterlogin"
android:layout_width="match_parent"
android:layout_height="match_parent">
</RelativeLayout>
</RelativeLayout>
Fragment.java file
EditText UsernameEt, PasswordEt;
Button login;
View v;
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
v = inflater.inflate(R.layout.fragment_clientlogin, container, false);
UsernameEt = (EditText) v.findViewById(R.id.cusername);
PasswordEt = (EditText) v.findViewById(R.id.cpassword);
login = (Button) v.findViewById(R.id.clogin);
login.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String username = UsernameEt.getText().toString();
String password = PasswordEt.getText().toString();
String type = "login";
BackgroundWorker backgroundWorker = new
BackgroundWorker(getActivity());
backgroundWorker.execute(type, username, password);
}
});
return v;
}
BackgroundWorker.java < /b>
Context context;
AlertDialog alertDialog;
BackgroundWorker(Context ctx) {
context = ctx;
}
@Override
protected String doInBackground(String...params) {
String type = params[0];
String login_url = "http://192.168.10.2/login.php";
if (type.equals("login")) {
try {
String user_name = params[1];
String password = params[2];
URL url = new URL(login_url);
HttpURLConnection httpURLConnection =
(HttpURLConnection) url.openConnection();
httpURLConnection.setRequestMethod("POST");
httpURLConnection.setDoOutput(true);
httpURLConnection.setDoInput(true);
OutputStream outputStream = httpURLConnection.getOutputStream();
BufferedWriter bufferedWriter = new BufferedWriter(new OutputStreamWriter(outputStream, "UTF-8"));
String post_data = URLEncoder.encode("user_name", "UTF-
8 ")+" = "+URLEncoder.encode(user_name,"
UTF - 8 ")+" & " +
URLEncoder.encode("password", "UTF-
8 ")+" = "+URLEncoder.encode(password,"
UTF - 8 ");
bufferedWriter.write(post_data);
bufferedWriter.flush();
bufferedWriter.close();
outputStream.close();
InputStream inputStream = httpURLConnection.getInputStream();
BufferedReader bufferedReader = new BufferedReader(new
InputStreamReader(inputStream, "iso-8859-1"));
String result = ""; String line = "";
while ((line = bufferedReader.readLine()) != null) {
result += line;
}
bufferedReader.close(); inputStream.close(); httpURLConnection.disconnect();
return result;
}
catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
@Override
protected void onPreExecute() {
alertDialog = new AlertDialog.Builder(context).create();
alertDialog.setTitle("Login Status");
}
@Override
protected void onPostExecute(String result) {
Client_login cl = new Client_login();
alertDialog.setMessage(result);
//alertDialog.show();
if (result.contentEquals("login success !!!!! Welcome user")) {
//Code Here.
} else {
Toast toast = Toast.makeText(context, "Email or password is wrong",
Toast.LENGTH_SHORT);
toast.show();
}
}
@Override
protected void onProgressUpdate(Void...values) {
super.onProgressUpdate(values);
}