From f34ac71bd7c8de1aaf445e845a301e3e484d8635 Mon Sep 17 00:00:00 2001 From: "m.metwally@arabia-it.com" Date: Thu, 5 Sep 2019 10:55:00 +0200 Subject: [PATCH 1/5] migrate to kotlin , androidx --- app/build.gradle | 23 ++- .../CustomBottomSheetDialogFragment.java | 17 --- .../CustomBottomSheetDialogFragment.kt | 15 ++ .../bottomsheets/MainActivity.java | 133 ------------------ .../bottomsheets/MainActivity.kt | 114 +++++++++++++++ app/src/main/res/layout/activity_main.xml | 11 +- .../layout/content_dialog_bottom_sheet.xml | 5 +- build.gradle | 12 +- gradle.properties | 4 +- gradle/wrapper/gradle-wrapper.properties | 4 +- 10 files changed, 166 insertions(+), 172 deletions(-) delete mode 100644 app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.java create mode 100644 app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt delete mode 100644 app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.java create mode 100644 app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt diff --git a/app/build.gradle b/app/build.gradle index d8192dd..c315889 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -1,13 +1,15 @@ apply plugin: 'com.android.application' +apply plugin: 'kotlin-android-extensions' +apply plugin: 'kotlin-android' android { - compileSdkVersion 23 - buildToolsVersion "23.0.2" + compileSdkVersion 28 + buildToolsVersion "28.0.3" defaultConfig { applicationId "com.androidtutorialshub.bottomsheets" minSdkVersion 15 - targetSdkVersion 23 + targetSdkVersion 28 versionCode 1 versionName "1.0" } @@ -16,12 +18,19 @@ android { minifyEnabled false proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } + debug{ + } } } dependencies { - compile fileTree(dir: 'libs', include: ['*.jar']) - testCompile 'junit:junit:4.12' - compile 'com.android.support:appcompat-v7:23.2.1' - compile 'com.android.support:design:23.2.1' + implementation fileTree(dir: 'libs', include: ['*.jar']) + testImplementation'junit:junit:4.12' + implementation 'androidx.appcompat:appcompat:1.0.2' + implementation 'com.google.android.material:material:1.0.0' + compile "androidx.core:core-ktx:+" + implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" //implementation +} +repositories { + mavenCentral() } diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.java b/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.java deleted file mode 100644 index edb777b..0000000 --- a/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.androidtutorialshub.bottomsheets; - -import android.os.Bundle; -import android.support.design.widget.BottomSheetDialogFragment; -import android.view.LayoutInflater; -import android.view.View; -import android.view.ViewGroup; - -public class CustomBottomSheetDialogFragment extends BottomSheetDialogFragment { - @Override - public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { - View v = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false); - return v; - } - - -} diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt b/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt new file mode 100644 index 0000000..339da64 --- /dev/null +++ b/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt @@ -0,0 +1,15 @@ +package com.androidtutorialshub.bottomsheets + +import android.os.Bundle +import android.view.LayoutInflater +import android.view.View +import android.view.ViewGroup +import com.google.android.material.bottomsheet.BottomSheetDialogFragment + +class CustomBottomSheetDialogFragment : BottomSheetDialogFragment() { + override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { + return inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false) + } + + +} diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.java b/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.java deleted file mode 100644 index 295e13f..0000000 --- a/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.java +++ /dev/null @@ -1,133 +0,0 @@ -package com.androidtutorialshub.bottomsheets; - -import android.os.Bundle; -import android.support.design.widget.BottomSheetBehavior; -import android.support.v7.app.AppCompatActivity; -import android.support.v7.widget.Toolbar; -import android.util.Log; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; - -public class MainActivity extends AppCompatActivity implements View.OnClickListener { - - // BottomSheetBehavior variable - private BottomSheetBehavior bottomSheetBehavior; - - // TextView variable - private TextView bottomSheetHeading; - - // Button variables - private Button expandBottomSheetButton; - private Button collapseBottomSheetButton; - private Button hideBottomSheetButton; - private Button showBottomSheetDialogButton; - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_main); - - initViews(); - initListeners(); - - - } - - /** - * method to initialize the views - */ - private void initViews() { - Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar); - setSupportActionBar(toolbar); - - bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottomSheetLayout)); - bottomSheetHeading = (TextView) findViewById(R.id.bottomSheetHeading); - expandBottomSheetButton = (Button) findViewById(R.id.expand_bottom_sheet_button); - collapseBottomSheetButton = (Button) findViewById(R.id.collapse_bottom_sheet_button); - hideBottomSheetButton = (Button) findViewById(R.id.hide_bottom_sheet_button); - showBottomSheetDialogButton = (Button) findViewById(R.id.show_bottom_sheet_dialog_button); - - - } - - - /** - * method to initialize the listeners - */ - private void initListeners() { - // register the listener for button click - expandBottomSheetButton.setOnClickListener(this); - collapseBottomSheetButton.setOnClickListener(this); - hideBottomSheetButton.setOnClickListener(this); - showBottomSheetDialogButton.setOnClickListener(this); - - // Capturing the callbacks for bottom sheet - bottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() { - @Override - public void onStateChanged(View bottomSheet, int newState) { - - if (newState == BottomSheetBehavior.STATE_EXPANDED) { - bottomSheetHeading.setText(getString(R.string.text_collapse_me)); - } else { - bottomSheetHeading.setText(getString(R.string.text_expand_me)); - } - - // Check Logs to see how bottom sheets behaves - switch (newState) { - case BottomSheetBehavior.STATE_COLLAPSED: - Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED"); - break; - case BottomSheetBehavior.STATE_DRAGGING: - Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING"); - break; - case BottomSheetBehavior.STATE_EXPANDED: - Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED"); - break; - case BottomSheetBehavior.STATE_HIDDEN: - Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN"); - break; - case BottomSheetBehavior.STATE_SETTLING: - Log.e("Bottom Sheet Behaviour", "STATE_SETTLING"); - break; - } - } - - - @Override - public void onSlide(View bottomSheet, float slideOffset) { - - } - }); - - - } - - /** - * onClick Listener to capture button click - * - * @param v - */ - @Override - public void onClick(View v) { - switch (v.getId()) { - case R.id.collapse_bottom_sheet_button: - // Collapsing the bottom sheet - bottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED); - break; - case R.id.expand_bottom_sheet_button: - // Expanding the bottom sheet - bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED); - break; - case R.id.hide_bottom_sheet_button: - // Hiding the bottom sheet - bottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN); - break; - case R.id.show_bottom_sheet_dialog_button: - // Opening the Dialog Bottom Sheet - new CustomBottomSheetDialogFragment().show(getSupportFragmentManager(), "Dialog"); - break; - - } - } -} diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt b/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt new file mode 100644 index 0000000..5541ed0 --- /dev/null +++ b/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt @@ -0,0 +1,114 @@ +package com.androidtutorialshub.bottomsheets + +import android.os.Bundle +import android.util.Log +import android.view.View +import android.widget.Button +import android.widget.TextView + +import androidx.appcompat.app.AppCompatActivity +import androidx.appcompat.widget.Toolbar + +import com.google.android.material.bottomsheet.BottomSheetBehavior + +class MainActivity : AppCompatActivity(), View.OnClickListener { + // BottomSheetBehavior variable + private var bottomSheetBehavior: BottomSheetBehavior<*>? = null + // TextView variable + private var bottomSheetHeading: TextView? = null + // Button variables + private var expandBottomSheetButton: Button? = null + private var collapseBottomSheetButton: Button? = null + private var hideBottomSheetButton: Button? = null + private var showBottomSheetDialogButton: Button? = null + + override fun onCreate(savedInstanceState: Bundle?) { + super.onCreate(savedInstanceState) + setContentView(R.layout.activity_main) + + initViews() + initListeners() + + + } + + /** + * method to initialize the views + */ + private fun initViews() { + val toolbar = findViewById(R.id.toolbar) as Toolbar + setSupportActionBar(toolbar) + + bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottomSheetLayout)) + bottomSheetHeading = findViewById(R.id.bottomSheetHeading) as TextView + + expandBottomSheetButton = findViewById(R.id.expand_bottom_sheet_button) as Button + collapseBottomSheetButton = findViewById(R.id.collapse_bottom_sheet_button) as Button + hideBottomSheetButton = findViewById(R.id.hide_bottom_sheet_button) as Button + showBottomSheetDialogButton = findViewById(R.id.show_bottom_sheet_dialog_button) as Button + + + } + + + /** + * method to initialize the listeners + */ + private fun initListeners() { + // register the listener for button click + expandBottomSheetButton!!.setOnClickListener(this) + collapseBottomSheetButton!!.setOnClickListener(this) + hideBottomSheetButton!!.setOnClickListener(this) + showBottomSheetDialogButton!!.setOnClickListener(this) + + // Capturing the callbacks for bottom sheet + bottomSheetBehavior!!.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { + override fun onStateChanged(bottomSheet: View, newState: Int) { + + if (newState == BottomSheetBehavior.STATE_EXPANDED) { + bottomSheetHeading!!.text = getString(R.string.text_collapse_me) + } else { + bottomSheetHeading!!.text = getString(R.string.text_expand_me) + } + + // Check Logs to see how bottom sheets behaves + when (newState) { + BottomSheetBehavior.STATE_COLLAPSED -> Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED") + BottomSheetBehavior.STATE_DRAGGING -> Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING") + BottomSheetBehavior.STATE_EXPANDED -> Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED") + BottomSheetBehavior.STATE_HIDDEN -> Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN") + BottomSheetBehavior.STATE_SETTLING -> Log.e("Bottom Sheet Behaviour", "STATE_SETTLING") + } + } + + + override fun onSlide(bottomSheet: View, slideOffset: Float) { + + } + }) + + + } + + /** + * onClick Listener to capture button click + * + * @param v + */ + override fun onClick(v: View) { + when (v.id) { + R.id.collapse_bottom_sheet_button -> + // Collapsing the bottom sheet + bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_COLLAPSED) + R.id.expand_bottom_sheet_button -> + // Expanding the bottom sheet + bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_EXPANDED) + R.id.hide_bottom_sheet_button -> + // Hiding the bottom sheet + bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_HIDDEN) + R.id.show_bottom_sheet_dialog_button -> + // Opening the Dialog Bottom Sheet + CustomBottomSheetDialogFragment().show(supportFragmentManager, "Dialog") + } + } +} diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 323ef5f..0165c41 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,5 +1,6 @@ - - - - + @@ -28,4 +29,4 @@ - + diff --git a/app/src/main/res/layout/content_dialog_bottom_sheet.xml b/app/src/main/res/layout/content_dialog_bottom_sheet.xml index 9d95666..70458c9 100644 --- a/app/src/main/res/layout/content_dialog_bottom_sheet.xml +++ b/app/src/main/res/layout/content_dialog_bottom_sheet.xml @@ -1,9 +1,10 @@ - Date: Thu, 5 Sep 2019 17:41:04 +0200 Subject: [PATCH 2/5] adjustsize dialogfragment, STATE_EXPANDED bottomsheet --- app/build.gradle | 7 +- .../CustomBottomSheetDialogFragment.kt | 44 +++++++- .../bottomsheets/MainActivity.kt | 105 +----------------- app/src/main/res/layout/activity_main.xml | 13 +-- .../main/res/layout/content_bottom_sheet.xml | 28 ----- .../layout/content_dialog_bottom_sheet.xml | 44 +++++--- app/src/main/res/layout/content_main.xml | 26 +---- app/src/main/res/values/styles.xml | 1 - 8 files changed, 92 insertions(+), 176 deletions(-) delete mode 100644 app/src/main/res/layout/content_bottom_sheet.xml diff --git a/app/build.gradle b/app/build.gradle index c315889..5591ffe 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -28,9 +28,6 @@ dependencies { testImplementation'junit:junit:4.12' implementation 'androidx.appcompat:appcompat:1.0.2' implementation 'com.google.android.material:material:1.0.0' - compile "androidx.core:core-ktx:+" + implementation "androidx.core:core-ktx:+" implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version" //implementation -} -repositories { - mavenCentral() -} +} \ No newline at end of file diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt b/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt index 339da64..2c36648 100644 --- a/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt +++ b/app/src/main/java/com/androidtutorialshub/bottomsheets/CustomBottomSheetDialogFragment.kt @@ -4,12 +4,52 @@ import android.os.Bundle import android.view.LayoutInflater import android.view.View import android.view.ViewGroup +import android.view.WindowManager +import android.widget.RelativeLayout +import android.widget.Toast +import androidx.fragment.app.DialogFragment +import com.google.android.material.bottomsheet.BottomSheetBehavior import com.google.android.material.bottomsheet.BottomSheetDialogFragment +import kotlinx.android.synthetic.main.content_dialog_bottom_sheet.* +import android.widget.FrameLayout +import com.google.android.material.bottomsheet.BottomSheetDialog +import android.content.DialogInterface + + + +class CustomBottomSheetDialogFragment : BottomSheetDialogFragment(){ -class CustomBottomSheetDialogFragment : BottomSheetDialogFragment() { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? { - return inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false) + //Here is our bottomsheet dialog customization + initDialogListeners() + + val view = inflater.inflate(R.layout.content_dialog_bottom_sheet, container, false) + return view } + private fun initDialogListeners() { + dialog?.window?.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE) + dialog.setOnShowListener { dialog -> + //BottomSheetDialog wrapInBottomSheet() method + val bottomSheet = (dialog as BottomSheetDialog).findViewById(com.google.android.material.R.id.design_bottom_sheet) as FrameLayout? + BottomSheetBehavior.from(bottomSheet).state = BottomSheetBehavior.STATE_EXPANDED + } + } + + override fun onStart() { + super.onStart() + initviews() + + } + private fun initviews() { + bottomSheetSubmitButton.setOnClickListener { + val text = bottomSheetInput.text.toString() + Toast.makeText(context, "submit :: " + text, Toast.LENGTH_SHORT).show() + } + + + } + + } diff --git a/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt b/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt index 5541ed0..636526b 100644 --- a/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt +++ b/app/src/main/java/com/androidtutorialshub/bottomsheets/MainActivity.kt @@ -1,114 +1,19 @@ package com.androidtutorialshub.bottomsheets import android.os.Bundle -import android.util.Log -import android.view.View -import android.widget.Button -import android.widget.TextView - import androidx.appcompat.app.AppCompatActivity -import androidx.appcompat.widget.Toolbar - -import com.google.android.material.bottomsheet.BottomSheetBehavior - -class MainActivity : AppCompatActivity(), View.OnClickListener { - // BottomSheetBehavior variable - private var bottomSheetBehavior: BottomSheetBehavior<*>? = null - // TextView variable - private var bottomSheetHeading: TextView? = null - // Button variables - private var expandBottomSheetButton: Button? = null - private var collapseBottomSheetButton: Button? = null - private var hideBottomSheetButton: Button? = null - private var showBottomSheetDialogButton: Button? = null +import kotlinx.android.synthetic.main.content_main.* +class MainActivity : AppCompatActivity() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - - initViews() initListeners() - - - } - - /** - * method to initialize the views - */ - private fun initViews() { - val toolbar = findViewById(R.id.toolbar) as Toolbar - setSupportActionBar(toolbar) - - bottomSheetBehavior = BottomSheetBehavior.from(findViewById(R.id.bottomSheetLayout)) - bottomSheetHeading = findViewById(R.id.bottomSheetHeading) as TextView - - expandBottomSheetButton = findViewById(R.id.expand_bottom_sheet_button) as Button - collapseBottomSheetButton = findViewById(R.id.collapse_bottom_sheet_button) as Button - hideBottomSheetButton = findViewById(R.id.hide_bottom_sheet_button) as Button - showBottomSheetDialogButton = findViewById(R.id.show_bottom_sheet_dialog_button) as Button - - } - - /** - * method to initialize the listeners - */ private fun initListeners() { - // register the listener for button click - expandBottomSheetButton!!.setOnClickListener(this) - collapseBottomSheetButton!!.setOnClickListener(this) - hideBottomSheetButton!!.setOnClickListener(this) - showBottomSheetDialogButton!!.setOnClickListener(this) - - // Capturing the callbacks for bottom sheet - bottomSheetBehavior!!.setBottomSheetCallback(object : BottomSheetBehavior.BottomSheetCallback() { - override fun onStateChanged(bottomSheet: View, newState: Int) { - - if (newState == BottomSheetBehavior.STATE_EXPANDED) { - bottomSheetHeading!!.text = getString(R.string.text_collapse_me) - } else { - bottomSheetHeading!!.text = getString(R.string.text_expand_me) - } - - // Check Logs to see how bottom sheets behaves - when (newState) { - BottomSheetBehavior.STATE_COLLAPSED -> Log.e("Bottom Sheet Behaviour", "STATE_COLLAPSED") - BottomSheetBehavior.STATE_DRAGGING -> Log.e("Bottom Sheet Behaviour", "STATE_DRAGGING") - BottomSheetBehavior.STATE_EXPANDED -> Log.e("Bottom Sheet Behaviour", "STATE_EXPANDED") - BottomSheetBehavior.STATE_HIDDEN -> Log.e("Bottom Sheet Behaviour", "STATE_HIDDEN") - BottomSheetBehavior.STATE_SETTLING -> Log.e("Bottom Sheet Behaviour", "STATE_SETTLING") - } - } - - - override fun onSlide(bottomSheet: View, slideOffset: Float) { - - } - }) - - - } - - /** - * onClick Listener to capture button click - * - * @param v - */ - override fun onClick(v: View) { - when (v.id) { - R.id.collapse_bottom_sheet_button -> - // Collapsing the bottom sheet - bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_COLLAPSED) - R.id.expand_bottom_sheet_button -> - // Expanding the bottom sheet - bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_EXPANDED) - R.id.hide_bottom_sheet_button -> - // Hiding the bottom sheet - bottomSheetBehavior!!.setState(BottomSheetBehavior.STATE_HIDDEN) - R.id.show_bottom_sheet_dialog_button -> - // Opening the Dialog Bottom Sheet - CustomBottomSheetDialogFragment().show(supportFragmentManager, "Dialog") + show_bottom_sheet_dialog_button.setOnClickListener { it -> + CustomBottomSheetDialogFragment().show(supportFragmentManager, "Dialog") } } -} +} \ No newline at end of file diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_main.xml index 0165c41..a0ac5e1 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_main.xml @@ -1,11 +1,9 @@ - + - - - + diff --git a/app/src/main/res/layout/content_bottom_sheet.xml b/app/src/main/res/layout/content_bottom_sheet.xml deleted file mode 100644 index 5c28a5d..0000000 --- a/app/src/main/res/layout/content_bottom_sheet.xml +++ /dev/null @@ -1,28 +0,0 @@ - - - - - - - \ No newline at end of file diff --git a/app/src/main/res/layout/content_dialog_bottom_sheet.xml b/app/src/main/res/layout/content_dialog_bottom_sheet.xml index 70458c9..88e3a47 100644 --- a/app/src/main/res/layout/content_dialog_bottom_sheet.xml +++ b/app/src/main/res/layout/content_dialog_bottom_sheet.xml @@ -2,28 +2,46 @@ - + android:layout_margin="@dimen/activity_horizontal_margin" + /> - + +