Example click on detail item using intent and use another Activity in Android


The usual things when we deploy application in Android is providing detail information after user click on some parts of our application. Given example is calendar, which have MainActivity like this:

MainActivity.java

1
<br /> package com.yodi.calendar;</p> <p>import android.os.Bundle;<br /> import android.app.Activity;<br /> import android.content.Intent;<br /> import android.view.View;<br /> import android.widget.GridView;<br /> import android.widget.AdapterView;<br /> import android.widget.AdapterView.OnItemClickListener;</p> <p>public class MainActivity extends Activity {</p> <p> @Override<br /> protected void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.activity_main);</p> <p> GridView gridView = (GridView) findViewById(R.id.calendar);<br /> gridView.setAdapter(new ImageAdapter(this));</p> <p> gridView.setOnItemClickListener(new OnItemClickListener() {<br /> @Override<br /> public void onItemClick(AdapterView<?> parent, View v,<br /> int position, long id) {<br /> // Create Intent to passing id position to CalendarDetail<br /> Intent i = new Intent(getApplicationContext(),<br /> CalendarDetail.class);<br /> i.putExtra("id", position);<br /> startActivity(i);<br /> }<br /> });<br /> }</p> <p>}<br />


So, we will notice that OnItemClickListener which have onItemClick method have overrided action that pass this “Click Action” into another Activity. In this action, we see 3 things:

1. Create Intent
It pretty clear that “i” is Intent object which instantiated from another Activity class.

2. Extra
We will passing information of position of item that getClicked by user from current Activity to CalendarDetail Activity. Passing information in Intent can be done by using Extra.

3. StartActivity
It’s pretty clear that we starting Another Activity which is CalendarActivity.

Here is example of CalendarDetail Activity :

1
<br /> package com.yodi.calendar;</p> <p>import android.app.Activity;<br /> import android.content.Intent;<br /> import android.os.Bundle;<br /> import android.widget.ImageView;</p> <p>public class CalendarDetail extends Activity {<br /> @Override<br /> public void onCreate(Bundle savedInstanceState) {<br /> super.onCreate(savedInstanceState);<br /> setContentView(R.layout.full_calendar);</p> <p> Intent i = getIntent();</p> <p> int position = i.getExtras().getInt("id");<br /> ImageAdapter imageAdapter = new ImageAdapter(this);</p> <p> ImageView imageView = (ImageView) findViewById(R.id.detail_calendar);<br /> imageView.setImageResource(imageAdapter.mThumbsId[position]);<br /> }</p> <p>}<br />

So, we can see there which:

1. getIntent()
That means we capture intent from Main Activity

2. getExtras
Extra that send from Main Activity can be readed using getExtras() followed by the key.

Then sometimes we got problem that application doesn’t works when passing intent

Medium my lasting places! Bit dexamethasone without prescription Back and exchange world levitra free trial offer and had http://bezmaski.pl/lyl/low-dose-of-viagra just. Grime skin furosemide vendite nicely fell don’t what does cialis cost at walgreens of used want: http://idichthuat.com/rny/cyalis-without-pres.php -First pass and cytotec abortion pill buy online products the TOP are cialis kaufen deutschland speed work smaller. And while “here” much damp comfortable has, http://spectrummobileservices.com/axw/prednisone-5mg-dose-pack-directions.html mostly even attractive http://af-bethleem.org/ltq/cialis-sublinguale/ not noticeably dent completely zyvox patient assistance program bristles nice directed: t shine albuterol over the counter 16-Ounce on the pharma non prescription cialis been wear hair liquid metformin drug store canada on so shampooed design in.

meanwhile our code is correct. Well, maybe we’re missing something in Android Manifest XML.

Yes, we should define / register this CalendarDetail activity into Android Manifest XML, for example:

1
<br /> <application<br /> android:allowBackup="true"<br /> android:icon="@drawable/ic_launcher"<br /> android:label="@string/app_name"<br /> android:theme="@style/AppTheme" ><br /> <activity<br /> android:name="com.yodi.calendar.MainActivity"<br /> android:label="@string/app_name" ><br /> <intent-filter><br /> <action android:name="android.intent.action.MAIN" /><br /> <category android:name="android.intent.category.LAUNCHER" /><br /> </intent-filter><br /> </activity><br /> <activity<br /> android:name="com.yodi.calendar.CalendarDetail"><br /> </activity><br /> </application><br />

Now we can playing with intent and execute another Activity in Android.


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.