Advertisemnet

Earn Extra Money At Home. Starting from Now!!!

Thursday, March 29, 2012

Android Chapter 6 : Passing Values

Hello everyone, welcome back. This is Android Chapter 6 : Passing Values. I'm Mihiran Rupasinghe. As I said earlier today I'm going to show you how you can pass values from one activity to another.

Its very simple.

I think you remember this code.


Button btn.(Button)findViewById(R.id.btnStart);
btn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {

Intent intent = new Intent(SolutionDudeActivity.this,Login.class);
startActivity(intent);
});


When I press the START button in my main page, I can move to the Login screen. 

Now think, I want to display my name in welcome screen when I logged in to the application. I'm using putExtra and getExtra methods. All I want to do is modify the code like below.

Intent intent = new Intent(Login.this,Welcome.class);
intent.putExtra("user", name.getText().toString());
startActivity(intent);

You can use "putExtra" method to pass values. First parameter is to identify what you are passing. In here I'm getting the string value of EditTextBox and pass it to welcome screen. I have to do one thing. That is change the code of Welcome.java to catch the passed value.

package com.solution.dude;
import android.app.Activity;
public class Welcome extends Activity {
@Override
protected void 
onCreate(Bundle savedInstanceState)

super.onCreate(savedInstanceState);
setContentView(R.layout.weLcome);
TextView tv.(TextView)findViewById(R.id.user);
tv.setText("Hello " + getIntent().getExtras().getString("user") + ",");

I have a textview called user and Log out button at top of my Welcome.xml.
And the output will be appeared like this.



And that's it.
Next chapter we'll look at how to create a simple loading screen..
See you in the next chapter.

2 comments:

  1. Thanks for this post its really interesting i bookmark your blog for future stuff like this..
    2004 Mini Cooper AC Compressor

    ReplyDelete