Below is part of my current layout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginBottom="6dp"
android:id="#+id/domainContainer"
android:weightSum="1.0">
<EditText
android:layout_width="wrap_content"
android:layout_height="44dp"
android:inputType="text|textNoSuggestions"
android:id="#+id/txtDomain"
android:theme="#style/LoginEditText"
android:drawableLeft="#drawable/ic_globe"
android:hint="#string/domain"
android:layout_marginRight="0dp"
android:textColorHint="#4F4F4F"
android:paddingRight="0dp"
android:textSize="16sp"
android:gravity="center_vertical|right" />
<EditText
android:layout_weight="1.0"
android:layout_height="44dp"
android:paddingLeft="0dp"
android:layout_marginLeft="0dp"
android:inputType="none|textNoSuggestions"
android:id="#+id/txtHostname"
android:theme="#style/LoginEditText"
android:hint=".mydomain.com"
android:textColor="#9E9E9E"
android:textSize="16sp"
android:clickable="false"
android:cursorVisible="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_marginBottom="6dip"
android:gravity="center_vertical|left" />
</LinearLayout>
I have zero padding between the text of the two edittexts, but how would I achieve zero padding between the two underlines of the two edittext views (i.e. so it looks like one long line underline).
You can try to set a negative margin to bring the edittext widgets closer. Be careful as you could cause overlap though.
android:layout_marginRight="-8dp"
That said, I would discourage you from doing that as it can be confusing to the user to have 2 edittext views when it appears to be a single one.
Related
I want go design chip layout like below.
When Text is short enough to fit withing the layout I want to view like this.
When Text is too long to fit in I want it display like this and animate text horizontally.
Is it possible to design a layout that fulfill both about needs.
I have tried solution and i have filed because those layout designs only fulfill one condition.
Solution one
<RelativeLayout
android:id="#+id/relative_store_name_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:layout_weight="9"
android:visibility="visible">
<FrameLayout
android:id="#+id/relative_shop_name_container"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/relative_shop_name_text"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:background="#drawable/selected_store_name_text_background"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingStart="10dp"
android:ellipsize="marquee"
android:text="" />
</FrameLayout>
<FrameLayout
android:id="#+id/relative_layout_delete_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_toEndOf="#id/relative_shop_name_container">
<ImageView
android:layout_width="wrap_content"
android:layout_height="21dp"
android:background="#drawable/selected_store_name_delete_background"
android:paddingEnd="5dp"
android:paddingStart="10dp"
android:src="#drawable/store_name_chip_delete" />
</FrameLayout>
</RelativeLayout>
Problem with this is when text is too long delete button disappears. this layout on work for short text.
Solution two
<LinearLayout
android:id="#+id/store_name_liner_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginEnd="10dp"
android:layout_weight="9"
android:orientation="horizontal"
android:visibility="visible"
>
<LinearLayout
android:id="#+id/liner_shop_name_container"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="9">
<TextView
android:id="#+id/liner_shop_name_text"
android:layout_width="wrap_content"
android:layout_height="21dp"
android:background="#drawable/selected_store_name_text_background"
android:gravity="center_vertical"
android:maxLines="1"
android:paddingStart="10dp"
android:text="short Text" />
</LinearLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_weight="1">
<ImageView
android:layout_width="wrap_content"
android:layout_height="21dp"
android:background="#drawable/selected_store_name_delete_background"
android:paddingEnd="5dp"
android:paddingStart="10dp"
android:src="#drawable/store_name_chip_delete"
/>
</FrameLayout>
</LinearLayout>
Problem with this is when text is short delete button is not going to fit just after text. I can't make that happen by removing weight from both text view container and delete image view container but then I get the same problem in my solution one.
Please give solution to handle this.
You need to make a custom view. That way you can measure the text and resize to fit.
Just add ellipsize="end" to your chip
Just add **ellipsize="end"** to your chip
<com.google.android.material.chip.Chip
<-- android:ellipsize="end" -->
/>
I need to fit 2 TextViews in one line. I tried to use LinearLayout, and now my best approach is to use RelativeLayout.
Here you can see XML for it
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="start"
android:visibility="visible">
<TextView
android:id="#+id/partner_full_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="left"
android:layout_alignParentLeft="true"
android:textColor="#color/black"
android:maxLines="2"
android:textSize="12sp"
android:layout_toLeftOf="#+id/session_duration"
android:text="#string/dummy_text" />
<TextView
android:id="#+id/session_duration"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="16dp"
android:maxLines="1"
android:textSize="12sp"
android:layout_alignParentRight="true"
android:text="asdadsd"
android:textColor="#android:color/darker_gray"
/>
</RelativeLayout>
And the result
As you can see it's fit's okay, but second TextView is on the right side, when I want it to be after first TextView.
When I used LinearLayout I faced problem with size of first TextView (if it have to many text in it, second TextView will go off screen). Another approach with LinearLayout gave me similar results to RelativeLayout with same problem (wrong position of second view)
This happens because you are using wrap_content. If textView1 needs the space of the whole screen, then textView2 will get nothing. Instead you can use layout_weight to always give a View his space.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:gravity="center"
android:visibility="visible"
android:orientation="horizontal">
<TextView
android:id="#+id/partner_full_name"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:gravity="left"
android:maxLines="2"
android:text="Super Long String"
android:textColor="#color/black"
android:textSize="12sp" />
<TextView
android:id="#+id/session_duration"
android:layout_width="0dp"
android:layout_weight=".5"
android:layout_height="wrap_content"
android:maxLines="1"
android:text="I will always show"
android:textColor="#android:color/darker_gray"
android:textSize="12sp" />
</LinearLayout>
Remove this line from second TextView :
android:layout_alignParentRight="true"
I have a relative layouts for some parts of my app, made up of a checkbox, a button, some text, and another button (please see first screenshot, ex: [5min] after screen off [except]).
The problem is for other languages, the text to the left of the last button could be much longer, pushing the "except" button slightly or completely off screen. How else could I do this so that the button (along with the text) wrap around to the next line? Right now only the text does. Here is the code:
<RelativeLayout
android:id="#+id/layout_after_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<Button
android:id="#+id/button_set_time_turn_off"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_centerVertical="true"
android:textSize="16sp"/>
<TextView
android:id="#+id/textView_data_check_minutes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_toRightOf="#+id/button_set_time_turn_off"
android:layout_toEndOf="#+id/button_set_time_turn_off"
android:layout_centerVertical="true"
android:textSize="16sp"
android:textColor="#android:color/primary_text_dark"
android:text="#string/text_disable_after_time" />
<Button
android:id="#+id/button_set_disable_exceptions"
android:layout_width="wrap_content"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
android:layout_toRightOf="#+id/textView_data_check_minutes"
android:layout_toEndOf="#+id/textView_data_check_minutes"
style="?android:attr/buttonStyleSmall"
android:layout_centerVertical="true"
android:text="#string/button_disable_except"
android:textSize="16sp"/>
</RelativeLayout>
Normal layout:
"except" button pushed off screen:
You can wrap these in a LinearLayout instead since, after all, they are just linear. Then position the LinearLayout how you need to according to your needs.
This will allow you to make use of layout_weight which will make sure that each View only takes up as much space as needed. You would need to play with the weights to get what you want but something like 1|2|1 looks close to what you want.
<LinearLayout
android:id="#+id/layout_after_time"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp">
<Button
android:id="#+id/button_set_time_turn_off"
android:layout_width="0dp"
android:layout_height="35sp"
android:layout_weight="1"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_gravity="center_vertical"
android:textSize="16sp"/>
<TextView
android:id="#+id/textView_data_check_minutes"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:layout_marginLeft="3dp"
android:layout_marginStart="3dp"
android:layout_gravity="center_vertical"
android:textSize="16sp"
android:textColor="#android:color/primary_text_dark"
android:text="#string/text_disable_after_time" />
<Button
android:id="#+id/button_set_disable_exceptions"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="35sp"
android:paddingTop="0dp"
android:paddingBottom="0dp"
style="?android:attr/buttonStyleSmall"
android:layout_gravity="center_vertical"
android:text="#string/button_disable_except"
android:textSize="16sp"/>
</LinearLayout>
Note that I made the width of each "0dp" because you typically need that when using weight for a horizontal layout. I also replaced the android:layout_centerVertical="true" with android:layout_gravity="center_vertical" to match the corresponding attributes of LinearLayout. I also removed the to_right_of and such attributes as those are no longer needed with a LinearLayout.
I haven't tested it but this should get you close.
LinearLayout docs
Another option may be to use the maxWidth property of TextView.
I have a little Chat application. At the bottom is the EditText line and a send button. In the EditText maxLines is set to 5. How can I move the button always to center_horizontal, even if new lines (1-5) are added to the textview? The button actually just stays at it's position.
My try:
<LinearLayout
android:id="#+id/llLine"
android:layout_width="match_parent"
android:layout_alignParentBottom="true"
android:gravity="center_horizontal"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/txtChatline"
android:layout_width="0dp"
android:layout_weight="0.70"
android:hint="#string/hint_message"
android:layout_height="wrap_content"
android:maxLength="500"
android:maxLines="5"
android:ems="10" />
<Button
android:id="#+id/cmdSendMessage"
android:layout_width="0dp"
android:layout_weight="0.30"
android:enabled="false"
android:gravity="center"
android:layout_height="wrap_content"
android:text="#string/send" />
</LinearLayout>
If your EditText grows vertically, you need to use android:gravity="center_vertical".
(or android:gravity="center" if you want to use both, but I assume it was a mistake).
I want to put in the same row a TextView, and Edittext and a button but I am having the problem that the button is not aligned properly to left and in small screens edittext fills entire with.
Small screen:
Big Screen:
My codification is as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="right" >
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</RelativeLayout>
</LinearLayout>
Apply a weight to your EditText so it will take up as much room as it can while letting the other two elements do the normal wrap_content. To do this, remove the relative layout container and then change the EditText width to "0dp" and give it a layout_weight of "1" as follows:
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:id="#+id/address_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="50dp"
android:layout_height="35dp"
android:text="Go" />
</LinearLayout>
First, many people will tell you that hint is Android's solution for not needing the label. I don't care if you use the label or not but it does save you space, especially on smaller screens. That was just an FYI.
Now, your RelativeLayout that only has a Button appears to be useless...I would remove that. You can use layout_weight so that each View takes up the appropriate amount of space. Make sure to make the layout_width="0dp". So it may look something like
<LinearLayout
android:id="#+id/layout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<TextView
android:id="#+id/address_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center"
android:text="#string/address_textview"
android:textStyle="bold" />
<EditText
android:id="#+id/address_edittext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:hint="#string/address_textview_hint"
android:imeActionLabel="#string/search_button"
android:imeOptions="actionDone"
android:inputType="textCapWords"
android:nextFocusDown="#id/address_edittext"
android:nextFocusUp="#id/address_edittext"
android:singleLine="true" />
<Button
android:id="#+id/go_button"
android:layout_width="0dp"
android:layout_height="35dp"
android:layout_weight="1"
android:text="Go" />
</LinearLayout>
Here I used 2,3,1 for the weights of your TextView, EditText, and Button respectively. You may need to change those to get exactly what you want but that should give you a start.
Layout weigth is ideal for designing layouts that adjust to screen size. However, make sure to set layout_width to 0dp, or it won't work properly.
Use like this:
android:layout_width="0dp"
android:layout_weight="1"
I'm going to assume you mean the button is not properly aligned to the right.
It's because your RelativeLayout's android:width="wrap_content", but it should be android:width="match_parent".
Also, you'd be better off setting your EditText's android:width="0dp" and adding android:weight="1" so that it expands/contracts between screen sizes.