Support
Quality
Security
License
Reuse
kandi has reviewed material-components-android and discovered the below as its top functions. This is intended to give you an instant insight into material-components-android implemented functionality, and help decide if they suit your requirements.
Modular and customizable Material Design UI components for Android
How does this android code get the activity from ? material-components-android-codelabs
// getters
val newThing = stuff.getThing()
val newThing = stuff.thing
// setters
stuff.setThing(newThing)
stuff.thing = newThing
// boolean setters
dog.setGood(true)
dog.isGood = true
// boolean getters
val goodBoy = dog.isGood()
val goodBoy = dog.isGood
BottomNavigationView - why is there not enough space for the badge Drawable?
val menuItemId: Int = bottomNavigation.getMenu().getItem(1).getItemId()
val badge: BadgeDrawable = bottomNavigation.getOrCreateBadge(menuItemId)
badge.setVisible(true)
badge.setNumber(4)
badge.setVerticalOffset(10) //value in pix
badge.setHorizontalOffset(-10) //value in pix
Return MaterialTimePicker
MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.build();
pick.setOnClickListener(v -> materialTimePicker.show(getSupportFragmentManager(),
DiConstant.TIME_PICKER));
materialTimePicker.addOnPositiveButtonClickListener(dialog -> {
int newHour = materialTimePicker.getHour();
int newMinute = materialTimePicker.getMinute();
String time = String.format(Locale.getDefault(), "%02d:%02d", newHour, newMinute);
pick.setText(time);
});
return materialTimePicker.show(getSupportFragmentManager(),
"TIME_PICKER");
-----------------------
MaterialTimePicker materialTimePicker = new MaterialTimePicker.Builder()
.setTimeFormat(TimeFormat.CLOCK_24H)
.build();
pick.setOnClickListener(v -> materialTimePicker.show(getSupportFragmentManager(),
DiConstant.TIME_PICKER));
materialTimePicker.addOnPositiveButtonClickListener(dialog -> {
int newHour = materialTimePicker.getHour();
int newMinute = materialTimePicker.getMinute();
String time = String.format(Locale.getDefault(), "%02d:%02d", newHour, newMinute);
pick.setText(time);
});
return materialTimePicker.show(getSupportFragmentManager(),
"TIME_PICKER");
LiveData update of BadgeDrawable in ToolBar MenuItem
// This is an indicator of whether we need to show the badge or not
private var isFilterOn: Boolean = false
private var filterBadge: BadgeDrawable? = null
@SuppressLint("UnsafeExperimentalUsageError")
override fun onPrepareOptionsMenu(menu: Menu) {
super.onPrepareOptionsMenu(menu)
val filterItem = menu.findItem(R.id.action_filter)
val toolbar = requireActivity().findViewById<Toolbar>(R.id.toolbar)
if(filterBadge != null) {
BadgeUtils.detachBadgeDrawable(filterBadge!!, toolbar, R.id.action_filter)
filterBadge = null
}
if(isFilterOn) {
filterBadge = BadgeDrawable.create(requireContext()).also {
BadgeUtils.attachBadgeDrawable(it, toolbar, R.id.action_filter)
}
}
CountDown Timer with CircularProgressIndicator
progressIndicator.progress = (millisUntilFinished / 1000).toInt()
Floating Action Button Android Studio does not show me background color and image in preview
Android Studio > Menu bar > Help>check for updates.
How to change the Material Design TextInputLayout Hint Text Color?
<style name="CustomOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
<item name="android:textColorHint">@color/text_color_hint</item>
<item name="hintTextColor">@color/green</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/green" android:state_focused="true"/>
<item android:alpha="..." android:color="@color/green" android:state_hovered="true"/>
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/> <!-- unfocused -->
</selector>
-----------------------
<style name="CustomOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
<item name="android:textColorHint">@color/text_color_hint</item>
<item name="hintTextColor">@color/green</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/green" android:state_focused="true"/>
<item android:alpha="..." android:color="@color/green" android:state_hovered="true"/>
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/> <!-- unfocused -->
</selector>
-----------------------
<style name="CustomOutlineBox" parent="Widget.MaterialComponents.TextInputLayout.OutlinedBox">
<item name="boxStrokeColor">@color/text_input_layout_stroke_color</item>
<item name="android:textColorHint">@color/text_color_hint</item>
<item name="hintTextColor">@color/green</item>
</style>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/>
</selector>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:alpha="..." android:color="@color/green" android:state_focused="true"/>
<item android:alpha="..." android:color="@color/green" android:state_hovered="true"/>
<item android:alpha="..." android:color="@color/red" android:state_enabled="false"/>
<item android:alpha="..." android:color="@color/blue"/> <!-- unfocused -->
</selector>
Refresh Fragment view when RecyclerView.Adapter is changed
class StaggeredProductCardRecyclerViewAdapter(private val theList: List) {
private var listOfItems = theList
fun removeItem(position: Int) {
listOfItems = listOfItems.remove(position)
notifyDatasetChanged()
}
}
val featured = view.findViewById(R.id.featured) as Button
// set on-click listener
featured.setOnClickListener {
adapter.removeItem(1)
}
class StaggeredProductCardRecyclerViewAdapter(private val initList: List<ProductEntry>?) : RecyclerView.Adapter<StaggeredProductCardViewHolder>() {
private var productList: List<ProductEntry>? = initList
override fun getItemViewType(position: Int): Int {
return position % 3
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
var layoutId = R.layout.shr_staggered_product_card_first
if (viewType == 1) {
layoutId = R.layout.shr_staggered_product_card_second
} else if (viewType == 2) {
layoutId = R.layout.shr_staggered_product_card_third
}
val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return StaggeredProductCardViewHolder(layoutView)
}
override fun onBindViewHolder(holder: StaggeredProductCardViewHolder, position: Int) {
if (productList != null && position < productList.size) {
val product = productList[position]
holder.productTitle.text = product.title
holder.productPrice.text = product.price
ImageRequester.setImageFromUrl(holder.productImage, product.url)
}
}
override fun getItemCount(): Int {
return productList?.size ?: 0
}
fun replaceList(items: List<ProductEntry>?) {
productList = items
notifyDatasetChanged()
}
}
...
featured.setOnClickListener {
adapter.replaceList(ProductEntry.initProductEntryList(resources, "featured"))
}
-----------------------
class StaggeredProductCardRecyclerViewAdapter(private val theList: List) {
private var listOfItems = theList
fun removeItem(position: Int) {
listOfItems = listOfItems.remove(position)
notifyDatasetChanged()
}
}
val featured = view.findViewById(R.id.featured) as Button
// set on-click listener
featured.setOnClickListener {
adapter.removeItem(1)
}
class StaggeredProductCardRecyclerViewAdapter(private val initList: List<ProductEntry>?) : RecyclerView.Adapter<StaggeredProductCardViewHolder>() {
private var productList: List<ProductEntry>? = initList
override fun getItemViewType(position: Int): Int {
return position % 3
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
var layoutId = R.layout.shr_staggered_product_card_first
if (viewType == 1) {
layoutId = R.layout.shr_staggered_product_card_second
} else if (viewType == 2) {
layoutId = R.layout.shr_staggered_product_card_third
}
val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return StaggeredProductCardViewHolder(layoutView)
}
override fun onBindViewHolder(holder: StaggeredProductCardViewHolder, position: Int) {
if (productList != null && position < productList.size) {
val product = productList[position]
holder.productTitle.text = product.title
holder.productPrice.text = product.price
ImageRequester.setImageFromUrl(holder.productImage, product.url)
}
}
override fun getItemCount(): Int {
return productList?.size ?: 0
}
fun replaceList(items: List<ProductEntry>?) {
productList = items
notifyDatasetChanged()
}
}
...
featured.setOnClickListener {
adapter.replaceList(ProductEntry.initProductEntryList(resources, "featured"))
}
-----------------------
class StaggeredProductCardRecyclerViewAdapter(private val theList: List) {
private var listOfItems = theList
fun removeItem(position: Int) {
listOfItems = listOfItems.remove(position)
notifyDatasetChanged()
}
}
val featured = view.findViewById(R.id.featured) as Button
// set on-click listener
featured.setOnClickListener {
adapter.removeItem(1)
}
class StaggeredProductCardRecyclerViewAdapter(private val initList: List<ProductEntry>?) : RecyclerView.Adapter<StaggeredProductCardViewHolder>() {
private var productList: List<ProductEntry>? = initList
override fun getItemViewType(position: Int): Int {
return position % 3
}
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): StaggeredProductCardViewHolder {
var layoutId = R.layout.shr_staggered_product_card_first
if (viewType == 1) {
layoutId = R.layout.shr_staggered_product_card_second
} else if (viewType == 2) {
layoutId = R.layout.shr_staggered_product_card_third
}
val layoutView = LayoutInflater.from(parent.context).inflate(layoutId, parent, false)
return StaggeredProductCardViewHolder(layoutView)
}
override fun onBindViewHolder(holder: StaggeredProductCardViewHolder, position: Int) {
if (productList != null && position < productList.size) {
val product = productList[position]
holder.productTitle.text = product.title
holder.productPrice.text = product.price
ImageRequester.setImageFromUrl(holder.productImage, product.url)
}
}
override fun getItemCount(): Int {
return productList?.size ?: 0
}
fun replaceList(items: List<ProductEntry>?) {
productList = items
notifyDatasetChanged()
}
}
...
featured.setOnClickListener {
adapter.replaceList(ProductEntry.initProductEntryList(resources, "featured"))
}
-----------------------
supportFragmentManager.beginTransaction().detach(yourFragment).commitNow()
supportFragmentManager.beginTransaction().attach(yourFragment).commitNow()
Extending MaterialButton: Should it be done? SavedState has package visibility
public class MyButton extends MaterialButton
{
private String text;
//....
static class SavedState extends AbsSavedState {
@Nullable CharSequence myText;
SavedState(Parcelable superState) {
super(superState);
}
SavedState(@NonNull Parcel source, ClassLoader loader) {
super(source, loader);
myText = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(source);
}
@Override
public void writeToParcel(@NonNull Parcel dest, int flags) {
super.writeToParcel(dest, flags);
TextUtils.writeToParcel(myText, dest, flags);
}
@NonNull
@Override
public String toString() {
return "MyButton.SavedState{"
+ " text="
+ myText
+ "}";
}
public static final Creator<SavedState> CREATOR =
new ClassLoaderCreator<SavedState>() {
@NonNull
@Override
public SavedState createFromParcel(@NonNull Parcel in, ClassLoader loader) {
return new SavedState(in, loader);
}
@Nullable
@Override
public SavedState createFromParcel(@NonNull Parcel in) {
return new SavedState(in, null);
}
@NonNull
@Override
public SavedState[] newArray(int size) {
return new SavedState[size];
}
};
}
@Nullable
@Override
public Parcelable onSaveInstanceState() {
Parcelable superState = super.onSaveInstanceState();
SavedState ss = new SavedState(superState);
ss.myText = text;
return ss;
}
@Override
public void onRestoreInstanceState(@Nullable Parcelable state) {
if (!(state instanceof SavedState)) {
super.onRestoreInstanceState(state);
return;
}
SavedState ss = (SavedState) state;
super.onRestoreInstanceState(ss.getSuperState());
text = ss.myText.toString();
}
}
Material DateRangePicker enable Accept button with single date
@SuppressLint("RestrictedApi")
class CustomMaterialDatePicker : RangeDateSelector() {
private var first = true
private var firstDate: Long? = null
override fun select(selection: Long) {
if (first || selection < firstDate!!) {
super.select(selection)
super.select(selection)
firstDate = selection
first = false
} else {
super.select(firstDate!!)
super.select(selection)
if (selection != firstDate) {
first = true
}
}
}
}
@SuppressLint("RestrictedApi")
private fun showDatePicker() {
val selector = CustomMaterialDatePicker()
val builder = MaterialDatePicker.Builder.customDatePicker(selector)
builder.setTheme(R.style.CustomCalendarDatePickerTheme)
builder.setCalendarConstraints(limitRange().build())
val picker = builder.build()
picker.addOnPositiveButtonClickListener {
tv_date.text = formatRange(it.first!!, it.second!!)
startDate = it.first
endDate = it.second
}
picker.show(supportFragmentManager, picker.toString())
}
-----------------------
@SuppressLint("RestrictedApi")
class CustomMaterialDatePicker : RangeDateSelector() {
private var first = true
private var firstDate: Long? = null
override fun select(selection: Long) {
if (first || selection < firstDate!!) {
super.select(selection)
super.select(selection)
firstDate = selection
first = false
} else {
super.select(firstDate!!)
super.select(selection)
if (selection != firstDate) {
first = true
}
}
}
}
@SuppressLint("RestrictedApi")
private fun showDatePicker() {
val selector = CustomMaterialDatePicker()
val builder = MaterialDatePicker.Builder.customDatePicker(selector)
builder.setTheme(R.style.CustomCalendarDatePickerTheme)
builder.setCalendarConstraints(limitRange().build())
val picker = builder.build()
picker.addOnPositiveButtonClickListener {
tv_date.text = formatRange(it.first!!, it.second!!)
startDate = it.first
endDate = it.second
}
picker.show(supportFragmentManager, picker.toString())
}
QUESTION
Android material CardView ripple effect changes content color
Asked 2021-Dec-18 at 08:01I recently moved from using custom card components to material design 3 cards. To my surprise, the ripple color, when pressing on the card, changes the content color. I tried this on the catalog project of the material-design-components repository and the card behaves the same way. My current code for changing the ripple color and the card background:
<style name="Widget.App.Card.Filled" parent="Widget.Material3.CardView.Filled">
<item name="rippleColor">#4D4D4D</item>
<item name="cardBackgroundColor">@android:color/transparent</item>
</style>
I also got a demo showing this behavior: https://i.imgur.com/t4WW4CY.mp4. The font color changes to a light gray based on the ripple effect. I am also using material buttons but there the ripple effect is not affecting the content. I am using the version 1.5.0-rc01
.
Does anyone have an idea if that is intended and how I can only change the background color on press using the attributes given by the material design components.
Update:
My card view XML (its the one from the material-components github + custom style):
<com.google.android.material.card.MaterialCardView
style="@style/Widget.App.Card.Filled"
android:id="@+id/card"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.4"
android:layout_margin="16dp"
android:clickable="true"
android:focusable="true"
app:contentPadding="@dimen/cat_card_content_padding">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="@string/cat_card_states_card_content"
android:textAppearance="?attr/textAppearanceBody1"/>
</com.google.android.material.card.MaterialCardView>
ANSWER
Answered 2021-Dec-18 at 08:01I think this is happening because of the ripple color applying to the foreground instead of background. So yeah this is intended and i don't think you can change that.
Have a look at the source code : MaterialCardViewHelper.java
Note : This does not happen in buttons bcoz buttons applies ripple to the background instead of foreground.
Source code for button : MaterialButtonHelper.java
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
No vulnerabilities reported
Save this library and start creating your kit
Explore Related Topics
Save this library and start creating your kit