TwoLineListItem - Custom attributes - android

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.

Related

Add un text onto an image programmatically using Kotlin

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".

How to make parent background opaque but keep child element non opaque?

Hi guys I am new to Android .I want to make the layout given in the image.
I want to achieve the effect inside the red box.How can I make the background opaque while its keep its child opaque?
I tried this as,
The part inside the red box as RelativeLayout, which has a TextView (value as Contact details) I gave android:alpha=0.3 for relative layout,but the problem is,it makes the TextView also opaque (which is logical)
I just want some thoughts to move in that direction.
Create a color resource with an alpha component of 0.3 and set it as the background of the RelativeLayout
In the colors.xml
<color name="opaque_black">#4c000000</color>
In the layout of the View
<RelativeLayout
...
android:background="#color/opaque_black"
...>

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

How to make TextView's background to be transparent?

There is a tablelayout having four tablerows, the first tablerow contains a TextView :
As you can see the background of the TextView having the text "Vidy" is seen ! So how to make it transparent ? I tried to add android:alpha="0" to the attribute of the TextView but at runtime the text is not seen !
You can try:
android:background="#null"
Or:
android:background="#android:color/transparent"
Check also: Android Transparent TextView?
If you really need transparency, #Nermeen's answer is great, you can also get it done programmatically like:
myTextView.setBackgroundColor(Color.TRANSPARENT);
but as long as you have solid background color below, it's better due to performance to make your TextView's background color the same as the layout below (with alpha, app needs to redraw more regions). It won't really matter for such a simple layout, but it's worth remembering when you have many of them using transparency.

Getting ListView drawables and applying it manually

i am trying to apply the style that a list item has, when it is selected, to a View (In my case a TextView)
Example
here it would be the orange-painted style. I found that this look is defined by a Drawable Object. So i tried, so get the drawable and applied it to my view
TextView tv = new TextView(this);
tv.setText("Hello, Android");
tv.setBackgroundDrawable(new ListView(this).getSelector());
setContentView(tv);
but it doesn't work.
Does anybody have an idea how to do that?
Ok I figured out how to do that. I found out hat ListView uses the ColorStateList list_selector_background which is defined in android.R.drawable. Using ResourceBrowser I got to know that the fourth color is the selected drawable so i added the following to my code
StateListDrawable listDrawables= (StateListDrawable)getResources().getDrawable(android.R.drawable.list_selector_background);
listDrawables.selectDrawable(3);
Drawable highlightDrawable = listDrawables.getCurrent();
Now I can set my Background using higlightDrawable. I don't know if it is possible to access the drawable in xml I did not try it yet. Thanks for help!
It would be a pain to do it by code.
You need to implement a selector and let android know which resource needs to be used when the view is pressed, enabled or focused.
Here is sample example on how to achieve the same.

Categories

Resources