shamanland / floating-action-button

Implementation of a circular button made of paper that lifts and emits ink reactions on press.
http://shamanland.github.io/floating-action-button
Apache License 2.0
371 stars 88 forks source link

.setColor doesnt do nothing #4

Closed rfermontero closed 9 years ago

rfermontero commented 10 years ago

Hi, im trying to set eh color of button programatically, but when i do a btn.setColor(Color.GREEN), the color doesnt change..

rfermontero commented 10 years ago

sorry i did the initBackgroud before the changes.

rfermontero commented 10 years ago

Sorry but if i set the initBackground() the color doesnt change either..

ok3141 commented 10 years ago

I will check this issue soon.

rfermontero commented 10 years ago

Ok, thanks you.

ok3141 commented 10 years ago

@colymore do you want to change color of FAB programmatically?

rfermontero commented 10 years ago

Yes.

ok3141 commented 10 years ago

Well, this code works fine for me:

fab.setColor(Color.RED);
fab.initBackground();

Could you show your code?

rfermontero commented 10 years ago

This is my code (ReplyparentView its only abstract class extending View):

public class TextReplyView extends ReplyParentView {

    @InjectView(R.id.btnAction)
    FloatingActionButton btnAction;
    @InjectView(R.id.etText)
    BootstrapEditText etText;
    @InjectView(R.id.tvText)
    RobotoTextView tvText;

    public TextReplyView(Context context) {
        super(context);
    }

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

    public TextReplyView(Context context, int attrs) {
        super(context);
    }

    @Override
    public void initializeComponents() {
        LayoutInflater layoutInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View view = layoutInflater.inflate(R.layout.reply_text, this, true);
        ButterKnife.inject(this, view);
        initButtons();
    }

    @Override
    public String getResult() {
        return tvText.getText().toString();
    }

    @OnClick(R.id.btnAction)
    public void btnActionClick(FloatingActionButton btnActionClick) {
        changeVisibilities();
    }

    private void initButtons() {
        btnAction.setImageResource(R.drawable.ic_write);
        btnAction.initBackground();
    }

    private void setTextViewVisible() {
        tvText.setVisibility(View.VISIBLE);
    }

    private void setTextViewINVisible() {
        tvText.setVisibility(View.GONE);
    }

    private void setEditTextViewVisible() {
        etText.setVisibility(View.VISIBLE);
    }

    private void setEditTextViewINVisible() {
        etText.setVisibility(View.GONE);
    }

    private void changeText() {
        tvText.setText(etText.getText().toString());
        etText.setText("");
    }

    private void setButtonGreenAndChangeIcon() {
        btnAction.setColor(getResources().getColor(R.color.green_success));
        btnAction.setImageResource(R.drawable.ic_enter);
        btnAction.initBackground();
    }

    private void changeVisibilities() {
        if (etText.getVisibility() == View.GONE) {
            setEditTextViewVisible();
            setTextViewINVisible();
            setButtonGreenAndChangeIcon();
        } else {
            changeText();
            setEditTextViewINVisible();
            setTextViewVisible();
            initButtons();
        }
    }

}

I can see the color green only when i have the button pressed, but when not the color returns to default.

ok3141 commented 10 years ago

According to your code, you need to set default color here:

    private void initButtons() {
        btnAction.setImageResource(R.drawable.ic_write);
+       btnAction.setColor(getResources().getColor(R.color.default_color));
        btnAction.initBackground();
    }

But if you want to handle pressed state for the button, you may use ColorStateList for the color:

res/color/fab_color.xml

    <?xml version="1.0" encoding="utf-8"?>
    <selector xmlns:android="http://schemas.android.com/apk/res/android">
        <item android:state_pressed="true" android:color="@color/green_success"/>
        <item android:color="@color/default_color"/>
    </selector>

res/layout/my_layout.xml

    <com.shamanland.fab.FloatingActionButton
        android:id="@+id/btnAction"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:floatingActionButtonColor="@color/fab_color"
        />

After that you don't need to change color in Java-code

rfermontero commented 10 years ago

I have not explained well, I have the button grayed out and when I click to turn green, right now when I click only changes to green color in pressed state when I do not push the button color turns to gray