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.
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
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.
So here's how my grid view looks like:
a http://imageshack.com/a/img41/7857/roir.png
I use some online JSON parsing and get the image thumbnail links and then pass it on as an array to the custom adapter and it sets images for which I again use some 3rd party library etc. But the 3rd party library is just used to set the image to the imageview. That's it.
So I have a main FragmentActivity. It has 2 fragments inside of it. The textbox u see there is inside one fragment(search fragment) and on top of it, the gridview is held by the second fragment. (results fragment)
Code for the results fragment:
<RelativeLayout 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"
android:background="#f0f0f0"
tools:context="com.example.wikipicsearch.MainActivity" >
<GridView
android:id="#+id/gridView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:drawSelectorOnTop="true"
android:stretchMode="columnWidth" >
</GridView>
</RelativeLayout>
Code for the layout that I pass onto the custom Adapter:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="5dp"
android:clickable="true"
android:background="#drawable/grid_color_selector"
android:focusable="true">
<ImageView
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="0dp"
android:layout_gravity="center" >
</ImageView>
</LinearLayout>
Now, I have no clue why on earth is there no horizontal spacing between each of that linear layout. I tried using android:layout_marginLeft etc etc... nothing works at all! I made the fragment holding the gridview look grey so that the each element in the grid will leave some spacing and it should look good. Please help me out with this. TIA!
Android:horizontalSpacing="20dp" this doesn't work, if you have a fixed size in the root element (ex- card) of the item layout that you are setting for the grid
Set the width like this - android:layout_width="match_parent"
Activiy layout
<?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"
tools:context=".views.legacy_grid_view.LegacyGridActivity">
<GridView
android:layout_height="match_parent"
android:id="#+id/legacyGrid"
android:background="#color/teal_700"
android:layout_width="match_parent"
android:numColumns="2"
android:horizontalSpacing="20dp"
android:verticalSpacing="20dp"/>
</LinearLayout>
Item layout
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView 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="200dp"
app:cardCornerRadius="10dp"
app:cardElevation="5dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="32dp"
android:layout_marginTop="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="60dp"
android:scaleType="fitCenter"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#android:drawable/presence_away" />
<TextView
android:id="#+id/legacyGridEmail"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="16dp"
android:text="Email"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
Result
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.