Last active
January 3, 2022 10:25
-
-
Save sandeeptengale/c7249f67d56c9b51cef881b3b8fb724f to your computer and use it in GitHub Desktop.
Sample code to create BaseActivity for Android
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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() | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | |
} | |
} |
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();
}
}
}
@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
@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.