Android layout _background fit it's content - android

I am trying to design a custom dialog.
The problem I met was..the background of the textview ,which is the body of the dialog can't change whenever the text get more line ..
wrap_content didn't work ....
PS (there are there part , title , content, buttons )
( title and content are two TextView with two image as background)
one line is fine
but when lines increased... they were eaten !!
if I use wrap_content ... it goes too big !
https://www.dropbox.com/s/in78b8gg0hfkfxh/toobig.png
here is the code of xml
`
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
>
<LinearLayout
android:id="#+id/dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="120dip"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dip"
android:orientation="vertical"
>
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/title"
android:text="Title"
style="#style/TitleStyle"
android:gravity="bottom|center_horizontal"
android:paddingBottom="15dip"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="89dip"
android:background="#drawable/content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="L\na\ny\no\nu\nt\n"
android:gravity="center"
style="#style/TextStyle"
android:paddingRight="20dip"
android:paddingLeft="20dip"
android:paddingTop="10dip"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="0dip"
android:paddingTop="0dip" >
<Button
android:id="#+id/dialog_button_cancel"
android:layout_width="0dip"
android:layout_height="51dip"
android:layout_weight="0.50"
style="#style/BtnStyle"
android:background="#drawable/btn_action"
android:src="#drawable/btn"
android:text="取消"
android:gravity="center"
/>
<Button
android:id="#+id/dialog_button_ok"
android:layout_width="0dip"
android:layout_height="51dip"
android:layout_weight="0.50"
android:background="#drawable/btn_action"
android:src="#drawable/btn"
android:layout_marginLeft="10dip"
style="#style/BtnStyle"
android:text="確定"
android:gravity="center"
/>
</LinearLayout>
</LinearLayout>
`

For second (content) TextView wrapper (LinearLayout) try to use wrap_content instead of fixed layout height 89dip:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/content"
android:orientation="vertical" >
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="L\na\ny\no\nu\nt\n"
android:gravity="center"
style="#style/TextStyle"
android:paddingRight="20dip"
android:paddingLeft="20dip"
android:paddingTop="10dip"
/>
</LinearLayout>
Edit: If the problem is in the image (by the image IMHO you mean white rectangle which wraps title and content views) try to set up rectangle in background in XML instead of an image usage you use for now:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/listview_background_shape">
<stroke android:width="2dp" android:color="#ffffff" />
<padding android:left="2dp"
android:top="2dp"
android:right="2dp"
android:bottom="2dp" />
<corners android:radius="5dp" />
<solid android:color="#8000000" /> <!-- transparent -->
</shape>
Details here: Can I draw rectangle in XML?

Related

Custom EditText into Android

I'd like to put attach file icon into EditText for example how it was made into WhatsApp. How can I do it?
That is not a custom EditBox. Here EditText is wrapped with parent view with other ImageViews.
In whatsapp example there is a Imogi ImageView, An EditText, An Attach ImageView, and a camera ImageView inside a LinearLayout. And parent LinearLayout is having round corner background.
You can also use Drawable left and Drawable right, but I will not suggest that because that does not have much control.
Just a suggestion:
I always want to save time. Here is an whatsapp clone, you can just pull it and use its views.
https://github.com/Shahar2k5/whatsappClone
This guy has done good work in this library.
Use drawableRight attribute in EditText
<EditText
android:id="#+id/account_et"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:drawableRight="#drawable/icon_backall" //your drawable which you want to use
android:ems="10"
android:hint="#string/str_md_email"
android:padding="10dp" >
</EditText>
You have to manage like below image :
For above UI design code is below :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="#dimen/_10sdp"
android:background="#drawable/stockbg"
android:orientation="horizontal">
<EditText
android:id="#+id/edt_sendMeassage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/_10sdp"
android:layout_marginRight="#dimen/_10sdp"
android:layout_weight="1"
android:maxLines="4"
android:backgroundTint="#android:color/transparent"
android:paddingLeft="#dimen/_10sdp"
android:paddingRight="#dimen/_10sdp"
android:textSize="#dimen/normal_input_text_size" />
<LinearLayout
android:id="#+id/ll_send"
android:layout_weight="6"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="#dimen/_40sdp"
android:layout_height="#dimen/_20sdp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:src="#drawable/ic_next_arrow"
android:tint="#color/color_gray" />
</LinearLayout>
</LinearLayout>
I'd like to put attach file icon into EditText for example how it was made into WhatsApp.
if you want to add icon in EditText you can use
android:drawableStart=""
android:drawableEnd=""
android:drawableTop=""
android:drawableLeft=""
android:drawableRight=""
how it was made into WhatsApp. How can I do it?
That is not Custom Edittext
You need to wrap your ImageView and EditText in side a ViewGroup like LinearLayout or RelativeLayout
SAMPLE CODE
<LinearLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:padding="10dp"
android:background="#drawable/test"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_menu_camera" />
<EditText
android:layout_width="0dp"
android:background="#android:color/transparent"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:src="#drawable/ic_menu_camera" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dp"
android:layout_marginRight="2dp"
android:src="#drawable/ic_menu_camera" />
</LinearLayout>
android:background="#drawable/test"
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<stroke
android:width="2dp"
android:color="#color/colorPrimary" />
<corners android:radius="25dp" />
</shape>
OUTPUT
You need to put edittext and attach icon in layout (according to your need) and set rounded background to parent layout
like this
<RelativeLayout
android:id="#+id/sendLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/roundedbg"
android:orientation="horizontal">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/attachFile"
android:background="#null"
android:padding="#dimen/five_dp" />
<ImageView
android:id="#+id/attachFile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon"
/>
</RelativeLayout>
roundedbg.xml
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android">
<solid android:color="#FFFFFF"/>
<stroke android:width="3dp" android:color="#B1BCBE" />
<corners android:radius="10dp"/>
<padding android:left="0dp" android:top="0dp" android:right="0dp" android:bottom="0dp" />
</shape>
better you use wrap edittext and imageview in relative layout like below
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp">
<EditText
android:id="#+id/etPassword"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/custom_edittext"
android:hint="Password"
android:minLines="10"
app:passwordToggleEnabled="true"
android:singleLine="true"
android:inputType="textPassword"
android:textSize="14sp"
android:paddingLeft="20dp"
android:layout_weight="1"
android:paddingRight="20dp"
tools:ignore="MissingPrefix" />
<ImageView
android:id="#+id/ivPasswordToggle"
android:layout_width="24dp"
android:elevation="10dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="15dp"
android:layout_height="24dp"
android:background="#drawable/ic_remove_red_eye_black_24dp"/>
</RelativeLayout>
output :
in java code use click on imageview

Rounding the corners of views in notepad android app

I currently have this user interface on my app.
I would like to round the edges of the notes like I managed to do in the composing section of the app, as below
I was able to do this by using the following in a file in drawable called timetable_item_border.xml,
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle" >
<stroke
android:width="0dp"
android:color="#000" />
<solid android:color="#color/tile_background" />
<padding
android:bottom="#dimen/tile_padding"
android:left="#dimen/tile_padding"
android:right="#dimen/tile_padding"
android:top="#dimen/tile_padding" />
<!-- <corners android:radius="1dp" /> -->
<corners android:radius="10dp"/>
</shape>
I tried to apply android:background:#drawable/timetable_item_border at the start of the following file but it curved the corners of the notes but removed all the padding, which I didnt want. (I put it at the start of the frame layout but before the linear layout).
This is file:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/note_tile_margin">
<LinearLayout
android:id="#+id/tile_clickable"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:background="#drawable/tile_selector"
android:clickable="true"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:id="#+id/noteTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_margin="#dimen/tile_padding"
android:layout_toLeftOf="#+id/btn_tile_expand"
android:ellipsize="end"
android:maxLines="#integer/max_tile_lines"
android:singleLine="false"
android:text="Write your message here... "
android:textColor="#color/tile_text" />
<LinearLayout
android:id="#+id/btn_tile_expand"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#drawable/click_selector"
android:gravity="top" >
<ImageView
android:id="#+id/btn_tile_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="7dp"
android:src="#drawable/icon_dark_expand" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/tile_options"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:gravity="right"
android:visibility="gone" >
<ImageView
android:id="#+id/btn_tile_links"
style="#style/btn_tile_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="Hyperlinks"
android:src="#drawable/icon_dark_web" />
<ImageView
android:id="#+id/btn_tile_delete"
style="#style/btn_tile_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="#string/deleteNote"
android:src="#drawable/icon_dark_delete" />
<ImageView
android:id="#+id/btn_tile_share"
style="#style/btn_tile_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="#string/share"
android:src="#drawable/icon_dark_share" />
<ImageView
android:id="#+id/btn_tile_copy"
style="#style/btn_tile_option"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:clickable="true"
android:contentDescription="Copy to clipboard"
android:src="#drawable/icon_dark_copy" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
How should I curve the notes? What am I not seeing correctly?
Note: The original source code comes from here, https://github.com/mick88/notepad, I am just redoing the graphics.
Google have created a view for this very case. The CardView http://developer.android.com/training/material/lists-cards.html
If you use cardview try this
card_view:cardCornerRadius=""

how to create textview with the image and border like the image attached

I want to create a textview like the below image. I tried but not able to get like this format.
This is my xml file.
<View android:background="#ff340c1c"
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_above="#+id/genere"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/genere"
android:text="Search by Movie Genere"
android:layout_above="#+id/rating"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="78dp"
android:textSize="20sp"/>
<View android:background="#ff340c1c"
android:layout_width="fill_parent"
android:layout_height="0.5dip"
android:layout_alignBottom="#+id/genere"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
Use custom background with border and drawableLeft -
drawable/my_rect.xml
<?xml version="1.0" encoding="utf-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android" >
<stroke
android:width="0.5dp"
android:color="#color/gray" />
<solid android:color="#color/white" />
<corners android:radius="0px" />
</shape>
In XML:
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/genere"
android:text="Search by Movie Genere"
android:layout_above="#+id/rating"
android:drawableLeft="#drawable/someimage" // ADD
android:background="#drawable/my_rect" // ADD
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginBottom="78dp"
android:textSize="20sp"/>
Note: Many possible solution exists.
It can be done by using a simple linear layout as a border.
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EEEEEE"
android:orientation="horizontal"
android:padding="1dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFF"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/ic_search_category_default" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Search By Venue" />
</LinearLayout>
</LinearLayout>
For adding an image to the TextView, see android:drawableLeft. For adding a border, you could take a look at 9-patch images. Generate a 9-patch image as described, and set that as the background of the TextView.

Android: Adding a divider into a list item with dynamic height

I'm currently trying to create a list item similar to the one in the official design guidelines. Still missing is the vertical divider next to the button/image on the right side of each item. Most people seem to use a separate View with a fixed width for that, but the height doesn't scale in my case. It's only shown when I set a fixed height for the divider itself or the parent RelativeLayout. Is it possible to let it scale automatically?
My layout XML for the list item looks like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/sensors_list_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:padding="10dp" />
<TextView
android:id="#+id/sensors_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_toRightOf="#id/sensors_list_img"
android:paddingTop="5dp" />
<TextView
android:id="#+id/sensors_list_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:layout_below="#id/sensors_list_name"
android:layout_toRightOf="#id/sensors_list_img"
android:paddingBottom="5dp" />
<ImageView
android:id="#+id/sensors_list_details"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:src="#drawable/ic_angle_right" />
<View
android:id="#+id/sensors_list_divider"
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_toLeftOf="#id/sensors_list_details"
android:background="#ff0000" />
</RelativeLayout>
Try to assign an height related to the image like:
android:alignTop="#id/myimage"
android:alignBottom="#id/myimage"
otherwise you can create a xml drawable and use it with an ImageView :
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
<stroke android:width="1dp" android:color="#000000"/>
</shape>
Update
Remember android:shape="line" doesn't work without stroke
Let's go with a LinearLayout as a parent:
<?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:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/sensors_list_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:paddingTop="5dp" />
<TextView
android:id="#+id/sensors_list_location"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15sp"
android:paddingBottom="5dp" />
</LinearLayout>
<View
android:id="#+id/sensors_list_divider"
android:layout_width="2dp"
android:layout_height="match_parent"
android:background="#ff0000" />
<ImageView
android:id="#+id/sensors_list_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/ic_angle_right" />
</LinearLayout>
Check this
listView.setDividerHeight(1);

Adding two buttons which are touching each other in Framelayout

I want to add two buttons one below another in a FrameLayout but they should be touching. However, I am not managing to do this. Here is my code:
<FrameLayout
android:id="#+id/lytOptions"
android:layout_below="#id/imgLine"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<Button
android:id="#+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_via_email"
android:textColor="#android:color/white"
android:textStyle="bold" />
<Button
android:id="#+id/btnSignUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_sign_up"
android:textColor="#android:color/white"
android:textStyle="bold" />
</FrameLayout>
Try this
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="49dp"
android:text="Button" />
</RelativeLayout>
to get the output like this
Use vertical LinearLayout instead...
<LinearLayout
android:layout_width="fill_parent"
android:layout_below="#id/imgLine"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_via_email"
android:textColor="#android:color/white"
android:textStyle="bold" />
<Button
android:id="#+id/btnSignUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_sign_up"
android:textColor="#android:color/white"
android:textStyle="bold" />
</LinearLayout>
By using framelayout you are actually placing one image on another, instead use linearlayout with buttons width and height as wrap content,and avoid using layout weights of the buttons.
Let me know if this solves your problem
As others have pointed out, the key answer is to use a LinearLayout to position the buttons next to each other. However, to a "touching" effect, you will need to do one or more of the following:
Change the padding on each Button.
Change the margin on each Button.
Change the button background image so that it goes right to the edge of the View area.
My preference would be to set the margins and paddings to zero and to set the image to be the effect you want:
<LinearLayout
android:layout_width="fill_parent"
android:layout_below="#id/imgLine"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- options 1 and 2 -->
<Button
android:id="#+id/btnLogin"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_via_email"
android:textColor="#android:color/white"
android:padding="0dp" <!-- Set there to be no space between the content and the surrounding views -->
android:padding="0dp" <!-- Set to be no space int the content area but between the content and the edge of the content area -->
android:textStyle="bold" />
<!-- option 3 -->
<Button
android:id="#+id/btnSignUp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/action_sign_up"
android:textColor="#android:color/white"
android:background="#drawable/my_background"
android:textStyle="bold" />
</LinearLayout>
You will need to define my_background as an imagine the drawable directory or res. You may the section on 9 patch images from http://developer.android.com/guide/topics/graphics/2d-graphics.html helpful.
Use a TableLayout instead:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:background="#drawable/button_style"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" android:background="#drawable/button_style"/>
</TableRow>
</TableLayout>
Assign a drawable button_style to the buttons:
<?xml version="1.0" encoding="UTF-8"?>
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<solid android:color="#38393B"/>
<stroke android:width="0.5dp" android:color="#8aaa"/>
<size android:width="120dp" android:height="80dp" />

Categories

Resources