Instead of just a textview on a button, I'd like the button to contain a layout with two TextViews. See the mockup below. When a user uses the button to add a category, I'd like to update the percentage on the right. I have this working with an included layout, but I want to use a button so that the user instinctively knows to click it.
Button is a View not a ViewGroup. To achieve that, use a horizontal LinearLayout, style it as a button and add a ClickListener to it. Something like this:
<RelativeLayout
android:clickable="true"
android:background="#android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text1"
android:text="Category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/text2"
android:text="value"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
Note that the background of the layout is set to the default android button so it will look like a button.
Related
I have something like "There are 200€" but I want to style the value (ex. textColor Red)
Is there any way of having two textViews to seem like an entire one?
A textView inside a textView or a textViewContainer or something.
I can achieve it on the run like this answer: Change text color of one word in a TextView
But I want to do from the layout file. Any chance?
Thanks
EDIT
<TextView
android:id="#+id/lastIncome"
android:text="Tu último ingreso fueron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_font_size"/>
<TextView
android:id="#+id/lastIncomeValue"
android:text="200€"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_value_font_size"
android:textColor="#color/greensea"
android:layout_gravity="right"/>
You can achieve this with a horizontal LinearLayout. The LinearLayout is the container for the two side by side TextViews. This layout can be placed in any other container (LinearLayout, RelativeLayout, etc.) in your XML.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are " />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200€"
android:textColor="#ff0000"/>
</LinearLayout>
You can use a easy approach to do this within one textview using SpanableString Builder Click here to get this approach
How can i do 1 button with 2 labels with different style inside in android?
(Like the image)
Thank's
You can create a custom view. I have used Layout as a button by setting custom button style to the layout and have added two textViews to it, this way:
<LinearLayout android:id="#+id/customButtonLayout"
android:layout_height="wrap_content" style="#android:style/Widget.Button"
android:layout_width="wrap_content">
<TextView android:text="First" android:id="#+id/firstTextView"
android:layout_width="wrap_content" android:layout_height="wrap_content"
android:textColor="#000"></TextView>
<TextView android:textColor="#000" android:text="Second"
android:layout_height="wrap_content" android:id="#+id/secondTextView"
android:layout_width="wrap_content" android:layout_marginLeft="10dp"></TextView>
</LinearLayout>
The only way I can think with which you could applay different textstyles to a Button is to use HTML (with HTML.fromHTML()). But I wouldn't recommend that, because probably it wouldn't look good. Use a Layout instead(Linear - or RealtivLayout). Add two TextViews to it and set a selector as the background(to have a "click-effect").
I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigo for this answer. But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-
In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?
Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.
You don't need any drawable and listening to the click event of the android buttons is not a question anymore.
Its absolutely correct
Use this , It worked for me -
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/id_search_EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingRight="40dp"
android:hint="Enter your feelings and connect" />
<ImageButton android:id="#+id/id_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/id_search_EditText"
android:layout_alignBottom="#+id/id_search_EditText"
android:layout_alignRight="#+id/id_search_EditText"
android:background="#drawable/ic_launcher"/>
</RelativeLayout>
I think you try to search set click event for compound drawable. You can find some solutions here
handling-click-events-on-a-drawable-within-an-edittext
there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.
The only possible solution is to use a RelativeLayout that allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.
Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.html maybe it can helps you :)
Use this this code may work.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/REFReLayTellFriend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/editext_rounded"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="start"
android:hint="Search"
android:singleLine="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtSearch"
android:background="#drawable/ic_action_content_filter_list"/>
</RelativeLayout>
</LinearLayout>
I would like to add button myButton to TextView. How to get it in a single line so that it looks like one set of lines. Here is sample of the text to be added
Please press this here to refresh the set of values.
I want to have "here" as clickable button. How can I do it. Any snippet on it would be helpful.
Set background of Button as null by android:background="#null" give the same text color as given to other TextView's.
or You can take 3 TextView's and setOnClickListener to middle one.
Set style of Button with borderless android attribut to remove background and border:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:text="here" />
make three textView ex: textView1, textView2 and textView3.
<RelativeLayout android:id="#+id/textLay"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please press this"/>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="here"
android:layout_toRightOf="#+id/textView1" />
<TextView android:id="#+id/textView3" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="to refresh the set of values."
android:layout_toRightOf="#+id/textView2" />
</RelativeLayout>
Just add below attribute to TextView in XML layout file to make it clickable.
android:clickable="true"
In your Java file add OnClickListener to complete click action on text view.
Add android:clickable="true" and android:onClick="yourclickname" for the TextView in the XML. Then define yourclickname in your activity. This should make your TextView clickable and triggers the specified method when clicked.
I am creating a custom button. I want to set the position of text on button.
I have two background images for button (button_off and button_on) first is for normal button and second is for onFocus / onPressed state.
Now I have to put text on button then it should look like:
But i am unable to set the exact position of the text on this background button image.
Please share your suggestion to solve this issue.
You can try using 9-patch image as a background. More information on this here: http://developer.android.com/guide/topics/graphics/2d-graphics.html#nine-patch
You can set these properties in XML file, `
<Button
android:id="#+id/button1"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:drawablePadding="5dp"
android:drawableLeft="#android:drawable/ic_lock_lock"
android:text="All"
android:gravity="center"/>
`
Align the text , use gravity option ! or try with padding
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#android:drawable/arrow_down_float"
android:background="#drawable/btn_all_bg"
android:text="All">
Button>
You can use the drawableLeft/right/top/bottom to add icon on a button and text would automatically adjust.
Few ways :
define a button and try android:drawableStart
extend Button and draw yourself
try to define a LinearLayout that hold a text and an image with a
background of your button,
set both of them weight of 1 and put them inside another LinearLayout and make them onClick
can't assure 3 will work without a tweak or two but it worth trying
You can use the layout as a button style="#android:style/Widget.Button", and configure it as you want.
Paste this in your xml file:
<LinearLayout
android:id="#+id/button"
style="#android:style/Widget.Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:text="TextView" />
</LinearLayout>