How to create custom edittext with button in android? - android

How can I create this kind of edittext with button on android.
I've done relative layout but it just won't look very presentable.
Thanks ahead.

Use android:drawableRight property of EditText.Code as
<EditText
android:id="#+id/edit_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/enter_the_value"
android:drawableRight="#drawable/ic_launcher" />

Related

How to use setHint() in autocompletetextview programmatically in android

I am using a autocompletetextview in my app.I want to set hint in this by code.Is it possible?
Used Xml code is..
<AutoCompleteTextView
android:id="#+id/autoCompletedTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginRight="50dp"
android:background="#android:color/transparent"
android:singleLine="false"
android:textColor="#color/text_white"
android:textSize="#dimen/text_size12sp" />
code is........
autocompleted.setHint(getResources().getString(R.string.select_pickuplocation));
Yes it is possible and easy. Try with this...
autoCompleteTextView.setHint("Your Hint");
One way is with xml
<AutoCompleteTextView
android:id="#+id/autoCompleteTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/passwordEntry"
android:ems="10"
android:text=""
android:hint="Test" />
Other way is programatically
autoCompleteTextView1.setHint("YourHint");
and if you want to set Text from your strings resources you can do it like this:
autoCompleteTextView1.setHint(getResources().getString(R.string.yourString));
Hope to help!
If you want to display hint at the bottom of the matching list, use setCompletionHint. If you want to use normal hint, use setHint.

How can i do a button with 2 labels inside in android?

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

Clickable Button (or any View) inside EditText in android

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>

Android EditText look

I wanna make the editText to look like this:
what else should I do after I set the editText's background how can I make the text start inside the edittext via code.
Are you trying to get the EditText to say "Username" by default, but not have that be the actual text?
For that you can use EditText.setHint() (inherited from TextView) or in the xml definition use android:hint, such as
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="Username" />
To make text look like that, set the Gravity attribute to left. The gravity of EditText element.
As well as setting the background of the EditText view, set left and right padding, e.g.
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/edittext_bg"
android:paddingLeft="7dp"
android:paddingRight="7dp" />

Alignment of text on button

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>

Categories

Resources