Friday, May 25, 2012

Intend Example in Android

Here in this Example i am showing the use of intend(redirecting to another page and opening url)
<Button android:text="next page" android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
<Button android:text="uripage" android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content">
</Button>
Now find the id of button, set the intend in setOnClickListener event
Code for Redirecting to one page to another page
final Context context=this;
Button b=(Button)findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() 

@Override 
public void onClick(View v)
{ // TODO Auto-generated method stub 
Intent intent=new Intent(context,Example1.class);
startActivity(intent); 
}
});
Code for opening the webpage
Button b1=(Button)findViewById(R.id.button2);
b1.setOnClickListener(new View.OnClickListener()
{
@Override 
public void onClick(View v)
{ // TODO Auto-generated method stub
Intent browseintent=new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.google.com")); 
startActivity(browseintent);
}
});
Set the activity in manifest page

<activity android:label="@string/app_name" android:name=".Example1">
</activity>

No comments:

Post a Comment