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);
}
}
});
Related
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
}
});
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.
I have a android file explorer application and I need to highlight the selected item. I could select touched items using v.setBackgroundColor() method inside onListItemClick(). But when I touch another file/folder still previous one highlighted. I need only current touched item display as selected. How to do this?
not perfect but works good
loop through all items of list and try something like this
list.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
for (int i = 0; i < arg0.getCount(); i++) {
View view = arg0.getChildAt(i);
if (i == arg2) {
view.setBackgroundColor(Color.RED);
} else {
view.setBackgroundColor(Color.GREEN);
}
}
}
});
I want to change the color of XML based on the value selected in the spinner. Here is the code that i had tried.
public class MainActivity extends Activity implements OnItemClickListener{
Spinner obj;
String[] str={"Red","Green","Yellow","Gray"};
ArrayAdapter<String> adapter;
ViewGroup vg;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
obj=(Spinner)findViewById(R.id.spinner);
vg=(ViewGroup)findViewById(R.id.relative);
adapter=new ArrayAdapter<String>(getBaseContext(),android.R.
layout.simple_dropdown_item_1line,str);
obj.setAdapter(adapter);
obj.setOnItemClickListener(this);
}
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String color=obj.getSelectedItem().toString();
if(color=="Red")
{
}
}
}
Iam trying to use setBackground method but it gives me error
Try below code!
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {
String color=str[arg2]; //where arg2 is position of selected item
if(color=="Red")
{
View someView = findViewById(R.id.randomViewInMainLayout);
// Find the root view
View root = someView.getRootView()
// Set the color
root.setBackgroundColor(Color.RED);
}
}
Make sure the xml has a relative layout, or any main layout. On Item selected call, get the layout, and use setbackground(getResources().getColor(R.color.color_name_here).
Hope that helps.!
get the id og your main layout like
Yourlayout(linear or realtive) layout = (Yourlayout)findviewbyid(R.id.yourlayoutid);
on on click of spinner
if(color=="Red")
{
layout.setbackground(getResources().getColor(R.color.color_name_here);
}
I am a android developer and i want to develop an application. In this app, i want to show a gallery image slider.In this slider ,the focus able image see should large and place to middle point and other image see should be small to same bellow picture.
I also try the below code .It is also show the large focus able image but not same .
gallery.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
Log.d("position", "" + arg3);
ProductByCategoryData productByCategoryData = AllProductByCategoryData
.getAllProductByCategoryData(arg2);
Constant.producdata = productByCategoryData;
Intent i = new Intent(ModeltypeActivity.this, OrderDetailInformation.class);
startActivity(i);
}
});
gallery.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> arg0, View arg1,
int arg2, long arg3) {
if (lastSelectedView != null)
{
lastSelectedView.setLayoutParams(new Gallery.LayoutParams(
lowLavelWidth, lowtLavelheight));// 150, 200//100, 150
}
arg1.setLayoutParams(new Gallery.LayoutParams(heighLavelWidth, heightLavelheight));// 220,
// 315//150, 200
lastSelectedView = arg1;
int vectorPosition = arg2;
ProductByCategoryData product = AllProductByCategoryData
.getAllProductByCategoryData(vectorPosition);
product_name.setText(product.getProduct_name());
}
#Override
public void onNothingSelected(AdapterView<?> arg0) {
}
});
Now how to show the gallery slider image same to above image.please help to me.It is Possibel ?