package net.somethingdreadful.MAL;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.drawable.GradientDrawable;
import android.graphics.drawable.LayerDrawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.WindowManager;
import android.widget.RelativeLayout;
import android.widget.TextView;


public class Card extends RelativeLayout {
    public TextView Header;
    public boolean center;
    public RelativeLayout Card;
    public RelativeLayout Content;
    LayoutInflater inflater;
    int CardColor;
    int CardColorPressed;
    onCardClickListener listener;
    int screenWidth;
    Float density;
    Integer minHeight;

    public Card(Context context, AttributeSet attrs) {
        super(context, attrs);

        /*
         * Get attributes
         */
        TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.Card, 0, 0);
        center = a.getBoolean(R.styleable.Card_header_Title_center, false);
        String TitleText = a.getString(R.styleable.Card_header_Title);
        int TitleColor = a.getResourceId(R.styleable.Card_header_Title_Color, android.R.color.black);
        int HeaderColor = a.getResourceId(R.styleable.Card_header_Color, R.color.card_content);
        Integer maxWidth = a.getInteger(R.styleable.Card_card_maxWidth, 0);
        minHeight = a.getInteger(R.styleable.Card_card_minHeight, 0);
        Integer divide = a.getInteger(R.styleable.Card_card_divide, 0);
        CardColor = a.getResourceId(R.styleable.Card_card_Color, R.color.card_content);
        CardColorPressed = a.getResourceId(R.styleable.Card_card_ColorPressed, R.color.card_content_pressed);

        /*
         * Setup layout
         */
        inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.card_layout_base, this);

        /*
         * Get the views
         */
        Card = (RelativeLayout) findViewById(R.id.BaseCard);
        Content = (RelativeLayout) findViewById(R.id.content);
        Header = (TextView) this.findViewById(R.id.CardTitle);

        /*
         * Apply attributes
         */
        if (divide != 0 || maxWidth!= 0)
            setWidth(divide, maxWidth);
        Header.setText(TitleText);
        Header.setTextColor(getResources().getColor(TitleColor));
        setHeaderColor(HeaderColor);
        setCardColor(CardColor);

        a.recycle();
    }

    /*
     * Add content to the card
     *
     * Also checks if the view contains a listview to apply the right paddings
     */
    public void setContent(int res) {
        inflater.inflate(res, Content);
        if (this.findViewById(R.id.ListView) != null)
            Content.setPadding(0, 0, 0, Content.getPaddingBottom() / 12 * 6);
    }

    /*
     * Convert dp to pixels
     */
    private int convert(int number) {
        return (int) (getDensity() * number);
    }

    /*
     * Change the padding of a card
     */
    public void setPadding(int left, int top, int right, int bottom) {
        Content.setPadding(convert(left), convert(top), convert(right), (bottom != -1 ? bottom : convert(4)));
    }

    /*
     * Recalculate the required height of a listview and apply it
     */
    public void refreshList(Integer total, Integer normalHeight, Integer headers, Integer headerHeight, Integer divider) {
        if (total == 0) {
            this.setVisibility(View.GONE);
        } else {
            Integer normal = total - headers;

            int Height = convert(normal * normalHeight);
            Height = Height + convert(headers * headerHeight);
            Height = Height + convert((total - 1) * divider);

            if (this.findViewById(R.id.ListView) != null)
                this.findViewById(R.id.ListView).getLayoutParams().height = Height;
        }
    }

    /*
     * Set the card at the right side of another card
     */
    public void setRightof(Card res, int amount, int screen) {
        if (convert(screen) <= getScreenWidth()) {
            RelativeLayout.LayoutParams card = new LayoutParams(getWidth(amount, 0), convert(minHeight));
            card.addRule(RelativeLayout.RIGHT_OF, res.getId());
            card.setMargins(convert(4), 0, 0, 0);
            this.setLayoutParams(card);
        }
    }

    /*
     * Change the background color
     */
    public void setCardColor(int color) {
        LayerDrawable layers = (LayerDrawable) Card.getBackground();
        GradientDrawable shape = (GradientDrawable) (layers.findDrawableByLayerId(R.id.card_content_drawable));
        shape.setColor(getResources().getColor(color));
    }

    /*
     * Change the header color
     */
    public void setHeaderColor(int color) {
        GradientDrawable shape = (GradientDrawable) Header.getBackground();
        shape.setColor(getResources().getColor(color));
    }

    public void setOnClickListener(int res, onCardClickListener callback) {
        listener = callback;
        (this.findViewById(R.id.actionableIcon)).setVisibility(View.VISIBLE);
        Content.findViewById(res).setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onCardClickListener(v.getId());
            }
        });
    }

    /*
     * Wraps the width of a card
     */
    public void wrapWidth(boolean header) {
        int width;
        Header.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        Card.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);
        Content.measure(MeasureSpec.UNSPECIFIED, MeasureSpec.UNSPECIFIED);

        if (Content.getMeasuredWidth() >= Header.getMeasuredWidth() || !header)
            width = Content.getMeasuredWidth();
        else
            width = Header.getMeasuredWidth();

        Header.setLayoutParams(new LayoutParams(width, Header.getLayoutParams().height));
        Card.setLayoutParams(new LayoutParams(width, Card.getMeasuredHeight()));
        if (center)
            Header.setGravity(Gravity.CENTER);
    }

    /*
     * Set the card width
     *
     * amount is the amount of cards besides each other
     * maxWidth is the maximum width in dp
     */
    public void setWidth(Integer amount, Integer maxWidth) {
        Card.getLayoutParams().width = getWidth(amount, maxWidth);
    }

    /*
     * get the card width
     *
     * amount is the amount of cards besides each other
     * maxWidth is the maximum width in dp
     */
    public int getWidth(Integer amount, Integer maxWidth) {
        if (amount == 0)
            amount = 1;
        int divider = amount - 1;
        divider = convert((divider * 4) + 16);
        int card = (getScreenWidth() - divider) / amount;
        maxWidth = convert(maxWidth);

        if (card > maxWidth && maxWidth != 0)
            return maxWidth;
        else
            return card;
    }

    @SuppressLint("InlinedApi")
    private Integer getScreenWidth() {
        if (screenWidth == 0) {
            try {
                screenWidth = convert(getResources().getConfiguration().screenWidthDp);
            } catch (NoSuchFieldError e) {
                screenWidth = ((WindowManager) getContext().getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay().getWidth();
            }
        }
        return screenWidth;
    }

    /*
     * Get the display density
     *
     */
    public Float getDensity() {
        if (density == null)
            density = (getResources().getDisplayMetrics().densityDpi / 160f);
        return density;
    }

    /*
     * Interface for clicklistener
     */
    public interface onCardClickListener {
        public void onCardClickListener(int id);
    }
}
@@ -529,21 +529,20 @@
 lic 
-TextView Head
+boolean cent
 er;%0A
@@ -548,36 +548,37 @@
 %0A    public 
-boolean cent
+TextView Head
 er;%0A    publ
@@ -644,32 +644,8 @@
     
-LayoutInflater inflater;
 %0A   
@@ -672,79 +672,38 @@
 int 
-CardColorPressed;%0A    onCardClickListener listener;%0A    int screenWidth
+screenWidth;%0A    int minHeight
 ;%0A  
@@ -723,29 +723,70 @@
 ty;%0A
+%0A
     
-Integer minHeight
+LayoutInflater inflater;%0A    onCardClickListener listener
 ;%0A%0A 

Reorder variables (Card)