title: Android Studio - Redirect to another page
body: <p>I've made an android app to login to a database and I need it to redirect to a user profile page when the button is clicked, so far it only displays a message to say that login was successful and then does nothing else. The three code snippets I have below are the XML for the application, the Java for the same page, and BackgroundWorker, where a lot of the underlying stuff is done. Could somebody show me how to redirect to a page called Activity_user_profile.xml?</p>
  
  <p>EDITED ANSWER:</p>
  
  <p>I added the line: startActivity(new Intent(this,UserProfileActivity.class)); to my onLogin method, this allowed me to link it to the page I wanted</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="utf-8"?&gt;
  &lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:tools="http://schemas.android.com/tools"
  android:id="@+id/activity_main"
  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="com.example.programmingknowledge.finalyearproject2017.MainActivity"&gt;
  
  &lt;Button
      android:text="Register"
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:id="@+id/btnRegister"
      android:onClick="OpenReg"
      android:layout_below="@+id/btnLogin"
      android:layout_alignLeft="@+id/btnLogin"
      android:layout_alignStart="@+id/btnLogin"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" /&gt;
  
  &lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:ems="10"
      android:id="@+id/et_doctor_number"
      android:layout_alignParentTop="true"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:hint="Doctor number "
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:inputType="number" /&gt;
  
  &lt;EditText
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:inputType="textPassword"
      android:ems="10"
      android:id="@+id/et_Password"
      android:layout_below="@+id/et_doctor_number"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true"
      android:hint="Password" /&gt;
  
  &lt;Button
      android:layout_width="wrap_content"
      android:layout_height="wrap_content"
      android:text="Login"
      android:id="@+id/btnLogin"
      android:onClick="OnLogin"
      android:layout_below="@+id/et_Password"
      android:layout_alignParentLeft="true"
      android:layout_alignParentStart="true"
      android:layout_alignParentRight="true"
      android:layout_alignParentEnd="true" /&gt;
  
  &lt;/RelativeLayout&gt;
  </code></pre>
  
  <p>Main activity:</p>
  
  <pre><code>package com.example.programmingknowledge.finalyearproject2017;
  
  import android.content.Intent;
  import android.support.v7.app.ActionBarActivity;
  import android.os.Bundle;
  import android.view.View;
  import android.widget.EditText;
  
  public class MainActivity extends ActionBarActivity {
  
  EditText EmailEt, PasswordEt;
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      super.onCreate(savedInstanceState);
      setContentView(R.layout.activity_main);
      EmailEt = (EditText) findViewById(R.id.et_doctor_number);
      PasswordEt = (EditText) findViewById(R.id.et_Password);
  }
  
  public void OnLogin(View view){
      String email = EmailEt.getText().toString();
      String password = PasswordEt.getText().toString();
      String type = "Login";
  
      BackgroundWorker backgroundWorker = new BackgroundWorker(this);
      backgroundWorker.execute(type, email, password);
      startActivity(new Intent(this,UserProfileActivity.class));
  }
  
  public void OpenReg(View view){
      startActivity(new Intent(this,Register.class));
  }
  </code></pre>
  
  <p>}</p>
  
  <pre><code>Background Worker:
  package com.example.programmingknowledge.finalyearproject2017;
  
  import android.content.Context;
  import android.os.AsyncTask;
  import android.app.AlertDialog;
  
  import java.io.BufferedReader;
  import java.io.BufferedWriter;
  import java.io.IOException;
  import java.io.InputStream;
  import java.io.InputStreamReader;
  import java.io.OutputStream;
  import java.io.OutputStreamWriter;
  import java.net.HttpURLConnection;
  import java.net.MalformedURLException;
  import java.net.URL;
  import java.net.URLEncoder;
  
  /**
  * Created by john on 25-Feb-17.
  */
  
  public class BackgroundWorker extends AsyncTask&lt;String, Void, String&gt; {
      Context context;
      AlertDialog alertDialog;
      BackgroundWorker (Context ctx){
          context = ctx;
      }
      @Override
      protected String doInBackground(String... params) {
          String type = params[0];
          String login_url = "http://10.0.2.2/login.php";
          String register_url = "http://10.0.2.2/register.php";
          if (type.equals("login")){
              try {
                  String email = 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("email", "UTF-8")+"="+URLEncoder.encode(email, "utf-8")+"&amp;"+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();
              }
          } else if(type.equals("register")){
              try {
                  String patient_name = params[1];
                  String check_in_date = params[2];
                  String room_number = params[3];
                  String bed_number = params[4];
                  String notes = params[5];
                  URL url = new URL(register_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("patient_name", "UTF-8")+"="+URLEncoder.encode(patient_name, "utf-8")+"&amp;"
                      + URLEncoder.encode("check_in_date", "UTF-8")+"="+URLEncoder.encode(check_in_date, "utf-8")+"&amp;"
                      + URLEncoder.encode("room_number", "UTF-8")+"="+URLEncoder.encode(room_number, "utf-8")+"&amp;"
                      + URLEncoder.encode("bed_number", "UTF-8")+"="+URLEncoder.encode(bed_number, "utf-8")+"&amp;"
                      + URLEncoder.encode("notes", "UTF-8")+"="+URLEncoder.encode(notes, "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.setMessage("Login Status");
  }
  
  @Override
  protected void onPostExecute(String result) {
      alertDialog.setMessage(result);
      alertDialog.show();
  }
  
  @Override
  protected void onProgressUpdate(Void... values) {
      super.onProgressUpdate(values);
  }
  </code></pre>
  
  <p>}</p>