Skip to content

Instantly share code, notes, and snippets.

@silmood
Last active June 25, 2016 22:06
Show Gist options
  • Select an option

  • Save silmood/2b250c41e59ef9a308ef5e670f8e0d92 to your computer and use it in GitHub Desktop.

Select an option

Save silmood/2b250c41e59ef9a308ef5e670f8e0d92 to your computer and use it in GitHub Desktop.
Kotlin common classes for android
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
layout="@layout/toolbar"/>
</LinearLayout>
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.widget.Toolbar
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Created by Silmood on 04/2016
*/
abstract class BaseActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(getLayout())
setupToolbar()
}
fun setupToolbar() {
val toolbar : Toolbar? = findViewById(R.id.toolbar) as Toolbar
if(toolbar != null) {
setSupportActionBar(toolbar)
}
}
abstract fun getLayout() : Int
}
import android.os.Bundle
import android.support.v4.app.Fragment
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*
* Created by Silmood on 02/2016.
*/
abstract class FragmentContainerActivity : BaseActivity() {
override fun onCreate(savedInstanceState: Bundle?){
super.onCreate(savedInstanceState)
val fragment : Fragment? = supportFragmentManager.findFragmentById(R.id.fragment_container)
if(fragment == null){
supportFragmentManager.beginTransaction()
.add(R.id.fragment_container, createFragment())
.commit()
}
}
abstract fun createFragment() : Fragment
}
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar
android:id="@+id/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:background="?attr/colorPrimary"
android:minHeight="?attr/actionBarSize"
app:titleTextColor="@android:color/white"
app:popupTheme="@style/ThemeOverlay.AppCompat.Dark"/>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment