3
\$\begingroup\$

I have 3 ToolBars that I use across my whole application

The first one is just a ToolBar with a close button.

The second one is a ToolBar with a close and delete icon.

The third one is a ToolBar with a close and home icon.

Here is my case, I use this ToolBar in many places, and I dont want to use Fragment for some reasons so I am forced to use Activity.

For usability, I created methods to handle their functions, but I am sure I can do something to make this easier as I repeat this code in each activity.

toolbar_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/app_bg"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/CloseImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_chevron_left_white_48dp" />

        <TextView
            android:id="@+id/toolbarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToolBar Title"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="18sp"
            android:textColor="@color/white" />
    </RelativeLayout>


</android.support.v7.widget.Toolbar>

toolbar_layout_delete_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/app_bg"
    >

    <android.support.constraint.ConstraintLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/CloseImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_chevron_left_white_48dp"
            tools:layout_constraintBottom_creator="1"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:layout_constraintLeft_creator="1"
            app:layout_constraintLeft_toLeftOf="parent"
            app:layout_constraintTop_toTopOf="parent" />

        <TextView
            android:id="@+id/toolbarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToolBar Title"
            android:gravity="center"
            android:textSize="18sp"
            android:textColor="@color/white"
            tools:layout_constraintBottom_creator="1"
            android:layout_marginStart="84dp"
            app:layout_constraintBottom_toBottomOf="parent"
            tools:layout_constraintLeft_creator="1"
            android:layout_marginBottom="16dp"
            app:layout_constraintLeft_toRightOf="@+id/CloseImageView"
            android:layout_marginLeft="84dp"
            app:layout_constraintHorizontal_bias="0.54" />

        <ImageView
            android:id="@+id/DeleteImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_delete_icon_36dp"
            tools:ignore="MissingConstraints"
            app:layout_constraintTop_toTopOf="parent"
            app:layout_constraintRight_toRightOf="parent"
            app:layout_constraintBottom_toBottomOf="parent"
            android:layout_marginEnd="16dp"
            android:layout_marginRight="16dp" />
    </android.support.constraint.ConstraintLayout>


</android.support.v7.widget.Toolbar>

toolbar_layout_home_icon.xml

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:minHeight="?attr/actionBarSize"
    android:background="@color/app_bg"
    >
    <RelativeLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <ImageView
            android:id="@+id/CloseImageView"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_chevron_left_white_48dp" />

        <TextView
            android:id="@+id/toolbarTitle"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="ToolBar Title"
            android:gravity="center"
            android:layout_centerVertical="true"
            android:layout_centerHorizontal="true"
            android:textSize="18sp"
            android:textColor="@color/white" />

        <ImageView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            app:srcCompat="@drawable/ic_home_white_36dp"
            android:layout_centerVertical="true"
            android:layout_alignParentRight="true"
            android:layout_alignParentEnd="true"
            android:layout_marginRight="15dp"
            android:layout_marginEnd="15dp"
            android:id="@+id/HomeImageView" />
    </RelativeLayout>


</android.support.v7.widget.Toolbar>

java method that handles the ToolBar :

private void setupToolbar() {
        Toolbar mytoolbar = (Toolbar) findViewById(R.id.mytoolbar);
        TextView toolbarTitle = (TextView) mytoolbar.findViewById(R.id.toolbarTitle);
        toolbarTitle.setText(getResources().getString(R.string.submit_claim));
        ImageView closeButton = (ImageView) mytoolbar.findViewById(R.id.CloseImageView);
        closeButton.setOnClickListener(v -> finish());
        ImageView HomeButton = (ImageView) mytoolbar.findViewById(R.id.HomeImageView);
        HomeButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent mIntent = new Intent(PolicyAndContactDetailActivity.this , MainActivity.class);
                mIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
                startActivity(mIntent);
                finish();
            }
        });
    }

I need to minimize my code as much as possible, and any help with @+id names will be appreciated.

\$\endgroup\$
3
\$\begingroup\$

Create a Base Activity with ToolBar and let all your activity extend the BaseActivity like below code I've given :

BaseActivity

public class BaseActivity extends AppCompatActivity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
    }
}

Main.xml (Base activity layout)

    <android.support.v7.widget.Toolbar
        android:id="@+id/toolbar"
        android:theme="@style/toolbarTheme"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:background="@color/green"
        android:minHeight="?attr/actionBarSize" />

Now every Activity extends BaseActivity instead of AppCompatActivity or ActionBarActivity so you can get access to the Toolbar in every Activity.

Your MainActivity

public class YourActivity extends BaseActivity{ //your code }
\$\endgroup\$
0
0
\$\begingroup\$

Use include Tag in your activity layouts and toolbar method in separate class to reuse in all activities. change your method signature:-

private void setupToolbar(View.OnClickListener listener);

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.