Wednesday, July 6, 2011

Passing value to another Activity2

The CounterDemo class will pass a key/pair (name/"Your name here") to another class Activity2 via an Intent.
The Activity2 is refer as the TARGET activity.
The Activity2 may not be in the same project. Android will smart enough to look for it in your phone.


package com.ugrowit;

import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;

public class Activity2 extends Activity implements OnClickListener{
      
       private EditText txtUsername;
      
       @Override
       public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
              setContentView(R.layout.activity2);
              Button btnSubmit = (Button)findViewById(R.id.btnSubmit);
              btnSubmit.setOnClickListener(this);
             
              String defaultName = "";
             
              Bundle extras = getIntent().getExtras();
             

              if ( extras != null ) {
                     defaultName = extras.getString("Name");
              }
              txtUsername = (EditText) findViewById(R.id.txtUsername);
              txtUsername.setHint(defaultName);
             
       }

       @Override
       public void onClick(View arg0) {
             
              Intent data = new Intent();

              // Pass the data(user name) back to the calling Activity
              data.setData(
                           Uri.parse(txtUsername.getText().toString()));
             
              setResult(RESULT_OK, data);
             
              finish();
             
       }     
}




package com.test;

import android.app.Activity;
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.RadioButton;

public class CounterDemo extends Activity implements OnClickListener{
      
    private Button btnCount;
    private Button btnGo;
    private EditText txtCount;
    private RadioButton radioUp;
    private RadioButton radioDown;
    private int count = 0; // Initial count
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
       
        radioUp = (RadioButton)findViewById(R.id.radioUp);
        radioDown = (RadioButton)findViewById(R.id.radioDown);
       
        txtCount = (EditText)findViewById(R.id.txtCount);
        txtCount.setText(String.valueOf(count));
       
        // Retrieve a reference to the Button view by finding it by its id
        btnCount = (Button)findViewById(R.id.btnCount);
        btnCount.setOnClickListener(this);
       
        btnGo = (Button)findViewById(R.id.btnGo);
        btnGo.setOnClickListener(new OnClickListener(){
              @Override
              public void onClick(View arg0) {
                     Intent i = new Intent("com.ugrowit.ACTIVITY2");
                    
                     Bundle extras = new Bundle();
                     extras.putString("Name", "Your name here");
                     i.putExtras(extras);
                     startActivityForResult(i,1);
              }
        }
        );
    }

       @Override
       public void onClick(View arg0) {
              if(radioUp.isChecked()){
                     count++;
              }
              if(radioDown.isChecked()) {
                     count--;
              }
              txtCount.setText(String.valueOf(count));
             
             
       }
}

No comments:

Post a Comment