user1763198
user1763198

Reputation: 83

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.

Upvotes: 1

Views: 157

Answers (2)

rgrocha
rgrocha

Reputation: 1461

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

Upvotes: 1

Stephane
Stephane

Reputation: 171

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...).

Upvotes: 0

Related Questions