John Joe John Joe - 2 months ago 4
Android Question

How can I have a long line under icon and editText and also leave some space at the end of editText?

I want to have a dialog looked similar to this

Desired Dialog Look

I tried to code but the design of the dialog looked weird.


How can I have a long line under icon and editText and also leave some
space at the end of editText?


enter image description here

xml layout

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">


<ImageView
android:id="@+id/img"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/add_new_task"
android:layout_marginLeft="13dp"/>

<EditText
android:id="@+id/edtText"
android:layout_marginLeft="70dp"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:hint="Add a new task"/>

<ImageView
android:id="@+id/dateImg"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/imgdate"
android:layout_below="@+id/edtText"
android:layout_marginLeft="13dp"/>

<EditText
android:id="@+id/date"
android:layout_marginLeft="70dp"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_below="@+id/edtText"
android:hint="Date"/>

<ImageView
android:id="@+id/timeImg"
android:layout_width="35dp"
android:layout_height="35dp"
android:src="@mipmap/imgtime"
android:layout_below="@+id/date"
android:layout_marginLeft="13dp"/>

<EditText
android:id="@+id/time"
android:layout_marginLeft="70dp"
android:layout_width="270dp"
android:layout_height="wrap_content"
android:layout_below="@+id/date"
android:hint="Time"/>

<Button
android:id="@+id/cancelBtn"
android:background="@color/red"
android:textColor="@color/white"
android:text="Cancel"
android:layout_width="165dp"
android:layout_height="40dp"
android:layout_below="@+id/time"
android:layout_marginLeft="13dp"/>

<Button
android:id="@+id/okBtn"
android:background="@color/light_sky_blue"
android:textColor="@color/white"
android:text="Save"
android:layout_width="165dp"
android:layout_height="40dp"
android:layout_below="@+id/time"
android:layout_marginLeft="190dp"/>


</RelativeLayout>


Code

@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_add_task:
AlertDialog.Builder builder = new AlertDialog.Builder(this);
LayoutInflater inflater = getLayoutInflater();
builder.setView(inflater.inflate(R.layout.dialog_with_edittext,null));
builder.show();
return true;

default:
return super.onOptionsItemSelected(item);
}
}

Answer

Try this xml i changed the padding on the Relative layout so that it doesnt looks weird from the top/bottom and the sides, also added the lines using the link i provided in my comment (Vertical line using XML drawable) and also centered the buttons a little bit since they seemed a little bit off with another relative view inside and removed the underbar of the editTexts

<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout  xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:paddingTop="8dp"
    android:paddingBottom="5dp"
    android:paddingLeft="5dp"
    android:paddingRight="5dp"
    android:layout_height="wrap_content">


    <ImageView
        android:id="@+id/img"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="#000"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:layout_marginTop="11dp"
        android:layout_marginBottom="11dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/img"
        android:background="@android:color/transparent"
        android:id="@+id/edtText"
        android:layout_width="270dp"
        android:layout_height="wrap_content"
        android:hint="Add a new task"/>
    <View
        android:layout_below="@id/edtText"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000"
        android:layout_marginTop="-4dp"/>
    <ImageView
        android:id="@+id/dateImg"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="#000"
        android:layout_below="@+id/edtText"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:layout_marginTop="11dp"
        android:layout_marginBottom="11dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/dateImg"
        android:background="@android:color/transparent"
        android:id="@+id/date"
        android:layout_width="270dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/edtText"
        android:hint="Date"/>
    <View
        android:layout_below="@id/date"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000"
        android:layout_marginTop="-4dp"/>

    <ImageView
        android:id="@+id/timeImg"
        android:layout_width="35dp"
        android:layout_height="35dp"
        android:background="#000"
        android:layout_below="@+id/date"
        android:layout_marginLeft="13dp"/>

    <EditText
        android:layout_marginTop="11dp"
        android:layout_marginBottom="11dp"
        android:layout_marginLeft="20dp"
        android:layout_toRightOf="@id/timeImg"
        android:background="@android:color/transparent"
        android:id="@+id/time"
        android:layout_width="270dp"
        android:layout_height="wrap_content"
        android:layout_below="@+id/date"
        android:hint="Time"/>

    <View
        android:layout_below="@id/time"
        android:layout_width="fill_parent"
        android:layout_height="1dp"
        android:background="#000"
        android:layout_marginTop="-4dp"/>

<RelativeLayout
    android:layout_centerHorizontal="true"
    android:layout_below="@+id/time"
    android:layout_width="match_parent"
    android:layout_height="wrap_content">
    <Button
        android:layout_marginTop="5dp"
        android:id="@+id/cancelBtn"
        android:background="#fc2a2a"
        android:textColor="#ffffff"
        android:text="Cancel"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_marginLeft="15dp"
        android:layout_alignParentLeft="true"/>

    <Button
        android:layout_marginTop="5dp"
        android:id="@+id/okBtn"
        android:background="#64e3ff"
        android:textColor="#fff"
        android:text="Save"
        android:layout_width="165dp"
        android:layout_height="40dp"
        android:layout_marginRight="15dp"
        android:layout_alignParentRight="true"/>
</RelativeLayout>

</RelativeLayout>

enter image description here