How to handle drop event in Drag and drop of an imageview? - java

I need drag and drop in my new project. I referred this blogpost. But I'm facing a problem while performing Drop. I cannot get the image which I hold and can't drop into view. DragListner which I used is given below. I don't know how to handle the dropevent.
class MyDragListener implements OnDragListener {
#Override
public boolean onDrag(View view, DragEvent dragEvent) {
int dragAction = dragEvent.getAction();
View dragView = (View) dragEvent.getLocalState();
if (dragAction == DragEvent.ACTION_DRAG_EXITED) {
System.out.println("exit------------");
containsDragable = false;
} else if (dragAction == DragEvent.ACTION_DRAG_ENTERED) {
System.out.println("enter------------");
containsDragable = true;
} else if (dragAction == DragEvent.ACTION_DRAG_ENDED) {
System.out.println("end------------");
dragView.setVisibility(View.VISIBLE);
} else if (dragAction == DragEvent.ACTION_DROP && containsDragable) {
dragView.setVisibility(View.VISIBLE);
}
return true;
}

maybe at first you can print out the
DragEvent.ACTION_DROP
code, and compare it to the value of the dragAction .
if the value is the same then the problem are at containsDragable function.
hope this helps

Related

ExpandableListView is behaving abnormally for the last item in the list

I am using ExpandableListsViews in the Android Nav Drawer instead of menu's.
And OnExpandlistner i am changing the background color. The first two items are behaving normally and their bachground color is changed. But the last item is doing the opposite.i have tried everything, i have debugged the code. On the last item the set background color code is runnig but it isn't changed. the next time i click the item, it change the color. And after if i click the other lists item. last item color also changes.
simpleExpandableListView.setOnGroupCollapseListener(new ExpandableListView.OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int i) {
if(i == 0){
View view = getGroupView(simpleExpandableListView,0);
view.setBackgroundColor(Color.parseColor("#282828"));
}
if(i == 1){
View view = getGroupView(simpleExpandableListView,1);
view.setBackgroundColor(Color.parseColor("#282828"));
}
if(i == 2){
View view = getGroupView(simpleExpandableListView,2);
view.setBackgroundColor(Color.parseColor("#282828"));
}
}
});
simpleExpandableListView.setOnGroupExpandListener(new ExpandableListView.OnGroupExpandListener() {
#Override
public void onGroupExpand(int i) {
if(i == 0){
View view = getGroupView(simpleExpandableListView,0);
view.setBackgroundColor(Color.parseColor("#616262"));
}
if(i == 1){
View view = getGroupView(simpleExpandableListView,1);
view.setBackgroundColor(Color.parseColor("#616262"));
}
if(i == 2){
View view = getGroupView(simpleExpandableListView,2);
view.setBackgroundColor(Color.parseColor("#616262"));
}
}
});
I am Even Getting the respected Views for each item.But Still.
Here another Logic i have tried.
simpleExpandableListView.setOnGroupClickListener(new ExpandableListView.OnGroupClickListener() {
#Override
public boolean onGroupClick(ExpandableListView parent, View v, int groupPosition, long id) {
//get the group header
GroupInfo headerInfo = deptList.get(groupPosition);
Log.d("GroupPosition", "onGroupClick: " + groupPosition);
if(parent.isGroupExpanded(groupPosition)){
v.setBackgroundColor(getResources().getColor(R.color.colorMenuBackground));
}
if(!parent.isGroupExpanded(groupPosition)){
v.setBackgroundColor(getResources().getColor(R.color.colorPrimary));
}
//display it or do something with it
/*Toast.makeText(getBaseContext(), " Header is :: " + headerInfo.getName(),
Toast.LENGTH_LONG).show();*/
return false;
}
});
But Still Getting the Same Result.
If Someone can point me in the right direction, and tell me what am i doing wrong, It will be much appreciated.
Thanks you
Problem Solved! This can all be solved by using expandableListView xml attribute. "android:listSelector="any Color you want " "

JavaFX - Different layouts in a custom ListCell

I'm building a chat app using JavaFX. Now for chat messages display, I use a simple ListView of ChatItems with a custom cell factory, ChatCell.
ChatItem could be a ChatLabel (server announcements, status change, etc), or a ChatMessage (message sent by self or other parties). And ChatMessage could be extended to show various content (just ChatTextMessage and ChatImageMessage for now)
My custom cell checks the type/characteristics of the item and provides the proper view accordingly.
I'd like to show a timestamp on each ChatMessage view on bottom right corner of the chat bubble. BUT if the text is only one line, I want the timestamp to show on an extra space on the right of the text. (kinda like WhatsApp chat or most chat apps).
Here's a demo:
My current implementation (shown in the demo) uses a boolean property in ChatMessage that determines if the view should be multiline or not. It checks it on every update. I also add a listener to ListView width and call refresh() when it changes. All of this seems excessive. And I don't think adding a "view-related" property to a "model" is a good design. Not to mention that on the first item update, the timestamp is not at the correct place for some reason.
How can I do this properly?
UPDATE 1:
I added a property extractor for the ListView items, and added a listener for the ListView width that calls updateItem on width changes. Now I don't have to call refresh()
Here is ChatCell class:
public class ChatCell extends ListCell<ChatItem> {
private ChatItemView view;
private InvalidationListener listViewListener;
private InvalidationListener listViewWidthListener;
public ChatCell() {
setContentDisplay(ContentDisplay.GRAPHIC_ONLY);
listViewWidthListener = e -> {
updateItem(getItem(), isEmpty());
};
listViewListener = e -> {
if(getView() == null) return;
getView().unbindWidth();
if(listViewProperty().get() != null) {
getView().bindWidth(listViewProperty().get().widthProperty());
listViewProperty().get().widthProperty().addListener(listViewWidthListener);
listViewWidthListener.invalidated(listViewProperty().get().widthProperty());
}
};
}
public ChatItemView getView() {
return view;
}
private void initChatLabelView() {
view = new ChatLabelView();
}
private void initChatMessageView(ChatMessage message) {
view = new ChatMessageViewWrapper(message);
}
private void resetView() {
if(view == null) return;
view.unbindWidth();
listViewProperty().removeListener(listViewListener);
if(getListView() != null)
getListView().widthProperty().removeListener(listViewWidthListener);
view = null;
}
private void newViewCreated() {
listViewProperty().addListener(listViewListener);
setGraphic(view);
listViewListener.invalidated(listViewProperty());
}
#Override
protected void updateItem(ChatItem item, boolean empty) {
super.updateItem(item, empty);
if(item == null || empty) {
setGraphic(null);
setText(null);
resetView();
} else {
// First-level comparison: Label vs. Message
if(item.getViewType() == ViewType.LABEL) {
if(view == null || !(view instanceof ChatLabelView)) {
resetView();
initChatLabelView();
newViewCreated();
}
}
else if(item.getViewType() == ViewType.MESSAGE) {
ChatMessage message = (ChatMessage) item;
if(view == null || !(view instanceof ChatMessageViewWrapper)) {
resetView();
initChatMessageView(message);
newViewCreated();
}
else {
ChatMessageViewWrapper bubble = (ChatMessageViewWrapper) view;
// Second-level comparison: Self-message vs. other user message,
// Text vs. Image (or file), multiline vs. oneline
if( (bubble.getContentType() != message.getContentType())
|| (bubble.isSelf() ^ message.isSelf())
|| (bubble.isMultiline() ^ message.isMultiline())) {
resetView();
initChatMessageView(message);
newViewCreated();
}
}
}
else {
return;
}
view.setItem(item);
}
}
#Override
public Orientation getContentBias() {
return Orientation.HORIZONTAL;
}
}

Android. Remote the android tv box from a remote control

You have propaply seen those pretty cheap android tv boxes available on the market. They are usually followed with a remote control that has some functionalities like clicking, swipe or slide to left and right up and down.
Recently i made an app and tried to navigate it using the remote control. I have some gesture methods in the project. I tried to swipe to left and right but the app didnt do anything while when i try it on my phone that has a screen gets the gesture and do what it should. Like opening the navigation drawer etc.
Now to my question: does one need to use speciel methods? Is there some rules that one should be ware of?
Edit
This is what Ive done so far. I made a class that defines the action: It's from google.developer
public class Dpad {
public final static int UP = 0;
public final static int LEFT = 1;
public final static int RIGHT = 2;
public final static int DOWN = 3;
public final static int CENTER = 4;
int directionPressed = -1; // initialized to -1
public int getDirectionPressed(InputEvent event) {
if (!isDpadDevice(event)) {
return -1;
}
// If the input event is a MotionEvent, check its hat axis values.
if (event instanceof MotionEvent) {
// Use the hat axis value to find the D-pad direction
MotionEvent motionEvent = (MotionEvent) event;
float xaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_X);
float yaxis = motionEvent.getAxisValue(MotionEvent.AXIS_HAT_Y);
// Check if the AXIS_HAT_X value is -1 or 1, and set the D-pad
// LEFT and RIGHT direction accordingly.
if (Float.compare(xaxis, -1.0f) == 0) {
directionPressed = Dpad.LEFT;
} else if (Float.compare(xaxis, 1.0f) == 0) {
directionPressed = Dpad.RIGHT;
}
// Check if the AXIS_HAT_Y value is -1 or 1, and set the D-pad
// UP and DOWN direction accordingly.
else if (Float.compare(yaxis, -1.0f) == 0) {
directionPressed = Dpad.UP;
} else if (Float.compare(yaxis, 1.0f) == 0) {
directionPressed = Dpad.DOWN;
}
}
// If the input event is a KeyEvent, check its key code.
else if (event instanceof KeyEvent) {
// Use the key code to find the D-pad direction.
KeyEvent keyEvent = (KeyEvent) event;
if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_LEFT) {
directionPressed = Dpad.LEFT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_RIGHT) {
directionPressed = Dpad.RIGHT;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_UP) {
directionPressed = Dpad.UP;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_DOWN) {
directionPressed = Dpad.DOWN;
} else if (keyEvent.getKeyCode() == KeyEvent.KEYCODE_DPAD_CENTER) {
directionPressed = Dpad.CENTER;
}
}
return directionPressed;
}
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
}
In my MainActivity I have a navigationdrawer. which I want to open and close when remotecontrol D-pad wants it
mDrawerLayout.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.RIGHT:
// Do something for UP direction press Open the drawer
mDrawerLayout.openDrawer(Gravity.START);
return true;
case Dpad.LEFT:
mDrawerLayout.closeDrawer(Gravity.START);
return true;
}
}
return false;
}
});
In one of the fragments I have a media player and using D-pad up and D-pad down I change the video.
v.setOnGenericMotionListener(new View.OnGenericMotionListener() {
#Override
public boolean onGenericMotion(View view, MotionEvent motionEvent) {
if (Dpad.isDpadDevice(motionEvent)) {
int press = mDpad.getDirectionPressed(motionEvent);
switch (press) {
case Dpad.UP:
// Do something for UP direction press
UP(); // Change the video to next
return true;
case Dpad.DOWN:
DOWN(); // Change the video the earlier on
return true;
}
}
return false;
}
});
EDIT
It' now become a problem since it doesn't response to any motion. I tried on a emulator with physical keyboard and not a single action happens. I would appritiate if someone gives me a hint.
i mean this kind of remote control in the picture below
Thanks in advance
I think the code on Android Developer is wrong:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
!= InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}
The condition should be "==" instead of "!=", so this is correct:
public static boolean isDpadDevice(InputEvent event) {
// Check that input comes from a device with directional pads.
if ((event.getSource() & InputDevice.SOURCE_DPAD)
== InputDevice.SOURCE_DPAD) {
return true;
} else {
return false;
}
}

How can I show a button when all views are viewed in viewpager?

Right now I have a save button that I want to show only if all the views inside a viewpager are shown. That means that when the user swipes between views and have seen all views inside a viewpager, then show a save button.
I want to show the save button on every view when they have seen all views hereby after.
The trouble I am having is how to set up the logic. I started out with setting the save button invisible until the last view of the viewpager. On the last view of the viewpager, show the save button. But the problem is when the user goes to the last view (there's a save button) and then goes back to a previous view, the save button is gone.
So, I was wondering how can I show the save button permanently on all views after the user has seen all views?
Here's what I have so far:
I have this snippet inside my InstantiateItem() :
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
System.out.println("Is this called? isViewed = true");
}else if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
where
#Override
public void onPageSelected(int position) {
isViewed = true;
}
EDIT:
I tried the following solutions but with no luck.
Button save_button = (Button) findViewById(R.id.save);
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.length-1 && !isViewed[position]) {
isViewed[position] = true;
save_button.setVisibility(Button.VISIBLE);
}
isViewed[position] =true;
And
isViewed[position] = true;
if (isViewed[position] == isViewed[numberOfPages.length-1]) {
save_button.setVisibility(Button.VISIBLE);
}
if (isViewed[position]) {
save_button.setVisibility(Button.VISIBLE);
} else {
save_button.setVisibility(Button.INVISIBLE);
}
In your onPageSelected, do the following
if(isViewed)
{
save_button.setVisibility(Button.VISIBLE);
}
if (position == numberOfPages.size()-1) {
isViewed = true;
save_button.setVisibility(Button.VISIBLE);
}
Note the above are two seperate if statements.
Make your isViewed global and default to false.
boolean []isViewed = new boolean[noOfPages.size()];
#Override
public void onPageSelected(int position) {
if(isViewed[position])
{
save_button.setVisibility(Button.VISIBLE);
}
else {
save_button.setVisibility(Button.GONE);
}
isViewed[position] = true;
}

Flipper and ListView in android

I am using Flipper as parent and Listview as child. My problem here is the flipping and clicking of item in listview. When I flip to next page (by dragging from right to left) I accidentally click a list Item.
How will I disable the onClick of listview when I already made a gesture for flipping?
Code:
Flipper Ontouch:
public boolean dispatchTouchEvent(MotionEvent touchevent) {
super.dispatchTouchEvent(touchevent);
switch (touchevent.getAction()) {
case MotionEvent.ACTION_DOWN: {
lastX = touchevent.getX();
break;
}
case MotionEvent.ACTION_UP: {
float currentX = touchevent.getX();
if (lastX - 100 > currentX) {
if (result_pageNum < max_pageNum) {
result_pageNum++;
if (vf.getDisplayedChild() == 0) {
listView[1].setClickable(false);
setListView(1);
} else {
listView[0].setClickable(false);
setListView(0);
}
vf.setInAnimation(this, R.anim.in_from_right);
vf.setOutAnimation(this, R.anim.out_to_left);
vf.showNext();
}
} else if (lastX + 100 < currentX) {
if (result_pageNum > 0) {
result_pageNum--;
if (vf.getDisplayedChild() == 1) {
listView[0].setClickable(false);
setListView(0);
} else {
listView[1].setClickable(false);
setListView(1);
}
vf.setInAnimation(this, R.anim.in_from_left);
vf.setOutAnimation(this, R.anim.out_to_right);
vf.showPrevious();
}
}
break;
}
}
return false;
}
listView onClick:
private void listView_onClick() {
for (int i = 0; i < listView.length; i++) {
listView[i].setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.maketext(this,"Working!",Toast.LENGTH_LONG).show();
}
});
}
}
In your dispatchTouchEvent method, when a fling gesture has been detected (under ACTION_UP event), try returning true as a boolean value rather that returning false every time. When no fling gesture is detected according to your movement calculation, then only return false .
You shouldn't really be using dispatchTouchEvent(MotionEvent) for this purpose.
Instead you should use onInterceptTouchEvent(MotionEvent) and onTouchEvent(MotionEvent).
The relationship between these methods are documented in the methods' Javadoc, but as a summary:
onInterceptTouchEvent(MotionEvent) decides whether a ViewGroup intercepts a user touch events from any of it's child views. For instance, your ViewFlipper decides whether the ListView recieves the events.
onTouchEvent(MotionEvent) is where your ViewFlipper can actually react to it's touch events.

Categories

Resources