MTU-Dining/app/src/main/java/com/thebrokenrail/mtudining/widget/CategoryView.java
2024-02-16 23:02:42 -05:00

78 lines
3.2 KiB
Java

package com.thebrokenrail.mtudining.widget;
import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.util.TypedValue;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatTextView;
import com.google.android.material.card.MaterialCardView;
import com.thebrokenrail.mtudining.R;
/**
* Widget that shows a category of items.
*/
public class CategoryView extends FrameLayout {
private final MaterialCardView card;
public final LinearLayout children;
private final AppCompatTextView title;
public CategoryView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
LinearLayout inner = new LinearLayout(context);
inner.setOrientation(LinearLayout.VERTICAL);
addView(inner);
// Set Margin
LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
int margin = getResources().getDimensionPixelSize(R.dimen.margin);
layoutParams.setMargins(margin, margin, margin, margin);
inner.setLayoutParams(layoutParams);
// Add Title
title = new AppCompatTextView(context);
TypedValue value = new TypedValue();
context.getTheme().resolveAttribute(androidx.appcompat.R.attr.colorPrimary, value, true);
title.setTextColor(value.data);
title.setTextSize(TypedValue.COMPLEX_UNIT_PX, getResources().getDimension(R.dimen.category_font_size));
title.setTypeface(null, Typeface.BOLD);
title.setClickable(true);
title.setFocusable(true);
inner.addView(title);
LinearLayout.LayoutParams innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
title.setLayoutParams(innerLayoutParams);
// Setup Card
card = new MaterialCardView(context, null, com.google.android.material.R.attr.materialCardViewElevatedStyle);
inner.addView(card);
innerLayoutParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
innerLayoutParams.setMargins(0, margin, 0, 0);
card.setLayoutParams(innerLayoutParams);
// Setup Children
children = new LinearLayout(context);
children.setOrientation(LinearLayout.VERTICAL);
card.addView(children);
MaterialCardView.LayoutParams cardLayoutParams = new MaterialCardView.LayoutParams(MaterialCardView.LayoutParams.MATCH_PARENT, MaterialCardView.LayoutParams.WRAP_CONTENT);
children.setLayoutParams(cardLayoutParams);
}
/**
* Setup widget.
* @param isOpen If category is open
* @param titleText The category's title
* @param onClickTitle Callback when clicking on title
*/
public void setup(boolean isOpen, String titleText, Runnable onClickTitle) {
titleText = (isOpen ? "" : "") + " " + titleText;
title.setText(titleText);
card.setVisibility(isOpen ? VISIBLE : GONE);
title.setOnClickListener(v -> onClickTitle.run());
}
}