I'm using FrameLayout cause for some reason when I add fragments to my main layout the new fragment needs to be a FrameLayout to get the proper match_parent height.
But now I am facing an issue with laying out the content of the FrameLayout. It is complaining because I am using a LinearLayout inside a FrameLayout.
How do you layout content in a FrameLayout?
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:src="#drawable/lhf"
android:contentDescription="#string/Logo"
android:alpha="0"
android:rotation="0"
android:scaleX="0.0"
android:scaleY="0.0"
android:layout_gravity="top"
android:x="150dp"
android:y="150dp"
/>
<TextView
android:id="#+id/txtWelcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/welcome"
android:textSize="12sp"/>
</LinearLayout>
</FrameLayout>
UPDATED
I get the following warning
Multiple annotations found at this line:
- This LinearLayout layout or its FrameLayout parent is useless
- This tag and its children can be replaced by one <TextView/> and a compound drawable
It is not an error it is a lint warning. You can replace the ImageView using compound drawable of that TextView and no need of that LinearLayout.
You can set drawable on that TextView using one of these attributes.
android:drawableLeft
android:drawableTop
android:drawableRight
android:drawableBottom
http://developer.android.com/reference/android/widget/TextView.html#setCompoundDrawablesWithIntrinsicBounds%28int,%20int,%20int,%20int%29
Related
I have tried to move the button using graphical interface and android:layout_alignParentLeft in XML file, anyway it does not work. My Android Studio version is 2.2.3. Have you ever had this problem?
You need RelativeLayout or other simlilar layout as parent container because FrameLayout just draw the views one over another plus you should check the properties section to see the attibute properties that you can apply on your layout.
To read further about ViewGroups and it's sub-types with their behaviours
If you forced to use FrameLayout(e.g in Toolbar or so) and you have only one element(or small amount) to operate with(button) you can use android:layout_gravity="" attribute to align element in the FrameLayout.
<FrameLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>
</FrameLayout>
If you have have few elements in your layout, you can: 1) Change FrameLayout to RelativeLayout or 2)Wrap all items into Relative layout and set parameters to match_parent
<FrameLayout
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/app_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
</RelativeLayout>
</FrameLayout>
I have needed to create view like and this view will be placed like
I am trying to create this using frame layout but finish with failure. How can i do this?
Either use a FrameLayout and add this as an imageview last with specified right and top layout margins for this view and some slightly larger right margins for the underlying layout.
Or a RelativeLayout and you align this view to the parent right using (layout_alignParentRight="true") and with margins as with previous example.
Example trying to mimic your layout a bit, tweak the margins and image to your needs:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#bbbb00"
android:layout_marginRight="2dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888899"
android:layout_margin="20dp">
</LinearLayout>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right"
android:layout_marginTop="80dp"
android:src="#drawable/test"/>
</FrameLayout>
I am trying to add a custom border to a framelayout. The framelayout is used as container for the fragment. Thus I want to display fragment with a border. The border should be external to framelayout and should not consume the space inside of framelayout. And it should adjust with change in screen size.
What i can think right now is some sort of custom ViewGroup, add ImageView for border. Use image processing to get the inside area of imageview and in that area inflate a frame layout.
I am looking for some easy way out.
The easiest solution for this would be to create a 9-patch drawable and set it as the background for your FrameLayout. I have created the below 9-patch drawable that you can use. Hope this suffice your requirement.
You can obtain the effect via layout XML using for example LinearLayouts with proper layout_weight, so your centre-most FrameLayout will expand to take up available space.
There are 4 drawables used: for top, bottom, left and right part of frame.
Check out this code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/border_left" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/border_top" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<!-- FrameLayout content goes here -->
</FrameLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:src="#drawable/border_top" />
</LinearLayout>
<ImageView
android:layout_width="wrap_conttXY"
android:layout_height="match_parght"
android:scaleType="fient"
android:src="#drwable/border_right" />
</LinearLayout>
I have a ListView and a LinearLayout (it contains 2 buttons) inside a LinearLayout (this one is the parent of both).
I want to make the inner LinearLayout (the one which contains 2 buttons) to be always shown on the bottom of the screen, is there anyway to do that.
Currently, the LinearLayout does show at the bottom of the screen.
However if the ListView gets too big, it will fill the whole screen.
Sorry, I don't have the code with me now. But the format looks somehow like this:
<LinearLayout>
<ListView>
</ListView>
<LinearLayout>
<button 1>
<button 2>
</LinearLayout>
</LinearLayout>
I'd usually think about using RelativeLayout too, but with a LinearLayout you could use something like this:
<LinearLayout>
<ListView
android:layout_height="0dp"
android:layout_weight="1">
</ListView>
<LinearLayout
android:layout_height="wrap_content">
<button 1>
<button 2>
</LinearLayout>
</LinearLayout>
I normally use a RelativeLayout as the outer container for a ListView.
I use to add a header TextView and a footer one, with another TextView to serve as the emplty listview warning.
In your case, the header is not needed and the footer might be a LinearLayout with the buttons you need.
Here's a skeletal implementation of the layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/rlHistory"
>
<LinearLayout
android:id="#+id/llButtons"
android:layout_alignParentBottom="true"
android:layout_width="fill_parent"
android:layout_height="48dp"
android:padding="8dp"
android:gravity="center"
>
<!-- Put your buttons HERE -->
</LinearLayout>
<ListView
android:id="#+id/lvwHistory"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_above="#id/llButtons"
android:listSelector="#drawable/list_item_colors"
android:background="#drawable/list_border"
style="#style/ListViews"
/>
</RelativeLayout>
Just add the buttons in their LinearLayout container.
Use RelativeLayout instead, more precisely the android:layout_alignParentBottom attribute :
<RelativeLayout >
<ListView>
</ListView>
<LinearLayout
// ...
android:layout_alignParentBottom="true"
// ...>
<button 1>
<button 2>
</LinearLayout>
</RelativeLayout >
If you want to do it with a LinearLayout, one solution might be to set the outer LinearLayout's weightSum property and then set the layout_weight property of the list view and the inner linearLayout to values that properly divide the available space to your like.
If you only have two buttons you might like to do layout_weight="0.75" in the list and layout_weight="0.25" in the inner LinearLayout in case the weightSum of the outer layer was set to 1.
I also think the outer LinearLayout would have to be layout_height set to "match_parent" so it fills all the window, and places the buttons of the inner LinearLayout to the bottom.
It would look like:
<LinearLayout
android:layout_height="match_parent"
android:layout_width="match_parent"
android:weightSum="1">
<ListView
android:layout_height="0dp"
android:layout_weight="0.75">
</ListView>
<LinearLayout
android:layout_height="0dp"
android:layout_width="match_parent"
android:layout_weight="0.25">
<button 1>
<button 2>
</LinearLayout>
</LinearLayout>
Hope it helps :)
I am trying to 'float' a RelativeLayout to the bottom of a Window, and have the following layout:
<?xml version="1.0" encoding="utf-8"?>
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<com.my.activity.CanvasView
android:id="#+id/canvas"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusable="true" />
<RelativeLayout
android:id="#+id/toolbar_notification"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:gravity="bottom|center_vertical">
<ImageView
android:id="#+id/toolbar_notification_icon"
android:layout_width="40dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/content_desc_save_icon"
android:gravity="center|center_vertical" />
</RelativeLayout>
</merge>
Unfortunately, the toolbar_notification RelativeLayout is not being bottom-aligned. Replacing the merge tag with a FrameLayout tag results in the desired effect, but in the name of efficiency I'd like to remove that if possible.
Anyone seen this issue, or have any suggestions? Is the fact that the merge element is not a 'parent' in the true sense resulting in the toolbar_notification element being unable to place itself?
android:gravity="bottom|center_vertical" is referred to children of RelativeLayout and not to RelativeLayout itself.
If you want to align RelativeLayout to the bottom of the parent (that is a FrameLayout) you have to use layout_gravity instead.
...
<RelativeLayout
android:id="#+id/toolbar_notification"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|center_vertical">
...