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.
Related
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);
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.
Let's say I have 4 images:
ImageView a = (ImageView) findViewById(R.id.a);
ImageView b = (ImageView) findViewById(R.id.b);
ImageView c = (ImageView) findViewById(R.id.c);
ImageView d = (ImageView) findViewById(R.id.d);
And then I set the visibility of those images by this:
a.setVisibility(View.INVISIBLE);
b.setVisibility(View.INVISIBLE);
c.setVisibility(View.INVISIBLE);
d.setVisibility(View.INVISIBLE);
Is there anyway I can group those 4 images and then set the visibility to that group of images?
Something like this:
images[] = {a,b,c,d};
images.setVisibility(View.INVISIBLE);
Thank you.
If you don't want to use a ViewGroup your basic idea is good. I use it often. Just do
ImageView images[] = {a,b,c,d};
for (ImageView view : images) {
view.setVisibility(View.INVISIBLE);
}
The usual approach would be to have all the views in the same ViewGroup i.e. a layout and then set the visibility of the group in order to apply it to all children. But this really depends on how the views are laid out in the first place.
You can put all those images in layout and then control visibility of that layout.
Hi I am developing an app in android where I use a subclass that extends BaseExpandableListAdapter. Right now I have problem to combine ImageView and TextView in the list that is showing. Yesterday I found this link on stackoverflow that helped me to make this combination.
Overlay text over imageview in framelayout programmatically - Android
So it works FINE! - until I click on a listItem. The app chrash and the logcat tells me that
"android.widget.RelativeLayout" cannot be cast to android.widget.ImageView. This exception comes the getGroupView() in the class that extends BaseExpandableListAdapter . Why does this happen? (RelativeLayout extends View).
Am I completely on the wrong way when I try to return a RelativeLayout instead of an ImageView?
Here's my code from getGroupView (I'm a bit messy because I am in a teststate) :
public View getGroupView(int groupPosition, boolean isExpanded, View convertView, ViewGroup parent) {
ImageView row = (ImageView) convertView;
RelativeLayout rLayout = new RelativeLayout(mContext);
LayoutParams rlParams = new LayoutParams(LayoutParams.FILL_PARENT
,LayoutParams.FILL_PARENT);
rLayout.setLayoutParams(rlParams);
if(row == null) {
row = new ImageView(mContext);
}
row.setImageResource(R.drawable.ic_launcher);
row.setScaleType(ImageView.ScaleType.FIT_START);
RelativeLayout.LayoutParams tParams = new RelativeLayout.LayoutParams
(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
tParams.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
tParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, RelativeLayout.TRUE);
TextView text=new TextView(mContext);
text.setText("GOLDEN Gate");
text.setTextColor(Color.WHITE);
text.setTypeface(Typeface.DEFAULT_BOLD);
text.setLayoutParams(tParams);
rLayout.addView(row);
rLayout.addView(text);
return rLayout;
}
The statement ImageView row = (ImageView) convertView; gives the exception because u r trying to convert the RelativeLayout to ImageView.
You must return the convertView in getViewGroup() method but you are returning the RelativeLayout i.e., return rLayout;
Solution :
Add a ImageView and TextView inside a RelativeLayout. Set that layout for ListItem by inflating it inside getView() method and assign the inflated layout to convertView. Return the convertView in getView() method;
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
}