Skip to content

Instantly share code, notes, and snippets.

@sandeeptengale
Last active January 3, 2022 10:25
Show Gist options
  • Save sandeeptengale/c7249f67d56c9b51cef881b3b8fb724f to your computer and use it in GitHub Desktop.
Save sandeeptengale/c7249f67d56c9b51cef881b3b8fb724f to your computer and use it in GitHub Desktop.
Sample code to create BaseActivity for Android
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
app:contentInsetEnd="0dp"
app:contentInsetLeft="0dp"
app:contentInsetRight="0dp"
app:contentInsetStart="0dp"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="@color/color_primary_vtu"
android:contentInsetEnd="0dp"
android:contentInsetLeft="0dp"
android:contentInsetRight="0dp"
android:contentInsetStart="0dp">
<include layout="@layout/layout_toolbar_back" />
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="@+id/layout_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
package com.vtuforum.android.views
import android.app.ProgressDialog
import android.os.Bundle
import android.support.design.widget.CoordinatorLayout
import android.support.v7.app.AppCompatActivity
import android.widget.FrameLayout
import android.widget.ImageButton
import android.widget.TextView
import com.vtuforum.vtustudies.R
abstract class BaseActivity : AppCompatActivity() {
lateinit var mTextViewScreenTitle: TextView
lateinit var mImageButtonBack: ImageButton
lateinit var mProgressDialog: ProgressDialog
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
mProgressDialog = ProgressDialog(this)
mProgressDialog.setMessage("Loading")
mProgressDialog.setCancelable(false)
mProgressDialog.isIndeterminate = true
}
override fun setContentView(layoutResID: Int) {
var coordinatorLayout: CoordinatorLayout = layoutInflater.inflate(R.layout.activity_back_base, null) as CoordinatorLayout
var activityContainer: FrameLayout = coordinatorLayout.findViewById(R.id.layout_container)
mTextViewScreenTitle = coordinatorLayout.findViewById(R.id.text_screen_title) as TextView
mImageButtonBack = coordinatorLayout.findViewById(R.id.image_back_button)
layoutInflater.inflate(layoutResID, activityContainer, true)
super.setContentView(coordinatorLayout)
}
fun setScreenTitle(resId: Int) {
mTextViewScreenTitle.text = getString(resId)
}
fun setScreenTitle(title: String) {
mTextViewScreenTitle.text = title
}
fun getBackButton(): ImageButton {
return mImageButtonBack;
}
fun showProgressDialog() {
if(!mProgressDialog.isShowing) {
mProgressDialog.show()
}
}
fun dismissProgressDialog() {
if (mProgressDialog.isShowing) {
mProgressDialog.dismiss()
}
}
}
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@color/color_primary_vtu">
<ImageButton
android:id="@+id/image_back_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="16dp"
android:background="@android:color/transparent"
android:src="@drawable/baseline_arrow_back_white_24" />
<TextView
android:id="@+id/text_screen_title"
style="@style/TextStyle16Sp"
fontPath="fonts/Roboto-Light.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginStart="24dp"
android:layout_toEndOf="@id/image_back_button"
android:fontFamily="@font/roboto_light"
android:textSize="20sp"
tools:text="Screen Title" />
</RelativeLayout>
package com.vtuforum.android
import android.os.Bundle
import android.support.v4.content.ContextCompat
import com.vtuforum.android.R
import com.vtuforum.android.views.BackActivity
public class MainActivity: BackBaseActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
initlizeViews()
}
private fun initlizeViews() {
setScreenTitle(R.string.home_labmanuals)
}
}
@leandro-jimenez
Copy link

leandro-jimenez commented Mar 28, 2019

I made the equivalent code without kotlin, I hope it helps someone
`
import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

abstract class BaseActivity extends AppCompatActivity {
TextView mTextViewScreenTitle;
ImageButton mImageButtonBack;
ProgressDialog mProgressDialog;
@OverRide
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mProgressDialog = new ProgressDialog(this);
mProgressDialog.setMessage("Loading");
mProgressDialog.setCancelable(false);
mProgressDialog.setIndeterminate(true);
}

@Override
public void setContentView(int layoutResID) {
    ConstraintLayout constraintLayout= (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base,null);
    FrameLayout activityContainer = constraintLayout.findViewById(R.id.layout_container);
    mTextViewScreenTitle = (TextView) constraintLayout.findViewById(R.id.text_screen_title);
    mImageButtonBack = (ImageButton) constraintLayout.findViewById(R.id.image_back_button);
    super.setContentView(layoutResID);
}

public  void setScreenTitle(int resId){
    mTextViewScreenTitle.setText(resId);
}

public  void setScreenTitle(String resId){
    mTextViewScreenTitle.setText(resId);
}

public ImageButton getBackButton(){
    return mImageButtonBack;
}

public void showProgressDialog(){
    if(!mProgressDialog.isShowing()){
        mProgressDialog.show();
    }
}

public void dismissProgressDialog(){
    if(mProgressDialog.isShowing()){
        mProgressDialog.dismiss();
    }
}

}
`

@nitinmehtaa
Copy link

nitinmehtaa commented Apr 14, 2020

@pavi2410 If your toolbar design is different for different Activities then ideally you should create them in respective activity_layout.xml file or else if your base layout is same(like background/color of toolbar) but you want to show different icons and text styling then you should create toolbar in Base activity only and use their visibility in respective places.

@pavi2410
Copy link

@nitinmehtaa I have now learnt that a toolbar per screen is the best approach. Because some screens have different designs and some wouldn't even need a toolbar. Also, in order to use AppBarLayout, you create it in the Fragment's view as the layout can vary according to the content. This also works well with ViewBinding.

@Rishabhsarawat
Copy link

Rishabhsarawat commented May 10, 2020

I have made an equivalent code for java, the correct one,
package com.example.proteangallery;

import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;

import android.app.ProgressDialog;
import android.os.Bundle;
import android.widget.FrameLayout;
import android.widget.ImageButton;
import android.widget.TextView;

public abstract class BaseActivity extends AppCompatActivity {

TextView mTextViewScreenTitle;
ImageButton mImageButtonBack;
ProgressDialog mProgressDialog;

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mProgressDialog = new ProgressDialog(this);
    mProgressDialog.setMessage("Loading");
    mProgressDialog.setCancelable(false);
    mProgressDialog.setIndeterminate(true);
}

@Override
public void setContentView(int layoutResID) {
    ConstraintLayout constraintLayout= (ConstraintLayout) getLayoutInflater().inflate(R.layout.activity_base,null);
    FrameLayout activityContainer = constraintLayout.findViewById(R.id.layout_container);
    mTextViewScreenTitle = constraintLayout.findViewById(R.id.text_screen_title);
    
    getLayoutInflater().inflate(layoutResID, activityContainer, true);
    super.setContentView(layoutResID);
}

public  void setScreenTitle(int resId){
    mTextViewScreenTitle.setText(resId);
}

public  void setScreenTitle(String resId){
    mTextViewScreenTitle.setText(resId);
}

public ImageButton getBackButton(){
    return mImageButtonBack;
}

public void showProgressDialog(){
    if(!mProgressDialog.isShowing()){
        mProgressDialog.show();
    }
}

public void dismissProgressDialog(){
    if(mProgressDialog.isShowing()){
        mProgressDialog.dismiss();
    }
}

}

@ganpatmalekar
Copy link

@nitinmehtaa @sandeeptengale how to write this same base activity in kotlin using ViewBinding?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment