Android customly created xml button is not showing up in preview

1

I have created a button with custom attributes (actually a linear layout, which has an ImageView and a TextView, and functions like a button). The problem is that when I try to implement this custom button in the xml layout definiton for an acitivty, it isn't displayed. How can I fix this?

FacebookCustomButton

package com.android.neuron.custombuttons;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

public class FacebookCustomButton extends LinearLayout {
    private Context context;

    private ImageView iconView;
    private TextView textView;

    private String iconPath;
    private Drawable iconResource;
    private String textSource;

    private int iconWidth;
    private int iconHeight;
    private int iconGravity = 0x11; //Gravity.CENTER
    private int iconMarginStart;
    private int iconMarginEnd;
    private int iconMarginTop;
    private int iconMarginBottom;

    private int textGravity = 0x11; //center
    private int textSize;
    private int textColor;



    public FacebookCustomButton(Context context) {
        super(context);
        this.context = context;

        initializeButton();
    }

    public FacebookCustomButton(Context context, AttributeSet attrs) {
        super(context, attrs);
        initAttrs(context, attrs);

        initializeButton();
    }

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

        initializeButton();
    }

    void initAttrs(Context context, AttributeSet attrs) {
        if(attrs!=null) {
            TypedArray attributeArray = context.obtainStyledAttributes(attrs, R.styleable.FacebookCustomButton);

            iconResource = attributeArray.getDrawable(R.styleable.FacebookCustomButton_icon_resource);

            String text = attributeArray.getString(R.styleable.FacebookCustomButton_text_source);
            if(text==null) { //no fb textView attribuute
                text = attributeArray.getString(R.styleable.FacebookCustomButton_android_text);
            }
            textSource = text;

            iconWidth = (int) attributeArray.getDimension(R.styleable.FacebookCustomButton_icon_width, Constants.fbButton_icon_default_width);
            iconHeight = (int) attributeArray.getDimension(R.styleable.FacebookCustomButton_icon_height, Constants.fbButton_icon_default_height);
            iconGravity = attributeArray.getInt(R.styleable.FacebookCustomButton_icon_gravity, Constants.fbButton_icon_default_gravity);

            iconMarginStart = attributeArray.getInt(R.styleable.FacebookCustomButton_icon_marginStart, Constants.fbButton_icon_default_margin_start);
            iconMarginEnd = attributeArray.getInt(R.styleable.FacebookCustomButton_icon_marginEnd, Constants.fbButton_icon_default_margin_end);
            iconMarginTop = attributeArray.getInt(R.styleable.FacebookCustomButton_icon_marginTop, Constants.fbButton_icon_default_margin_top);
            iconMarginBottom = attributeArray.getInt(R.styleable.FacebookCustomButton_icon_marginBottom, Constants.fbButton_icon_default_margin_bottom);

            textGravity = attributeArray.getInt(R.styleable.FacebookCustomButton_text_gravity, Constants.fbButton_icon_default_gravity);
            textSize = (int) attributeArray.getDimension(R.styleable.FacebookCustomButton_text_size, Constants.fbButton_text_size);

            String textFontFamily = attributeArray.getString(R.styleable.FacebookCustomButton_text_font);
            Typeface font = Typeface.createFromAsset(context.getAssets(), textFontFamily);
            textView.setTypeface(font);

            textColor = attributeArray.getColor(R.styleable.FacebookCustomButton_text_color, getResources().getColor(R.color.fb_defaultColor));

        }
    }

    /**
     * Initalizes the button:
     *   - the container (linear layout)
     *   - the iconView view
     *   - the textView view
     */
    private void initializeButton() {
        initializeButtonContainer();
        iconView = initializeIconView();
        textView = initializeTextView();
    }

    private void initializeButtonContainer() {
        this.setOrientation(LinearLayout.HORIZONTAL);

        if(this.getLayoutParams() == null) {
            LayoutParams containerParams = new LayoutParams(LayoutParams.WRAP_CONTENT, Constants.fbButton_default_height);

            containerParams.setMargins(Constants.fbButton_default_margin_start,
                    Constants.fbButton_default_margin_top,
                    Constants.fbButton_default_margin_end,
                    Constants.fbButton_default_margin_bottom);

            this.setLayoutParams(containerParams);
        }

        this.setGravity(Gravity.CENTER);

        this.setBackground(context.getDrawable(R.drawable.background_facebook_sign_up_button));
    }

    private TextView initializeTextView() {
        if(textSource == null) {
            textSource = context.getResources().getString(R.string.fbButton_default_text_source);
        }

        TextView textView = new TextView(context);
        textView.setText(textSource);

        textView.setGravity(textGravity);
        textView.setTextColor(textColor);
        textView.setTextSize(textSize);
        textView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));

        /*
        Typeface font = Typeface.createFromAsset(context.getAssets(), Constants.default_font_path);
        textView.setTypeface(font);
        */

        return textView;
    }

    private ImageView initializeIconView() {
        if(iconResource != null) {
            ImageView iconView = new ImageView(context);
            iconView.setImageDrawable(iconResource);

            LayoutParams iconViewParams = new LayoutParams(iconWidth, iconHeight);
            iconViewParams.gravity = iconGravity;
            iconViewParams.setMarginStart(iconMarginStart);
            iconViewParams.setMarginEnd(iconMarginEnd);

            iconView.setLayoutParams(iconViewParams);

            return iconView;
        } else {
            System.out.println("[Neuron.custombuttons.FCB]: Error getting iconView resource! Reutnring null.");
            return null;
        }
    }
}

attrs.xml

<?xml version="1.0" encoding="utf-8"?>
<resources>
    <declare-styleable name="FacebookCustomButton">
        <!-- Default Android attrs -->
        <attr name="android:enabled" />
        <attr name="android:text" />
        <attr name="android:textSize" />
        <attr name="android:textAllCaps" />
        <attr name="android:textStyle" />
        <attr name="android:fontFamily" />

        <attr name="icon_width" format="dimension"/>
        <attr name="icon_height" format="dimension"/>

        <attr name="icon_gravity" format="enum">
            <enum name="top" value="0x30" />
            <enum name="bottom" value="0x50" />
            <enum name="left" value="0x03" />
            <enum name="right" value="0x05" />
            <enum name="center_vertical" value="0x10" />
            <enum name="fill_vertical" value="0x70" />
            <enum name="center_horizontal" value="0x01" />
            <enum name="fill_horizontal" value="0x07" />
            <enum name="center" value="0x11" />
            <enum name="fill" value="0x77" />
            <enum name="clip_vertical" value="0x80" />
            <enum name="clip_horizontal" value="0x08" />
            <enum name="start" value="0x00800003" />
            <enum name="end" value="0x00800005" />
        </attr>

        <attr name="icon_resource" format="integer"/>

        <attr name="icon_marginStart" format="dimension"/>
        <attr name="icon_marginEnd" format="dimension"/>
        <attr name="icon_marginTop" format="dimension"/>
        <attr name="icon_marginBottom" format="dimension"/>

        <attr name="text_source" format="string"/>

        <attr name="text_gravity" format="enum">
            <enum name="top" value="0x30" />
            <enum name="bottom" value="0x50" />
            <enum name="left" value="0x03" />
            <enum name="right" value="0x05" />
            <enum name="center_vertical" value="0x10" />
            <enum name="fill_vertical" value="0x70" />
            <enum name="center_horizontal" value="0x01" />
            <enum name="fill_horizontal" value="0x07" />
            <enum name="center" value="0x11" />
            <enum name="fill" value="0x77" />
            <enum name="clip_vertical" value="0x80" />
            <enum name="clip_horizontal" value="0x08" />
            <enum name="start" value="0x00800003" />
            <enum name="end" value="0x00800005" />
        </attr>

        <attr name="text_size" format="dimension"/>
        <attr name="text_font" format="string"/>
        <attr name="text_color" format="color"/>
    </declare-styleable>
</resources>

Implementation in RegisterActivity:

<com.android.neuron.custombuttons.FacebookCustomButton
                android:layout_width="match_parent"
                android:layout_height="40dp"
                app:text_source="@string/facebook_signup_button_text"
                app:text_size="14sp"
                app:text_color="@color/white"
                app:text_font="@font/roboto_medium"
                app:text_gravity="center"
                app:icon_resource="@drawable/ic_fb_white"
                app:icon_gravity="center"
                app:icon_height="24dp"
                app:icon_width="24dp"
                app:icon_marginBottom="0dp"
                app:icon_marginEnd="42dp"
                app:icon_marginStart="8dp"
                app:icon_marginTop="0dp" />
android
android-studio
android-layout
asked on Stack Overflow Sep 9, 2019 by AnimaVivens

1 Answer

1

You are only loading the LinearLayout. You are not adding the child views you created to your parent LinearLayout. Try this

private void initializeButton() {
        initializeButtonContainer();
        iconView = initializeIconView();
        textView = initializeTextView();
        this.addView(iconView);
        this.addView(textView); 
    }
answered on Stack Overflow Sep 9, 2019 by sunil sunny

User contributions licensed under CC BY-SA 3.0