How to embed one layout inside another in my case? - android

If I have a layout called bottom.xml,
bottom.xml:(simply contain a textview and edit text view)
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="#string/username"
/>
<EditText
android:id="#+id/name"
android:layout_width="120dip"
android:layout_height="50dip"
android:layout_gravity="center_horizontal"
/>
</LinearLayout>
Is there any way to embed the above bottom.xml layout inside other layouts instead of repeatly writing the same code in several layout files (when other layouts have a part which contains the same layout as bottom.xml)?
For example, if my admin.xml layout also contain part of the layout which looks exactly the same as bottom.xml, how to just embed the bottom.xml inside admin.xml instead of writing the same code again?
admin.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
...
...
<!--How to embed bottom.xml here-->
...
</LinearLayout>
If there is no way to do it in Android, what could be the workaround??
----------Update-----------
Like #xevincent suggested, I can reuse the bottom.xml by use <include> tag,
But How to change the id of the elements inside the resued layout?
For example, insdie bottom.xml, I would like to change the id of <editText android:id="#+id/name"> to <editText android:id="#+id/other_name"> when I reuse the bottom.xml layout in other layout, how to change the id ?

See this doc reusing layouts.

Just upvote xevincent's anwser. I added this answer because SO recommends to "Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline."
So, basically, his link explains that you should use <include />.
<com.android.launcher.Workspace
android:id="#+id/workspace"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
launcher:defaultScreen="1">
<include android:id="#+id/cell1" layout="#layout/workspace_screen" />
<include android:id="#+id/cell2" layout="#layout/workspace_screen" />
<include android:id="#+id/cell3" layout="#layout/workspace_screen" />
</com.android.launcher.Workspace>
And know that you can override the layout parameters:
<include android:layout_width="fill_parent" layout="#layout/image_holder" />

Have a look on this doc, Link updated
http://android-developers.blogspot.com/2009/02/android-layout-tricks-2-reusing-layouts.html

Related

#id is not working inside Relative Layout

As far as I know, the difference between #+id and #id is to create a resource id first time and reuse that already existed resource id in different places. For instance, If we have a Relative layout having two textViews one below another, we shall use #resourceId for the second textView which refers to the first TextView.
The problem is, after updating the android studio to 3.0, #resourceId is not working anymore.To place second textView below the first one, I need to use #+firstTextViewId instead of #firstTextViewId. More specifically I need to use,
android:layout_below="#+id/totalQty"
instead of
android:layout_below="#id/totalQty"
Here is the code
<RelativeLayout
android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>
Is it an understanding issue? or a problem from any end? Can anyone please explain?
I just remove + sign at #+id from your code. Here's the updated code
<RelativeLayout android:id="#+id/relBottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
xmlns:android="http://schemas.android.com/apk/res/android">
<TextView
android:id="#+id/totalQty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="abcdef"/>
<TextView
android:id="#+id/totalPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalQty"
android:text="saasdfdsdfsdf"/>
<TextView
android:id="#+id/totalNetPrice"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/totalPrice"
android:text="abcdsadfsafddgfdgfgdef"
/>
</RelativeLayout>

Android Layout Cloning

<RelativeLayout
android:id="#+id/component1"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:background="#drawable/my_shape">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
<RelativeLayout
android:id="#+id/component2"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginLeft="50dp"
android:layout_marginTop="50dp"
android:background="#drawable/my_shape"
android:visibility="gone">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</RelativeLayout>
android:id="#+id/component2"
visibility is gone; which I want to attach in
android:id="#+id/component1"
at runtime; not a single one but N-numbers of component 2 (changing property)
All I mean to say, I want to make cloning of component2
Can anyone please assist me with snip of code ?
Thanks in Advance.
I thnik you are looking for re-using layout
you can create all your component in one layout and call that using include tag
<include layout="#layout/component"/>
<include layout="#layout/component"/>
<include layout="#layout/component"/>
link here
Even with two different layouts for different rows, you still can use a ListView and use different ViewTypes in the ListAdapter.
An other way would be to have just a container layout in your layout xml (e.g. LinearLayout wrapped in a ScrollView) and add the components programmatically. You could have the layout for the component in a separate layout xml and in code, inflate that layout xml to a View (or ViewGroup), set the texts or whatever and finally add the component view to the container view.
I can provide code examples too, if one of the suggestions makes sense for you.

Custom Layout With XML

I've got layout in my app which will contain scrolling banner (it is not finnished yet if you look in my XML), and this banner will be used in other activities. So I want to make it a custom layout so I dont copypaste it X number of times.
here is my XML (well... I am not sure if all is correct so any criticism in this part is appreciated)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#+id/baseID">
<RelativeLayout
android:layout_centerInParent="true"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:id="#+id/id1"
android:background="#ff00ff">
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"/>
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="1dp"
android:layout_alignTop="#id/id1"
android:layout_alignBottom="#id/id1"
android:id="#+id/id2"
android:background="#08000000"
android:orientation="horizontal">
<Button
android:id="#+id/button1"
android:layout_width="20dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="1" />
<TextView
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:text="to jest moj tekst"
android:layout_weight="16"/>
<Button
android:id="#+id/button2"
android:layout_width="20dp"
android:layout_height="fill_parent"
android:layout_weight="1"
android:onClick="onClick"
android:text="1" />
</LinearLayout>
</RelativeLayout>
For now this layout contains only banner, but there will be more stuff.
THe question is: How do I put it to an external class ?
I know that I have to make a new class which extends RelativeLayout(int this case). But what then ? How do I set layout to this class ?
Also I've made some research but I didnt find any simple and accurate tutorial for this. If you know any - please post it.
You could use <include> like:
<include layout="#layout/menu" />
You could even rewrite attributes of the root tag of the included xml layout, like in
<include android:id="#+id/my_menu" layout="#layout/menu" />
See the Developers Blog for a more detailed explanation at
http://android-developers.blogspot.com.es/2009/02/android-layout-tricks-2-reusing-layouts.html
you'll have to work with Fragment:
http://developer.android.com/reference/android/app/Fragment.html
Fragments enable developers to split VIEW/Controller into differents classes.
So, you will add to your xml will differents fragments and each fragment are in charge of his owns components (textview, button...).

Android Layout Matching Peers

I'm looking for some insights into the layout system. Is anyone aware of a method in the Android layout system that allows peer View elements (meaning Views grouped together in the same container) to match their height and width attributes without creating an interstitial container just to govern their sizes?
For example: A layout has two TextView elements. The first should always wrap width to its text content, but the second should match the width of the first. A solution that WORKS is this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textTwo"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
However, I had to create a special container for both views to ensure they both match the width of whatever text is in the first; adding unnecessary complexity to the hierarchy. What I would love to know is if there is a layout attribute I could use to do something like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textTwo"
android:layout_widthMatchPeer="#id/textOne" <!-- An attribute like this -->
android:layout_height="wrap_content"
android:layout_below="#id/textOne" />
</RelativeLayout>
Creating a hard dimension as the width is defeating the purpose of letting the primary element wrap. Is there a technique someone is aware of, or an attribute I've missed, to accomplish this? Could be in any standard layout, I just chose RelativeLayout as an example.
Additionally, what if the elements are not located near each other?
Cheers.
I think the RelativeLayout class can do what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textTwo"
android:layout_alignLeft="#id/textOne"
android:layout_alignRight="#id/textOne"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textOne" />
</RelativeLayout>
since the views are in a relatve layout, use android:layout_below="id/firsttv" android:layout_alignLeft="id/firsttv" and android:layout_alightRight="id/firsttv"

How to modify the childs text from a merge layout from the include xml tag?

I want to somehow modify the content of the merge layout from the include tag. Is that possible?
Layout picAndText.xml:
<merge xmlns:android="http://schemas.android.com/apk/res/android">
<ImageView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scaleType="center"
android:src="#drawable/golden_gate" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="20dip"
android:layout_gravity="center_horizontal|bottom"
android:padding="12dip"
android:background="#AA000000"
android:textColor="#ffffffff"
android:text="Golden Gate" />
</merge>
And the layout xml that uses picAndText.xml:
<include layout="#layout/picAndText" android:id="#+id/picText" />
Please note this is an example of what I would like to acomplish. I want to reuse complex layouts with multiple children but I need different text on them every time I reuse these without having to copy the whole layout everywhere...
Thanks!
-Jona
If I understand what you are trying to do correctly, I don't think its possible from XML. Instead, you can update the text when you load the layout in code.

Categories

Resources