Download this Blogger Template by Clicking Here!

Ad 468 X 60

Share

Widgets

Thursday, December 15, 2011

Widgets

Working with Intent in Android :


# We have to create an "Intent"  for moving one activity to another activity  :



  • Create an xml file named as "second.xml" and copy and paste below code to this xml file.
  • Create a class file under "src" folder named as "SecondExampleActivity" and renamed first class file to "IntentExampleActivity".
  • Pick two images file and copy this in res/drawable folder.(for first and second activity)
// Source Code of  main.xml :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

     <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       
    <TableRow >    
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    </TableRow>
    <TableRow >
    <Button 
        android:id="@+id/btnNext"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go to Second Activity"/>
    </TableRow>
   <TableRow >
   <ImageView 
       android:id="@+id/first"
       android:src="@drawable/first"
       android:layout_width="280px"
       android:layout_height="320px"/>
    </TableRow>
    </TableLayout>
    </LinearLayout>


// Source Code of  second.xml :

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >
   
     <TableLayout
        xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content">
       
    <TableRow >
    <TextView
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="@string/hello" />
    </TableRow>
    <TableRow >
    <Button 
        android:id="@+id/btnEnd"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:text="Go to first activity" />
    </TableRow>
    <TableRow >
    <ImageView 
        android:id="@+id/second"
        android:src="@drawable/second"
        android:layout_width="280px"
        android:layout_height="320px"/>
    </TableRow>
    </TableLayout>
</LinearLayout>



// Source Code of  IntentExampleActivity.java :

package com.myApp.IntentExample;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;

public class IntentExampleActivity extends Activity
{
    /** Called when the activity is first created. */
Button btnNext;
    @Override
    public void onCreate(Bundle savedInstanceState)
    {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
       
         Button btnNext= (Button) findViewById(R.id.btnNext);
         btnNext.setOnClickListener(new View.OnClickListener()
         {
public void onClick(View arg0)
{
// TODO Auto-generated method stub
Intent firstintent=new Intent(IntentExampleActivity.this,SecondExampleActivity.class);
startActivity(firstintent);
}
         });
     }
}

// Source Code of  SecondExampleActivity.java :

package com.myApp.IntentExample;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.content.Intent;

public class SecondExampleActivity extends Activity {
    /** Called when the activity is first created. */
Button btnEnd;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.second);
       
        Button btnEnd=(Button) findViewById(R.id.btnEnd);
        btnEnd.setOnClickListener(new View.OnClickListener() {

public void onClick(View v) {
// TODO Auto-generated method stub
Intent secondintent=new Intent(SecondExampleActivity.this,IntentExampleActivity.class);
startActivity(secondintent);

}
});
       
}
   
}


Output :
First Activity


Second Activity







SHARE THIS POST   

  • Facebook
  • Twitter
  • Myspace
  • Google Buzz
  • Reddit
  • Stumnleupon
  • Delicious
  • Digg
  • Technorati
Author: Mohammad
Mohammad is the founder of STC Network which offers Web Services and Online Business Solutions to clients around the globe. Read More →

0 comments: