For #9506: Implement banner custom view based on material design specs.

pull/81/head
mcarare 4 years ago committed by Mihai Adrian Carare
parent 20794296dc
commit 653a279101

@ -0,0 +1,68 @@
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
package org.mozilla.fenix.browser
import android.annotation.SuppressLint
import android.content.Context
import android.view.LayoutInflater
import android.view.View.GONE
import android.view.ViewGroup
import android.view.ViewGroup.LayoutParams.MATCH_PARENT
import android.view.ViewGroup.LayoutParams.WRAP_CONTENT
import kotlinx.android.synthetic.main.info_banner.view.*
import org.mozilla.fenix.R
/**
* Displays an Info Banner in the specified container with a message and an optional action.
* The container can be a placeholder layout inserted in the original screen, or an existing layout.
*
* @param context - A [Context] for accessing system resources.
* @param container - The layout where the banner will be shown
* @param message - The message displayed in the banner
* @param dismissText - The text on the dismiss button
* @param actionText - The text on the action to perform button
* @param actionToPerform - The action to be performed on action button press
*/
class InfoBanner(
private val context: Context,
private val container: ViewGroup,
private val message: String,
private val dismissText: String,
private val actionText: String? = null,
private val actionToPerform: (() -> Unit)? = null
) {
@SuppressLint("InflateParams")
private val bannerLayout = LayoutInflater.from(context)
.inflate(R.layout.info_banner, null)
internal fun showBanner() {
bannerLayout.banner_info_message.text = message
bannerLayout.dismiss.text = dismissText
if (actionText.isNullOrEmpty()) {
bannerLayout.action.visibility = GONE
} else {
bannerLayout.action.text = actionText
}
container.addView(bannerLayout)
val params = bannerLayout.layoutParams as ViewGroup.LayoutParams
params.height = WRAP_CONTENT
params.width = MATCH_PARENT
bannerLayout.dismiss.setOnClickListener {
dismiss()
}
bannerLayout.action.setOnClickListener {
actionToPerform?.invoke()
}
}
internal fun dismiss() {
container.removeView(bannerLayout)
}
}

@ -0,0 +1,53 @@
<?xml version="1.0" encoding="utf-8"?><!-- This Source Code Form is subject to the terms of the Mozilla Public
- License, v. 2.0. If a copy of the MPL was not distributed with this
- file, You can obtain one at http://mozilla.org/MPL/2.0/. -->
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/banner_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="21dp"
android:background="?foundation"
android:paddingStart="11dp"
android:paddingTop="21dp"
android:paddingEnd="16dp">
<TextView
android:id="@+id/banner_info_message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="10dp"
android:paddingEnd="10dp"
android:textAppearance="@style/Body14TextStyle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:text="Banner info message." />
<com.google.android.material.button.MaterialButton
android:id="@+id/dismiss"
style="@style/CreateShortcutDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="8dp"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@id/action"
app:layout_constraintTop_toBottomOf="@+id/banner_info_message"
tools:text="Dismiss" />
<com.google.android.material.button.MaterialButton
android:id="@+id/action"
style="@style/CreateShortcutDialogButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginEnd="3dp"
android:textAllCaps="true"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="@+id/banner_info_message"
tools:text="Action" />
</androidx.constraintlayout.widget.ConstraintLayout>
Loading…
Cancel
Save