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.