Skip to content

Commit ddab0c7

Browse files
added bottom sheets android example
1 parent da42dc5 commit ddab0c7

49 files changed

Lines changed: 1128 additions & 0 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

BottomSheetAndroid/.gitignore

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
*.iml
2+
.gradle
3+
/local.properties
4+
/.idea/caches
5+
/.idea/libraries
6+
/.idea/modules.xml
7+
/.idea/workspace.xml
8+
/.idea/navEditor.xml
9+
/.idea/assetWizardSettings.xml
10+
.DS_Store
11+
/build
12+
/captures
13+
.externalNativeBuild
14+
.cxx

BottomSheetAndroid/app/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
/build
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
apply plugin: 'com.android.application'
2+
apply plugin: 'kotlin-android'
3+
apply plugin: 'kotlin-android-extensions'
4+
5+
android {
6+
compileSdkVersion 30
7+
buildToolsVersion "30.0.1"
8+
9+
defaultConfig {
10+
applicationId "com.example.bottomsheetandroid"
11+
minSdkVersion 24
12+
targetSdkVersion 30
13+
versionCode 1
14+
versionName "1.0"
15+
16+
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
17+
}
18+
19+
buildTypes {
20+
release {
21+
minifyEnabled false
22+
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
23+
}
24+
}
25+
}
26+
27+
dependencies {
28+
implementation fileTree(dir: "libs", include: ["*.jar"])
29+
implementation "org.jetbrains.kotlin:kotlin-stdlib:$kotlin_version"
30+
implementation 'androidx.core:core-ktx:1.3.1'
31+
implementation 'androidx.appcompat:appcompat:1.2.0'
32+
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
33+
implementation 'com.google.android.material:material:1.2.0'
34+
testImplementation 'junit:junit:4.12'
35+
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
36+
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
37+
38+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
# Add project specific ProGuard rules here.
2+
# You can control the set of applied configuration files using the
3+
# proguardFiles setting in build.gradle.
4+
#
5+
# For more details, see
6+
# http://developer.android.com/guide/developing/tools/proguard.html
7+
8+
# If your project uses WebView with JS, uncomment the following
9+
# and specify the fully qualified class name to the JavaScript interface
10+
# class:
11+
#-keepclassmembers class fqcn.of.javascript.interface.for.webview {
12+
# public *;
13+
#}
14+
15+
# Uncomment this to preserve the line number information for
16+
# debugging stack traces.
17+
#-keepattributes SourceFile,LineNumberTable
18+
19+
# If you keep the line number information, uncomment this to
20+
# hide the original source file name.
21+
#-renamesourcefileattribute SourceFile
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package com.example.bottomsheetandroid
2+
3+
import androidx.test.platform.app.InstrumentationRegistry
4+
import androidx.test.ext.junit.runners.AndroidJUnit4
5+
6+
import org.junit.Test
7+
import org.junit.runner.RunWith
8+
9+
import org.junit.Assert.*
10+
11+
/**
12+
* Instrumented test, which will execute on an Android device.
13+
*
14+
* See [testing documentation](http://d.android.com/tools/testing).
15+
*/
16+
@RunWith(AndroidJUnit4::class)
17+
class ExampleInstrumentedTest {
18+
@Test
19+
fun useAppContext() {
20+
// Context of the app under test.
21+
val appContext = InstrumentationRegistry.getInstrumentation().targetContext
22+
assertEquals("com.example.bottomsheetandroid", appContext.packageName)
23+
}
24+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
3+
package="com.example.bottomsheetandroid">
4+
5+
<application
6+
android:allowBackup="true"
7+
android:icon="@mipmap/ic_launcher"
8+
android:label="@string/app_name"
9+
android:roundIcon="@mipmap/ic_launcher_round"
10+
android:supportsRtl="true"
11+
android:theme="@style/AppTheme">
12+
<activity android:name=".MainActivity">
13+
<intent-filter>
14+
<action android:name="android.intent.action.MAIN" />
15+
16+
<category android:name="android.intent.category.LAUNCHER" />
17+
</intent-filter>
18+
</activity>
19+
</application>
20+
21+
</manifest>
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.example.bottomsheetandroid
2+
3+
import android.os.Bundle
4+
import android.view.View
5+
import androidx.appcompat.app.AppCompatActivity
6+
import com.google.android.material.bottomsheet.BottomSheetBehavior
7+
import com.google.android.material.bottomsheet.BottomSheetBehavior.BottomSheetCallback
8+
import kotlinx.android.synthetic.main.activity_main.*
9+
import kotlinx.android.synthetic.main.layout_persistent_bottomsheet.*
10+
11+
12+
class MainActivity : AppCompatActivity() {
13+
override fun onCreate(savedInstanceState: Bundle?) {
14+
super.onCreate(savedInstanceState)
15+
setContentView(R.layout.activity_main)
16+
17+
btnModalBottomsheet.setOnClickListener {
18+
MyBottomSheetDialogFragment().apply {
19+
show(supportFragmentManager, tag)
20+
}
21+
}
22+
23+
val standardBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet)
24+
25+
standardBottomSheetBehavior.addBottomSheetCallback(object : BottomSheetCallback() {
26+
27+
override fun onSlide(bottomSheet: View, slideOffset: Float) {
28+
29+
}
30+
31+
override fun onStateChanged(bottomSheet: View, newState: Int) {
32+
33+
}
34+
})
35+
36+
btnPersistentBottomsheet.setOnClickListener {
37+
standardBottomSheetBehavior.state = BottomSheetBehavior.STATE_EXPANDED
38+
}
39+
}
40+
}
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
package com.example.bottomsheetandroid
2+
3+
data class Item(val name: String, val image: Int)
Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,42 @@
1+
package com.example.bottomsheetandroid
2+
3+
import android.os.Bundle
4+
import android.view.LayoutInflater
5+
import android.view.View
6+
import android.view.ViewGroup
7+
import androidx.recyclerview.widget.GridLayoutManager
8+
import com.google.android.material.bottomsheet.BottomSheetDialogFragment
9+
import kotlinx.android.synthetic.main.layout_modal_bottomsheet.*
10+
11+
12+
class MyBottomSheetDialogFragment : BottomSheetDialogFragment() {
13+
14+
override fun onCreateView(
15+
inflater: LayoutInflater,
16+
container: ViewGroup?,
17+
savedInstanceState: Bundle?
18+
): View? {
19+
return inflater.inflate(R.layout.layout_modal_bottomsheet, container, false)
20+
}
21+
22+
override fun onActivityCreated(savedInstanceState: Bundle?) {
23+
super.onActivityCreated(savedInstanceState)
24+
25+
26+
val items = mutableListOf<Item>()
27+
items.add(Item("Folder",R.drawable.ic_baseline_create_new_folder_24))
28+
items.add(Item("File",R.drawable.ic_baseline_insert_drive_file_24))
29+
items.add(Item("Photo",R.drawable.ic_baseline_add_a_photo_24))
30+
items.add(Item("Post",R.drawable.ic_baseline_post_add_24))
31+
items.add(Item("Poll",R.drawable.ic_baseline_poll_24))
32+
items.add(Item("Group",R.drawable.ic_baseline_group_add_24))
33+
items.add(Item("Alarm",R.drawable.ic_baseline_alarm_add_24))
34+
items.add(Item("Library",R.drawable.ic_baseline_library_add_24))
35+
36+
37+
recyclerview.layoutManager = GridLayoutManager(context, 3)
38+
val adapter = RecyclerviewAdapter(items)
39+
recyclerview.adapter = adapter
40+
41+
}
42+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.example.bottomsheetandroid
2+
3+
import android.view.LayoutInflater
4+
import android.view.View
5+
import android.view.ViewGroup
6+
import androidx.core.content.ContextCompat
7+
import androidx.recyclerview.widget.RecyclerView
8+
import kotlinx.android.synthetic.main.adapter_create_new.view.*
9+
10+
class RecyclerviewAdapter(val items: List<Item>) : RecyclerView.Adapter<MyViewHolder>() {
11+
12+
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): MyViewHolder {
13+
val view = LayoutInflater.from(parent.context).inflate(R.layout.adapter_create_new, parent, false)
14+
return MyViewHolder(view)
15+
}
16+
17+
override fun getItemCount(): Int {
18+
return items.size
19+
}
20+
21+
override fun onBindViewHolder(holder: MyViewHolder, position: Int) {
22+
23+
val item = items[position]
24+
holder.name.text = item.name
25+
holder.img.background = ContextCompat.getDrawable(holder.itemView.context, item.image)
26+
}
27+
28+
}
29+
30+
class MyViewHolder(view: View) : RecyclerView.ViewHolder(view) {
31+
val name = view.name
32+
val img = view.img
33+
}

0 commit comments

Comments
 (0)