This is my current xml layout:
<android.support.design.widget.TextInputLayout
android:id="#+id/emailAddressLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:focusable="true"
android:clickable="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="9dp"
android:padding="5dp"
android:focusable="false"
android:clickable="false">
<AutoCompleteTextView
android:id="#+id/emailAddress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusableInTouchMode="true"
android:hint="email address"
android:inputType="textEmailAddress"
android:imeOptions="actionNext"
android:maxLines="1" />
<Button
android:id="#+id/emailTextClearButton"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_marginRight="5dp"
android:layout_marginEnd="5dp"
android:layout_gravity="end|center_vertical"
android:background="#drawable/ic_clear_black_svg" />
</FrameLayout>
</android.support.design.widget.TextInputLayout>
I need my FrameLayout in order to accomplish a (x) clear button within my AutoCompleteTextView.
The problem is that the FrameLayout is negating the effect of TextInputLayout so the text is not pushed up when user inputs text or clicks on AutoCompleteTextView.
Current result: (Missing Email address header text)
Wanted result: (I added an x clear button to show what's missing when not using FrameLayout)
TextInputLayout has some internal behavior that isn't exactly what you'd expect from a "normal" ViewGroup. Most of it is undocumented, but the Javadoc for the class itself does tell you a little bit:
Note: The actual view hierarchy present under TextInputLayout is NOT guaranteed to match the view hierarchy as written in XML. As a result, calls to getParent() on children of the TextInputLayout -- such as an TextInputEditText -- may not return the TextInputLayout itself, but rather an intermediate View. If you need to access a View directly, set an android:id and use View#findViewById(int).
What you should take away from this is that TextInputLayout will both potentially "re-write" your declared XML layout and also that you should do your best to make sure that your XML layout is set up in the way TextInputLayout "expects".
I believe that your specific problem stems from the fact that your XML layout specifies no EditText that is a direct child of the TextInputLayout. If you look at the source for TextInputLayout#addView(), you'll see this:
#Override
public void addView(View child, int index, final ViewGroup.LayoutParams params) {
if (child instanceof EditText) {
...
setEditText((EditText) child);
} else {
...
}
}
Because you have a FrameLayout wrapping the EditText, that call to setEditText() is never made. Therefore, none of the special functionality of TextInputLayout will work.
How to solve this is a more difficult question. I would start by changing your layout to have this structure instead:
<FrameLayout>
<TextInputLayout>
<EditText/>
</TextInputLayout>
<Button/>
</FrameLayout>
This way you can have a "normal" TextInputLayout hierarchy and a button to clear the text. You'll have to do a little more work to position the button so that everything looks natural, but this should be a workable starting point.
I ended up solving my question using the following in my XML:
<android.support.design.widget.TextInputLayout
android:id="#+id/emailAddressTextInput"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<AutoCompleteTextView
android:id="#+id/emailAddress"
style="#style/body_text_style"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:focusable="true"
android:focusableInTouchMode="true"
android:hint="#string/prompt_email_address"
android:imeOptions="actionNext"
android:inputType="textEmailAddress"
android:maxLines="1"/>
<!--work around solution using negative marginTop-->
<Button
android:id="#+id/emailTextClearButton"
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="end|center_vertical"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_centerVertical="true"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
//work around solution
android:layout_marginTop="-32dp"
android:background="#drawable/ic_clear_black_svg"/>
</android.support.design.widget.TextInputLayout>
The WORKAROUND is to set my button underneath my AutoCompleteTextView and use a negative marginTop on the button to be able to set it where it needs to be set.
Result:
Note: I'm using a RelativeLayout as parent of TextInputLayout
Related
I create a layout with button, this button have logic and i dont want to integrate it.
So i think like:
When i have button, i need to add a char $, so i create a frameLayout and add here a TextView, i do this, but my TextView is under button, so i didnt see this TextView,
Here is my code:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/button_cash"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:background="#drawable/button_radius"
android:paddingRight="12dp"
android:text="#string/_5000"
android:textColor="#color/white"
android:textSize="14sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginRight="14dp"
android:layout_marginTop="15dp"
android:text="#string/dollar_$"
android:textColor="#color/white"
android:textSize="14sp" />
</FrameLayout>
And my question is, how to move item under other item in FrameLayout?
Thanks for advice
This is probably due to the elevation parameter. By default a Button has a non-zero value of elevation while TextView's elevation is 0dp.
Try manipulating elevation (either increase elevation for TextView or decrease elevation for Button) to match your exact case.
I suggest you apply both:
app:elevation="<your_value_in_dp>"
android:elevation="<your_value_in_dp>"
I want to create a textview with a single line which is inside a swipelayout. Every textview represents an action on which the user can touch. All textviews have a width of 100dp. Now i have the problem that the text of a textview is too long and it wraps to the next line. I don't want to use android:singleLine="true". Now i added android:maxLines="1" and android:inputType="text" and it works fine. But now i get the warning:
Attribute android:inputtype should not be used with <TextView>: Change
element type to <EditText>
I don't want to enter text so i don't need an EditText.
Is there another way or should i just ignore this warning?
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:maxLines="1"
android:inputType="text"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_visibility_white_36dp"
android:gravity="center"
android:padding="10dp"
android:text="#string/rename"
android:textColor="#color/item_title_listname"/>
Solution:
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fillViewport="true"
android:minWidth="100dp"
android:scrollbars="none">
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:drawableTop="#drawable/ic_visibility_white_36dp"
android:gravity="center"
android:maxLines="1"
android:text="#string/rename"
android:textColor="#color/item_title_listname"/>
</HorizontalScrollView>
Have you read what it says ?
Using a <TextView> to input text is generally an error, you should be
using <EditText> instead. EditText is a subclass of TextView, and some
of the editing support is provided by TextView, so it's possible to
set some input-related properties on a TextView. However, using a
TextView along with input attributes is usually a cut & paste error.
To input text you should be using <EditText>.
InputType -Bit definitions for an integer defining the basic content type of text held in an Editable object. Supported classes may be combined with variations and flags to indicate desired behaviors.
Attribute android:editable should not be used with <TextView> and thats the reason!
you can remove that inputType in your textView
without android:singleLine="true" but with android:maxLines="1"
Alternatives
1.use what compiler asks :P EditText and make it act as textView
<EditText
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:inputType="text"
android:maxLines="1"
android:text="Helloo"
android:id="#+id/Id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent" >
</EditText>
2.you can also use
<HorizontalScrollView
android:fillViewport="true"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/item_list_bottom_rename"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:maxLines="1"
android:text="Heloo" />
</HorizontalScrollView>
I don't know why is this happening.. In other activities EditText is working fine. This is how my fragment layout looks:
<EditText
android:gravity="center"
android:inputType="text"
android:background="#android:color/transparent"
android:layout_below="#+id/profile_image"
android:text="Dusan Dimitrijevic"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:textColor="#android:color/black"
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
Is something affecting in layout these EditTexts so they can't be editable. If i need to show you Java code, let me know. I can set text to EditText by the way, but can't edit it..
Remove the background and the text. Use hint instead of text
<EditText
android:gravity="center"
android:inputType="text"
android:hint="Dusan Dimitrijevic"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:layout_marginTop="20dp"
android:textColor="#android:color/black"
android:id="#+id/et_username"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
I encountered this and resolved by identifying an overlapping layout/element declared below editText thus preventing interaction with the editText element. My editText element envelop (parent) layout had the same:
android:layout_below="#+id/xxxxxx"
specification as the layout below it.
When I use Material Design Support TextInputLayout in a Classic Layout, I have no problem, I get the hint animation.
But when I use it inside a listview, I lose this, and it behaves like a classical Edittext.
Why???
Here is my code ( one cell xml):
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/ll_row"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<android.support.design.widget.TextInputLayout
android:id="#+id/til_valor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:hint="test2"
android:imeOptions="actionNext">
<EditText
android:id="#+id/valor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="test"
android:inputType="text" />
</android.support.design.widget.TextInputLayout>
<ImageButton
android:id="#+id/cambutton"
android:layout_width="50dp"
android:layout_height="match_parent"
android:background="#drawable/button_azul_states"
android:contentDescription="Tomar foto"
android:gravity="center"
android:src="#drawable/ic_action_device_access_camera" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
Initially, I had it with LinearLayout, I tried to use NestedScrollView, without success...
Try removing both hint in layout and call setHint only on TextInputLayout instance from code
EDIT:
I realized that my answer is wrong. My case is different because I use a RecyclerView, I've tried with the ListView and the animated hint just won't work...
I suggest you to convert to RecyclerView, it's easy and solves the issue
While testing some designs for a LinearLayout for the elements in a ListView, I stumbled upon some weird behaviour. As you can see in the added code, I have three TextViews in a horizontal LinearLayout. I wanted to set the padding for one of these TextViews, but it seems that this value is also applied to the other TextViews as a margin of some sort (see pictures).
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="48dp">
<TextView
android:id="#+id/search_list_row_symbol"
android:layout_width="wrap_content"
android:layout_height="16dp"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
android:textSize="16sp"
android:background="#android:color/holo_blue_light"
android:text="O"/>
<TextView
android:id="#+id/search_list_row_name"
android:layout_width="0dp"
android:layout_height="48dp"
android:layout_weight="1"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
android:textSize="16sp"
android:background="#android:color/holo_red_light"
android:text="oxygen"/>
<TextView
android:id="#+id/search_list_row_number"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:paddingTop="0dp"
android:layout_marginTop="0dp"
android:textSize="16sp"
android:background="#android:color/holo_green_light"
android:text="8"/>
</LinearLayout>
Set android:paddingTop="0dp" on all TextViews
Set android:paddingTop="16dp" only on first TextView
Do any of you know why the padding cannot be set independently on one of these TextViews?
The behavior seems correct as padding is applied to the content of a view - not to the view itself. However, the remaining child views following the padding from first child view seems erroneous.
As an alternate, I would suggest you to replace paddingTop with layout_marginTop and it should work fine.
Because you have given padding to only one text view. If you want all text view than you will have to keep all three within one LinearLayout and set property i.e setPadding() or setPaddingTop().
I think this might help you.