title: Android Login Screen with php mysql
body: <p>I have successfully created Login page ... I have checked the username&amp;password with my database successfully.Then I have set the condition <code>if(result!=null)</code> means start second activity.
  What am I getting now na ,,even if username or password not correct also ,,it starts second page.What i have to do to make correct login screen...
  Suggesstion plz ,Thanks in advance </p>
  
  <p>AsynTask onpostexecute function</p>
  
  <pre><code> protected void onPostExecute(String result) {
          //View your result here.
             if(result!= null) 
             {
                 val=result;
                 Intent intent=new Intent(Login.this,Child_Profile.class);
                 intent.putExtra("child_name", result);
                 startActivity(intent);
  
             }
  
      }
  </code></pre>
  
  <p>My login.xml </p>
  
  <pre><code>&lt;LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
      android:layout_width="match_parent"
      android:layout_height="match_parent"
      android:orientation="vertical"
      android:weightSum="6"
      android:background="#1E8EB6" &gt;
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
  
      android:background="#1E8EB6"
      android:layout_weight="1.5"&gt;
  
      &lt;TextView
          android:id="@+id/textView1"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_below="@+id/imageView1"
          android:layout_centerHorizontal="true"
          android:gravity="center_vertical"
          android:text="@string/title"
          android:textColor="#FFFFFF"
          android:textSize="30sp" /&gt;
  
      &lt;ImageView
          android:id="@+id/imageView1"
          android:layout_width="50dp"
          android:layout_height="50dp"
          android:layout_alignLeft="@+id/textView1"
          android:layout_alignParentTop="true"
          android:layout_marginLeft="94dp"
          android:layout_marginTop="21dp"
          android:contentDescription="@string/title_img"
          android:src="@drawable/camera" /&gt;
  
    &lt;/RelativeLayout&gt;
  
  
  
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:background="#00677F"
      android:layout_weight="3.5"
      android:paddingLeft="10dp"
      android:paddingRight="10dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"
  
      &gt;
  
      &lt;EditText
          android:id="@+id/username"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:layout_alignParentTop="true"
          android:layout_centerHorizontal="true"
          android:layout_marginTop="30dp"
          android:layout_marginBottom="10dp"
          android:paddingLeft="5dp"
          android:ems="10" 
          android:background="#FFFFFF"
          android:drawableLeft="@android:drawable/stat_notify_more"
          android:inputType="text"
          android:gravity="center_vertical"
          android:hint="@string/username"&gt;
  
          &lt;requestFocus /&gt;
      &lt;/EditText&gt;
  
      &lt;EditText
          android:id="@+id/password"
          android:layout_width="match_parent"
          android:layout_height="40dp"
          android:layout_below="@+id/username"
          android:layout_centerHorizontal="true"
          android:ems="10" 
          android:background="#FFFFFF"
          android:drawableStart="@android:drawable/ic_secure"
          android:inputType="textPassword"
          android:paddingLeft="5dp"
          android:gravity="center_vertical"
           android:hint="@string/password"/&gt;
  
      &lt;Button
          android:id="@+id/login"
          android:layout_width="100dp"
          android:layout_height="40dp"
          android:layout_alignRight="@+id/password"
          android:layout_below="@+id/password"
          android:layout_marginTop="19dp"
          android:text="@string/login" 
          android:background="#054579"
          android:textColor="#FFFFFF" /&gt;
  
      &lt;CheckBox
          android:id="@+id/remember"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignBaseline="@+id/login"
          android:layout_alignBottom="@+id/login"
          android:layout_alignLeft="@+id/password"
          android:text="@string/remember" 
          android:textColor="#FFFFFF"/&gt;
  
      &lt;Button
          android:id="@+id/forget"
          android:layout_width="wrap_content"
          android:layout_height="wrap_content"
          android:layout_alignParentBottom="true"
          android:layout_centerHorizontal="true"
          android:layout_marginBottom="18dp"
          android:text="@string/forget"
          android:textColor="#FFFFFF" 
          style="?android:attr/borderlessButtonStyle"
          android:textSize="20sp" /&gt;
  
    &lt;/RelativeLayout&gt;
  
  
  &lt;RelativeLayout
      android:layout_width="match_parent"
      android:layout_height="0dp"
      android:layout_weight="1"
  
  
      android:paddingRight="10dp"
      android:layout_marginLeft="10dp"
      android:layout_marginRight="10dp"&gt;
  
      &lt;Button
          android:id="@+id/signup"
          android:layout_width="150dp"
          android:layout_height="40dp"
          android:layout_alignParentRight="true"
          android:layout_centerVertical="true"
          android:background="#054579"
          android:text="@string/signup"
          android:textColor="#FFFFFF" /&gt;
  
    &lt;/RelativeLayout&gt;
  
  
  
  
  &lt;/LinearLayout&gt;
  </code></pre>
  
  <p>My Login.java, Final working code</p>
  
  <pre><code>   package com.example.certify.schoolapp;
  
  import java.io.BufferedReader;
  import java.io.InputStreamReader;
  import java.io.OutputStreamWriter;
  import java.net.URL;
  import java.net.URLConnection;
  import java.net.URLEncoder;
  
  import android.app.Activity;
  import android.content.Intent;
  import android.os.AsyncTask;
  import android.os.Bundle;
  import android.view.View;
  import android.view.View.OnClickListener;
  import android.widget.Button;
  import android.widget.EditText;
  import android.widget.TextView;
  import android.widget.Toast;
  
  public class Login extends Activity {
      private TextView head;
      private EditText username,password;
      private Button login,signup,forget;
      private static String user,pass,val,result="";
  
  @Override
  protected void onCreate(Bundle savedInstanceState) {
      // TODO Auto-generated method stub
      super.onCreate(savedInstanceState);
      setContentView(R.layout.login);
      head=(TextView) findViewById(R.id.textView1);
      username=(EditText) findViewById(R.id.username);
      password=(EditText) findViewById(R.id.password);
      login=(Button) findViewById(R.id.login);
      signup=(Button) findViewById(R.id.signup);
      forget=(Button) findViewById(R.id.forget);
      login.setOnClickListener(new OnClickListener() {
  
          @Override
          public void onClick(View v) {
              // TODO Auto-generated method stub
              new sendPostData().execute();
  
          }
      });
  }
  
  private class sendPostData extends AsyncTask&lt;String, Void, String&gt;
  {
          @Override
      protected String doInBackground(String... params) {  
  
   try{
  
               user = username.getText().toString(); 
               pass = password.getText().toString();
               String link="http://192.168.1.5:81/arun/loginpost.php?";
                  String data  = URLEncoder.encode("username", "UTF-8") 
                  + "=" + URLEncoder.encode(user, "UTF-8");
                  data += "&amp;" + URLEncoder.encode("password", "UTF-8") 
                  + "=" + URLEncoder.encode(pass, "UTF-8");
                  URL url = new URL(link);
                  URLConnection conn = url.openConnection(); 
                  conn.setDoOutput(true); 
                  OutputStreamWriter wr = new OutputStreamWriter
                  (conn.getOutputStream()); 
                  wr.write( data ); 
                  wr.flush(); 
                  BufferedReader reader = new BufferedReader
                  (new InputStreamReader(conn.getInputStream()));
                  StringBuilder sb = new StringBuilder();
                  String line = null;
                  // Read Server Response
                  while((line = reader.readLine()) != null)
                  {
                     sb.append(line);
                     break;
                  }
                 return sb.toString();
               }catch(Exception e){
                  return new String("Exception: " + e.getMessage());
               }
      }
         @Override
      protected void onPostExecute(String result) {
          //View your result here.
             if(result.equals("wrong"))
             {
                 Toast toast=Toast.makeText(Login.this,"incorrect password", Toast.LENGTH_LONG);
                 toast.show(); 
             }
             else
             {
                 Intent intent=new Intent(Login.this,Child_Profile.class);
                 intent.putExtra("child_name", result);
                 startActivity(intent);
             }
  
  
  
  
          }
   }
  }
  </code></pre>
  
  <p>My loginpost.php</p>
  
  <pre><code>&lt;?php
  $con=mysql_connect("localhost","arun","sachin11");
  
  $db_select = mysql_select_db('my_db', $con);
  $username = $_POST['username'];
  $password = $_POST['password'];
  $result = mysql_query("SELECT Role FROM table1 where Username='$username' and Password='$password'",$con);
  $row = mysql_fetch_array($result);
  $data = $row[0];
  if($data){
  echo $data;
  }
  else 
  {
      echo "wrong";
  }
  mysql_close($con);
  ?&gt;
  </code></pre>