新品価格 |
ProgressDialogがAndroid8.0でdeprecatedになりました
ただマスタカはProgressDialogを使うので
DialogFramgentを拡張して作ってみた
・ProgressFragment
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 |
class AAAProgressDialog : DialogFragment() { private lateinit var mBinding: LayoutProgressDialogBinding override fun onCreateDialog(savedInstanceState: Bundle?): Dialog { super.onCreateDialog(savedInstanceState) val builder = AlertDialog.Builder(context!!) mBinding = DataBindingUtil.inflate(LayoutInflater.from(context), R.layout.layout_progress_dialog, null, false) arguments?.getInt(RES_STRING_ID)?.let { mBinding.progressMessage.text = getText(it) } builder.setView(mBinding.root) return builder.create() } fun setMessage(@StringRes id: Int) { arguments?.putInt(RES_STRING_ID, id) } override fun onPause() { super.onPause() dismiss() } companion object { const val RES_STRING_ID = "RES_STRING_ID" fun newInstance(@StringRes id: Int): AAAProgressDialog { val f = AAAProgressDialog() val args = Bundle() args.putInt(RES_STRING_ID, id) f.arguments = args return f } } } |
・layout
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 |
<?xml version="1.0" encoding="utf-8"?> <layout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/res-auto"> <android.support.constraint.ConstraintLayout android:layout_width="wrap_content" android:layout_height="wrap_content"> <ProgressBar android:id="@+id/progress" android:layout_width="40dp" android:layout_height="40dp" android:layout_gravity="center" android:layout_marginBottom="16dp" android:layout_marginStart="16dp" android:layout_marginTop="16dp" app:layout_constraintBottom_toBottomOf="parent" app:layout_constraintLeft_toLeftOf="parent" app:layout_constraintTop_toTopOf="parent" /> <TextView android:id="@+id/progress_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginStart="16dp" android:textColor="@android:color/black" app:layout_constraintBottom_toBottomOf="@+id/progress" app:layout_constraintLeft_toRightOf="@+id/progress" app:layout_constraintTop_toTopOf="@+id/progress" app:layout_constraintVertical_bias="0.507" /> </android.support.constraint.ConstraintLayout> </layout> |
・呼び出し方
1 2 3 |
val TAG = "AAA" val mProgressDialog = AAAProgressDialog.newInstance(R.string.hogehoge) mProgressDialog.show(getFragment(), TAG) |
・参考
https://qiita.com/Uchikoba/items/478d604f417465700ba1
http://furudate.hatenablog.com/entry/2014/01/09/162421
https://stackoverflow.com/questions/34967868/how-to-use-data-binding-in-dialog
関連記事:
- AndroidでLiveDataのTransformationsを使ってみた
- androidでDataBinding + ViewModel + LiveDataで双方向Bindingしてみた
- Android Studio 4.0のMotion Editorでアニメーションを作ってみた