xml file vs xml layout - android

I created two layout files.
Android XML Layout File which created this xml
<?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="match_parent"
android:orientation="vertical" >
</LinearLayout>
and an Android XML File
<?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="match_parent"
android:orientation="vertical" >
</LinearLayout>
From the looks of it they are both the same. When to use one over the other since they are both creating the same xml?

Yeah they are prograticaly the same, there isn't different type of xml. What matters, is the location of the xml (layout, values, ...).
In eclipse, there maybe is a small difference in the IDE view, i'm not sure that you have the Graphical Layout view if you create a Android XML file instead of a layout file (it's a detail because, you still can add this view ;) )
But results apk will be identical.

The reason why you would use an Android XML, is if you wanted to use the Layout Tool (the GUI layout editor). The Eclipse IDE will react to this file differently.
But, the code is the same, so the APK you create will also be the same - it doesn't matter in your final output.
I always use Android XML files, so they integrate into the ADT tools better.

the only difference as far as I can see is that in xml you can't use both "android:layout-width:" and "android:layout-height:" attributes which makes xml for example suitable for the use of menu and item bar and other uses.

Related

How to bring back RelativeLayout?

I am quite familiar with RelativeLayout of Android. However, since I have up updated Android Studio(2.3), ConstraintLayout is set as the default layout of all the new projects.
The problem is:
I am not able to delete the ConstraintLayout.
How can I set it (RelativeLayout) as default so that I don't have to tweak around.
I was able to learn about the tweak around from this answer: How to switch from the default ConstraintLayout to RelativeLayout in Android Studio 2.3.3
Also please provide me any helpful information about ConstraintLayout, ie. why use it over relative layout, tutorial/help text regarding ConstraintLayout so that I can learn it.
To change layout go to your xml code and change your root layout to desired layout, if it shows error, post error message here.
This part is from my other answer:
https://stackoverflow.com/a/42779569/3870382
To change defalut layout when creating an Activity, you can modify default template layout file in Android Studio resources, go to your Android Studio folder and go to following folders path to it:
\plugins\android\lib\templates\activities\common\root\res\layout
Edit file simple.xml.ftl and change layout to your choice, notice that some layouts require additional elements (e. g. LinearLayout needs android:orientation), save file and create activity in Android Studio, you should get an Activity with xml layout that you changed.
Mine looks like this (I had 2.2.3 in that time so I have still RelativeLayout)
(You should see your constrainLayout in this file, so change it to relativeLayout)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
<#if hasAppBar && appBarLayoutName??>
xmlns:app="http://schemas.android.com/apk/res-auto"
</#if>
android:id="#+id/${simpleLayoutName}"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
<#if hasAppBar && appBarLayoutName??>
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/${appBarLayoutName}"
</#if>
tools:context="${relativePackage}.${activityClass}">
<#if isNewProject!false>
<TextView
<#if includeCppSupport!false>
android:id="#+id/sample_text"
</#if>
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</#if>
</RelativeLayout>
You may have already tried this, but if not. Go to the top menu of your Android Studio with a project open. "Window" tab, Then pull-down to "Store Current Layout as default" and "Restore to default layout". If the default is not what you like, download a layout you would like as default and set it as your default layout.
I pretty much always use RelativeLayout or LinearLayout as well.. Unfortunately I am not versed on ConstraintLayout.
Hope this helps! :)
I am answering this for android studio 2.3.1.
One of the easiest way to set RelativeLayout as main layout instead of the constraint layout is going to text mode and editing the xml file as followed
Change this line
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
To
<android.widget.RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
And do check your ending tag changes to this
</android.widget.RelativeLayout>
Also go ahead and delete this line if its being shown in grey.
xmlns:app="http://schemas.android.com/apk/res-auto"
Hope it solves the issue.

Uri is not registered for layout in test resources

I'm new at Android testing and I'm trying to test a custom view in an android library and i'm using Android Studio.
I found here : Android: How to test a custom view? that I should use ActivityUnitTestCase and a mock activity.
I have put my xml layout inside projectname/library/src/androidTest/res/layout and my test activity know my layout R id thanks to this : Configuring res srcDirs for androidTest sourceSet.
My issues is in the layout content.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<eu.custom.customView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
In my case, "http://schemas.android.com/apk/res/android" display in red with an error "URI not registered" and the IDE doe's not recognize my custom view if I add it in this xml.
Is there a way to have this layout working without putting it in production code ?
Thanks
Well, in my Eclipse IDE your code looks correct (there are no red lines) may I suggest you to download the ADT-Bundle again or use Android Studio? I think your IDE is corrupted or something in your settings is wrong. Also check if you have downloaded the up-to-date SDK build tools.

layout_width is missed after using <android.support.v4.widget.SlidingPaneLayout> tag

I'm new to Android development and trying to learn it by the book Android for Absolute Beginners
One of the chapters is about android U.I Design and i'm learning about different android layouts in this chapter .
My problem starts after adding <android.support.v4.widget.SlidingPaneLayout > Tag to my code in order to use android SlidindPaneLayout .
Before Adding this Tag I could use android:layout_width or android:layout_height and ... attribute in all of objects . But now it can be only used inside my sliding layout tag
Here is a little simplified version of my code which as you can see android:layout_width is not used inside Linear Layout because Eclipse doesn't accept it ;)
for more detail look at the link below Please ;)
<?xml version="1.0" encoding="UTF-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="hamid.traingin.hellouniverse.MainActivity"
android:id="#+id/spLayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:orientation="horizontal"
android:background="#drawable/milkyway1">
</LinearLayout>
</android.support.v4.widget.SlidingPaneLayout>
Here is a picture of my problem in Eclipse IDE:
The xml code is valid, you can and should add the layout_width and layout_height to you LinearLayout without raising a compiler error. I'm not too sure why Eclipse does not allow you to do so, but Android Studio does:

Android Framework modification : Adding a custom layout

I am trying to add a new layout to the framework say my_layout.
I succeeded partially in that .
I defined my layout in an xml and put it inside frameworks/base/core/res/res/layout
I then mentioned the id in frameworks/base/core/res/res/values
public.xml and ids.xml
And after performing a complete build :
make framework
make update-api
make
i am able to access it as android.R.layout.my_layout
But now i want to access it through xml also :Eg: "#android:layout/my_layout"
In which place in the source do i mention this reference ?
Try to declare the id in your my_layout.xml file in your layout folder.
for example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/home_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
...
Happy coding :)

using merge with layouts giving error for one file, no error on other. same code!

I am creating a few layouts with the root element being merge. inside the merge element i have a ScrollView containing a TextView. Here is the file:
<?xml version="1.0" encoding="utf-8"?>
<merge
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:id="#+id/scroll"
android:padding="6dp"
android:layout_below="#id/headingLayout"
android:layout_above="#id/tabsLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/aboutTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</ScrollView>
</merge>
I am getting 2 errors: there is no resource to match the layout_below and layout_above names i have put in.
I have another layout xml file including very similar components, some of which also reference #id/headingLayout and #id/tabsLayout. Both of these XML layout files do not contain the component with name headingLayout or tabsLayout
Why is it that one layout file has no errors about these references and the other layout file does? What the crap am i doing wrong, the app will build and run like i expect, until i add this merge to another layout?
I have even tried copying the xml from the working layout file, to find that it gives the same errors, something must be wrong with my new layout file. I have tried cleaning, rebuilding, opening/closing eclipse..
Wow, this is a strange fix i have found for this. the layout i have given in my question above(the one giving errors) is named about.xml. the layout with no errors which also includes merge and references to other xml components is named home.xml. the xml file including the referenced components is named base_layout.xml.
in the eclipse Package Explorer under the layout folder, items are listed alphabetically. so the home.xml file came after base_layout.xml, and could therefore reference the components inside it. Since about.xml came before base_layout.xml, about.xml could not reference anything inside base_layout.xml.
so i just renamed base_layout.xml to aaa_base_layout.xml so it would be first, and all errors go away.

Categories

Resources