- android get the drawable that has sett as drawabletop of textview
- 4 Answers 4
- Not the answer you’re looking for? Browse other questions tagged android textview or ask your own question.
- Linked
- Related
- Hot Network Questions
- Subscribe to RSS
- Get Drawable from image view
- 4 Answers 4
- How to create Drawable from resource
- 8 Answers 8
- how to get the source of ImageView in order to change it?
- 7 Answers 7
- How to do that?
- What if I already have a tag on my view
- Android Shape Drawables Tutorial
- Have you ever wanted to reduce your Android application’s size or make it look more interesting? If yes, then you should try out ShapeDrawables.
- Why should you use ShapeDrawables?
- Are there any disadvantages?
- Let’s start coding
- Create a simple gradient ShapeDrawable in XML
android get the drawable that has sett as drawabletop of textview
i have textview
and i want on activity jave to know which image has set on drawableTop , how ?
4 Answers 4
Also if you want to compare two drawable.
First, give your TextView an ID so you can find it in your Activity :
Second, in your Activity find your TextView and get the compound drawables in your onCreate method after calling setContentView :
I think you should use textView.getCompoundDrawables(); As for getting from drawable top i would think it would be the second drawable eg
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.
Not the answer you’re looking for? Browse other questions tagged android textview or ask your own question.
Linked
Related
Hot Network Questions
Subscribe to RSS
To subscribe to this RSS feed, copy and paste this URL into your RSS reader.
site design / logo © 2021 Stack Exchange Inc; user contributions licensed under cc by-sa. rev 2021.12.3.40888
By clicking “Accept all cookies”, you agree Stack Exchange can store cookies on your device and disclose information in accordance with our Cookie Policy.
Источник
Get Drawable from image view
When I do the clip drawable example described in this document ImageView.getDrawable always return null. Can anyone pls help me?
In MainActivity.java onCreate
4 Answers 4
You have to override onWindowsFocusChanged()
the imageView was not yet displayed properly when you tried to get its value(drawable image). onWindowsFocusChanged() will inform the user that the view has been loaded already that’s the time you can get its data.
May be you will get null drawable so this error will occure.
First set any image to ImageView so you will get Drawable.
This is an easy approach if you can use the view’s id member variable: just store the R.drawable id using v.setId(). Then get it back with v.getId().
None of the answers till now is correct
The issue here is that in the image view the drawable is attached using android:background=»@drawable/clip» and it is retrieved using ClipDrawable drawable = (ClipDrawable) imageview.getDrawable();
Change the imageview xml
OR
Change the button callback code
ClipDrawable drawable = (ClipDrawable) imageview.getDrawable(); -> ClipDrawable drawable = (ClipDrawable) imageview.getBackground();
Don’t change both, otherwise the problem will get created again. Change any one.
Источник
How to create Drawable from resource
I have an image res/drawable/test.png (R.drawable.test).
I want to pass this image to a function which accepts Drawable , e.g. mButton.setCompoundDrawables() .
So how can I convert an image resource to a Drawable ?
8 Answers 8
Your Activity should have the method getResources. Do:
As of API version 21 this method is deprecated and can be replaced with:
If you need to specify a custom theme, the following will apply it, but only if API is version 21 or greater:
This code is deprecated:
Use this instead:
The getDrawable (int id) method is deprecated as of API 22.
Instead you should use the getDrawable (int id, Resources.Theme theme) for API 21+
Code would look something like this.
I would just like to add that if you are getting «deprecated» message when using getDrawable(. ) you should use the following method from the support library instead.
You do not have to use getResources() when using this method.
This is equivalent to doing something like
This works on both pre and post Lollipop versions.
Get Drawable from vector resource irrespective of, whether its vector or not:
Источник
how to get the source of ImageView in order to change it?
I know that changing the ImageView resource is not big deal just using myImageView.setImageResource(mynewImageDrawable)
but what I want to do is to check the current ImageSource before changing it.
basically, I want to implement my own group radio buttons using imageViews. so every time any imageView is clicked, the oncliked event method will change the Image Resources of my group.
and sorry for my poor English.
7 Answers 7
There is no getDrawableId function so you’ll need to do something like set a tag for the ImageView when you change its drawable. For instance, set the drawable id as a tag for the ImageView so you could just get the drawable id from the tag.
How to do that?
I’d say 90% of the time, your views wont have any tag on them, so the easiest way is to assume your tag is the only tag:
What if I already have a tag on my view
Android views can host multiple tags at the same time, as long as they have a unique identifier. You’d need to create a unique id resource and add it as the first argument to the setTag method call. Leaving the code like this:
To get Image source, you can use Drawable.ConstantState . For example, in my problem I was implementing a button click to change the ImageView. I created two Drawable.ConstantState objects and compared them. The code is given below:
Are you saying that you intend to check the resource id to determine if a radio button is selected or not? While this could probably work, it isn’t a very good design. Generally speaking you want to separate the model from the view (otherwise known as the Model-View-Controller paradigm). To do this, you might have your radio button group maintain an index for the selected item, and have each of your items stored in a list. When an item is selected, you can determine its index, update the group’s model, and then update the UI to reflect that change.
Its not that what you’re proposing wont work, it just isn’t good design and leads to poor maintainability (such as if the resource names change).
Источник
Android Shape Drawables Tutorial
Nov 3, 2017 · 6 min read
Have you ever wanted to reduce your Android application’s size or make it look more interesting? If yes, then you should try out ShapeDrawables.
First, we will go over the advantages and disadvantages of the ShapeDrawables. Then we will create some Drawables that could be used in your app and lastly for the grand finale we will try to replicate a gradient as can be seen in the Spotify app/website.
Why should you use ShapeDrawables?
When you want to use PNG or JPEG images in your applic a tion, you have to provide multiple copies of the same image for different screen densities. That, of course, clutters your app with copies of the same image. Yes, sometimes that is the path we have to choose because we can’t use Drawables for every single case, but we can dramatically reduce our application’s size if we can use Drawables instead. ShapeDrawables are a series of commands that tell how to draw something on the screen. That is why they can be resized and stretched as much as we want, without losing any quality. We can recolor and manipulate them even when the app is running and use the same ShapeDrawable multiple times in our app. Since ShapeDrawables are a subclass of the Drawable abstract class, we can use them in methods where a Drawable is expected. Click for the documentation of the ShapeDrawable.
Are there any disadvantages?
Of course, just like I have mentioned before we can’t use them in every case. I have said before that ShapeDrawable class is a subclass of the Drawable abstract class. There are other subclasses as well and every one of them has its own use case. You can click here to check other Drawable types and figure out which one is right for your case. Another issue is that they took a bit longer to draw than a Bitmap since there is a lot of parsing and drawing going on behind the scenes. But I think that is not a huge problem if your Drawables are simple.
My opinion is that you should use Drawables (ShapeDrawables) wherever you can, because they are easy to modify and they don’t take much space.
Let’s start coding
First let’s take a look at a simple example and then we will recreate a gradient as can be seen in the Spotify app/website.
Create a simple gradient ShapeDrawable in XML
First create a new drawable resource file.
Right click on res/drawable > New > Drawable resource file > give your file a name > use shape as a root element > click Ok
Shape root element defines that this is a ShapeDrawable.
This is how the first example looks like:
Источник