Android OnLongClickListener issue - android

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;
}
};

Related

Add ImageView programatically with own onTouchListener

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);

Android: How to detect touch events on dynamically created ImageView

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
}
});

Find out which view is touching in multiple layouts in linearlayout in android?

I have to create layout dynamically using Java.
I have created 9 linear layouts dynamically as below:
for (int i=0;i<10;i++)
{
Element e = (Element) nl.item(i);
cpName = parser.getValue(e,"coverpage");
System.out.println("coverpage name :" + cpName);
LinearLayout lbottomLayoutu1=new LinearLayout(getApplicationContext());
lbottomLayoutu1.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams layoutParams1u1= new LinearLayout.LayoutParams(width,height/4);
layoutParams1u1.weight=1.0f;
lbottomLayoutu1.setLayoutParams(layoutParams1u1);
lbottomLayoutu1.setBackgroundColor(Color.RED);
lbottomLayoutu1.setBackgroundResource(R.drawable.image);
linearLayout.addView(lbottomLayoutu1);
ImageView iv1=new ImageView(getApplicationContext());
LinearLayout.LayoutParams layoutParams= new LinearLayout.LayoutParams(width/4,height/4);
iv1.setLayoutParams(layoutParams);
iv1.setImageResource(R.drawable.coverpage3);
lbottomLayoutu1.addView(iv1);
lbottomLayoutu1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
System.out.println("Calling coverpage");
String Url = WebUrl+cpName;
System.out.println("web Url: " + Url);
Intent in = new Intent(getApplicationContext(),CurlActivity.class);
Bundle bundle = new Bundle();
bundle.putString("Image Url",Url);
in.putExtras(bundle);
startActivity(in);
}
});
setContentView(linearLayout);
}
I put a touch event for every layout. But if I touch any layout how can I know which layout is touching?
I think it's better to use custom listView (like here http://www.codeproject.com/Articles/507651/Customized-Android-ListView-with-Image-and-Text)
You need to set the ids of each layout as
obj.setId(i)
so that in your touch event you can identify it with
int id=obj.getId()
and work accordingly!
If you implement the OnClickListener for the view, like you did...
lbottomLayoutu1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// ...
}
}
... the parameter View v tells you which layout has been touched. You can then cast it to your expected layout, like:
LinearLayout clickedLayout = (LinearLayout) v;
This will be the layout that has been clicked.
If you want, however, know the index that you used in the loop, you have to overwrite your LinearLayout and give it a member that holds the index. Then you can cast it to your custom LinearLayout class and get the index. (If you need more help on this, let me know in the comments).

Linearlayout >> empty eventlistener method neccessarry to get focus?

I created a drawable (background_row.xml) with a state_focused and a default item.
Now i want to use this drawable to color a linearlayout when selected.
Linearlayout row = new LinearLayout(this);
row.setFocusableInTouchMode(true);
row.setBackgroundResource(R.drawable.background_row);
This doesn't work.
I tryed a lot and finally i found out that it works, when i implement an empty setOnClickListener like this:
row.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
Sorry.. but that makes no sense for me..
Why do i have to implement an empty OnClickListener and why is it working when i do so?

I need to add onClickListener to a bunch of ImageButtons

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()

Categories

Resources