Put a text on top of a button - android - java

i have a regular android button
<Button
android:id="#+id/btn_0"
style="#style/def_button_numb"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="0" />
First:
I need to put some text on top of this button, just like keyboards does.
And second:
On longpress needs to show a popup (also like keyboards), to let user choose two more options of buttons.
is there an ease way to do so?

you can simply add this code
android:text="SEND DATA TO FRAGMENT B"

Related

How to group multiple buttons together so, that only one can stay focussed?

Please click here to see the screenThis is a screen of a blood bank, I am developing but I am stuck at implementing the choose blood group option. Should I use them as buttons or text views? Also I want only one option to be selected, since a person cant have more than 1 type of blood group. Also the option selected must move on to the next activity.
There's two way to do it.
Chip Group
Radio Button.
Here I am implementing source code of Chip group.
<com.google.android.material.chip.ChipGroup
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.google.android.material.chip.Chip
android:id="#+id/chip4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Text"/>
</com.google.android.material.chip.ChipGroup>
You have to add more chip. Only single chip can be selected at once. If you want to enable multiple chip. Than, you have to remove ChipGroup or, create more ChipGroup.To do like that picture you have to change colors.
RadioGroup :
<RadioGroup
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RadioButton
android:id="#+id/radioButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
<RadioButton
android:id="#+id/radioButton2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="RadioButton" />
</RadioGroup>
Radio Button works like that but, radioButton design isn't like as you shown.
Use MaterialButtonToogleGroup from Material Components. Inside it use MaterialButton for choices. For more information please refer to this link

How can I change the font size in Android Studio?

I don't want to change the font size of Android Studio itself, but rather the font size of the app i'm working on. All the search results I have found talk about the user interface or zooming in.
I'm Working on a tic-tac-toe beginner project and the button text always is so small. Any tips on how to increase it?
Here is its part in the xml file:
<Button
android:id="#+id/button_0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
Many thanks in advance for anyone who helps
Add android:textSize property into Button, eg:
<Button
android:id="#+id/button_0"
android:layout_width="0dp"
android:textSize="20sp"
android:layout_height="match_parent"
android:layout_weight="1"
/>
https://developer.android.com/reference/android/widget/TextView#attr_android:textSize
One way is to change the textSize attribute in the XML file. The other way is to mention it in the program.
Button btn = findViewById(R.id.button_0);
btn.setTextSize(20.0f);
where btn is the name of the button.
Note: The size mentioned in the setTextSize() method must be a float value.

Android: Scale background image of RadioButton or another way to make a RadioButton an Image

I'm making an app that is related to a card game and as such has many images of cards appear throughout the app. In one section I need the user to select one of four cards, with only one selection ever being possible and I want to have the card to change color slightly after being clicked to show that it is the currently selected one.
For the bulk of this task a RadioGroup with RadioButtons makes perfect sense since the functionality for having a checked/unchecked state and unselecting all other options when a new one is picked is built in; however, the issue is that I want the buttons to be the cards themselves and therefore an image with no text whatsoever.
At first this post looked promising: Adding custom radio buttons in android
My use of this strategy:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_weight="5"
android:orientation="horizontal">
<TextView
android:id="#+id/label_seatGod"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8"
android:gravity="center_vertical"
android:text="#string/ex2"
android:textColor="#color/font_condition_labels"
android:textSize="#dimen/font_gen_label_size"
android:textStyle="bold" />
<RadioGroup
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="8" >
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:button="#drawable/power_aphrodite"
android:checked="false"
android:text="#null" />
</RadioGroup>
</LinearLayout>
which resulted in:
This is almost perfect. No text and the entire region of the clickable button space is composed of the image I want, EXCEPT that as you can see the image is not scaled at all and is cropped on the edges. Above the RadioButton you can see a LinearLayout with 4 ImageButtons that demonstrate exactly how I want the RadioButton/card image to appear (ignore the grey for now I haven't finished padding the views).
Essentially, I want a RadioButton that functions normally but looks exactly like one of the four ImageButtons in the top section. Ideally scaleType="fitCenter" would be the fix since it is what I am using in the example ImageButtons, but you cannot apply that attribute to the button drawable of a RadioButton or its background directly.
So how can I get the image to scale and not be cropped without resorting to some performance tanking layout jurry rigging or creating an entirely new RadioButton class?
Thanks in advance.
Maybe you could try adding this to your RadioButton in the xml:
android:scaleType="centerCrop"

Android custom ListView item

I've set up a ListView and assigned a custom layout to make the ListView items better looking. However, I'm looking for a way to add a button that would appear with each element in it's top right corner, like Youtube's videos: they all have that little options button with things like add to, share, etc. The problem is that you cant put a widget inside of a widget, at least I don't know a way to do it. The custom layout contains a TextView which gives the ListView items the looks. Can someone suggest a way on how should I approach this?
Use FrameLayout for your custom ListView items. like this:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
<Button android:id="#+id/button"
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_gravity="top|right"
/>
</FrameLayout>
NB - when widgets overlaps in layout, last written widget will be topmost.
Here is a link where you will find what you want.
It shows you how to add button in list view.
You will need to change some XML code and adjust the onClickListeners but it's similar to what you are looking for.

Basic clickable item in android

I've been searching for this for quite a while but can't seem to find it.
Is there any predefined widget in Android to implement a basic clickable item (with one of two lines of text) whose background turns to the theme's color when clicked, like the ones seen here, for example:
And I mean, without using the PreferenceActivity class, which implements this automatically. Does this exist or do I have to implement my own customized view?
Thanks!
Edit:
I've added a TwoLineListItem like this and it doesn't react to clicks (background doesn't change)
<TwoLineListItem
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:clickable="true"
>
<TextView android:id="#android:id/text1"
android:layout_marginTop="1dip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:textStyle="bold"
android:text="Meh" />
<TextView android:id="#android:id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#android:id/text1"
android:layout_alignLeft="#android:id/text1"
android:paddingBottom="4dip"
android:includeFontPadding="false"
android:textSize="15sp"
android:textStyle="normal"
android:text="Moo" />
</TwoLineListItem>
Does it only work inside ListViews? Because that way I'll have to define my buttons in code and not in the XML, right?
Thanks!
The default layout for that is called android.R.two_line_list_item given by android.
All the items in a ListView support this transition.
Best wishes,
Tim

Categories