2

I have researched a lot and find many sources to this problem but no solutions working for me I don't know what's going wrong.

Some resources i have look into

Custom view style, android's attributes are ignored

https://androidpedia.net/en/tutorial/1446/creating-custom-views

https://infinum.com/the-capsized-eight/how-to-support-themes-in-custom-views-for-android-apps

This is repo for this problem

https://github.com/burhankhanzada199888/CustomView-Style-StackOverflow-Question/tree/master/app

I have 2 custom view in my activity first without style tag and second with style tag but cardView style only apply when using in xml

main_activity

custom_view.xml

<merge 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"
    tools:parentTag="com.google.android.material.card.MaterialCardView"
    tools:style="@style/CustomCardView">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_marginBottom="4dp"
        android:background="?colorSurface"
        android:gravity="center"
        android:orientation="horizontal">

        <ImageView
            android:id="@+id/imageView"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:padding="8dp"
            android:src="@mipmap/ic_launcher"
            app:srcCompat="@mipmap/ic_launcher" />

        <TextView
            android:id="@+id/textView"
            android:layout_width="0dp"
            android:layout_height="match_parent"
            android:layout_weight="1"
            android:background="?colorAccent"
            android:gravity="center"
            android:text="Lorem ipsum"
            android:textColor="@android:color/white"
            android:textSize="25sp" />

    </LinearLayout>

</merge>

activity_main.xml

 <com.myapp.CustomCardView
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1" />

 <com.myapp.CustomCardView
     style="@style/CustomCardView"
     android:layout_width="match_parent"
     android:layout_height="0dp"
     android:layout_weight="1" />

CustomCardView.java

public class CustomCardView extends MaterialCardView {

public CustomCardView(Context context) {
    this(context, null);
}

public CustomCardView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

public CustomCardView(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);

    TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.CustomCardView, defStyleAttr, R.style.CustomCardView);

    float imageSize = a.getDimension(R.styleable.CustomCardView_imageSize, 0);
    float textSize = a.getDimension(R.styleable.CustomCardView_textSize, 0);

    a.recycle();

    inflate(context, R.layout.custom_view, this);

    ImageView imageView = findViewById(R.id.imageView);
    imageView.getLayoutParams().height = (int) imageSize;
    imageView.getLayoutParams().width = (int) imageSize;

    TextView textView = findViewById(R.id.textView);
    textView.setTextSize(textSize);

}

}

styles.xml

<style name="CustomCardView" parent="Widget.MaterialComponents.CardView">
    <item name="cardElevation">8dp</item>
    <item name="cardCornerRadius">16dp</item>
    <item name="android:layout_margin">8dp</item>
    <item name="cardBackgroundColor">?colorPrimary</item>
    <item name="imageSize">150dp</item>
    <item name="textSize">50sp</item>
</style>

attrs.xml

<declare-styleable name="CustomCardView">
    <attr name="imageSize" format="dimension|reference" />
    <attr name="textSize" format="dimension|reference" />
</declare-styleable>
Edric
  • 24,639
  • 13
  • 81
  • 91
Burhan Khanzada
  • 945
  • 9
  • 27

2 Answers2

0

You can add in the attrs.xml:

<attr format="reference" name="customCardViewStyle"/>

Change the constructor:

public CustomCardView(Context context, AttributeSet attrs) {
    this(context, attrs, 0);
}

to

public CustomCardView(Context context, AttributeSet attrs) {
    this(context, attrs, R.attr.customCardViewStyle);
}

Then in your app theme add:

<item name="customCardViewStyle">@style/CustomCardView</item>

Just pay attention to <item name="android:layout_margin">...</item>. The LayoutParams are specific to the parent view type and you can't apply it with a theme.You can set the layout attributes in a style on in the layout.

Gabriele Mariotti
  • 320,139
  • 94
  • 887
  • 841
-3

It happens because you've removed super calls in public CustomCardView(Context context) and public CustomCardView(Context context, AttributeSet attrs) constructors. Indeed your constructors must be like these:

public CustomCardView(Context context) {
    super(context)
    this(context, null);
}

and

public CustomCardView(Context context, AttributeSet attrs) {
    super(context, attrs)
    this(context, attrs, 0);
}
hosseinAmini
  • 2,184
  • 2
  • 20
  • 46
  • 1
    IIRC, there's no actual need for calling _both_ the superclass' constructor and the custom class' constructor. – Edric Mar 29 '20 at 15:41