0

I have a code as follow.

MainActivity.java

package com.example.androidphpmy;
import com.example.androidphpmy.MainActivity;
import com.example.androidphpmy.R;
import com.example.androidphpmy.PaymentActivity;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;




 public class MainActivity extends Activity {

Button btnVacant1,btnVacant2,btnVacant3,btnVacant4,
     btnVacant5,btnVacant6,btnVacant7,btnVacant8;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    setupMessageButton1();
    setupMessageButton2();
    setupMessageButton3();
    setupMessageButton4();
    setupMessageButton5();
    setupMessageButton6();
    setupMessageButton7();
    setupMessageButton8();

   }


private void setupMessageButton1(){
    Button messageButton = (Button) findViewById(R.id.button1);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 1",Toast.LENGTH_LONG).show();

    startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}

private void setupMessageButton2(){
    Button messageButton = (Button) findViewById(R.id.button2);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 2",Toast.LENGTH_LONG).show();

    startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}



private void setupMessageButton3(){
    Button messageButton = (Button) findViewById(R.id.button3);
    messageButton.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 3",Toast.LENGTH_LONG).show();

    startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}


private void setupMessageButton4(){
    Button messageButton = (Button) findViewById(R.id.button4);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 4",Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}


private void setupMessageButton5(){
    Button messageButton = (Button) findViewById(R.id.button5);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 5",Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}


private void setupMessageButton6(){
    Button messageButton = (Button) findViewById(R.id.button6);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 6",Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}


private void setupMessageButton7(){
    Button messageButton = (Button) findViewById(R.id.button7);
    messageButton.setOnClickListener(new View.OnClickListener() {

@Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 7",Toast.LENGTH_LONG).show();

startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}


private void setupMessageButton8(){
    Button messageButton = (Button) findViewById(R.id.button8);
    messageButton.setOnClickListener(new View.OnClickListener() {

    @Override
public void onClick(View v) {
Toast.makeText(MainActivity.this,"You clicked on block 8",Toast.LENGTH_LONG).show();

    startActivity(new Intent(MainActivity.this,PaymentActivity.class));
        }
    });
}       
}

Next is my payment page.

PaymentActivity.java

package com.example.androidphpmy;

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;
import android.widget.RemoteViews;


public class PaymentActivity 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.activity_payment);

    b = (Button)findViewById(R.id.Button01);  
    et = (EditText)findViewById(R.id.accountno);
    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(PaymentActivity.this, "", 
                    "Validating user...", true);
             new Thread(new Runnable() {
                    public void run() {
                        payment();                        
                    }
                  }).start();               
        }
    });
}

void payment(){
    try{            

httpclient=new DefaultHttpClient();
httppost= new HttpPost("http://tanushreedutta.site40.net/payment_new/check.php");
     //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("accno",et.getText().toString().trim()));  
       // $Edittext_value = $_POST['Edittext_value'];
nameValuePairs.add(newBasicNameValuePair("bpassword",pass.getText().toString().trim())); 
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
//Execute HTTP Post Request
response=httpclient.execute(httppost);
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.startsWith("User Found")){
runOnUiThread(new Runnable() {
        public void run() {
Toast.makeText(PaymentActivity.this,"Payment Successful",Toast.LENGTH_SHORT).show();

                }
            });

startActivity(new Intent(PaymentActivity.this, MainActivity.class));
}else{
showAlert();                
}

    }catch(Exception e){
        dialog.dismiss();
        System.out.println("Exception : " + e.getMessage());
    }
}
public void showAlert(){
PaymentActivity.this.runOnUiThread(new Runnable() {
     public void run() {
AlertDialog.Builder builder = new AlertDialog.Builder(PaymentActivity.this);
       builder.setTitle("Payment 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();               
        }
    });
}
  }

I have buttons in my MainAcitivity.java named as "V1,V2,...V8". Now I want such a functionality that if user clicks on "V1" and goes to payment page and payment is successful (PaymentActivity.java does payment part) then when its redirected to the MainActivity it should change my button from "V1" to "R1". Similarly for other buttons too. Any suggestions or advices will be highly appreciated. Thank you.

  • you should look into intent.putExtra and startActivityForResult – JRowan Mar 10 '14 at 23:55
  • Thank you JRowan for quick reply. Can u be more specific ? If possible then with the code changes that I should do in order to achieve this ! A code for only one button. – user3399423 Mar 10 '14 at 23:59

1 Answers1

1

Whe you start PaymentActivity from MainActivity, start using startActivityForResult() and in MainActivity, write code to get the result send by PaymentActivity using setResults. Then according to the result, you can change the button text.

Another option is you can use sharedPreferences. In PaymentActivity, once payment is successful, set a sharedPreferences flag and after returning back to MainActivity, check that sharedprefernces flag and change the button accordingly.

I would prefer the first option. Hope it helps

Sushil
  • 8,250
  • 3
  • 39
  • 71
  • Thank you for your quick response. Can you please write a sample code for it to refer it as an example ? – user3399423 Mar 11 '14 at 00:05
  • you can look all over the place and find code snippets relating to this – JRowan Mar 11 '14 at 00:07
  • @JRowan- can you suggest any such link ? – user3399423 Mar 11 '14 at 00:09
  • http://stackoverflow.com/questions/10407159/android-how-to-manage-start-activity-for-result – JRowan Mar 11 '14 at 00:11
  • 1
    @user3399423 .. You can find many on net. For 1st option : http://www.javatpoint.com/android-startactivityforresult-example . For 2nd option : http://examples.javacodegeeks.com/android/core/content/android-sharedpreferences-example/ – Sushil Mar 11 '14 at 00:13
  • Thank you Sushil and JRowan ! – user3399423 Mar 11 '14 at 00:21
  • I am a bit confused. The first link suggested by sushil, it seems it changes the text but I want to change the name of the button itself. say if the button was "Vacant" first so after the payment is successful in the next activity the button should change to "Reserved" – user3399423 Mar 11 '14 at 00:34
  • If any of you can please write down the code in context of my given code then it would be great. Thank you. – user3399423 Mar 11 '14 at 00:35
  • @user3399423 Did you try anything? First you should try and if you get stuck and have specific questions, people in starkoverflow will try to help you. But am sorry, this site is not for spoon feeding or cooked up solutions. – Sushil Mar 11 '14 at 00:39
  • @sushil- basically the explanation given on that link is not what I wanted. I explained my doubt above in the comment. Thanks – user3399423 Mar 11 '14 at 00:41
  • @sushil- I have edited the code as per my understanding...http://stackoverflow.com/questions/22338870/how-to-make-a-button-visible-frominvisible-on-a-successful-event-happend-in-anot/22339003?noredirect=1#comment33951077_22339003 but it is not working as I want. Can you please look into this ? – user3399423 Mar 12 '14 at 18:51