Way to get the no of Imageview that is clicked - android

I have a lot of imageview . Lets say I have a 50 ImageViews . These will be in one screen . Horizontal and vertical scrolling will be available .
for(int j = 0; j < numCol; j++)
{
imageView[i*5+j] = new ImageView(this);
imageView[i*5+j].setImageResource(R.drawable.icon);
imageView[i*5+j].setMaxHeight(80);
imageView[i*5+j].setMinimumHeight(80);
imageView[i*5+j].setMaxWidth(90);
imageView[i*5+j].setMinimumWidth(90);
tblRow.addView(imageView[i*5+j],j);
imageView[i*5+j].setOnClickListener(this);
}
Suppose an user will click on imageview[25] . Is there any direct way to catch that imageview[25] is clicked ? Or I will have to iterate over all imageview .

imageView[i*5+j].setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v) {
someMethod((ImageView) v);
}
});
private someMethod(ImageView currentView) {
}

You can use
imageView[i*5+j].setTag(new Integer(j));
and then in the OnClockListener
public void onClick(View v) {
Integer i = (Integer)v.getTag();
}

Related

checkbox select all delete issue

i have checkboxes in custom list view and i have two seperate buttons one to select all and another to delete but i have big issue in it if i select one checkbox and select delete it works.but if i select all and delete it delete few of the data in lists ,for example i have 50 sms in list view once i select all and delete it delete few sms and again i press delete it delete another few like this..suggest some solutions
sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <sms.size() ; i++) {
if(sms.get(i).getChecked() == false ) {
sms.get(i).setChecked(true);
}else if(sms.get(i).getChecked()==true) {
sms.get(i).setChecked(false);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}
});
del.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for(int i=0; i<sms.size(); i++){
if(sms.get(i).getChecked() == true){
sms.remove(i);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}
I guess the problem arises when you remove the item from the ArrayList in the for loop there. It's size gets reduced as a result and this can induce undesirable side effects.
Try iterating your sms ArrayList in reverse order like this:
for (int i = sms.size()-1; i >= 0; i--) {
if(sms.get(i).getChecked() == true){
sms.remove(i);
}
}
This way reduction in the size of sms ArrayList won't effect the loop.
I think problem is upper for loop. In else if statement you are unchecking the checkboxes. try below code instead,
sel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i <sms.size() ; i++) {
if(sms.get(i).getChecked() == false ) {
sms.get(i).setChecked(true);
}else if(sms.get(i).getChecked()==true) {
sms.get(i).setChecked(true);
}
}
((datalist)mlistView.getAdapter()).notifyDataSetChanged();
}
});

get OnClick() from programmatically added buttons?

i have added some button in a layout:
LinearLayout row = (LinearLayout)findViewById(R.id.KeysList);
keys=db.getKeys(console);
my_button=new Button[keys.size()];
for (bt=0;bt<keys.size();bt++){
my_button[bt]=new Button(this);
my_button[bt].setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.FILL_PARENT));
my_button[bt].setText(keys.get(bt));
my_button[bt].setId(bt);
row.addView(my_button[bt]);
my_button[bt].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (my_button[bt].getId() == ((Button) v).getId()){
Toast.makeText(getBaseContext(), keys.get(bt), 0).show();
}
}
});
}
I want to know which button is clicked and how to get text of the clicked button?And I think using bt here dose not seem to work!
This code is running. I hope it help you :)
final ArrayList<String> Keys = new ArrayList<String>();
for(int i = 0; i < 10; i ++){
Keys.add("Keys is : " + String.valueOf(i));
}
LinearLayout Row = (LinearLayout)findViewById(R.id.KeysList);
final Button[] my_button = new Button[Keys.size()];
for (int bt = 0; bt < Keys.size(); bt ++){
final int Index = bt;
my_button[Index] = new Button(this);
my_button[Index].setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.WRAP_CONTENT));
my_button[Index].setText(Keys.get(Index));
my_button[Index].setId(Index);
my_button[bt].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (my_button[Index].getId() == ((Button) v).getId()){
Toast.makeText(getBaseContext(), Keys.get(Index), 0).show();
}
}
});
Row.addView(my_button[Index]);
}
ExampleProject id : Your project
You should probably use View#setTag to set some arbitrary data you'd like associate with the Button. Then you can just instantiate only one OnClickListener that then uses getTag and acts on that data in whatever way you need.
Another way is to have your Activity listen to all button clicks and then you just filter respective to the ID. You should not get the text of the button and use that at all. You should use your own type of identifier, ideally the idea should be enough. Or perhaps you use setTag as #qberticus described.
Consider This example :
public class MainActivity extends Activity implements View.OnClickListener
{
LinearLayout linearLayout;
Button [] button;
View.OnClickListener listener;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linearLayout=(LinearLayout)findViewById(R.id.parent_lay);
String[] array={"U123","U124","U125"};
int length=array.length;
System.out.println("11111111111111111111111111");
button=new Button[length];
for(int i=0;i<length;i++)
{
button[i]=new Button(getApplicationContext());
button[i].setId(i);
button[i].setText("User" + i);
button[i].setOnClickListener(this);
linearLayout.addView(button[i]);
}
}
#Override
public void onClick(View view)
{
view.getId();
Button button=(Button)findViewById(view.getId());
button.setText("Changed");
}
}
This works fine :)

setOnClick for 100 buttons

I have 100 buttons (from button000 to button 999). Is there any way to setOnClick for all of them? Because all button have the same function.
Thanks in advance.
If your buttons are inside a layout then do like this.
int childcount = ll.getChildCount();
for (int i=0; i < childcount; i++){
View v = ll.getChildAt(i);
v.setOnCLickListener(this);
}
Buddy try this way
import android.view.View.OnClickListener;
public class MyActivity extends Activity implements OnClickListener {
#Override public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button buttonA = (Button) findViewById(R.id.buttonA);
buttonA.setOnClickListener(this);
Button buttonB = (Button) findViewById(R.id.buttonB);
buttonB.setOnClickListener(this);
}
//etc... etc...
public void onClick(View v) {
switch (v.getId()) {
case R.id.buttonA:
// do something
break;
case R.id.buttonB:
// do something else
break;
}
}
At right now I can say easiest way in
use button000.setOnclicklistener(this);
:
:
button999.setOnclicklistener(this);
and implement Onclicklistener in this current class....
if you are sure that it's the best way for your app to create 1000 buttons, then it will be something like this:
Button [] my_button=new Button[1000];
LinearLayout ll=(LinearLayout)findViewById(R.id.mylayout);
for (int i=0;i<1000;i++){
my_button[i]=new Button(this);
my_button[i].setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT));
my_button[i].setText("button "+i);
ll.addView(my_button[i]);
my_button[i].setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
////click event
}
});
}
Just add your buttons in an array and
Just loop the buttons and on a listener you can call the following block of code:
for (int i=0; i < buttonArray.length; ++i){
buttonArray[i].onClick(this);
}
there are two methods one is you can set onClick in xml layout file by onClick method you can define the method, that should be invoked when button is clicked. This method is appropriate when you define buttons in xml.
If you are adding buttons in Activity, and if you are adding in a loop then you can do as
for(int i=0; i<100; i++)
{
//Create and Add button
btn.setOnClickListener(new OnClickListener()
{
public void onClick(View view)
{
//Operations
}
});
}
Best way to make Button Dynamically like
Integer[] button_Ids = {R.id.btn000,R.id.btn001...............,R.id.btn999};
for(int i=0;i<100;i++)
{
Button btn = (Button)findViewById(button_ids[i]);
btn.setOnClickListner(this);
}
#Override
public void onClick(View v)
{
Toast.make(getApplicationContext,"Hello",1000).show();
}
If all buttons have exactly same functionality then you can simple use
private OnClickListener mListenr=new OnClickListener(
#Override
public void onClick(View v) {
//Whatever you want
}
for(int i=0; i<100; i++)
{
mButton[i].setOnClickListener(mListenr);
}
you can refer this to see ways to implement listener.

Android how to call another Image after 3 times click on same Image?

I am developing application on Images.
In that if I click on same image the image will replace by another one that already into my Drawable
Right now onclick I am able to display only Toast Message; but I want to replace Image. I don't know How to do?
Any Help and suggestion appreciable.
you can take it images name as : img1 ,img2 ,img3.
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (v.isClickable()) {
i++;
String ii = new Integer(i).toString();
Log.i("Inside", ii);
if (ii.equals("3")) {
Toast.makeText(getApplicationContext(), "Call another Image ",Toast.LENGTH_SHORT).show();}
ImageView imgV = (ImageView) layout.findViewById(R.id.img1);
imgV.setClickable(true);
int counter = 0;
imgV.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
i++;
if(i == 3){
imgV.setImageResource(R.drawable.img2);
//Or you may want to use
//imgV.setBackgroundResource(R.drawable.img2);
i = 0;
}
}

how to apply different onclick action to each button in array of buttons in android

hi i am doing one application here i need to disply 6 images in 3 colunmns and 3 rows.and then when i click each image i need to perform different onclick action.i teried some way using arraylist with forloop.using below code i applied onclick function into all images but here 2 cloumn 3 images onclick function working but i first column 3 images not working onclick function.but i need to apply different onclick action to each button. so please any one help me how to apply onclick action to array of images.
game2 .class:
public class game2 extends Activity implements OnClickListener {
TableLayout layout;
int i=0;
int f=0;
Integer[] images={R.drawable.abig,R.drawable.cbig,R.drawable.dbig,R.drawable.abig,R.drawable.cbig,R.drawable.dbig};
List<Integer> solutionList = Arrays.asList(images);
Integer[] randomNumbers,randomNumbers1;
TableLayout.LayoutParams param,param1;
ImageView[] plus=new ImageView[6];
TableRow[] row = new TableRow[6];
RelativeLayout.LayoutParams lp2,lp1,lp3,lp4,lp5;
RelativeLayout linear;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main1);
layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(40,50) );
param=new TableLayout.LayoutParams(TableLayout.LayoutParams.WRAP_CONTENT,TableLayout.LayoutParams.WRAP_CONTENT);
param.setMargins(25, 0, 0, 0);
lp1=new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
linear=(RelativeLayout)findViewById(R.id.linear);
Collections.shuffle(solutionList);
randomNumbers = (Integer[])solutionList.toArray();
int unique=0;
for (f=0; f<3; f++) {
row[f] = new TableRow(this);
for (int c=0; c<2; c++) {
plus[f]=new ImageView(this);
plus[f].setBackgroundResource(randomNumbers[unique]);
plus[f].setOnClickListener(this);
row[f].addView(plus[f], 200,100);
unique++;
} // for
layout.addView(row[f],param);
} // for
linear.addView(layout,lp1);
setContentView(linear);
}
public void onClick(View view) {
if(view==plus[0])
{
Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
}
if(view==plus[1])
{
Toast.makeText(game2.this, "view1", Toast.LENGTH_SHORT).show();
}
if(view==plus[2])
{
Toast.makeText(game2.this, "view2", Toast.LENGTH_SHORT).show();
}
if(view==plus[3])
{
Toast.makeText(game2.this, "view3", Toast.LENGTH_SHORT).show();
}
if(view==plus[4])
{
Toast.makeText(game2.this, "view4", Toast.LENGTH_SHORT).show();
}
if(view==plus[5])
{
Toast.makeText(game2.this, "view5", Toast.LENGTH_SHORT).show();
}
}
}
Can this may be problem for (int c=0; c<2; c++)? use for (int c=0; c<3; c++) .. c<3 for 3 columns.. And let me know what happen..
EDIT:
Also
ImageView[] plus=new ImageView[9];
int unique=0;
for (f=0; f<3; f++) {
row[f] = new TableRow(this);
for (int c=0; c<3; c++) {
plus[unique]=new ImageView(this);
plus[unique].setBackgroundResource(randomNumbers[unique]);
plus[unique].setOnClickListener(this);
plus[unique].setId(unique);
row[f].addView(plus[unique], 200,100);
unique++;
}
And in onClick()
public void onClick(View view) {
switch(view.getId()){
case 0:
{
Toast.makeText(game2.this, "view", Toast.LENGTH_SHORT).show();
}
.
.
.
case 8:
{
Toast.makeText(game2.this, "view8", Toast.LENGTH_SHORT).show();
}
}
}
Use ArrayList instead of array.. Right now you are not adding all the elements.. In nested for loop your elemnts are getting replaced... So use ArrayList... And I don't see a need for such a complex code, You keep UI in XML and still can achieve the Same.. The present code is hard to understand and even you cannot after some days.. You cannot maintain this code..
Use plus[unique] in place of plus[f] in all 3 lines of below loop
for (int c=0; c<2; c++) {
... }
simple set ID to every imageView when creating ImageView. then setOnClickListener().
onClick(View v) {
switch(v.getId()) {
case 1st_ImageViewID:
break;
case 2nd_ImageView_ID:
break;
case 3rd_ImageView_ID:
break;
}
}

Categories

Resources