I would like to add text like this, is this possible with MaterialLayout & MaterialTextInput ??
It can be done in two ways.
The more complicated way is to extend the MaterialTextInput and override methods.
The simple way is to put the MaterialTextInput in a FrameLayout along with a TextView with a fixed size. Create a same size dummy Drawable (eg: ColorDrawable) and set it as end drawable.
Comment for further information.
Related
I have a image "myXmlImage" in my .xml file
In the .kt file, I want to paste another "newImage" image as well as a little text "newText".
myXmlImage.setImageDrawable(newImage.drawable)
myXmlImage.imageMatrix = newImage.imageMatrix
So far it has been working very well and the new image is in place.
I'm completely stuck on how to paste the little "newText" into it
Any ideas will be greatly appreciated, thanks in advance.
Perhaps there's more than what you've described here. But if those are really your only specifications, then what you're asking for is really easy.
I'm assuming that your xml file has a root of ConstraintLayout. If it doesn't, then you're going to either want to change it or at least wrap your ImageView inside of a ConstraintLayout.
Use the layout editor to place a TextView inside of your ImageView (NOT AS A CHILD; when I say "inside", I mean spacially inside). If you haven't used the layout editor very much, it might take you a couple of tries to place the TextView inside. Don't just drag it into the ImageView as if it were a child. Drag it and drop it under the ImageView inside of the ConstraintLayout and then use little circles on the sides to attach it to the sides of the ImageView. Don't forget to set the text of the TextView as "".
(I'm not actually sure if this step is necessary because I don't know what the default background of a TextView is, but I do it out of habit.)When you're done placing that TextView inside of the ImageView, go the code of your xml file and use android:background="#00FFFFFF" (or maybe somewhere in your project you have either a transparent background drawable or a transparent #color that you can reference by name).
I'm sure you know the rest. Just give that TextView an Id like newtext and inside of your activity retrieve the view: newText:TextView=findViewById(R.id.newtext). And then set the text: newText.text="new text".
I am trying to make something like this example:
I want the drawable to show when I verify the user name from the server.
I am using Material Design Text Input Field & EditText. Yes, I can do it with a simple EditText and an ImageView, but I want to use the standard elements.
I have looked at the official documentation, there is a way to add the image at the Right corner using XML, but I want to add it programmatically.
Use the setEndIconDrawable method:
textInputLayout.setEndIconDrawable(R.drawable.xxxx);
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
or in a layout:
<com.google.android.material.textfield.TextInputLayout
app:endIconDrawable="#drawable/xxxxx"
app:endIconMode="custom"
More info in the official doc.
You need to to set your drawable programmatically like below
inputTextEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)
The question is pretty much exactly what I want, lets say i have this text:
"{W}, {T}: Tap target creature."
I have to replace the {W} and the {T} for 2 tiny little images pretty much the same size of the 3 letters(in case {W} and {T})... Is that possible in some way?
Just to say, the TextView is inside a Listview...
the Position for {W} and {T} are not always the same...
the images are in my resource...
The one workaround you can try is to place your TextView and ImageView inside a FrameLayout, one on top of the other. Now when you want to change them, just make one of them invisible and initialize the other. This will probably work. Hope this helps.
Did you try the attribute drawableLeft of the text view? But since you have two images, you will have to use a single image file which consists of both {w}{t}. so you will have to put the images with all combinations of {w}{t} in your resources.
then use the following method in your code:
textView.setCompoundDrawables(leftDrawable, null, null, null);
There is no setBackground() method in the RemoteViews class, so I've used the following workaround:
Created a FrameLayout for my app widget with an ImageView as the background view.
Changed the ImageView image using setImageViewResource() method.
Unfortunately, when it comes to 9-patch drawables this method does not work. Also when an ImageView's android:src attribute points to 9-patch - it doesn't work too. Is there any way to change the AppWidget's background image programatically using a 9-patch drawable? Thanks in advance.
EDIT
Settings the 9-patch as initial background in the XML:
<ImageView
android:id="#+id/small_widget_layout_bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_lime" />
When I use android:src="#drawable/background_lime" it doesn't stretch the image properly, this code works fine. And the code to change the background from the AppWidgetProvider onUpdate method:
views.setImageViewResource(R.id.small_widget_layout_bg,
R.drawable.backgroung_lime);
This does not stretch the image as a 9-patch.
This answer was diagnosed in the above comments...
RemoteView doesn't allow access to View.setBackground(), so your workaround of using the android:src property of an ImageView is good, providing that the android:scaleType property is set to fitXY.
ImageView won't stretch it's foreground image unless you tell it to.
Please ignore if u find this trivial or irrelevant, but canT you try (assuming you are dealing with widgets):
Declaring different layouts (xml)for your widget.
Change the remoteView's source (layout.id) instead of trying to make alterations to the selected layout.
AFAIK, this is the most common approach to solving such problems. This is not perfect for two simple things I could note myself:
What do you do if you have n different "states" / "views" in your widget?
But as long as your 9-patch files are also static resources, n is painful but still theoretically manageable.
It s tedious to keep track of the changes in these parallel files.
I'd also love to find an easy way for this one...
This approach may not be an option for you also because it is basically the hard way. But it s an option nonetheless.
Suggestion #2
Have you tried using the method?
public void setInt (int viewId, String methodName, int value)
remoteView.setInt(R.id.viewid, "setBackgroundResource", R.drawable.backgroung_lime);
From another question: Change remoteView ImageView background
How can I override the XML attributes of TwoLineListItem. I don't want to do it Java. I'm trying to make the backgrounds of the view and the textViews transparent, but it isn't working when I do it in Java.
setBackGroundDrawable
setBackGroundColor
setBackGroundResource
These don't seem to do what I need them to do. I've tried making no background. I've tried setting an alpha value of 0 to the color, but I still get the same old white list boxes. I have a drawable background that is behind the TwoLineListItem view, and thats all I want to show for the background, not the Item background.
According to my understanding of your question you are trying to make your TextView's background to trasparent. this is what i use to do this:
TextView tv = (TextView)findViewById(R.id.textviewID);
tv.setBackgroundColor(Color.TRANSPARENT);
And I didn't get your point about TwoLineListItem. But if you want it to become single line, then write in xml properties of your TextView:
android:singleLine="true"
If this was not the question then please elaborate a bit.