Exercise : 11
Understanding of UI :
a. Create an UI such that , one screen have list of all the types
of cars.
b. On selecting of any car name, next screen should show Car details
like : name , launched date ,company name, images(using gallery) if available,
show different colors in which it is available.Screen Shot:
XML :
main.xml :
main1.xml :
JAVA :
ElevenActivity.java :
package kmn.Eleven;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class ElevenActivity extends Activity implements OnItemClickListener
{
/** @author Y@@D */
ListView lv;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
lv=(ListView) findViewById(R.id.lv);
lv.setClickable(true);
lv.setOnItemClickListener(this);
}
public void onItemClick(AdapterView arg0, View arg1, int arg2, long arg3)
{
try
{
Integer itn=Integer.valueOf(arg2);
String ext=itn.toString();
Intent i = new Intent(this,CarInfo.class);
i.putExtra("CarPos", ext);
startActivity(i);
}
catch(Exception e)
{
Toast.makeText(this, e.getMessage()+" OnItemClick", Toast.LENGTH_LONG).show();
}
}
}
CarInfo.java :
package kmn.Eleven;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.TextView;
public class CarInfo extends Activity
{
/** @author Y@@D */
TextView tv;
private Gallery gallery;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
tv=(TextView) findViewById(R.id.tv);
Intent i=this.getIntent();
String str =i.getStringExtra("CarPos");
gallery = (Gallery) findViewById(R.id.examplegallery);
gallery.setAdapter(new AddImgAdp(this,str));
if(str.equals("0"))
{
tv.setText("Name:Maruti Suzuki 800"+"\n"+"Launched Date:1/3/1987"+"\n"+"Company Name:Maruti Suzuki"+"\nColors Available:\nWhite\nBlue\nRed\nLight Yellow");
}
else if(str.equals("1"))
{
tv.setText("Name:Hyundai Accent Executive"+"\n"+"Launched Date:15/10/2010"+"\n"+"Company Name:Hyundai"+"\nColors Available:\nGray\nOcean Blue\nMidnight Black\nMaganta Blue");
}
else if(str.equals("2"))
{
tv.setText("Name:Chevrolet Beat"+"\n"+"Launched Date:11/11/2011"+"\n"+"Company Name:Chevrolet"+"\nColors Available:\nMidnight Black\nAmazon Green\nRoyal Gold\nSport Red");
}
else
{
tv.setText("No car available");
}
}
}
class AddImgAdp extends BaseAdapter
{
int GalItemBg;
private Context cont;
String positionLast;
private Integer[] Imgidb = {R.drawable.b1, R.drawable.b2, R.drawable.b3, R.drawable.b4};
private Integer[] Imgidh = {R.drawable.h1, R.drawable.h2, R.drawable.h3, R.drawable.h4};
private Integer[] Imgidm = {R.drawable.m1, R.drawable.m2, R.drawable.m3, R.drawable.m4};
public AddImgAdp(Context c,String pos)
{
cont = c;
positionLast=pos;
TypedArray typArray = c.obtainStyledAttributes(R.styleable.GalleryTheme);
GalItemBg = typArray.getResourceId(R.styleable.GalleryTheme_android_galleryItemBackground, 0);
typArray.recycle();
}
public int getCount()
{
if(positionLast.equals("0"))
{
return Imgidm.length;
}
else if(positionLast.equals("1"))
{
return Imgidh.length;
}
else if(positionLast.equals("2"))
{
return Imgidb.length;
}
else
{
return Imgidm.length;
}
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent)
{
ImageView imgView = new ImageView(cont);
if(positionLast.equals("0"))
{
imgView.setImageResource(Imgidm[position]);
}
else if(positionLast.equals("1"))
{
imgView.setImageResource(Imgidh[position]);
}
else if(positionLast.equals("2"))
{
imgView.setImageResource(Imgidb[position]);
}
else
{
imgView.setImageResource(Imgidm[position]);
}
// Fixing width & height for image to display
imgView.setLayoutParams(new Gallery.LayoutParams(200, 200));
imgView.setScaleType(ImageView.ScaleType.FIT_XY);
imgView.setBackgroundResource(GalItemBg);
return imgView;
}
}

