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>
Related
I am using the following code to put the google sign in button in my application. However the text in the button is off center. How can I make it centered?
<com.google.android.gms.common.SignInButton
android:id="#+id/sign_in_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:visibility="visible"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp" />
By inspecting with the debugger and the decompiled class code, I've noticed:
the SignInButton class sets up the real button in setStyle. It uses inter-process binding APIs to create some class from some other library, and I couldn't identify that class nor locate its decompiled class code.
the inner button has side paddings set, which is larger on the side with G icon.
the G icon is displayed via a background drawable, and not the TextView drawableStart.
Thus, it seems like some manual padding is put on the real button, on the side with the G icon. Why they would do this, no idea. From the above, it's pretty safe to "fix" the padding after the button is inflated from your XML. I think this is also safe in the case of RTL, since the padding on the side with the icon is always larger, I think (or I sure hope so, somebody please let me know if this is broken in RTL):
loginBinding!!.googleSignInButton.getChildAt(0)?.let {
val smaller = Math.min(it.paddingLeft, it.paddingRight)
it.setPadding(smaller, it.paddingTop, smaller, it.paddingBottom)
}
That's Kotlin, of course.
Better create a button by yourself.
I always prefer this approach because i can develop any kind of google login button as i want.
<RelativeView
android:height="wrap_content"
android:width="match_parent"
android:marginLeft="20dp"
android:marginRight="20dp">
<ImageButton
android:parentRight="true"
android:id="google_icon"
android:background="#null"
android:padding="10dp"/>
<TextView
android:id="google_text"
android:height="wrap_content"
android:width="match_parent"
android:centerHorizontal="true"
android:text="Google Login"
android:padding="10dp"/>
</RelativeView>
Above is the structure of the code that you can use exactly to achieve what you want.
Just use your own custom view button instead of using their built-in button. BUT you need to follow the guideline. Even facebook button i custom to my need..
<RelativeLayout
android:layout_width="300dp"
android:layout_height="50dp"
android:layout_marginTop="10dp"
android:gravity="center"
android:layout_gravity="center"
android:background="#drawable/border_radius_white"
android:clickable="true"
android:id="#+id/google_signin" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/g_logo"
android:adjustViewBounds="true"
android:layout_gravity="center_vertical"
android:padding="12dp"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:typeface="normal"
android:textSize="#dimen/log_button_size"
android:text="#string/text_sign_in_with_google"
android:textStyle="bold"
android:textColor="#color/scGrey"/>
</RelativeLayout>
An easy way I got around this was to add spaces after my text.. so Instead of
android:text="Connect with Google"
I changed it to
android:text="Connect with Google_____________"
replace the underscores with spaces
however this was using the custom button "com.shobhitpuri.custombuttons.GoogleSignInButton"
Use gravity to fix the center of text:
<com.google.android.gms.common.SignInButton
......
android:gravity="center_vertical|center"
...... />
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.
I have a textbox with a hint but I want the hint to always be shown, even when there is an input in the TB. Example is the "To" field in the Gmail app.
There are 3 approaches you could use (spoiler - use number 3), since as mentioned in my comment, in the Gmail example , it is not an actual hint:
Using a Linear Layout, getting a cleaner look in my opinion:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a Relative Layout, getting a result that mimics the Gmail App:
Note: might be problematic since the text will be displayed on top of the hint, see solution 3.
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:paddingLeft="10dp"
android:layout_marginBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
Result are as shown in this image:
Edit:
using a Drawable:
This seems a better solution (I personally just created it from snipping the 1:1 display of the TextView, will be in correct measurements this way), this will allow a cleaner layout code, and the text will be aware of the drawable:
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:gravity="top"
android:drawableLeft="#drawable/Hint"
android:layout_height="wrap_content"/>
The first thing that came to mind is to insert two editText layout relative layout.
First over second.at the top of that - put background as
android.R.color.transparent
Work with top elevent of relative layout! In item below - set hint(that you want) and try to put text. May be you will have specify padding of first element of top to greate display.
Oh. And one more thing.
Maybe put to editText background background image - but is the bad to scalability.
Is there somewhere the code of Google Search in Android? The xml-layout would be enough for me. Or maybe there is a possibility to add a button to the right of the EditText just like adding an icon in the corner? Thanks.
<LinearLayout
android:id="#+id/mainLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/bar"//set back ground for total look
android:orientation="horizontal" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="Your hint"
android:drawableRight="#drawable/search"//give your search icon
android:id="#+id/editBox"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageSpeak"
android:background="#drawable/iconSpeak"//icon for speak
/>
</LinearLayout>
You have a LinearLayout with horizontal orientation where you can have an EditText, an ImageView with the search image and an ImageView for the microphone image. All of them should have the required background images cut in such way in order to seem as one. This is the general notion.
Regarding the source code, I can't help you!
Hope this helps!
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>