Positioning in drag and drop layout - java

i have a few problem here regarding the drag and drop. I already create the code for drag and drop, its working finely. But here's the problem, as u can see in the images below each jar can fits only 4 sweets, but when i drag sweets from the second jar to the first one, it can still be drop into it. How can i make each jar can only be fixed with four sweets?
This is the coding that I've done for the drag and drop. Should I use the if else statement somewhere in this coding to solve my problem?
public boolean onDrag(View v, DragEvent event) {
int action = event.getAction();
View view = (View) event.getLocalState();
switch (event.getAction()) {
case DragEvent.ACTION_DRAG_STARTED:
// Do nothing
break;
case DragEvent.ACTION_DRAG_ENTERED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundDrawable(normalShape);
break;
case DragEvent.ACTION_DROP:
// Dropped, reassign View to ViewGroup
ViewGroup owner = (ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container = (LinearLayout) v;
container.addView(view);
view.setVisibility(View.VISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
//v.setBackgroundDrawable(normalShape);
if (dropEventNotHandled(event))
{
view.setVisibility(View.VISIBLE);
}
break;
default:
break;
}
return true;
}
private boolean dropEventNotHandled(DragEvent event) {
// TODO Auto-generated method stub
return !event.getResult();
}
}
}

inside the case DragEvent.ACTION_DRAG_ENDED block you should be checking to see if theres room in the jar and if not then return false so that the drop action fails.

Related

How to get where is my finger, over of which layout?

I have a block, I drag it somewhere then this cell transform to yellow then I exitted this cell, It transforms again gray but, When I drop it somewhere Everywhere is magenta, How can I paint only that block When I drop it, You can check pictures to understand my problem
Best regards
public boolean onDrag(View v,DragEvent event)
{
int action=event.getAction();
switch (action)
{
case DragEvent.ACTION_DRAG_STARTED:
Log.i("Script",num+"-ACTION_DRAG_STARTED");
if (event.getClipDescription().hasMimeType(ClipDescription.MIMETYPE_TEXT_PLAIN))
{
return (true);
}
return (false);
case DragEvent.ACTION_DRAG_ENTERED:
Log.i("Script",num+"-ACTION_DRAG_ENTERED");
v.setBackgroundColor(Color.YELLOW);
break;
case DragEvent.ACTION_DRAG_LOCATION:
break;
case DragEvent.ACTION_DRAG_EXITED:
v.setBackgroundColor(Color.GRAY);
break;
case DragEvent.ACTION_DROP:
View view=(View) event.getLocalState();
ViewGroup owner=(ViewGroup) view.getParent();
owner.removeView(view);
LinearLayout container=(LinearLayout) v;
container.addView(view);
view.setVisibility(View.INVISIBLE);
break;
case DragEvent.ACTION_DRAG_ENDED:
v.setBackgroundColor(Color.MAGENTA);
break;
}
return true;
}
In DragEvent.ACTION_DRAG_ENDED event you should find view.getX() and view.getY(). This will return x and y co-ordinate in pixels then according to your requirement you should calculate nearest view which occupy this blocks and change its color to MAGENTA.

Scroll view does not scroll on first touch

I have the following code, all I want is to detect touch on a edit text and scroll the scroll view:
View.OnTouchListener buttonFocus = new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
System.out.println("TOuch");
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
System.out.println("TOuch down");
getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
break;
case MotionEvent.ACTION_UP:
System.out.println("TOuch up");
((ScrollView) findViewById(R.id.m_scroll)).fullScroll(View.FOCUS_DOWN);
v.performClick();
break;
default:
break;
}
return false;
}
};
While as the keypad pops up at the start of activity the scroll does not occur, it only scroll when I touch the view for the second time. Am I doing something wrong here? Will implementing a thread help?
Returning true in that last line of yours might solve your problem.
Returning false means that the onTouch has been used completely and won't entertain any further touch events

Alternative to make an action when pressing/realising a button, android

I want to make an application with 9 buttons for android 4.0, I need each of the buttons to call a method when pressed and another method when released.
I'm kind of surprised there's not a really straight-forward method to accomplish what I just described but thanks to some questions and answers I read on stackoverflow I managed to accomplish what I need by adding a listener in each of the buttons, as shown for one of the buttons in the next code.
buttonUp.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_DOWN) {
buttonUpPressed();
} else if (event.getAction() == MotionEvent.ACTION_UP) {
buttonUpReleased();
}
return true;
}
});
I want to know if there's an alternative to accomplish this behavior without having to add a listener for every button. I see that as a waste of resources and a lot of coding. I can't imagine if I eventually have to add more buttons.
So any help would be appreciated.
implements your class with OnTouchListener
Button a,b;
Button[] bArray = {a,b};
for (int i = 0; i < bArray.length; i++) {
bArray[i].setOnTouchListener(this)
}
after that add unimplemented method
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
if(event.getAction() == MotionEvent.ACTION_DOWN){
switch (v.getId()) {
case R.id.button1:
break;
default:
break;
}
}
else if(event.getAction() == MotionEvent.ACTION_UP){
switch (v.getId()) {
case R.id.button1:
break;
default:
break;
}
}
return false;
}
i hope you'll like my logic :)
You can add all the buttons to an array or ArrayList and then define a listener field in your class.
OnTouchListener oclButtons = new OnTouchListener(...);
Then you can add the same listener to all the Buttons in the ArrayList.
for (Button b : listOfButtons) {
b.setOnTouchListener(oclButtons);
}
An alternative to setting IDs for each Button is to get/set tags (since each view needs a unique ID). Your listener can determine which button was touched by using v.getTag().

How to change text on spinner selection

I have a couple of textviews with a couple of spinners lined up in a table. There are 4 choices in the spinners, all of them the same, the first choice being '--'. I want the textview next to the spinner to be red when the default option '--' is selected, and white otherwise.
I coded this to handle this but when you select one spinner's different option, all of the textviews change It's behavior is just really strange.
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(parent.getId()) {
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView1.setTextColor(Color.RED);}else{textView1.setTextColor(Color.WHITE);}
case R.id.spinner2:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView2.setTextColor(Color.RED);}else{textView2.setTextColor(Color.WHITE);}
case R.id.spinner3:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView3.setTextColor(Color.RED);}else{textView3.setTextColor(Color.WHITE);}
}
}
You need to put a
break;
at the end of each of your cases.
public void onItemSelected(AdapterView<?> parent,
View view, int pos, long id) {
switch(parent.getId()) {
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView1.setTextColor(Color.RED);}else{textView1.setTextColor(Color.WHITE);}
break;
case R.id.spinner2:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView2.setTextColor(Color.RED);}else{textView2.setTextColor(Color.WHITE);}
break;
case R.id.spinner3:
if(parent.getItemAtPosition(pos).toString().equals("--")){textView3.setTextColor(Color.RED);}else{textView3.setTextColor(Color.WHITE);}
break;
}
}
You've forgotten "break" in your case blocks:
case R.id.spinner1:
if(parent.getItemAtPosition(pos).toString().equals("--")){
textView1.setTextColor(Color.RED);
} else {
textView1.setTextColor(Color.WHITE);
}
break;
and so on.

How to handle touch or press down events in Android?

Alright so i have 4 image views..
How can i control what happens when they are pressed down and up
So say like:
one of the images is press down a textview gets changed to 1 but when they let go of that one the text will change back to 0?
You need to use an OnTouchListener and have it listen for TouchEvents on your Views.
For Example:
MyTouchListener l = new MyTouchListener();
view.setOnTouchListener(l);
Here is an example of what MyTouchListener could look like:
class MyOnTouchListener implements OnTouchListener {
public boolean onTouch(View v, MotionEvent event) {
switch(event.getAction()) {
case MotionEvent.ACTION_DOWN:
// touch down code
break;
case MotionEvent.ACTION_MOVE:
// touch move code
break;
case MotionEvent.ACTION_UP:
// touch up code
break;
}
return true;
}
}

Categories