I have a gridview in my layout. I have added three imageviews to this gridview.
I have the following requirement. By default when I launch or resume the activity, I need to have an imageview selected by default. How can I accomplish that? Placing gridView.setSelection(1); in onResume() have no effect ie; the imageview does not get selected on launching or resume, I wonder why! Can someone please let me know why this does not get highlighted?
Following is my main activity and adapter class.
public class MyActivity extends Activity
{
GridView gridView;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
// set up the onClick listener
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Log.d("Pos", "setOnItemClickListener() - pos:" + position);
handleItemClick(position);
}
});
}
public void onResume() {
super.onResume();
gridView.setSelection(1);
}
private void handleItemClick(int position) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
switch(position) {
case 0: // Phone
intent.setClassName("com.android.contacts",
"com.android.contacts.DialtactsActivity");
break;
case 1: // People
intent.setClassName("com.android.contacts",
"com.android.contacts.activities.PeopleActivity");
intent.setAction("com.android.contacts.action.LIST_CONTACTS");
break;
case 2: // Places
intent.setClassName("com.android.contacts",
"com.android.contacts.DialtactsActivity");
break;
default:
break;
}
startActivity(intent);
}
}
Adapter class
public class ImageAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in an array
public Integer[] mThumbIds = {
R.drawable.call,
R.drawable.contacts,
R.drawable.music,
};
public ImageAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) {
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(350, 350));
} else {
imageView = (ImageView) convertView;
}
imageView.setImageResource(mThumbIds[position]);
imageView.setId(position);
return imageView;
}
}
You can try this:
public void onResume() {
super.onResume();
Thread.sleep(1);
gridview.requestFocusFromTouch();
gridview.setSelection(1);
}
or also this can help:
public void onResume() {
super.onResume();
mGridView.setSelection(1);
mGridView.requestFocusFromTouch();
mGridView.setSelection(1);
}
Just a guess may work
setContentView(R.layout.main);
gridView = (GridView) findViewById(R.id.grid_view);
// Instance of ImageAdapter Class
gridView.setAdapter(new ImageAdapter(this));
gridView.setSelection(1); <--- try to add this here
Related
i want when i click image from gridview, image send to new layout (Details Image)
this is MainActivity
public class MainActivity extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new ImageAdapter(this));
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Intent i = new Intent(getApplicationContext(), SingleViewActivity.class);
i.putExtra("arg3", arg2);
startActivity(i);
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "okey", Toast.LENGTH_SHORT).show();
}
});
}
ImageAdapter
public class ImageAdapter extends BaseAdapter {
public static final String URL ="http://api.androidhive.info/json/movies/";
Context mContext;
int mThumbIds = 18;
public ImageAdapter(Context c) {
mContext = c;
}
public int getCount() {
return mThumbIds;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new ImageView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView;
if (convertView == null) { // if it's not recycled, initialize some attributes
imageView = new ImageView(mContext);
imageView.setLayoutParams(new GridView.LayoutParams(95, 95));
// imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
imageView.setPadding(4, 4, 4, 4);
} else {
imageView = (ImageView) convertView;
}
Picasso.with(this.mContext)
.load(URL + position +".jpg")
// .placeholder(R.drawable.loader).error(R.drawable.ic_launcher).fit()
.into(imageView);
// imageView.setImageResource(mThumbIds[position]);
return imageView;
}
// references to our images }
in here i try get image from gridview, i try use intent, it's not work
Details Image
public class SingleViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.single_view);
Intent i = getIntent();
int position = i.getExtras().getInt("arg3");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.SingleView);
imageView.setImageResource(imageAdapter.getItemViewType(position));}}
can anyone help me
Two points:
Pass the URL of the image you selected from MainActivity to SingleViewActivity.
Use ImageLoader, and do not modify the width/heigth otherwise the memory cache will miss.
Others the cache will do everything for you.
I have a GridView with some images that when its clicked show in full screen.
But when I'm clicking image the app crash for Target must not be null :(
My Activity:
public class ArenaActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arena);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
GridView gv = (GridView) findViewById(R.id.grid_view);
gv.setAdapter(new ArenaGridViewAdapter(this));
gv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Intent i = new Intent(ArenaActivity.this, ArenaViewActivity.class);
i.putExtra("position", position);
startActivity(i);
}
});
}
}
My Adapter:
public class ArenaGridViewAdapter extends BaseAdapter {
private Context context = null;
public ArenaGridViewAdapter(Context context) {
this.context = context;
}
public int[] arenaImgs = {
R.drawable.ic_arena_01,
R.drawable.ic_arena_02,
R.drawable.ic_arena_03,
R.drawable.ic_arena_04,
R.drawable.ic_arena_05,
R.drawable.ic_arena_06
};
#Override
public View getView(int position, View convertView, ViewGroup parent) {
SquaredImageView view = (SquaredImageView) convertView;
if (view == null) {
view = new SquaredImageView(context);
view.setScaleType(CENTER_CROP);
}
Picasso.with(this.context) //
.load(arenaImgs[position]) //
.placeholder(R.drawable.ic_placeholder) //
.error(R.drawable.ic_error) //
.fit() //
.into(view);
return view;
}
#Override
public int getCount() {
return arenaImgs.length;
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
}
And Finally the Fullscreen:
public class ArenaViewActivity extends ActionBarActivity {
public int[] arenaImgs = {
R.drawable.ic_arena_01,
R.drawable.ic_arena_02,
R.drawable.ic_arena_03,
R.drawable.ic_arena_04,
R.drawable.ic_arena_05,
R.drawable.ic_arena_06
};
private ShareActionProvider mShareActionProvider;
private int urlshare;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_arena);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
Intent i = getIntent();
int position = i.getExtras().getInt("position");
this.urlshare = position;
ImageView iv = (ImageView) findViewById(R.id.arena_full_view);
Picasso.with(getApplicationContext())
.load(arenaImgs[position])
.fit()
.centerCrop()
.into(iv);
}
}
The error log says that the Target null is in: Picasso.with(getApplicationContext())
I solved the problem. Was a error in layout of fullscreen.
Changed to: setContentView(R.layout.arena_full_view);
Thanks everyone.
i have few images that i am displaying in gridview. what i want is when i click that image it should be displayed in full screen. i have also put viewpager to have sliding functionality. the problem that i am having is when i click on an image the first image is displayed(not that one).
he is my main activity
public class Ppdtsample extends Activity {
GridView grid;
static int[] imageId = { R.drawable.ppdt1, R.drawable.ppdt2,
R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
R.drawable.ppdt6, R.drawable.ppdt7, };
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ppdtsamples);
ImageAdapter adapter = new ImageAdapter(Ppdtsample.this, imageId);
grid=(GridView)findViewById(R.id.grid_view);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// TODO Auto-generated method stub
Intent i = new Intent(getApplicationContext(), FullImageActivity.class);
// passing array index
i.putExtra("id", position);
startActivity(i);
}
});
}
}
this is my activity to display in full screen with sliding functionality:
public class FullImageActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
// passing array index
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setBackgroundResource(0);
SlideImage adapter = new SlideImage(this);
viewPager.setAdapter(adapter);
}
}
and this is my class for viewpager
public class SlideImage extends PagerAdapter {
Context context;
private int[] GalImages = { R.drawable.ppdt1, R.drawable.ppdt2,
R.drawable.ppdt3, R.drawable.ppdt4, R.drawable.ppdt5,
R.drawable.ppdt6, R.drawable.ppdt7, };
SlideImage(Context context) {
this.context = context;
}
#Override
public int getCount() {
return GalImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
int padding = context.getResources().getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(GalImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
it is displaying the first image and not the image i click because i am not able to pass the 'position' of the clicked image. it is taking the position from this method in SlideImage.class
Object instantiateItem(ViewGroup container, int position)
which is the first position. i want to pass the position from fullImageActivity.class to the position in this line
imageView.setImageResource(GalImages[position]);
which is on SlideshowActivity.
This may look complicated but i just want to pass the value of position from FullImageActivity.class to SlideImage.class. Please help me
Your Ppdtsample looks strange.Here I tried to modify your Ppdtsample class,which worked for me:
public class Ppdtsample extends Activity {
GridView grid;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.grid_layout);
grid = (GridView) findViewById(R.id.grid_view);
// Instance of SlideImage Class
gridView.setAdapter(new SlideImage(this));
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
Intent i = new Intent(getApplicationContext(),FullImageActivity.class);
i.putExtra("id", position);
startActivity(i);
}
});
}
}
And the reason you can't pass the id is because you set the wrong adapter in Ppdtsample.It should be SlideImage in you code, not ImageAdapter.
I am trying to develop a application which contain a gallery with view flipper.
Gallery shows all the images.What I want to do is, the image which is shown in the view flipper should be highlighted in the gallery.
Currently I am able to highlight it on onClick.But not able to make communication between both.
I tried using following method
viewflipper.getDisplayedChild();
but it doesn't work.
following is my code
public class SliderActivity extends Activity
{
int mFlipping = 0 ;
public int currentimageindex=0;
Timer timer;
TimerTask task;
ImageView slidingimage;
android.widget.ViewFlipper flipper;
Gallery gallery;
private int[] IMAGE_IDS =
{
R.drawable.android0, R.drawable.android1, R.drawable.android2,R.drawable.android3,R.drawable.android4
};
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.mygame);
flipper = (android.widget.ViewFlipper) findViewById(R.id.flipper1);
for(int i=0;i<IMAGE_IDS.length;i++)
{
// This will create dynamic image view and add them to ViewFlipper
ImageView image = new ImageView(getApplicationContext());
image.setBackgroundResource(IMAGE_IDS[i]);
flipper.addView(image);
}
gallery=(Gallery)findViewById(R.id.gallery1);
gallery.setAdapter(new ImageAdapter(this));
if(mFlipping==0){
/** Start Flipping */
flipper.startFlipping();
mFlipping=1;
//mButton.setText(R.string.str_btn_stop);
}
else{
/** Stop Flipping */
flipper.stopFlipping();
mFlipping=0;
// mButton.setText(R.string.str_btn_start);
}
}
public class ImageAdapter extends BaseAdapter
{
int mGalleryItemBackground;
private Context mContext;
public ImageAdapter(Context c)
{
mContext = c;
TypedArray a = obtainStyledAttributes(R.styleable.Theme);
mGalleryItemBackground = a.getResourceId(
R.styleable.Theme_android_galleryItemBackground,0);
a.recycle();
}
public int getCount()
{
return IMAGE_IDS.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 i = new ImageView(mContext);
i.setImageResource(IMAGE_IDS[position]);
i.setLayoutParams(new Gallery.LayoutParams(150, 100));
i.setScaleType(ImageView.ScaleType.FIT_XY);
i.setBackgroundResource(mGalleryItemBackground);
return i;
}
}
public void onClick(View v) {
finish();
android.os.Process.killProcess(android.os.Process.myPid());
}
}
If I understand your question... I think you need to use setOnItemSelectedListener...
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapter, View gallery,
int position, long arg3) {
myPosition = position;
//tell your flipper something useful here using the current position of the gallery
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
// TODO Auto-generated method stub
}
});
And talk to your flipper from in the onItemSelected callback.
public class RenkKorluguTesti extends Activity {
/** Called when the activity is first created. */
ImageView imageView1;
EditText editText1=null;
Button button1;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.galeri);
imageView1 = (ImageView) findViewById(R.id.imageView1);
editText1 = (EditText) findViewById(R.id.editText1);
button1 = (Button) findViewById(R.id.button1);
Gallery gallery = (Gallery) findViewById(R.id.gallery);
gallery.setAdapter(new ImageAdapter(this));
gallery.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView parent, View v, int position, long id) {
switch(position) {
case 0:{
imageView1.setImageResource(R.drawable.plate01);
break;
}
case 1: imageView1.setImageResource(R.drawable.plate02); break;
case 2: imageView1.setImageResource(R.drawable.plate03); break;
}
//Toast.makeText(RenkKorluguTesti.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
public void tikla(View v) {
if(editText1.getText().toString()=="12")
editText1.setText("yov");
//Toast.makeText(RenkKorluguTesti.this, "Birinci testi geƧtiniz.",Toast.LENGTH_SHORT).show();
}
public class ImageAdapter extends BaseAdapter {
int mGalleryItemBackground;
private Context mContext;
private Integer[] mImageIds = {
R.drawable.plate01,
R.drawable.plate02,
R.drawable.plate03,
};
public ImageAdapter(Context c) {
mContext = c;
TypedArray attr = mContext.obtainStyledAttributes(R.styleable.RenkKorluguTesti);
mGalleryItemBackground = attr.getResourceId(
R.styleable.RenkKorluguTesti_android_galleryItemBackground, 0);
attr.recycle();
}
public int getCount() {
return mImageIds.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 imageView = new ImageView(mContext);
imageView.setImageResource(mImageIds[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(150, 100));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setBackgroundResource(mGalleryItemBackground);
return imageView;
}
}
}
I want to make a Toast if the text written is equal 12 in EditText box.
I used editText1.getText().toString() method but it seems not working.
What's wrong? Could you please check public void tikla() method? Thx.
On Java you cannot do this kind of comparation:
if(editText1.getText().toString()=="12")
You need to do:
if(editText1.getText().toString().equalsIgnoreCase("12"))
or
if(editText1.getText().toString().compareTo("12")==0)
This comparison in your code is not correct.
(editText1.getText().toString()=="12")
Comparing Strings is done via equals() not == operator.
You can do something like this:
if (editText1.getText().toString().compareTo("12") == 0)
using the java.lang.String.compareTo(java.lang.String) method.