Monday, 8 September 2014

Navigation in Android

There are two primary forms of intents you will use, They are:

  • Explicit Intent
  • Implicit Intent

Explicit Intent:
 After writing a single Activity, There comes a need  to navigate to another activity to perform another task . For this we use Explicit Inents.


activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="111dp"
        android:text="Call Act2"
        android:textSize="20dp" />

</RelativeLayout>

act2.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/textView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="This is Activity2"
        android:textAppearance="?android:attr/textAppearanceLarge" />

    <Button
        android:id="@+id/button1back"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Back" />

</LinearLayout>


MainActivity.java:
public class MainActivity extends Activity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button b = (Button)findViewById(R.id.button1);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent in = new Intent(getApplicationContext(), Act2.class);
startActivity(in);
}
});
}

}

Act2.java:
public class Act2 extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);

setContentView(R.layout.act2);
Button b = (Button)findViewById(R.id.button1back);
b.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
//startActivity(new Intent(getApplicationContext(), MainActivity.class));
finish();
}
});
}
}


AndroidManifest.xml:

  <activity android:name="Act2"></activity>



Implicit Intent:


 Implicit Intents do not specify the java class which should be called .
They specify the action which should be performed and optionally an URI which should be used for this action.
For example following tell the android system to view a webpage.
Intent intent = new Intent(Intent.ACTION_VIEW,URI.Parse("http://www.google.com"));



activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <Button
        android:id="@+id/googleButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="152dp"
        android:text="Google" />

</RelativeLayout>

public class MainActivity extends Activity {

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

Button gbtn= (Button)findViewById(R.id.googleButton);

gbtn.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

//Intent intent = new Intent(Intent.ACTION_VIEW,URI.Parse("http://www.google.com"));

Intent webIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com"));
startActivity(webIntent);

}

});
}

 
}


No comments:

Post a Comment