13

I want to achieve Something like this. A floating label with two edit texts. I Used TextInputLayout and some other libraries too, but all accept only single child as EditText. Any help is appreciated.

enter image description here

Both Edit Text must be focusable and the hint goes up from second one.

Peter Jacobsen
  • 618
  • 2
  • 6
  • 18
ADM
  • 20,406
  • 11
  • 52
  • 83
  • 2
    so what is issue?? – Nirav Ranpara May 05 '16 at 05:37
  • Have you try http://stackoverflow.com/questions/26458436/android-edittext-view-floating-hint-in-material-design – Nirav Ranpara May 05 '16 at 05:38
  • The issue is i can only have One Child in TextInputLayout . In that Way if i put these three (Two edit Text and the Line) in a linear . The the Floating label will animate above the second one .. But i want it on to the top left corner .. – ADM May 05 '16 at 05:40
  • Is this two editText ? – Shree Krishna May 05 '16 at 05:49
  • could you try with paddingLeft set to the second EditText? – Bob May 05 '16 at 05:57
  • No .. If i give padding the n how would i enter in first editText .. I wont accept Focus Then – ADM May 05 '16 at 06:09
  • Hmmm. This is quite a bit tricky. Possible, but tricky. It might not be possible to set this alone in the xml. – AL. May 05 '16 at 06:43
  • 1
    There're custom implementations, (search for "floating", "floating text" on android-arsenal for example) so in last resort you can use and modify one of these instead – Marcin Orlowski May 05 '16 at 07:32
  • Yeah I Think .. Well Thx for your concern .. I will make my own . – ADM May 05 '16 at 08:46
  • @ADM, Did you achieve any solution? If yes, then please answer your question and will offer a bounty for that – blueware Oct 13 '16 at 09:00
  • Yeah I found the solution . But in my case i used a work arround as code field was always filled in my case so took the static label .But if you want to build same like floating label view . You should create your own . its quite tricky, we just need to change label position and animate it to extreme left. – ADM Oct 14 '16 at 06:26

2 Answers2

7

This should do what you want (both EditText focusable, hint in one place [but changes on focus] ):

"Both Edit Text must be focusable and the hint goes up from second one."

\res\layout\edittext_main.xml:

<?xml version="1.0" encoding="utf-8"?><LinearLayout
      xmlns:android="http://schemas.android.com/apk/res/android"
      android:id="@+id/linlay0"
      android:layout_width="match_parent"
      android:layout_height="match_parent" 
      android:paddingTop="60dp"
      android:orientation="horizontal">

<android.support.design.widget.TextInputLayout
    android:id="@+id/TextInputLayout1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >  

  <EditText
    android:id="@+id/EditText1"
    android:hint="code"
    android:text="0044"
    android:inputType="phone"
    android:singleLine="true"
    android:layout_width="130dp"
    android:layout_height="wrap_content"/>

</android.support.design.widget.TextInputLayout>

<!-- separator -->
  <ImageView
    android:id="@+id/ImageViewSep"
    android:layout_width="20dp"
    android:layout_height="20dp"
    android:adjustViewBounds="true"
    android:contentDescription="separator"
    android:translationX="-60dp"
    android:translationY="20dp"
    android:src="@drawable/line" />    

  <EditText
    android:id="@+id/EditText2"
    android:hint="phone number"
    android:text="1234567890"
    android:inputType="phone"
    android:singleLine="true"
    android:layout_width="130dp"
    android:translationX="-60dp"
    android:translationY="7dp"
    android:layout_height="wrap_content"/>
</LinearLayout>

With this code in your Activity:

    private static final String TAG = EditActivity.class.getSimpleName();
    private Context             m_context;

    @Override
    protected void onCreate(Bundle savedInstanceState)
    {
        super.onCreate(savedInstanceState);
        m_context = getApplicationContext();

        setContentView(R.layout.edittext_main);
        final TextInputLayout tiv1 = (TextInputLayout) findViewById(R.id.TextInputLayout1);
        final EditText edit_Text1 = (EditText) findViewById(R.id.EditText1);
        final EditText edit_Text2 = (EditText) findViewById(R.id.EditText2);

        edit_Text1.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
//                  Toast.makeText(m_context, "edit_Text1 got the focus", Toast.LENGTH_LONG).show();
                    tiv1.setHint("code");
                }else {
//                  Toast.makeText(m_context, "edit_Text1 lost the focus", Toast.LENGTH_LONG).show();
                }
               }
            });

        edit_Text2.setOnFocusChangeListener(new OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if(hasFocus){
//                  Toast.makeText(m_context, "edit_Text2 got the focus", Toast.LENGTH_LONG).show();
                    tiv1.setHint(edit_Text2.getHint());
                }else {
//                  Toast.makeText(m_context, "edit_Text2 lost the focus", Toast.LENGTH_LONG).show();
                }
               }
            });
}//onCreate

Here are some images generated by the code:
enter image description here enter image description here

Here's some styles and themes I have played with to get the desired result.
How to Change the floating hint font size and colour etc... res\values\styles:

        <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">#ff0000</item>
        <item name="colorPrimaryDark">#ff0000</item>
        <item name="colorAccent">#ff0000</item>
    </style>

    <style name="Widget.Design.TextInputLayout" parent="AppTheme">
        <item name="hintTextAppearance">@style/AppTheme.TextFloatLabelAppearance</item>
        <item name="errorTextAppearance">@style/AppTheme.TextErrorAppearance</item>
        <item name="counterTextAppearance">@style/TextAppearance.Design.Counter</item>
        <item name="counterOverflowTextAppearance">@style/TextAppearance.Design.Counter.Overflow</item>
    </style>

    <style name="AppTheme.TextFloatLabelAppearance" parent="TextAppearance.Design.Hint">
        <!-- Floating label appearance here -->
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">10sp</item>
    </style>

    <style name="AppTheme.TextErrorAppearance" parent="TextAppearance.Design.Error">
        <!-- Error message appearance here -->
        <item name="android:textColor">#ff0000</item>
        <item name="android:textSize">20sp</item>
    </style>

<style name="TextHint" parent="Base.TextAppearance.AppCompat">
    <item name="android:textSize">18sp</item>
    <item name="android:textColor">#00FF00</item>
</style> 
Jon Goodwin
  • 9,053
  • 5
  • 35
  • 54
  • If I use your solution, it will not be the same as mentioned in the picture above. I need the Country Code `EditText` to has the same width as in the picture, now when the Mobile Number `EditText` has the focus, the first `TextInputlayout` which is refered to by 'tiv1' will get the hint but its cut as the Country Code `EditText`'s width is limited to country code value; i.e. like this "phone nu..". I think there is no solution to expand `TextInputLayout`'s hint width more than its child. – blueware Oct 16 '16 at 07:14
  • Yes, I know (as you can I see have tried to minimise that by reducing the font size and moving the edittext so Phone number does fit), that's why personally I would use two TextInputLayout's and have two hints. TextInputLayout is a wrapper for ONE edittext ONLY. you cannot draw OUTSIDE TextInputLayout's bounding box. – Jon Goodwin Oct 16 '16 at 10:20
  • What design problem ? – Jon Goodwin Oct 16 '16 at 13:46
  • Your second image still have design problem for pre-lollipop devices. May you fix this? – blueware Oct 16 '16 at 13:55
  • I don't understand, this code uses android-support-v7-appcompat, I tested it on API16 (4.11 Jelly Bean) device ! – Jon Goodwin Oct 16 '16 at 13:59
  • Are you asking how to change the font size (and other attributes) on the floating hint ? The answer is styles and themes. I can post that code if you want it. – Jon Goodwin Oct 16 '16 at 14:22
  • There you go some styles and themes (well styles ;O) )! – Jon Goodwin Oct 16 '16 at 14:36
1

I found a semi-solution for this online.

First, you'll need to add a dependency for the android design library in your primary build.gradle:

dependencies {
    compile fileTree(dir: 'libs', include: ['*.jar'])
    compile 'com.android.support:appcompat-v7:22.2.0'
    compile 'com.android.support:design:22.2.0'
}

Then, you can use the designs provided by the library in your XML by using:

<android.support.design.widget.TextInputLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:id="@+id/PhoneNumberTILayout"
    android:layout_marginTop="@strings/my_margin_top">

    <EditText
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:hint="Phone Number" />
</android.support.design.widget.TextInputLayout>

Now I can't seem to find a way to get 2 children of TextInputLayout... It's just not how it's meant to work... But you can simply add another one which will work just as well. In your case, all you need to do is make your primary layout Relative and then set the Country Code TILayout's position relative to the Phone Number TILayout's.

This is what I have for the relativelayout part:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.testapp.MainActivity" >

<android.support.design.widget.TextInputLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:id="@+id/TILayout"
    android:layout_alignParentTop="true"
    android:layout_alignParentStart="true"
    android:layout_marginStart="11dp"
    android:layout_marginTop="20dp">

    <EditText
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:hint="Code"
        android:textSize="26sp"
        android:ems="3"
        android:id="@+id/PhoneCode" />

    <android.support.design.widget.TextInputLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/PhoneNumberTILayout"
        android:layout_marginTop="-64dp"
        android:layout_marginLeft="100dp">

        <EditText
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:hint="Phone Number"
            android:textSize="26sp"
            android:ems="6"
            android:id="@+id/PhoneNumber" />
    </android.support.design.widget.TextInputLayout>
</android.support.design.widget.TextInputLayout>


</RelativeLayout>

Hope I helped :D

Ishan Manchanda
  • 459
  • 4
  • 19
  • I will try your hack tomorrow and will provide my notes, thanks – blueware Oct 15 '16 at 16:01
  • Can you please provide a piece of code of what you are doing? – blueware Oct 16 '16 at 06:40
  • The relative Layout part? – Ishan Manchanda Oct 16 '16 at 09:06
  • Yes please, the relative layout part – blueware Oct 16 '16 at 09:45
  • Can't comment the code... I'm editing the current answer – Ishan Manchanda Oct 16 '16 at 12:22
  • your solution makes this: (http://prntscr.com/cuzfpt), it did not solve my problem, its only makes the two edit texts have their hint enabled on their `TextInputLayout`s. What I need is to set the phone number hint on the first `TextInputLayout` without getting cut from its end. Btw, the Code `EditText` should not get larger as its smaller size than the Phone Number. – blueware Oct 16 '16 at 13:51
  • I still don't get what problem you're facing... Do you need the phone number to be displayed above the Phone Code textbox when Phone Number TB is selected and Code when Code TB is selected?? – Ishan Manchanda Oct 17 '16 at 10:27