Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment