Question

I have buttons V1-V8 visible and R1-R8 invisible initially in my activity_main.xml file

My MainActivity.java file

package com.example.buttonvtor;

import com.example.buttonvtor.MainActivity;
import com.example.buttonvtor.Payment1Activity;
import com.example.buttonvtor.Payment2Activity;
import com.example.buttonvtor.Payment3Activity;
import com.example.buttonvtor.Payment4Activity;
import com.example.buttonvtor.Payment5Activity;
import com.example.buttonvtor.Payment6Activity;
import com.example.buttonvtor.Payment7Activity;
import com.example.buttonvtor.Payment8Activity;
import com.example.buttonvtor.R;
import android.os.Bundle;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends Activity implements OnClickListener{

Button bv1;
Button bv2;
Button bv3;
Button bv4;
Button bv5;
Button bv6;
Button bv7;
Button bv8;
Button br1;

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

    bv1 = (Button)findViewById(R.id.button1);
    bv2 = (Button)findViewById(R.id.button2);
    bv3 = (Button)findViewById(R.id.button3);
    bv4 = (Button)findViewById(R.id.button4);
    bv5 = (Button)findViewById(R.id.button5);
    bv6 = (Button)findViewById(R.id.button6);
    bv7 = (Button)findViewById(R.id.button7);
    bv8 = (Button)findViewById(R.id.button8);
            br1 = (Button)findViewById(R.id.r1);
    bv1.setOnClickListener(this);
    bv2.setOnClickListener(this);
    bv3.setOnClickListener(this);
    bv4.setOnClickListener(this);
    bv5.setOnClickListener(this);
    bv6.setOnClickListener(this);
    bv7.setOnClickListener(this);
    bv8.setOnClickListener(this);


}

@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main, menu);
    return true;
}


public void onClick(View v) {
switch(v.getId()){

case R.id.button1:
Toast.makeText(MainActivity.this,"You clicked on block 1",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment1Activity.class),1);
break;

case R.id.button2:
Toast.makeText(MainActivity.this,"You clicked on block 2",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment2Activity.class),2);
break;

case R.id.button3:
Toast.makeText(MainActivity.this,"You clicked on block 3",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment3Activity.class),3);
break;

case R.id.button4:
Toast.makeText(MainActivity.this,"You clicked on block 4",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment4Activity.class),4);
break;

case R.id.button5:
Toast.makeText(MainActivity.this,"You clicked on block 5",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment5Activity.class),5);
break;

case R.id.button6:
Toast.makeText(MainActivity.this,"You clicked on block 6",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment6Activity.class),6);
break;

case R.id.button7:
Toast.makeText(MainActivity.this,"You clicked on block 7",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment7Activity.class),7);
break;

case R.id.button8:
Toast.makeText(MainActivity.this,"You clicked on block 8",Toast.LENGTH_LONG).show();
startActivityForResult(new Intent(MainActivity.this,Payment8Activity.class),8);
break;
     }

    }

 protected void onActivityResult (int requestCode, int resultCode, Intent data)
  {
    if (requestCode == 1)
    {// here you match the number you sent in startActivityForResult
        if (resultCode == RESULT_OK)
       {
            bv1.setVisibility(View.INVISIBLE);
            br1.setVisibility(0);


        }
             // do something
    }
      }
  }

Once I click on say button V1 it goes to the next activity called Payment1Activity.java and checks the users validity. If a user is valid and the payment is successful it comes back to my MainActivity.java.

Payment1Activity.java

package com.example.buttonvtor;

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 Payment1Activity 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_payment1);
    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(Payment1Activity.this, "", 
                    "Validating user...", true);
             new Thread(new Runnable() {
                    public void run() {
                        payment();                        
                    }
                  }).start();               
        }
    });
}

void payment(){
try{            

httpclient=new DefaultHttpClient();
httppost= new HttpPost("my url"); 
        //add your data
nameValuePairs = new ArrayList<NameValuePair>(2);
nameValuePairs.add(new BasicNameValuePair("accno",et.getText().toString().trim())); 
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(Payment1Activity.this,"Payment Successful", Toast.LENGTH_SHORT).show();
 Toast.makeText(Payment1Activity.this,"You reserved block 1",Toast.LENGTH_LONG).show(); 
                }
    });
    Intent returnIntent = new Intent();
returnIntent.putExtra("Payment Successful",RESULT_OK);
setResult(RESULT_OK,returnIntent);     
finish();

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

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

So I want something such that when my Payment1Activity.java says payment successful and redirects a user to main page at that time my V1 Button should get invisible and R1 should get visible. I tried different permutations and combinations to achieve this but it did not help. Please help. Any suggestions or advices will be highly appreciated. Thank you.

Was it helpful?

Solution

In your main Activity use startActivityForResult instead of startActivity. This allows you to return a value from the new Activity to the Main Activity. For example:

In main Activity

startActivityForResult(new Intent(MainActivity.this,Payment3Activity.class), 3);

Here, number 3 is a number you define that allows you to identify the result later. In your Payment#Activity you would have at some point (for example when you disimiss your Activity)

setResult(RESULT_OK); // or any result you want

The result is sent to the MainActivity, and you must override onActivityResult() to get the result.

protected void onActivityResult (int requestCode, int resultCode, Intent data)
{
    if (requestCode == 3) // here you match the number you sent in startActivityForResult
        if (resultcode == RESULT_OK)
             // do something
}

OTHER TIPS

You can start the second Activity with startActivityForResult() and catch the result in your first activity in onActivityResult() like e.g. here.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top