How to add a horizontal line to Button in Android? - android

I'm making a grid of cells and each cell is a button that is clickable. I want to add a horizontal line to the button when it is clicked like that:
Could any one help me?
Thanks in advance

The right way to do this would be to create your own 9-patches for the Button. You can create them with any image editor (I usually use Gimp), but make sure you resave them with the draw-9-patch tool in your android-sdk directory because it adds the necessary formating to the 9.png. Check out here for more information regarding 9-patches. Once you've finished creating the two 9-patches for the Button you will need to create an xml file in your drawable folder which will select which image to choose based on the Button's current state, check out here for more info on creating a selector for a button. It's a decent amount of work, but it's worth it to do it the correct way.

private static final StrikethroughSpan STRIKE_THROUGH_SPAN = new StrikethroughSpan();
Spannable spannable = (Spannable) button.getText();
spannable.setSpan(STRIKE_THROUGH_SPAN, 0, button.getText().length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
Have a look at these link for better understanding::
StrikethroughSpan
Spannable.setSpan

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 set an image as a RadioButton argument programmatically?

I have a RadioGroup which consists of five RadioButtons. What I'd like to achieve is setting an image as a RadioButton argument. So far found this way:
radio0.setBackgroundResource(<drawable ID, int>);
but it gives something like this (descriptions "isSelected" added in an image editor):
Is there any way to programmatically set background properties and move the image to the right?
Or maybe there is another way to set an image next to RadioButton instead of using it as a background image?
I'm interested only in programmatical solutions, as the RadioGroup is cleared up and filled again during loop work.
A friend of mine helped me to figure out this issue.
Instead of the code line posted in the question I use this one now:
radio0.setCompoundDrawablesWithIntrinsicBounds(0, 0, <drawable ID, int>, 0);
The third parameter corresponds to android:drawableRight XML parameter. More on this: click.
Thanks to it a picture is set to the right of the RadioButton and finally it looks like it should.

Can I replace a part of the text in a TextView for a Image in Android?

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);

Best way to program buttons/images

I am designing something that will have a homepage close to the Google+ android app.
I'm having trouble figuring out the best way to do this? Should I create a button with text and set the background as the image? Should I create an image with the text already programmed in the actual picture or should I program the text and picture to be buttons.
Any suggestions from you guys on past projects?
You can use a Button with text to whatever you like and then place the image for that button above the text (using android:drawableTop)like so:
<Button
android:id="#+id/imageButton"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="Photos"
android:drawableTop="#drawable/button_image" />
replacing buttton_image with your actual image. If you want the image in a different position (i.e. below text etc) use:
android:drawableLeft
android:drawableRight
android:drawableBottom
This would be how I would and do do it...
Since I am a newbie in android development I may be wrong but I suggest why not use a Grid View with each grid item haaving a textview and imageview.
My suggest to layout:
If you want to try something new in Android 4.0 , you can try GridLayout to layout , it can reduce the complicate of the nested layout , check out the blog about GridLayout:
http://android-developers.blogspot.com/2011/11/new-layout-widgets-space-and-gridlayout.html
You should write Buttons this way: http://developer.android.com/resources/articles/layout-tricks-merge.html
I normally use RelativeLayout but this is not important.
Write a class myClass extends RelativeLayout and inflate the XML with
LayoutInflater.from(context).inflate(R.layout.myCustomLayout, this, true);
I think this is best practice by Google.
use a regular button and set the android:drawableTop="#drawable/icon_...." value
Having the text burnt into you image is most certainly not the way to go.
One option for you is a GridView. You could also do this through a combination of Image button and scrollview. In my opinion, GridView is best and involves the least amount of code with most flexibility.
Remember that you can replace the button's background image with a state list drawable.

How to allow the user to select a text range in a TextView (Similar to EditText)

I've got a TextView that I would like to allow the user to select a range of text from within it. The TextView takes up the entire width and height of the device (minus some padding and a title at the top). In an EditText if you long-click you get a selection overlay that allows you to set your selection left and right bounds. I'd like this functionality in a TextView. I've read that in API level 9 (2.3) (http://developer.android.com/sdk/android-2.3.html) there are new text selection controls, but I'm having difficulty implementing this. I'm doing this right now:
eic = new InputConnection( bookTextView );
eic.beginBatchEdit();
But it doesn't do anything noticable. Does anyone know how to use InputConnection correctly? Thanks.
Edit: I don't necessarily need to use what I was attempting above. I ultimately want to use either a TextView or an EditText which looks and feels like a TextView and be able to select text using a dragging cursor. Then I would like to manipulate the selected text with various context menu options (or a menu that pops up above the selected text).
Here is an idea.. Add an EditText with a TextView background, Here is an example
<EditText
android:text=" This is not an editable EditText"
android:id="#+id/EditText01"
android:layout_width="wrap_content"
android:textColor = "#android:color/white"
android:editable = "false"
android:cursorVisible="false"
android:layout_height="wrap_content"
android:background = "#android:drawable/dark_header">
</EditText>
add this to your xml in the place of TextView
You can enable the TextView's Spannable storage. See Highlight Text in TextView or WebView for an example.
See also:
http://developer.android.com/reference/android/text/Spanned.html
You could display the text in a WebView and enable text selection. If you want to only use a textview/edittext, here is an answer that might help you and here is information on the Spannable class that might help you accomplish what you want.
Actually, you do not have to develop this feature by yourself. You just need to use EditText instead TextView, while you set the android:editable of EditText to false. My idea is the same as sandy's.
My code is here, hope it may help you:
https://stackoverflow.com/a/11026292/966405
After long internet surfing to find a solution, i prefered create my own class
https://github.com/orionsource/SelectableTextViewer
Goal features:
Easy to use - only one class
Support for text and Html.fromHtml
Can be in ScrollView with correct touches
Cursors can be redefined
Color of selection can be redefined
All the above solutions either too long or not working for me.
What you need is to add just textView.setTextIsSelectable(true)
in your activity or fragment or adapter.

Categories

Resources