import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Gallery1 extends Activity {
/** Called when the activity is first created. */
Button b1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
b1=(Button)findViewById(R.id.button1);
b1.setOnClickListener(new android.view.View.OnClickListener()
{
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent i=new Intent(getApplicationContext(),Gallery2.class);
startActivity(i);
}
});
}
}
Gallery
import android.app.Activity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Gallery;
import android.widget.ImageView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;
public class Gallery2 extends Activity {
ImageView im;
Gallery g;
Integer i[]={R.drawable.watch,R.drawable.face,R.drawable.crane};
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
g=(Gallery)findViewById(R.id.gallery);
g.setAdapter(new ShowAdapter(this));
g.setOnItemClickListener(new OnItemClickListener()
{
@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(),"picture " +(arg2+1)+ "selected",Toast.LENGTH_LONG).show();
im=(ImageView)findViewById(R.id.imageView1);
im.setImageResource(i[arg2]);
}
});
}
public class ShowAdapter extends BaseAdapter {
Context c;
int itembackground;
public ShowAdapter(Context con)
{
c=con;
//TypedArray a = obtainStyledAttributes(R.styleable.Gallery1);
//itembackground=a.getResourceId(R.styleable.Gallery1_android_galleryItemBackground,0);
//a.recycle();
}
public int getCount() {
// TODO Auto-generated method stub
return i.length;
}
@Override
public Object getItem(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public long getItemId(int position) {
// TODO Auto-generated method stub
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
ImageView iv=new ImageView(c);
iv.setImageResource(i[position]);
iv.setScaleType(ImageView.ScaleType.FIT_CENTER);
iv.setBackgroundResource(itembackground);
iv.setLayoutParams(new Gallery.LayoutParams(150, 120));
/*im=(ImageView)findViewById(R.id.image);
im.setImageResource(i[position]);*/
return iv;
}
}}
No comments:
Post a Comment