简体   繁体   中英

How to put getLocation / compass button inside edittext field android studio

I want to put the getlocation / compass button inside my editfield, I am not quite sure how I can do this or where to start, please can someone guide me. What I have to do is, something like the two screenshots below;

在此处输入图片说明

I have tried playing around with the layout but I can't seem to figure out how I can do this, I have the basic layout done;

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:ads="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:background="?attr/colorPrimary"
    android:orientation="vertical" >


    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="center_horizontal"
        android:layout_marginTop="100dp">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="textPostalAddress"
        android:ems="10"
        android:hint="Your Post Code"
        android:id="@+id/editText"
        android:layout_gravity="center_horizontal"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp"/>

    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="Proceed"
        android:background="#fff"
        android:id="@+id/button"
        android:layout_marginLeft="10dp"
        android:layout_marginRight="10dp" />
    </LinearLayout>
</LinearLayout>

You can use android:drawableRight attribute in xml:
https://developer.android.com/reference/android/widget/TextView.html#attr_android:drawableRight

Something like that:

<EditText 
android:layout_width="match_parent" 
android:layout_height="wrap_content"
android:inputType="textPostalAddress" 
android:ems="10" 
android:hint="Your Post Code" 
android:id="@+id/editText"
android:drawableRight="@drawable/YOUR_COMPASS_DRAWABLE"
android:layout_gravity="center_horizontal"
android:layout_marginLeft="10dp" 
android:layout_marginRight="10dp"/>

where YOUR_COMPASS_DRAWABLE is a compass picture in res/drawable/ folder (you must used different pictures for different screen: mdpi, hdpi, etc.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM