Search code examples
android-studiobutterknife

Change the floatingButton Background (ButterKnife) programmatically


I am new to Android Butterknife and want to change the background color of floating Button. I am unable to do this.How can i achieve this.Thanks in advance.Application crashes when i use .

 floatingActionButton.setBackgroundTintList(ColorStateList.valueOf(Color
            .parseColor("#33691E")));

This works fine if i donot integrate Butterknife. This is my floating Button

    <android.support.design.widget.FloatingActionButton
    android:layout_width="wrap_content"
    android:id="@+id/fab1"
    android:layout_height="wrap_content"
    android:layout_gravity="bottom|right"
    app:rippleColor="@android:color/white"
    android:layout_margin="16dp"
    android:src="@drawable/circle"
    android:onClick="newForm"
    app:layout_anchorGravity="bottom|right|end" />

And in MainActivity

public class MainActivity extends AppCompatActivity implements    
SearchView.OnQueryTextListener {

@BindView(R.id.fab1)
FloatingActionButton floatingActionButton;

@BindColor(R.color.colorFolatingButton)
int Floating_Button_Color;
@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    ButterKnife.bind(this);

    setSupportActionBar(toolbar);

  floatingActionButton.setBackgroundColor(Floating_Button_Color);

}
@OnClick(R.id.fab1)
public void  newForm (View view){

    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
    builder.setTitle("Confirm");
    builder.setMessage("Are you sure?");

    builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            Intent intent = new Intent(getApplicationContext(),BuilderPage.class);
            startActivity(intent);
        }
    });


    builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
        @Override
        public void onClick(DialogInterface dialog, int which) {
            dialog.dismiss();
        }
    });

    AlertDialog alert = builder.create();
    alert.show();
}

Solution

  • Use app:backgroundTint="@color/your_color"