Android How can I change image on gridview by position? - android

I have a slide puzzle game.
I want to change image on GridView by position(id).
How could I do that?

Try this
gridView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position, long arg3)
{
// You can get position of item or imageView and again you can set new image
// by setBackGround() by getting this position
}
});

Related

How to unselect an item in gridview on second click in android?

I was trying to give background colour to selected items on GridView and I did it successfully using the following code-
gv.setOnItemClickListener(new OnItemClickListener() { // gv is object of GridView
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
gv.getChildAt(arg2).setBackgroundColor(Color.rgb(125, 125, 125));
}
});
Now I want to remove the given background colour when clicked on each item the next time. How can I do it ? Also, when clicked again the background colour should appear and on next click background colour should be removed.
You can check the current color background and then perform some conditional operation to update the view accordingly.
gv.setOnItemClickListener(new OnItemClickListener() { // gv is object of GridView
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
View view = gv.getChildAt(arg2);
int desiredBackgroundColor = android.graphics.Color.rgb(125, 125, 125);
ColorDrawable viewColor = (ColorDrawable) view.getBackground();
if(viewColor == null) {
view.setBackgroundColor(desiredBackgroundColor);
return;
}
int currentColorId = viewColor.getColor();
if(currentColorId == desiredBackgroundColor) {
view.setBackgroundColor(Color.TRANSPARENT);
} else {
view.setBackgroundColor(desiredBackgroundColor);
}
}
});

OnClick Listner of images in Grid View Android

I have a Grid View that have 4 images .So i want to identify which image has been clicked so that corresponding to that i can start a new activity .
So please help me how can i get this
I have tried this
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
}
});
getView can be used for this purpose.for this in getView find View and apply onClicklistner their.to make all views clickable you need to setFocusable(false) on all focusable views.
I have got the solution
dataView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View v, int arg2,
long arg3) {
// TODO Auto-generated method stub
Toast.makeText(
getApplicationContext(),
links[arg2], Toast.LENGTH_SHORT).show();
}
});
int arg2 in the OnItemClick method specifies the position.Using that you can get the item clicked.

Not Getting Click Event On List View

here is my click listner...
mListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.i("", "adfadf");
Toast.makeText(ScoreListActivity.this, "Clicked",Toast.LENGTH_SHORT).show();
}
});
Whats wrong in this code ....\
I am not Getting Click when I click an item of LISTVIEW...
Call the following code on your list items as they are created:
listItem.setDescendantFocusability(FOCUS_BLOCK_DESCENDANTS);

create Listview without using listactivity.only use activty

I know how can implement listview without using listactivity.But i dont know how get the clicked item from the listview.Please anyone tell me how listen the clicked item from the listview without using listactivity.
Please Follow this Link. Here you can find simple Listview example using Activity.
Using listview.setOnItemClickListener you can get Click on Listview.
listView.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
You can implements OnItemClickListener and can write the method like below or just simply use eclipse to implement unimplemented method
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id) {
}
First you have to make instance of list view Clickable like
lv.setClickable(true);
then
use following
lv.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View view, int position,
long id)
{
}
use ListView.
Then with the listview object invoke
lv.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
//do your stuff
}
}
for click on listview in activiyt try this code...
ListView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
//write you action here
}
});

display item of listview

I have a list view having several items and it is multichoice list.
I want to display the checked item of list view.
how i can do this.can anyone help me???
Try this
m_cObjList.setOnItemClickListener( new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
// This will highlight the select view
m_cObjAdapter.setSelected(arg2);
}
});

Categories

Resources