Latest

Powered by Blogger.

Android Intents -Explicit – Tutorial



Android Intents -Explicit – Tutorial

Android intents are very important and they are used to trigger an event or action. For example calling one activity from another activity or its attributes just like in below screenshots…

____________________________________________


layout_thumb[1]

____________________________________________


In above screenshot when you click on button “Select Color ” then activity named as “colorSelectorActivity” is called. A person would select its specified color and when he clicks on “ok” button in “colorSelectorActivity” activity then he will be directed back to the main Activity. This is all done by Intents. There are two types of Intents named as Explicit and Implicit Intents. In this tut I am gonna show you Explicit Intent example. So lets get started …

 Steps for Evaluation

____________________________________________


Step 1 :  Firs of all create a  new Android project with a  name called “ExplicitIntents“.
Step 2 :  In your default package create another activity named as “Activity_2“.
Step 3 :  Rename your “MainActivity” to “Activity_1“.
Step 4 : Now rename your “activity_main.xml” to “activity_1.xml“. and create another xml layout with a name “activity_2.xml.
Step 5 :  Now create a button inside your main XML layout with a name “Go to Second Activity“.
Step 6 :  Now give an id to the button like “navigate“.
Step 7  : Now paste the following code inside your “Activity_1.java” file.

____________________________________________


 Code

____________________________________________

package com.baidar.explicitintent;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;

public class Activity_1 extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_1);
//when a user would click on the button , intent would trigger the second activity
Button button = (Button) findViewById(R.id.navigate);
button.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View arg0) {
//Transition from activity 1 to activity 2
Intent intent = new Intent(Activity_1.this, Activity_2.class);
startActivity(intent);

}
});
}
@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;
}

}

____________________________________________


Step 8 : Now add some text to the “activity_2.xml” file by dragging a “TextView” view to the layout. You can write “Welcome ! this is the second activity” or you can write something else.

____________________________________________


 Download the Project

____________________________________________


black-and-white-download-now-button

____________________________________________

- Reference : You can find the same blog at  http://baidartuts.altervista.org/android-intents-tutorial/#sthash.6gcn2bn2.dpuf
  • Blogger Comments
  • Facebook Comments
Item Reviewed: Android Intents -Explicit – Tutorial Description: Rating: 5 Reviewed By: Unknown
Scroll to Top