Android drag and drop TextView bug - android

I followed this tutorial for drag and drop functionality and it works except for a strange bug.
Dragging textview A onto Textview B is suppose to:
hide textview A
pass a tag from textview A to textview B
redisplay the passed data on textview B
Passing the data from textview A to B works 100% of the time, but oddly, textview A does not go invisible 100% of the time. I dont know if its the way I'm physically dragging it over textview B or something. Any idea how I can make that more stable?
public boolean onDrag(View v, DragEvent event) {
//handle drag events
switch (event.getAction()) {
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a drop view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
TextView dropTarget = (TextView) v;
//view being dragged and dropped
TextView dropped = (TextView) view;
//update the text in the target view to reflect the data being dropped
dropTarget.setText(dropped.getText());
//make it bold to highlight the fact that an item has been dropped
dropTarget.setTypeface(Typeface.DEFAULT_BOLD);
//if an item has already been dropped here, there will be a tag
Object tag = dropTarget.getTag();
//if there is already an item here, set it back visible in its original place
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
findViewById(existingID).setVisibility(View.VISIBLE);
}
//set the tag in the target view to the ID of the view being dropped
dropTarget.setTag(dropped.getId());
// check choice
checkChoice(dropped,dropTarget);
break;

Think I found the problem
if(tag!=null)
{
//the tag is the view id already dropped here
int existingID = (Integer)tag;
//set the original view visible again
//findViewById(existingID).setVisibility(View.VISIBLE); COMMENT THIS OUT
}

Related

How can I limit the number of objects that the user can drag and drop to a specific layout?

I developed a card sort app that the user can drag and drop cards on the screen according to the code here-
drag and drop code
how can I limit the number of objects that can be dragged to a specific layout?
(i would like to limit the number of objects to only one for each layout)
It appears as though it is in the DragEvent.ACTION_DROP where the view is added to the new parent. So when that happens you can simply check if the view your adding it to already has a certain number of children:
Change this:
case DragEvent.ACTION_DROP:
...
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);//remove the dragged view
LinearLayout container = (LinearLayout) view;
container.addView(v);
v.setVisibility(View.VISIBLE);
to this:
case DragEvent.ACTION_DROP:
...
LinearLayout container = (LinearLayout) view;
if (container.getChildCount() < 1) { // only move the view if the container has no kids
View v = (View) event.getLocalState();
ViewGroup owner = (ViewGroup) v.getParent();
owner.removeView(v);
container.addView(v);
}
v.setVisibility(View.VISIBLE);

how to set background to imageView by imageview casted View?

I want to drop an image over an image and I have a code in which View is type casted by ImageView with v.name 'dropped' and this view will display on other ImageView named 'droptarget' as background but cant happend, plzz tell me a solution. my code is here.:
case DragEvent.ACTION_DROP:
//handle the dragged view being dropped over a target view
View view = (View) event.getLocalState();
//stop displaying the view where it was before it was dragged
view.setVisibility(View.INVISIBLE);
//view dragged item is being dropped on
ImageView dropTarget = (ImageView) v;
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//it give me error in this line
**dropTarget.setBackgroundResource(dropped);**
You should give us more info about the code but anyway I'm going to suggest something.
I think you're calling setBackgroundResource on a null object. So you're mistake is on the next line
ImageView dropTarget = (ImageView) v;
There probably isn't a view called "v". Maybe you should change that?
I believe you are looking for ImageView.setImageResource. You can also set the backgroundImage, but best practice is to use setimageResource.

Change a TextView to an ImageView

In a Drag & Drop Activity,
I want to Drag an image and drop it over a TextView.
And want to change the text view to the image view, that I have dropped.
case DragEvent.ACTION_DROP:
View view = (View) event.getLocalState();
view.setVisibility(View.INVISIBLE);
//This is the textView of the Traget
TextView dropTarget = (TextView) dragView;
//Possibly now the View would be an image View now,
//view being dragged and dropped
ImageView dropped = (ImageView) view;
//Now here I want to set the textView of dropTarget as the ImageView of dropped, THIS I WANT TO UNDERSTAND TO HOW TO DO THIS
dropTarget.setAnimation(dropped.getDrawable());
break;
Please help!!!!
You will use TextView and set background to textview this will help to you.

LayerDrawable goes off the screen in a ListView and doesn't draw when I scroll ListView back to it

I've a ListView and when my audio is paused I draw a LayerDrawable to show progress of how much was played. The problem is when I sroll this ListView element off the screen and get it back - I don't see any of the Image back there.
This is part from the adapter :
public View getView(int position, View convertView, ViewGroup parent) {
View view = convertView;
allView=convertView;
if (view == null) {
view = lInflater.inflate(R.layout.item, parent, false);
}
............
switch (whatToDo) {
//here's my normal state of the Image
case 0 :
((ImageView) view.findViewById(R.id.ivImage)).setImageResource(p.imagestatus);
break;
//here's my Paused state of the Image - what I need to stay on the screen
case 1:
Log.e ("MyLog","paused! imagestatus="+p.imagestatus);
//Drawing progress
LayerDrawable ld= (LayerDrawable) ctx.getResources().getDrawable(R.drawable.onpause);
ClipDrawable cd=(ClipDrawable) ld.findDrawableByLayerId(R.id.clip_pause);
p.imagestatus=-p.imagestatus;
cd.setLevel(4500+(30*p.imagestatus));
((ImageView) view.findViewById(R.id.ivImage)).setImageDrawable(ld);
break;
...
return view;
}
so, as you see, pretty much , usual getView method.
But when this Image goes off the screen - it never come back.
How can I fix it?

Scrolling through ListView of ProgressBars

I'm working on an Android app that utilizes a ListView, in which each row is comprised of a text view and a progress bar. Things work smoothly unless the user has to scroll through a long list.
ProgressBars start taking on the progress of other ProgressBars not currently visible on the screen.
I understand that this is a common issue that stems from the implementation of GetView, but I'm wondering what the best course of action is to take with ProgressBars.
GetView:
public View getView(int position, View convertView, ViewGroup parent){
View row = convertView;
ViewWrapper wrapper;
ProgressBar myProgressBar;
int initProgress = myData.get(position).getProgress();
if (row == null){
LayoutInflater inflater = context.getLayoutInflater();
row = inflater.inflate(R.layout.row, null);
wrapper = new ViewWrapper(row);
row.setTag(wrapper);
}
else{
wrapper = (ViewWrapper)row.getTag();
}
RowModel model = getModel(position);
wrapper.getPid().setText(model.toString());
myProgressBar = wrapper.getProgressBar();
myProgressBar.setProgress(initProgress);
myProgressBar.setMax(100);
myProgressBar.setTag(new Integer(position));
return row;
}
ViewWrapper:
public class ViewWrapper {
View base;
TextView pid = null;
ProgressBar pb= null;
ViewWrapper(View base){
this.base = base;
}
TextView getPid(){
if(pid == null){
pid = (TextView)base.findViewById(R.id.pid);
}
return(pid);
}
ProgressBar getProgressBar(){
if(pb== null){
pb= (ProgressBar)base.findViewById(R.id.progressbar);
}
return(pb);
}
}
It seems that the issue is related to:
myProgressBar = wrapper.getProgressBar();
because that ProgressBar starts getting the behavior of a recycled ProgressBar. However, I want it to have its own behavior.
What's the best way to alleviate this? Thanks.
You may need to inflate the layout each time and not re-use the convertView that's passed in. This shouldn't be a problem unless you have A LOT of rows.
I had to implement a similar feature , here is what I did . I implemented the following outside getview
OnTouchListener - to listen to seekbar touch events
OnKeyListener - to listen to dpad and trakball event for the seekbar
I set these listeners for the seekbars from getView
Whenever the listeners were called , I would find the seekbars parent , then do the findviewbyid from the parent to the textview.
So now I have the textview which I have to update, and the seekbar. All I need to do was set the text from the array.
here is some code to help you.
private OnTouchListener touchListener = new OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
View parent = (View) v.getParent();
TextView textView = (TextView) parent
.findViewById(R.id.id_grade_tooltip);
if (textView == null) {
} else {
SeekBar seekBar = (SeekBar) v;
textView.setText(String.valueOf(seekBar.getProgress())
+ "%");
}
return false;
}
};

Categories

Resources