I have a button that I want to create a ImageView everytime I am pressing it, this ImageView can have his own onTouchListener right ?
I have a button create new and everytime I press it a ball will appear on the screen that I will be able to move it, when I finnish it moving when I press New again another ball will appear on the screen with the same properties and ability to drag and drop.I am asking because I have problem creating a ImageView programatically for example:
save = (Button) findViewById(R.id.savebtn);
save.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ImageView asds = new ImageView(this);
asds.setBackgroundResource(R.drawable.element_wall);
asds.setOnTouchListener(new OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return false;
}
});
}
});
For example, this is not working how it should.Thank you for the time acording reading this.
Firstly use setOnTouchListener outside setOnClickListener.
Secondly assign each imageview a unique tag using setTag() method and using image.getTag() you can get the tag and once you get the image with unique tag you can set OnTouchListener on the particular imageview
Two things.
You need to add the view to one of the ViewGroups on screen. For instance, if you have a FrameLayout containing all your views, you would do something like:
ImageView imageView = new ImageView(this);
FrameLayout frameLayout = (FrameLayout) findViewByid(R.id.container);
frameLayout.addView(imageView);
Since you are creating the ImageView inside of an anonymous inner class, the this keyword refers to the anonymous inner class, not the Activity. You need to qualify the this keyword with your Activity's name, e.g.
ImageView imageView = new ImageView(MyActivity.this);
Related
I have a scenario in which I have to to add text and multiple images, something like below
"fooo bar lorem ipsum somtext.
and all these images have clicklisteners so I can know which image is being clicked.
Till now I am able to place images but unable to implement click listeners on that
Associate a Tag with each imageview and
Set a common onclick Listner for both imageview in onclick even find the imageview by tag associated with View in onClick() method Like
Let two imageview are iv1 and iv2
iv1.setTag("TAG1");
iv2.setTag("TAG2');
OnClickListener oc=new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
String tag=(String) v.getTag();
if(tag.equals("TAG1"))
{
// Imageview is iv1
}else{
// Imageview is iv2
}
}
};
Try it and let me know if problem exist
I have the following design :
<LinearLayout>
<ScrollLayout>
<LinearLayout> .... </LinearLayout> ----> TextView and ImageView dynamically created in this LinearLayout
</ScrollLayout>
</LinearLayout>
I need to detect touch events on the dynamically created ImageView(s). I don't know how many ImageView will be created dynamically. How can I detect which ImageView was touched by the user ?
Thanks for your help.
You'll want to create one [maybe more] View.OnClickListeners in your java code. Then when each ImageView is added, you can assign that OnClickListener to it using View.setOnClickListener. To determine which was clicked, you can set a "Tag" using View.setTag() and then when onClick is called, use View.getTag() to determine which was clicked. The tag should be uniquely identifiable.
Here's some rough code (untested):
private OnClickListener imageViewListener extends View.OnClickListener{
public void onClick(View v){
Intent i = new Intent(v.getContext(), NewActivity.class);
i.putExtra("ImageURI", v.getTag().toString());
startActivity(i);
}
}
Then as you add images, you just set the tag and listener:
for(String uri: someList){
ImageView iv = new ImageView(this);
iv.setTag(uri);
iv.setImageUri(uri);
iv.setOnClickListener(imageViewListener);
}
You should use View.OnTouchLisetener class. So, after initializing your ImageView, add this call to it:
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent e) {
//Your code here
}
});
In my app I have multiple TextViews (the number of this elements is changing on activity creation). I want to execute some function on touch of each element: for instance change the background. I try to avoid writing the same function for each element.
I would like it to work like jQuery so if I trigger some event that are from some class the this element changes.
I hope it is clear, thanks!
Have your activity implement OnClickListener, and then in the onClick method put your common code, and call setOnClickListener(this); on each of your TextViews.
If you have more than one type of View being clicked, enclose the TextView specific code in:
if(<name of the view parameter in your onClick method) instanceof TextView)
{
//Code here
}
EDIT
Another method would be to create your own Custom TextView and override the method in that itself. Something like:
public class MyTextView extends TextView {
//Various constructors go here
#Override
public boolean onTouchEvent(MotionEvent event) {
// TODO Auto-generated method stub
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
//Do your stuff here, your textview has been touched
return true;
}
return super.onTouchEvent(event);
}
}
Then instead of using TextView tv = new TextView(context);, use MyTextView tv = new MyTextView(context);
simple:
onCreate( //...
// do your layout creation
TextView tv = new TextView(context)
tv.setOnClickListener(clickListener);
tv.setTag(0); // this tag can be any object. So feel free to add a KEY_string, or anything that u might use to later identify your view on the click listener.
tv = new TextView(context);
tv.setOnClickListener(clickListener);
tv.setTag(1);
} // finish on create
private OnClickListener clickListener = new OnClickListener() {
#Override
public void onClick(View v) {
int id = (Integer)v.getTag();
switch(id){
case 0):
// do stuff
case 1:
// do other stuff
}
};
you also can use several other listeners, depending on your needs, just use the auto complete on Eclipse to check all the options for tv.setOn...
I have created a bunch of ImageButtons programmatically while in a for loop. They have worked fine as the data displayed in a HorizontalScrollView. Now I need each one to go dim or bright when clicked. First click will setAlpha(45); second click will setAlpha(255);.
I don't think I fully understand how the Views and onClickListener works yet. It seems the onClick function examples I find take a View. How would that function know which button is clicked? Perhaps there is an easier way to do what I want?
Here are the ImageButtons.
TableRow tr0 = new TableRow(this);
tr0.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Is there something I need to do to make the buttons identifiable? And how would that pass in through into the onClick function to be used?
-: Added Information :-
I am starting to wonder if the problem isn't with the buttons, but with the way I built the screen. More information added.
The Game activity is the main game, which uses the PuzzleView for the upper part of the screen holding the game grid. The lower part is where the ImageButtons are and I built them in place in the Game class.
public class Game extends Activity{
//various variables and stuff
private PuzzleView puzzleView; // The PuzzleView is from another .java file
// public class PuzzleView extends View
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
LinearLayout mainPanel = new LinearLayout(this);
mainPanel.setOrientation(LinearLayout.VERTICLE);
puzzleView = new PuzzleView(this);
mainPanel.addView(puzzleView);
HorizontalScrollView bottom = new HorizontalScrollView(this);
mainPanel.addView(bottom);
TableLayout clues = new TableLayout(this);
bottom.addView(clues);
TableRow tr0 = new TableRow(this);
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setImageBitmap(ClueList.get(but).btmp);
tr0.addView(clueBut);
}
When I try to add the ClickListener(this) I get errors about this not being able to be a Game. I have similar problems in the onClick(View v) function referencing the View. Are these problems because I am building the buttons in the Game Activity instead of a View class?
Thanks
When you set up an OnClickListener and implement the onClick(View v) callback, it's the Dalvik VM the one that will call that method each time the View is clicked, and it will pass the View instance as a parameter. Thus, the code you write inside that method will be applied only to the View that received the click and not to any other View. Add something like this to your loop:
clueBut.setOnClickListener(new OnClickListener() {
public void onClick (View v) {
if (v.getAlpha() == 1f)
v.setAlpha(0.2f);
else
v.setAlpha(1f);
}
});
In the onClick event:
public void onClick(View currentView)
{
Button currentButton = (Button)CurrentView;
//Do whatever you need with that button here.
}
To identify each view uniquely use the property
View. setId(int)
In your case the code would look something like this
for(int but=0; but<ClueList.size(); but++){
ImageButton clueBut = new ImageButton(this);
clueBut.setBackgroundResource(0);
clueBut.setImageBitmap(ClueList.get(but).btmp);
clueBut.setId(but);
//clueBut.setOnClickListener(this);
tr0.addView(clueBut);
}
Inside the onclick listener match the id of the view using findViewByID()
I have a custom view, DisView(Context, bitmap), which I want to add a LongCLickListener to.
The view is displayed once something else is clicked.
public void onClick(View view) {
...
RelativeLayout toplayout = new RelativeLayout(this);
setContentView(toplayout);
Bitmap bmp2 = BitmapFactory.decodeResource(getResources(), R.drawable.tag3);
tag3 = new DisView(this,bmp2);
tag3.setOnLongClickListener(this);
I should add that originally the activity's contentview is set to a linearlayout, but on a button being clicked, setContentLayout() makes a relativelayout the new layout.
Next I did the onLongClick method ( a method of the activity, implementing onlongclicklistener):
#Override
public boolean onLongClick(View view) {
moveTag(view);
return true;
}
moveTag() is a very simple TranslateAnimation. I have no idea why it doesn't work. I have a feeling it may be because I changed the layout.
did you think about ViewSwitcher to change your displayed layout?
Jonathan
if toplayout is a LinearLayout and you write
RelativeLayout toplayout = new RelativeLayout(this);
the software will usually crash on startup(or when this view is activated), if what you meet now is the software crashes once it started(or when you start this activity), it is highly possible that the problem is caused by this statement.
In your example you did this:
tag3.setOnLongClickListener(this);
Why did you do that?
setOnLongClickListener takes a parameter of type OnLongClickLIstener. Something like:
tag3.setOnLongClickListener((OnLongClickListener) keyHdlr);
where keyHdlr looks something like this:
OnLongClickListener keyHdlr = new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
Log.d("long", "backspace phone land long clicked!!!!!!");
return false;
}
};