Android ListView onItemClick show Popup on exact position - java

I have a PopupWindow which is shown as a drop down when I click a Item on my ListView:
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
View v = getLayoutInflater().inflate(R.layout.popup_click_menu, null);
final PopupWindow mypopupWindow = new PopupWindow(v,300, RelativeLayout.LayoutParams.WRAP_CONTENT, true);
mypopupWindow.showAsDropDown(view, -153, 0);
}
The result is this:
My question is: I want to have a dynamic position of my pop up. So if I click on the middle of the Item then it should be shown in the middle. How can I do that?

You can solve this with a TouchListener:
list.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View view, MotionEvent event) {
positionX = (int) event.getX();
return false; // not consumed; forward to onClick
}
});
Then you have the exact x-position and you can move the PopupWindow by onItemClick:
mypopupWindow.showAsDropDown(view, positionX, 0);

Related

textView.setText does not update text but also gives no errors

I have a PopupWindow that has a TextView in it. I am trying to change the text in this TextView dynamically but can't seem to get it to work. The code executes, but it does nothing and gives no errors, making it hard to understand what's happening (as there is nothing that shows up in the debug logs). It's important to note that this TextView is not in the same layout as the layout given to setContentView - and that menu is called when a button is pushed in the main layout. Does anybody have any idea why this could be happening?
Here is my code:
public void popupView(View view, int viewPointer){//viewPointer is R.id.popup_layout
LayoutInflater inflater = (LayoutInflater)
getSystemService(LAYOUT_INFLATER_SERVICE);
View popupView = inflater.inflate(viewPointer, null);
// create the popup window
int width = LinearLayout.LayoutParams.WRAP_CONTENT;
int height = LinearLayout.LayoutParams.WRAP_CONTENT;
popupWindow = new PopupWindow(popupView, width, height,true);//add another parameter here (true) to make it so it goes away when clicked outside the menu itself
popupWindow.setElevation(20);
// show the popup window
// which view you pass in doesn't matter
popupWindow.showAtLocation(view, Gravity.CENTER, 0, 0);
// dismiss the popup window when touched
popupView.setOnTouchListener(new View.OnTouchListener() {
#SuppressLint("ClickableViewAccessibility")
#Override
public boolean onTouch(View v, MotionEvent event) {
popupWindow.dismiss();
return true;
}
});
}
and
public void menu(View view){
textView.setText("this won't change the text but will execute with zero errors...");
popupView(view, R.layout.popup_layout);
}
and in onCreate:
LayoutInflater layoutInflater = getLayoutInflater();
View text_view = layoutInflater.inflate(R.layout.popup_layout, null);
textView = text_view.findViewById(R.id.textViewID);

How to detect swipe gesture across multiple views in android?

I have a Grid Layout with multiple TextViews as children. I have added an event listener to each TextView. I want to detect when a user swipes across multiple TextViews in the layout
i.e. I want to identify all the TextViews that have been touched while swiping.
I recieve touch event for only the first TextView that has been touched , I do not receive any touch events for other TextViews when user swipes his finger across multiple TextViews in GridLayout.
I have tried using
TextView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
,
TextView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent event) {
}
});
and
TextView.setOnHoverListener(new View.OnHoverListener() {
#Override
public boolean onHover(View v, MotionEvent event) {
return false;
}
});
I have also tried overriding
#Override
public boolean onTouchEvent(MotionEvent event) {
}
What should i do now ?
try to grid view ItemClickListener.
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
// Get the GridView selected/clicked item text
String selectedItem = parent.getItemAtPosition(position).toString();
// Display the selected/clicked item text and position on TextView
tv.setText("GridView item clicked : " +selectedItem
+ "\nAt index position : " + position);
}
});

Drag an item out of recyclerview to another view

I created a recycler view where the items can be dragged. The following is from my adapter:
public void onBindViewHolder(final FootballPitchMainAdapter.ViewHolder holder, int position) {
holder.rvPlayerNameTop.setText("testing");
holder.linearLayoutFull.setOnTouchListener(new View.OnTouchListener() {
public boolean onTouch(View view, MotionEvent motionEvent) {
if (motionEvent.getAction() == MotionEvent.ACTION_DOWN) {
ClipData data = ClipData.newPlainText("", "");
View.DragShadowBuilder shadowBuilder = new View.DragShadowBuilder(
view);
view.startDrag(data, shadowBuilder, view, 0);
view.setVisibility(View.INVISIBLE);
return true;
} else {
return false;
}
}
});
Now this does work, I can drag the linearLayout, which contains an imageView and a texview. The drag allows me to move the items out of the recyclerview.
If I used motion, I couldn't move the linearlayout frame out of the RV.
But my problem is, once I drag the view, I want to be able to place it anywhere on the screen.
So in the class where the RV is created, I set the following:
pitchRelativeLayout.setOnDragListener(new MyDragListener());
class MyDragListener implements View.OnDragListener{
#Override
public boolean onDrag(View v, DragEvent event) {
switch(event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
//Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
break;
case DragEvent.ACTION_DRAG_EXITED :
break;
case DragEvent.ACTION_DRAG_LOCATION :
break;
case DragEvent.ACTION_DRAG_ENDED :
// Do nothing
break;
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
RelativeLayout container = (RelativeLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
default: break;
}
return true;
}
}
PitchLayout is a relativelayout which covers the entire screen. The idea is just to drag an item from RV and drop it anywhere.
Here is an image which I hope explains this better:
I am trying to tell the relative layout to listen to the drag item and on the drop, place it here.
The above code is from online tutorials I have tried, answers from Stackover Flow

Android: How do I make a gird of images views and have the image change when I drag another image over it?

I am trying to make a grid of image views, that when i drag another view over them(this view essentially acts as a mouse pointer), it changes the image I drag over in the grid to a different image. When I drag the "mouse" view away it will go back to normal.
Right now I have my mouse cursor view moving and have a grid of image views implemented with a grid view using a custom adaptor ( pictured below)
I can drag my cursor around but when ever I try to drag inside the gridview, it calls the image on the gridview's ontouch listener(only when i click on the image view, not when I slide over it). I also cannot seem to get onHover working on the images in the image View.
Also, if the mouse view is covering one of the grid images, the ontouchlistener of the image view the grid is not called.
What is the best way( types of views, etc) to use to go about implementing this?
Here is some relevant code from what I have so far.
This is where I set up my grid view in my activity
GridView gridView;
gridView = (GridView) findViewById(R.id.gridView);
gridView.setAdapter(new ImageAdapter(this,new String [25]));
gridView.setOnDragListener(new MyDragListener());
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v,
int position, long id) {
}
});
gridView.setClickable(false);
This is the getView function in my adapter
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View gridView;
if (convertView == null) {
gridView = inflater.inflate(R.layout.vibe_pad_section, null);
final ImageView imageView = (ImageView) gridView
.findViewById(R.id.grid_item_image);
imageView.setImageResource(R.drawable.vibepadcircle);
imageView.setPadding(10,10,10,10);
imageView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent e) {
Log.e("here","here");
switch (e.getAction()) {
case MotionEvent.ACTION_MOVE:
Log.e("here2","here2");
}
imageView.setImageResource(R.drawable.bar_high);
return true;
}
});
VibePad.padTargets.add(imageView);
} else {
gridView = (View) convertView;
}
return gridView;
}
class MyDragListener implements View.OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
ClipData dragData;
View p = (View) v.getParent();
Object index = p.getTag();
final int which = Integer.parseInt(index.toString());
boolean handled = true;
Log.e(" in on drag","in on drag") ;
switch (action) {
case DragEvent.ACTION_DRAG_EXITED:
// Report the drop/no-drop result to the user
((ImageView) v).setImageResource(R.drawable.bar_high);
break;
case DragEvent.ACTION_DRAG_ENTERED:
// just drop it
((ImageView) v).setImageResource(R.drawable.vibepadcircle);
break;
default :
break;
}
return handled;
}
You need to create a class that extends OnDragListener:
When ACTION_DRAG_ENTERED occurs, you can change the underlying image to something else. When you exist (you've dragged the view outside the bounding box of the the image on your gridview) then you can change it back to it's original image
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View v, DragEvent event) {
final int action = event.getAction();
ClipData dragData;
View p = (View) v.getParent();
Object index = p.getTag();
final int which = Integer.parseInt(index.toString());
boolean handled = true;
switch (action) {
case DragEvent.ACTION_DRAG_STARTED:
dragSku = event.getClipDescription().getLabel().toString();
break;
case DragEvent.ACTION_DRAG_ENDED:
// Report the drop/no-drop result to the user
final boolean dropped = event.getResult();
compareInMotion = false;
BaseAdapter lva = (BaseAdapter) gridview.getAdapter();
lva.notifyDataSetChanged();
break;
case DragEvent.ACTION_DROP:
// just drop it
((ImageView) v).setImageBitmap(emptyImg);
setUpCompareItem((ImageView) v, dragSku); break;
default :
break;
}
return handled;

Swipe detection on Listview not working for custom Listview Adapter

I implemented a gesture detector to find out when the user will fling my listview (and when the last element of the list is in view) so that I may add more data to the list. The list has a custom adapter that I've built. Each row has a few textviews in it and an image button. I was using the imagebutton (which is an arrow) with a click listener in my adapter to open another activity related to the pressed row.
Management wants the user to be able to click anywhere on the row in order to activate it. So:
#Override
public View getView(final int position, View convertView, ViewGroup parent)
{
View v = convertView;
final ViewHolder viewHolder;
.....
//viewHolder.ibShipmentDetails.setOnClickListener(new OnClickListener()
v.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View view)
{
....
}
}
But now, my fling detector won't work correctly. Sometimes it responds and afterwards works correctly; restart the activity, again it doesn't work.
Here is my gesture detector:
class MyGestureDetector extends GestureDetector.SimpleOnGestureListener
{
#Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if((e1.getY() - e2.getY() > SWIPE_MIN_DISTANCE && Math.abs(velocityY) > SWIPE_THRESHOLD_VELOCITY))
{
if(!loading && ((totalItemCount - visibleItemCount) <= firstVisibleItem))
{
if(allTabSelected)
{
allPageNo++;
}
else
{
openPageNo++;
}
new GetShipmentPreviews().execute(1);
}
}
}
And in my activity:
gestureDetector = new GestureDetector(this, new MyGestureDetector());
gestureListener = new View.OnTouchListener()
{
public boolean onTouch(View v, MotionEvent event)
{
return gestureDetector.onTouchEvent(event);
}
};
listView.setOnTouchListener(gestureListener);
What should I do to keep v.setOnClickListener and the onFling() command intact?
Turns out my MotionEvents from onFling are null. This apparently happens because the event gets somehow "eaten" by the onClickListener from my custom adapter.
I got the solution from this answer : https://stackoverflow.com/a/7159738/1688731
I added the following code to my Activity class:
#Override
public boolean dispatchTouchEvent(MotionEvent ev)
{
super.dispatchTouchEvent(ev);
return gestureScanner.onTouchEvent(ev);
}
You should use listViev.setOnItemClickListener() once, instead of calling v.setOnClickListener() for all list items.

Categories