I'm trying to put a copyright image in the bottom of my LinearLayout with an "include tag". I am doing it successfully, but when I'm trying to add android:layout_marginTop="330dp" or android:layout_marginBottom="20dp" for instance to my include - it doesn't show in the UI.
What is the problem ?
Here is my code:
copyrights.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:src="#drawable/copyrights"
android:id="#+id/copyrights"
android:layout_width="fill_parent"
android:layout_height="40dip"/>
</merge>
mylayout.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="10dip"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<include layout="#layout/copyrights" android:layout_marginTop="330dp"/>
</LinearLayout>
The <include /> tag is basically a copy&paste instruction for the android layout framework. It's not an actual object in your layout hierachy. That means that it can't have margin/padding/size/any other layout property.
Assign the margin to the <ImageView /> in your layout instead.
Related
But the same activity blows up loading on a zebra m6000. Let me state that I am a novice using Android Studio and Java. I have been a VB programmer for many years.
The XML has 2 includes. One foe each vertical linear layout. I can add the XML's for them if needed.
What could cause the rendering to work on one android device (which is running an earlier version of OS ) but fail on newer device.
Here is XML
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundGray"
tools:context="com.procatdt.stockright.activities.PutAwayAreaActivity">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
<include
android:id="#+id/include2"
layout="#layout/frm1"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="left" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:paddingLeft="5dp"
android:orientation="vertical">
<include
android:id="#+id/include"
layout="#layout/layout_numpad5"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_marginStart="220dp"
android:layout_weight="1"
android:layout_gravity="right"
/>
</LinearLayout>
</RelativeLayout>
Here is how it is rendering on MC67
Found Issue. One of the Buttons in my include was the culprit. KitKat didn't care but lollipop didn't like the backgroundtint value.
Looking through logcat I saw culprit was a button and from there it was process of elimination.
I have a axml layout design which i want to use in another next layout? How can i do that if it is possible.
My reusable sample axml code as follows
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:clickable="true"
android:weightSum="100"
android:background="#android:color/background_light"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="20"
android:gravity="center_horizontal">
<refractored.controls.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/door_image"
android:layout_width="70dp"
android:layout_height="70dp"
app:civ_border_width="0dp"
app:civ_border_color="#FF000000"
android:src="#drawable/fingerprint_icon" />
</LinearLayout>
</LinearLayout>
If you already know the layout that you want to re-use, create a new XML file and define the layout. For example, here's a layout that defines a title bar to be included in each activity (titlebar.xml):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/titlebar_bg"
tools:showIn="#layout/activity_main" >
<ImageView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/gafricalogo" />
</FrameLayout>
The root View should be exactly how you'd like it to appear in each layout to which you add this layout.
Use the include Tag
Inside the layout to which you want to add the re-usable component, add the tag. For example, here's a layout that includes the title bar from above:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/app_bg"
android:gravity="center_horizontal">
<include layout="#layout/titlebar"/>
<TextView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/hello"
android:padding="10dp" />
...
</LinearLayout>
Source: https://developer.android.com/training/improving-layouts/reusing-layouts.html#Include
i would like to ask you, if you have similar problem with constraint layout. fragment is not correctly formatted when fragment element contains id tag.
i have activity with only one fragment defined in xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/initial__initial_fragment"
android:name="com.touch4it.taxi.screens.initial.InitialFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:layout="#layout/initial__fragment__layout" />
fragment constraint one constrain layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/initial__fragment__layout__holder_CL"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/initial__fragment__layout__phone_number_ET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="phone"
android:text="Name"
android:singleLine="true"
app:layout_constraintLeft_toLeftOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/initial__fragment__layout__user_name_ET"
android:layout_marginTop="48dp"
app:layout_constraintRight_toRightOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginEnd="16dp" />
<EditText
android:id="#+id/initial__fragment__layout__user_name_ET"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="textPersonName"
android:text="Name"
android:singleLine="true"
app:layout_constraintLeft_toLeftOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginStart="16dp"
app:layout_constraintTop_toBottomOf="#+id/initial__fragment__layout__log_in_label_TV"
android:layout_marginTop="64dp"
app:layout_constraintRight_toRightOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginEnd="16dp" />
<Button
android:id="#+id/initial__fragment__layout__login_B"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Button"
app:layout_constraintLeft_toLeftOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginStart="16dp"
app:layout_constraintRight_toRightOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginBottom="16dp" />
<TextView
android:id="#+id/initial__fragment__layout__log_in_label_TV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
app:layout_constraintLeft_toLeftOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginStart="16dp"
app:layout_constraintTop_toTopOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginTop="48dp"
app:layout_constraintRight_toRightOf="#+id/initial__fragment__layout__holder_CL"
android:layout_marginEnd="16dp" />
</android.support.constraint.ConstraintLayout>
when fragment layout is used in activity directly, everything is ok.
This is caused by the fact that when getting the layout in a fragment, the id of the element will not be the one you used when defining the layout. For example, in your code you have:
app:layout_constraintLeft_toLeftOf="#+id/initial__fragment__layout__holder_CL"
and the parent element correctly contains the id:
<android.support.constraint.ConstraintLayout
...
android:id="#+id/initial__fragment__layout__holder_CL"
...
The problem is that your fragment tag itself has a different id, initial__initial_fragment:
<fragment
android:id="#+id/initial__initial_fragment"
...
So when the fragment loads the layout, the layout elements, which are referencing the id initial__fragment__layout__holder_CL, won't find it, and the views are thus not being able to be constrained.
There are two things you could do to fix this issue.
The first option is to use the same id in both places:
<fragment
android:id="#+id/initial__fragment__layout__holder_CL"
...
The second option (definitely preferrable) is to update to a newer version of ConstraintLayout (starting from alpha 5 -- but note that alpha 6 is available and fixes some issues in alpha 5). Alpha 5 introduced a new notation for this exact case -- instead of having to specify a parent id, you could instead use:
app:layout_constraintLeft_toLeftOf="parent"
Note too that with Android Studio 2.2 beta 1, the layout editor will automatically replace references to parent by the string "parent" instead of using an id. So the simplest thing to fix your layout would be to update to AS 2.2 beta 1, change your gradle file to point to constraintlayout alpha 6, open your layout and save it.
RelativeLayout doesn't seem to play nice in the v7 support library. I have three elements, One is aligned to the bottom and the other two stack above it. Rather than appear nicely stacked at the bottom, they are rendered off screen, this is unexpected.
I've included an example XML file which is for the custom view added to the toolbar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/name"
android:layout_above="#+id/divider"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/textLighter"
android:text="#string/example_sequence_name"
android:singleLine="true" />
<View
android:id="#+id/divider"
android:layout_width="wrap_content"
android:background="#color/textLighter"
android:layout_above="#+id/description"
android:layout_height="81dp"/>
<EditText
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/hint_description"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Subtitle"
android:textColor="#color/textLighter"
android:layout_alignParentBottom="true"
android:singleLine="true" />
Results in this view.
Below is the xml layout for my Android App. I am trying to positions a webview below the gridview on the screen.
Can anyone help explain what I need to do to get that to work?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/webView1" />
</LinearLayout>
</LinearLayout>
Thanks
I think two minor changes will help make this work for you. First, when creating a LinearLayout, the android:orientation attribute is required to indicate whether the children should be added horizontally or vertically. Since you want one view on top of the other, add android:orientation=vertical to the HomePage LinearLayout. Second, your GridView currently has a android:layout_height of fill_parent. This will cause it to expand vertically and fill up the entirety of the LinearLayout, which will keep your WebView from being displayed. Try changing this attribute to wrap_content.
Making the height of both children wrap_content could possibly cause you to have some empty space in your app. If you need either the GridView or WebView to take up all of the empty space in the view, you can use the android:layout_weight attribute. Assign the view you want to expand to fill all remaining space after the rest are drawn the attribute like this: android:layout_weight=1. (Here, the 1 represents 100% of the remaining space) If you want more granular control over what percentage of the parent your children occupy, look into using decimal values with the android:layout_weight attribute or android:weightSum on the parent in conjunction with a android:layout_weight on each of the children. Hope this helps!
Use android:layout_height="wrap_content" in Gridview
Set orientation attribute in inner linear layout.
android:orientation="vertical"
Give fixed height of WebView and set android:layout_weight="" in both child.
Try below code and change according to your webview height and grid view height.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/HomePage"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="2"
android:orientation="vertical">
<GridView
android:id="#+id/Grid"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:columnWidth="90dp"
android:numColumns="auto_fit"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:stretchMode="columnWidth"
android:gravity="center"
android:layout_weight="1"
android:scrollbars="vertical" />
<WebView
android:layout_width="fill_parent"
android:layout_height="200dp"
android:layout_weight="1"
android:id="#+id/webView1"
/>
</LinearLayout>
</LinearLayout>
In above code I give Height of WebView 200dp change according to your requirement.