how to set background to imageView by imageview casted View? - android

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.

Related

How to change border/background color for android gallery

I created a simple gallery following a tutorial.
Everything works fine but i wasn´t able change the border/background color.
Maybe a small image helps to understand my Problem.
desired colorchanges
I get a scaled image with a dark grey filled background. These rectangle is bordered in a light gray which I want to change.
I tried:
public View getView(int position, View convertView, ViewGroup parent) {
ImageView imageView = new ImageView(mContext);
imageView.setImageBitmap(imageBitmaps[position]);
imageView.setLayoutParams(new Gallery.LayoutParams(300, 200));
imageView.setScaleType(ImageView.ScaleType.FIT_CENTER);
imageView.setBackgroundResource(imageBackground);
**imageView.setBackgroundColor(Color.BLUE);**
...
but this doesn´t work because the dark grey part is painted in blue, too.
Any ideas?
Edit: adding some code snippets and wrong result
Using setBackgroundColor will lead to this result
My gallery creation:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGallery = (Gallery)findViewById(R.id.imgGallery);
mAdapter = new ImageAdapter(this);
mGallery.setAdapter(mAdapter);
But when
public View getView(int position, View convertView, ViewGroup parent)
is called
convertView is null.
Try something like this (I haven't tested though):
switch (position) {
case 1:
imageView.setBackgroundColor(convertView.getContext().getResources().getColor(R.color.blue));
break;
case 2:
break;
//... and so on
}
You could alse try this one imageView.setBackgroundColor(new ColorDrawable(Color.BLUE));
Let me know if this didn't help you!
EDIT:
Try with this and let me know if anything changes:
imageView.setBackgroundColor(Color.parseColor("#FF0000"));
Another option is, instead of instantiating that ImageView programatically, just build your own xml with your ImageView inside and later do an LayoutInflater.from(mContext).inflate(R.layout.your_image_view, parent, false);

Set visibility for a group of images

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.

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.

Android drag and drop TextView bug

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
}

How to get id of view in parent layout?

how to get view id in parent layout? I can get view by id, but if I can get id by view?
Update:
What i do
I create and add to LinearLayout some View:
LinearLayout ll = new LinearLayout();
// add few View
for (int i=0;i<100;i++){
MyView mView = new MyView(this);
// MyView has time field, and at creation time MyView stack ordered by time-field
mView.setTime(anyTime+i);
// When I click on mView - opens screen where I can modify time
mView.setOnClickListener(listener);
ll.addView(mView)
}
So after mView modifying I won to move mView in correct place, for this I need know index in LinearLayout (ll) of current MyView, index of previous and next MyView
Update 2:
Maybe I need List where I will sort MyView and redraw all LinearLayout
In my point of view - without ID you can't access the views.U can go for tag.for that In view you can setTag() and you can getTag().we can use tag also as ID somecases.
XML
<ImageView
android:id="#+id/image1"
android:layout_height="WRAP_CONTENT"
android:layout_width="FILL_PARENT"
/>
ImageView imageView = new ImageView(this);
String imageName = "image1"// Your ImageView id name
int id = getResources().getIdentifier(imageName, "id", getPackageName());
getIdentifier Description
Hope it helps you out!!
int ViewGroup.indexOfChild(View child) will help me. I will write more, when I test all decigion.

Categories

Resources