android get the drawable that has sett as drawabletop of textview - android

i have textview
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="#drawable/new" />
and i want on activity jave to know which image has set on drawableTop, how ?

Use
Drawable[] drawables = textView.getCompoundDrawables();
Also if you want to compare two drawable.
Bitmap bitmap = ((BitmapDrawable)drawables[1] ).getBitmap();
Bitmap bitmap2 = ((BitmapDrawable)getResources().getDrawable(R.drawable.twt_hover)).getBitmap();
if(bitmap == bitmap2)
{
//Code blcok
}

First, give your TextView an ID so you can find it in your Activity:
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:drawableTop="#drawable/new" />
Second, in your Activity find your TextView and get the compound drawables in your onCreate method after calling setContentView:
TextView textView = (TextView) findViewById(R.id.textView);
// Left, top, right, bottom drawables.
Drawable[] compoundDrawables = textView.getCompoundDrawables();
// This is the top drawable.
Drawable topCompoundDrawable = compoundDrawables[1];

I think you should use textView.getCompoundDrawables(); As for getting from drawable top i would think it would be the second drawable eg
Drawables tmp[] = textView.getCompoundDrawables();
tmp[1]; //this should be your top drawable but not 100% sure.

You cant get drawable ID in runtime. TextView uses ID only in it's constructor to load corresponding drawable. After drawable is loaded ID is gone.
In some cases there is no drawable ID at all. drawableTop can be a color rgb value.

Related

How to change color of drawable set as android:background?

I have an icon which look like this:icon in my constraint layout.
This is my layout xml for this image view:
<ImageView
android:id="#+id/vTotalPaidAvatar"
android:layout_width="40dp"
android:layout_height="40dp"
android:contentDescription="#null"
android:src="#drawable/ic_group_ic_total"
tools:src="#drawable/ic_group_ic_total"
app:layout_constraintLeft_toLeftOf="#+id/vSettleDebtsLayout"
android:layout_marginLeft="16dp"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="#+id/vSettleDebtsLayout"
android:layout_marginTop="16dp"
android:background="#drawable/avatar_circle" />
As you can see, the icon is just a dollar sign. I set that grey circle as background. The problem is that I need to change color of that background dynamically. The dollar sign need to stay white.
My question is, is there any way how to change only the color of background?
Another solution would be to use that circle as next imageview and then change color of that image view. But i think that there could be some more "clear solution for this issue.
Thanks for tips :)
Try:
yourImage.getBackground().setColorFilter(color, PorterDuff.Mode.SRC_IN);
This literally gets background (without src) from ImageView and covers its shape with color. The way the drawable and given color are combining is defined by mode of Porter Duff - here you have more about it.
Best way to do it:
public static void colorDrawable(View view, int color) {
Drawable wrappedDrawable = DrawableCompat.wrap(view.getBackground());
if (wrappedDrawable != null) {
DrawableCompat.setTint(wrappedDrawable.mutate(), color);
setBackgroundDrawable(view, wrappedDrawable);
}
}
public static void setBackgroundDrawable(View view, Drawable drawable) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
view.setBackgroundDrawable(drawable);
} else {
view.setBackground(drawable);
}
}
Well u must try
android:backgroundTint="your color"
in imageview tag
this will do the work
Did you try android:background="#color/my_color" and through code imageView.setBackgroundColor(Color.rgb(255, 255, 255)); ?

Android - draw9patch should help me? background of textview

I need to set background of the TextView without stretching like this:
I tried draw9patch but seems that it doesn't do that work. How can I reach this efect? I could also use ImageView, but I don't know how I can add text in ImageView (I cannot use layout or two views... image and text need to be in one view)
i think you should use drawableLeft
something like this :
<EditText
android:id="#+id/question_text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint=" your question"
android:drawableLeft="#drawable/ic_menu_info_details"
android:layout_weight="1"
android:singleLine="true" />
You can do it using a button and dont set any click listeners for it..:
Here is the code:
Button btnIconTxt=new Button(context);
btnIconTxt.setId(id);
//this aligns a image to the left of ur text view(This the important line).
btnIconTxt.setCompoundDrawablesWithIntrinsicBounds(new BitmapDrawable(context.getResources(),bitmapOfYourImage), null, null, null);
btnIconTxt.setText(message);
btnIconTxt.setTextSize(TypedValue.COMPLEX_UNIT_PX, sizeInPx);
btnIconTxt.setWidth(widthInPx);
btnIconTxt.setTextColor(Color.RED);
int currentApi = Build.VERSION.SDK_INT;
if(currentApi >= 16)
btnIconTxt.setBackground(btnColor);
else
btnIconTxt.setBackgroundDrawable(btnColor);
RelativeLayout.LayoutParams SDISP_params1 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
btnIconTxt.setLayoutParams(params1);
//U can set the x and y margin for this button using
//params1.leftMargin = xPixels;
// params1.topMargin = yPixels;
yourParentLayout.addView(btnIconTxt);

Parse RelativeLayout, Set Content, And Add It To A LinearLayout

Ok... here's my situation.
I have a carousel of images in a HorizontalScrollView - which contains a LinearLayout - in my Activity, like so:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/slider"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/carousel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
/>
</HorizontalScrollView>
I have a TypedArray, loop through it, and on each run, set these images programatically, add a ClickListener and a Tag, and add this ImageView to the LinearLayout (set in my Activity Layout), like so:
// Get the array
final TypedArray carouselArray = getResources().obtainTypedArray(R.array.carousel_array);
// Populate the Carousel with item
for (int i = 0 ; i < carouselArray.length() ; ++i) {
// Image Item
ImageView outerImage;
// Set the image view resource
if(i == 0) {
outerImage.setImageResource(R.drawable.toy_filter_clear);
}
else {
outerImage.setImageResource(carouselArray.getResourceId(i, -1));
}
// Set Touch Listener
outerImage.setOnTouchListener(this);
final String prepend = "CAROUSEL_";
final String index = String.valueOf(i);
final String tag = prepend.concat(index);
outerImage.setTag(tag);
/// Add image view to the Carousel container
mCarouselContainer.addView(outerImage);
}
But now, I just found out that I have to programatically add a second image to sit inside/on top of the first image at particular coordinates (damn you UI ppl!). I need these to be considered the same image/view essentially, so need to pack them together inside of a layout, I am assuming. So I have made a layout file, like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/carousel_item"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/carousel_outer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/toy_filter_normal"
/>
<ImageView
android:id="#+id/carousel_inner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/carousel_outer"
android:layout_alignRight="#+id/carousel_outer"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:src="#drawable/thumb_nofilter"
/>
</RelativeLayout>
This has the proper positioning, and the default images set on it. So what I want to be able to do is to reach into the Layout file, grab the ImageViews by their ID, overwrite the image if necessary, and then add that RelativeLayout to my LinearLayout at the end of my loop... sounds easy enough, right ?
My first attempt was to do it like this :
RelativeLayout item = (RelativeLayout) findViewById(R.id.carousel_item);
ImageView outerImage = (ImageView) item.findViewById(R.id.carousel_outer);
ImageView innerImage = (ImageView) item.findViewById(R.id.carousel_inner);
... but that gives me a NullPointer on the ImageView...So then I tried to inflate the RelativeLayout first, like this:
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.carousel_item_layout, null);
ImageView outerImage = (ImageView) view.findViewById(R.id.carousel_outer);
ImageView innerImage = (ImageView) view.findViewById(R.id.carousel_inner);
This gets rid of the NPE's, and (apparently) let's the images be set properly like so:
if(i == 0) {
outerImage.setImageResource(R.drawable.toy_filter_clear);
innerImage.setImageResource(0);
}
else {
outerImage.setImageResource(R.drawable.toy_filter_normal);
innerImage.setImageResource(carouselArray.getResourceId(i, -1));
}
but when I try to add the outerImage ImageView back to the LinearLayout, I get an NPE there:
mCarouselContainer.addView(outerImage);
More to the point, I don't want to add ONLY the one ImageView to the LinearLayout/HorizontalScrollView - I want to somehow pack the resulting images back into the RelativeLayout and add the whole thing back into my LinearLayout... but, it is worth mentioning, that this also gives me an NPE.
What is a guy to do ? Any thoughts appreciated...
Ok... Wow, thanks SO Code Monkey!
I managed to fix this with a one line fix, by adding the inflated View to the LinearLayout instead of the ImageView or the RelativeLayout (which wasn't doing anything), like so:
mCarouselContainer.addView(view);
Don't know why I hadn't tried that before, but I was unclear on whether as it's children were being updated if it would reflect the parent, so to speak... now I know it was.
I'm gonna keep the question up, as I think it's helpful... ?

How to get image from an ImageButton?

I'm not sure about the code, but I tried doing this:
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
int stuff2 = stuff.getId();
champs.setImageResource(stuff2);
But that didn't work, the Imagebutton was shrinked for some reason, there was no image, just a gray rectangle.
try this
Bitmap bitmap = ((BitmapDrawable)stuff.getDrawable()).getBitmap();
stuff.getId();
This will return the id of the View not the image resource associated with it. So you won't be having a resource related to this that is why you are not seeing the Image.
Set a valid drawable to setImageResource Some thing like R.drawable.drawable_id
Try this
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
Drawable d = stuff .getBackground();
champs.setBackgroundDrawable(d);
This will help get the drawable object and set the drawable object.You can also get the Bitmap object for image drawable This will set the exact drawable associated with the imagebutton
ImageButton mButton = new ImageButton(this);
Drawable drawable = mButton.getBackground();
champs.setImageDrawable(drawable)
You need to set the ID of drawable resource but you are setting ID of the ImageButton as image resource.
here stuff.getId() returns R.id.stuff.
set the valid drawable resource like
ImageButton stuff = (ImageButton) createView.findViewById(R.id.stuff);
stuff.setImageResource(R.drawable.id_of_image);

Placing one image over another image in android

I have a imageButton in android which user clicks. Then after his click I want t0 show a tick or a cross image on the toip of it.
How is it possible?
<ImageButton android:layout_width="wrap_content"
android:text="Button" android:id="#+id/button1"
android:layout_height="wrap_content" android:layout_gravity="center_horizontal"
android:background="#drawable/rhino" android:layout_marginRight="20dp"></ImageButton>
put your ImageButton and the tick image in a FrameLayout and make the visbility of the Tick image "Invisible" . So when you click on the ImageButton then change the state of Tick Image to Visible.
Get a reference to your ImageButton and then use one of its setImage methods, such as setImageResource.
You can achieve the same using ImageView. Use ImageView
testimage = (ImageView) findViewById(R.id.imageview);
testimage.setOnClickListener(listener);
write the logic to set both type of image to imageview in onclick event
public OnClickListener listener=new OnClickListener(){
#Override
public void onClick(View arg0) {
System.out.println("..set image button..");
Drawable[] layers = new Drawable[2];
layers[0] = getResources().getDrawable(R.drawable.btn_call);
layers[1] = getResources().getDrawable(R.drawable.blue_unfocus);
System.out.println(layers[1]+"...Drawable..."+layers[0]);
LayerDrawable layerDrawable = new LayerDrawable(layers);
testimage.setImageDrawable(layerDrawable);
}
};
Thanks
Deepak

Categories

Resources