android studio - How do I resize buttons based on text? - android

I'm making two buttons.
if One button is clicked, the other button's text is changed.
I coded initial status using setText
mBtn.setOnClickListener(this);
mBtn.setText("Connect");
and changing text using
mBtn.setText("Disconnect");
In this case, button's length is fixed initial length.
What can i do?

use this on your layout it will work. remove fixed width first.
android:layout_width="wrap_content"

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Connect"
android:padding="5dp"
/>

Related

Changing a TextView's style and text dynamically

I'm trying to make an app like the one in this mockup:
Supermarket
It's a very simple supermarket app. As you can see, there's a TextView at the bottom of the screen that tells me whether my Cart is empty, or has items in it. If the cart is not empty, the user is shown the total price he/she must pay. You can also notice that said TextView's style and text change according to a variable (in this case, "totalPrice").
How can I do this in Android? I know I can use simple if statements (if totalPrice == 0, then backgroundColor = grey and text = "EmptyCart", for example), but this seems somewhat... hardcoded. Is there a better way to do it? As you can see, the TextView's style and values also change when on the "Subproducts" activity (some Products have Subproducts, which you can see after clicking on them).
Thanks in advance!
I think Databinding is the best way rather than boilerplate code.
create a model class and change background of the view using ternary operation in databinding also manage visibility of price text using this.
To set a textview's text, you use textView.setText("new text"); To set its background, its textView.setBackgroundColor(color) where the color is the hexcode you want as an integer- for example 0xFFFF0000 for reg. Obviously these can be variables as well as hard coded.
It can simply be achieved with two TextViews in a RelativeLayout, and of course, by using basic TextView's methods. As an example layout:
<RelativeLayout
android:layout_height="75dp"
android:layout_width="match_parent"
android:background="#1EC4F3">
<TextView
android:text="PAY"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:typeface="sans"
android:textColor="#FFFFFF"
android:textSize="18sp"/>
<TextView
android:layout_height="wrap_content"
android:text="$25.79"
android:layout_width="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="8dp"
android:typeface="monospace"
android:textSize="14sp"
android:textColor="#FFFFFF"/>
</RelativeLayout>

Android Button widget hyphenation

I have a problem with my project. I use android.widget.Button. When word can not fit in one line in the Button then it cut it like this: Somelon>gword (where '>' is new line). How to fix it? And how to create inner padding for the text because the long word touches the button's borders?
You can try below code. Where paddingRight and paddingLeft keep padding of 25dp in right and left. If you want to text on in one line.Then
you can use android:singleLine="true"
<Button
android:id="#+id/she_was_good"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingRight="25dp"
android:paddingLeft="25dp"
android:ellipsize=”marquee”
android:text="#string/im_sorry_lol_str" />
Read this post
Try using the property android:singleLine="true" and android:padding="10dp" to your button widget.

What is the best way to implement a vertical flowed text in Android?

I'm having a changeable text like in the screenshot, where the quantity changes according to plus and minus buttons.
What is the best to implement that on Android ?
Could I make use of Spannable text in this case ? Or do I implement that with
a vertical LinearLayout with a TextView then a separator view then another TextView that changes ?
If you want to make it your own way, look for click events on the plus and minus buttons, change an integer variable (say mQuantity) according to these click event (mQuantity++ or mQuantity-- respectively), and change the TextView content with mQuantityLabel.setText(mQuantity+"");. That extra +"" is to avoid setText looking for a probably non existing id inside strings.xml. You could just need to convert the int to String, but this suffices for this case.
However, and it may be more sensible to go for already established solutions for number increase/decrease such as NumberPicker (after API 11) or SimonVT's NumberPicker (backport of NumberPicker, if the minSdkVersion is prior to API 11).
Managed to achieve this layout, using LinearLayout.
It was straightforward I thought that it might need tricky layout technique, but turned out to be easy.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/quantity"
android:textAppearance="#android:style/TextAppearance.Medium" />
<View
android:layout_width="match_parent"
android:layout_height="2px"
android:background="#color/black" />
<TextView
android:id="#+id/quantity_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="01"
android:textAppearance="#android:style/TextAppearance.Medium" />
</LinearLayout>

Add multiples images to Edit Text View Android

I have 2 images one is "(" and another ")" how I can add these to EditTextView backgroud and stretch them the size of the EditTextView changes .
I tried to use layer_list without any luck .
Why don't you put those into <ImageView>s and put one to the left and one to the right of the EditText. Something like this:
<RelativeLayout>
<ImageView
android:layout_toLeftOf="#+id/edit_text"/>
<EditText android:id="#id/edit_text"/>
<ImageView
android:layout_toRightOf="#id/edit_text"/>
</RelativeLayout>
Of course you will need to add more attributes to the above, but it is a template for you to get started.
you can use
android:drawableLeft="#android:drawable/left_bracket"
android:drawableRight="#android:drawable/right bracket".
N.B.
by using android:drawableLeft the drawtable to be drawn to the left of the text.
by using android:drawableRigh the drawable to be drawn to the right of the text.
<EditText
android:id="#+id/txtSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginRight="120dip"
android:layout_marginTop="10dip"
android:hint="#string/search_titles"
android:background="#android:color/transparent"
android:drawableRight="#drawable/search_bar_end_right_inactive"
android:drawableLeft="#drawable/search_bar_end_left_inactive"
</EditText>
How I can stretch the images when the textfield is re sizing .?

Split button text into two sections

I am trying to port my WP7 app to android.
Does anyone know how I can layout the text on a single button so that some text appears aligned left and other text appears aligned right? (See below). I need access to be able to dynamically change the percentage number on the right side using code but the text on the right is just static.
Anyone know the answer to this?
The image is here:
http://i.imgur.com/zW7YV.png
Yes you could make it two buttons.
Remove all padding and margin from between them.
Set the same background drawable.
And just ensure when the left is clicked it invokes the right's onPress method (so it looks as if they depress together).
Or wrap the buttons/imageviews/textviews in a layout and perform the onClick on that.
I would use a RelativeLayout for this.
<RelativeLayout
android:width="fill_parent"
android:height="wrap_content"
android:clickable="true"
android:background="#18a2e7"
android:padding="10dip">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Something" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_align_parentRight="true"
android:text="0%" />
</RelativeLayout>

Categories

Resources