D
D
di_gangsta2019-10-05 18:24:52
Android
di_gangsta, 2019-10-05 18:24:52

How to create XML file with program code for multiple Activities?

I created a header for several activities, which I add to them using the include tag
. The headera code itself:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:id="@+id/mainlayout"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/toolbar">
    </include>

    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="35dp"
        android:background="@color/colorRed">

        <Button
            android:layout_width="12dp"
            android:layout_height="12dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_centerVertical="true"
            android:layout_marginStart="38dp"
            android:layout_marginLeft="38dp"
            android:background="@drawable/icon_loupe" />

        <EditText
            android:id="@+id/text_search"
            android:layout_width="178dp"
            android:layout_height="34dp"
            android:layout_alignParentStart="true"
            android:layout_alignParentLeft="true"
            android:layout_alignParentBottom="true"
            android:layout_marginStart="58dp"
            android:layout_marginLeft="58dp"
            android:layout_marginBottom="0dp"
            android:background="@color/colorRed"
            android:hint="@string/search"
            android:inputType="text"
            android:textColor="@color/colorWhite"
            android:textColorHint="@color/colorWhite"
            android:textSize="10sp"
            android:fontFamily="@font/font_gotham_pro"

            android:visibility="visible" />

    </RelativeLayout>
</LinearLayout>

This header includes all the main elements, but I need to add a button click handler for them and some other logic components that can only be written in code. I could copy this piece of code with logic for each .java file that inherits AppCompatActivity, but I would like to create some kind of .java file that will describe all this logic and not have to make dirt. How can I do it?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2019-10-06
@di_gangsta

a couple of ideas:
- make a fragment. Write the code in a fragment.
- create a handler class, which will be delegated to handle clicks, and so on
- create a custom view. Which will be immediately inserted (instead of include)
to choose from. Based on your abilities and application requirements.

T
tiroman, 2019-10-05
@tiroman

So what's the problem with creating a "java file that inherits AppCompatActivity, in which all this logic will be described" and then inherit other activities from it?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question