Screen Inheritance - Android

6

Wanted creates a base (Activity) screen for the android app I'm developing so all other screens inherit from this base. Therefore, the screen would have an area reserved for the top of the page, a central region where the content will be variable and a footer area that would contain two buttons. How could I do that?

    
asked by anonymous 23.02.2015 / 12:29

3 answers

7

Other answers have given some alternatives, but I think I can add one more.

My suggestion would be to create a single layout as a real template and use ViewStub to define the dynamic content of this template when creating% .

For example:

template.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"
    android:padding="5dp">

    <!-- Aqui entra seu topo -->
    <include ... />

    <!-- O ViewStub guarda lugar para um layout
         a ser definido por quem usar -->
    <ViewStub android:id="@+id/stub"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:weight="1" />

    <!-- Aqui entra seu fundo -->
    <include ... />
</LinearLayout>

To dynamically change the layout while creating the Activity's , just do:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Pode "setar" normalmente seu layout
    setContentView(R.layout.template);

    ViewStub vs = findViewById(R.id.stub);

    // Define qual o layout a ser inflado
    vs.setLayoutResource(R.layout.seuLayoutAtual);
    View conteudoPrincipal = vs.inflate();
}

Remembering that Activity will exit the tree and will give way to every subtree that is defined in the layout that you pass in ViewStub method. The ViewStub.setLayoutResource method returns the root of the layout you inflated.

This way you can reuse the same layout just by changing the content you need.

For the function question / behaviors that belong to the template , you can create a class, which can be abstract if you prefer, inheriting from some subclass or itself inflate ( Activity FragmentActivity and so on) that contains this behavior that is common to all.

You would have something like:

public abstract class BaseActivity extends Activity {

    // Template method para retornar o conteudo
    // interno ao template.
    // Sendo abstrato para obrigar que a subclass forneca
    public abstract @LayoutRes int getContentLayoutId();

    // Metodo responsavel por configurar o conteudo do template
    public abstract void onCreateContent(View conteudo);

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        setContentView(R.layout.template);

        ViewStub vs = findViewById(R.id.stub);

        // Define qual o layout a ser inflado
        vs.setLayoutResource(getContentLayoutId());
        View conteudoPrincipal = vs.inflate();

        // Configuracao de listener de eventos e etc

        // Deixa a subclasse configurar o conteudo
        onCreateContent(conteudoPrincipal);
    }

    // Metodos comuns
}

Where each%

public class ActivityLoja extends BaseActivity {
    @Override
    public int getContentLayoutId() {
        return R.layout.loja;
    }

    @Override
    public void onCreateContent(View conteudo) {
        // Configuracao do conteudo
    }

    // Restante do codigo da sua Activity
}

public class ActivityCarrinho extends BaseActivity {
    @Override
    public int getContentLayoutId() {
        return R.layout.carrinho;
    }

    @Override
    public void onCreateContent(View conteudo) {
        // Configuracao do conteudo
    }

    // Restante do codigo da sua Activity
}

The registry is normal, as if there were no inheritance:

<activity
    android:name=".activity.ActivityLoja"
    android:label="@string/loja" />

<activity
    android:name=".activity.ActivityCarrinho"
    android:name="@string/carrinho" />

Some references:

24.02.2015 / 01:17
5

I believe that you are a Screen Designer (XML) and not a Screen Designer (activity), am I right?

If this is your question then, you want to know how to do an old practice of reuse of layouts as well as good practice on web pages.

To make a canvas or an xml layout that can be reused everywhere of the application, so that it has a top a body and a footer.

do ..

master_top.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dp" >

<ImageView
    android:id="@+id/imageView"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_alignParentLeft="true"
    android:layout_alignParentTop="true"
    android:layout_margin="5dp"
    android:src="@drawable/my_logo" />

</RelativeLayout>

activity_main.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#f0f0f0" >


<include android:id="@+id/topo_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    layout="@layout/master_top" />

<LinearLayout
    android:id="@+id/linearLayout1"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_margin="10dp"
    android:background="#ffffff"
    android:orientation="vertical"
    android:padding="5dp" >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="$$$$$$$ CORPO $$$$$$$"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:textColor="#1AA52F" />

</LinearLayout>

<include
    android:id="@+id/rodape_view"
    android:layout_width="fill_parent"
    android:layout_height="wrap_content"
    android:layout_alignParentBottom="true"
    layout="@layout/master_footer" />

</RelativeLayout>

master_footer.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ffffff"
android:padding="5dp" >

<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:layout_centerHorizontal="true"
    android:layout_centerVertical="true"
    android:gravity="center"
    android:text="-----------RODAPE---------------"
    android:textColor="#EFEFEF" />

</RelativeLayout>

...

You can also use < merge >

Official site REF. link

    
23.02.2015 / 14:14
4

Like, it would be like you do the inverse of the example! In the example just make a copy of the (XML) and change the body and ready. But there is no escape, a part of the layout you will not get out of the work to do.

To do that you just said the opposite.

Example:

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:background="#ffffff"
    android:padding="5dp" >

    <!-- Top -->
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_margin="5dp"
        android:src="@drawable/my_logo" />



<!-- Body -->
    <include android:id="@+id/topo_view"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        layout="@layout/my_body" />



    <!-- Footer --> 
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true"
        android:gravity="center"
        android:text="-----------RODAPE---------------"
        android:textColor="#EFEFEF" />

    </RelativeLayout>

my_body.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
                android:layout_width="fill_parent"
                android:layout_height="fill_parent"
                android:background="#f0f0f0" >


    <LinearLayout
        android:id="@+id/linearLayout1"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_margin="10dp"
        android:background="#ffffff"
        android:orientation="vertical"
        android:padding="5dp" >

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@@@@@@@ MY BODY @@@@@@@@"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:textColor="#1AA52F" />

    </LinearLayout>


    </RelativeLayout>
    
24.02.2015 / 00:39