1. Download our Official Android App: Forums for Android!

Apps android two buttons

Discussion in 'Android Development' started by droid13, Aug 7, 2013.

  1. droid13

    droid13 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Aug 7, 2013

    Aug 7, 2013
    4
    0
    5
    Hello
    Does anyone know how I can make two button in my app
    Now I have one button.
    But I want two.

    This is my code:
    Code:
    package com.Tim.wachtwoord.onthouder;

    import java.util.ArrayList;
    import java.util.List;
    import org.apache.http.HttpResponse;
    import org.apache.http.NameValuePair;
    import org.apache.http.client.HttpClient;
    import org.apache.http.client.ResponseHandler;
    import org.apache.http.client.entity.UrlEncodedFormEntity;
    import org.apache.http.client.methods.HttpPost;
    import org.apache.http.impl.client.BasicResponseHandler;
    import org.apache.http.impl.client.DefaultHttpClient;
    import org.apache.http.message.BasicNameValuePair;
    import android.app.Activity;
    import android.app.AlertDialog;
    import android.app.ProgressDialog;
    import android.content.DialogInterface;
    import android.content.Intent;
    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 register extends Activity {
    Button b;
    EditText et,pass;
    TextView tv;
    HttpPost httppost;
    StringBuffer buffer;
    HttpResponse response;
    HttpClient httpclient;
    List<NameValuePair> nameValuePairs;
    ProgressDialog dialog = null;

    @Override
    public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);

    b = (Button)findViewById(R.id.Button01);
    et = (EditText)findViewById(R.id.username);
    pass= (EditText)findViewById(R.id.password);
    tv = (TextView)findViewById(R.id.tv);

    b.setOnClickListener(new OnClickListener() {
    @Override
    public void onClick(View v) {
    dialog = ProgressDialog.show(register.this, "",
    "Validating user...", true);
    new Thread(new Runnable() {
    public void run() {
    login();
    }
    }).start();
    }
    });
    }


    void login(){
    try{

    httpclient=new DefaultHttpClient();
    httppost= new HttpPost("url"); // make sure the url is correct.
    //add your data
    nameValuePairs = new ArrayList<NameValuePair>(2);
    // Always use the same variable name for posting i.e the android side variable name and php side variable name should be similar,
    nameValuePairs.add(new BasicNameValuePair("username",et.getText().toString().trim())); // $Edittext_value = $_POST['Edittext_value'];
    nameValuePairs.add(new BasicNameValuePair("password",pass.getText().toString().trim()));
    httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
    //Execute HTTP Post Request
    response=httpclient.execute(httppost);
    // edited by James from coderzheaven.. from here....
    ResponseHandler<String> responseHandler = new BasicResponseHandler();
    final String response = httpclient.execute(httppost, responseHandler);
    System.out.println("Response : " + response);
    runOnUiThread(new Runnable() {
    public void run() {
    tv.setText("Response from PHP : " + response);
    dialog.dismiss();
    }
    });

    if(response.equalsIgnoreCase("User Found")){
    runOnUiThread(new Runnable() {
    public void run() {
    Toast.makeText(register.this,"Login Success", Toast.LENGTH_SHORT).show();
    }
    });


    startActivity(new Intent(register.this, ingelogd.class));
    }else{
    showAlert();
    }

    }catch(Exception e){
    dialog.dismiss();
    System.out.println("Exception : " + e.getMessage());
    }
    }
    public void showAlert(){
    register.this.runOnUiThread(new Runnable() {
    public void run() {
    AlertDialog.Builder builder = new AlertDialog.Builder(register.this);
    builder.setTitle("Login Error.");
    builder.setMessage("User not Found.")
    .setCancelable(false)
    .setPositiveButton("OK", new DialogInterface.OnClickListener() {
    public void onClick(DialogInterface dialog, int id) {
    }
    });
    AlertDialog alert = builder.create();
    alert.show();
    }
    });
    }
    }

    Can anyone help me?
    Thanks
     

    Advertisement

  2. droid13

    droid13 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Aug 7, 2013

    Aug 7, 2013
    4
    0
    5
  3. out of ideas

    out of ideas Android Enthusiast
    Rank:
    None
    Points:
    93
    Posts:
    640
    Joined:
    May 19, 2012

    May 19, 2012
    640
    145
    93
    same way you added the first one
     
  4. droid13

    droid13 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Aug 7, 2013

    Aug 7, 2013
    4
    0
    5
  5. droid13

    droid13 Lurker
    Thread Starter
    Rank:
    None
    Points:
    5
    Posts:
    4
    Joined:
    Aug 7, 2013

    Aug 7, 2013
    4
    0
    5
  6. Thayar

    Thayar Newbie
    Rank:
    None
    Points:
    35
    Posts:
    17
    Joined:
    Jul 15, 2013

    Jul 15, 2013
    17
    0
    35
    You have to add the pa.setOnclickListener method within the on create method
     

Share This Page

Loading...