1/*
2 * Copyright (C) 2016 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package android.support.design.widget;
18
19import android.content.Context;
20import android.support.v7.widget.AppCompatEditText;
21import android.util.AttributeSet;
22import android.view.ViewParent;
23import android.view.inputmethod.EditorInfo;
24import android.view.inputmethod.InputConnection;
25
26/**
27 * A special sub-class of {@link android.widget.EditText} designed for use as a child of
28 * {@link TextInputLayout}.
29 *
30 * <p>Using this class allows us to display a hint in the IME when in 'extract' mode.</p>
31 */
32public class TextInputEditText extends AppCompatEditText {
33
34    public TextInputEditText(Context context) {
35        super(context);
36    }
37
38    public TextInputEditText(Context context, AttributeSet attrs) {
39        super(context, attrs);
40    }
41
42    public TextInputEditText(Context context, AttributeSet attrs, int defStyleAttr) {
43        super(context, attrs, defStyleAttr);
44    }
45
46    @Override
47    public InputConnection onCreateInputConnection(EditorInfo outAttrs) {
48        final InputConnection ic = super.onCreateInputConnection(outAttrs);
49        if (ic != null && outAttrs.hintText == null) {
50            // If we don't have a hint and our parent is a TextInputLayout, use it's hint for the
51            // EditorInfo. This allows us to display a hint in 'extract mode'.
52            final ViewParent parent = getParent();
53            if (parent instanceof TextInputLayout) {
54                outAttrs.hintText = ((TextInputLayout) parent).getHint();
55            }
56        }
57        return ic;
58    }
59}
60