2

I want to show material design button like this:

enter image description here

I am using MaterialDesignLibrary. In XML, I can show it in this way:

<com.gc.materialdesign.views.ButtonRectangle
                android:id="@+id/button"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:background="#1E88E5"
                android:text="Button" />

How to show this button programmatically?

Joe Richard
  • 1,520
  • 7
  • 20
  • 31

1 Answers1

0

Update :

First extend the class in your project :

public class MyButtonRectangle extends ButtonRectangle {

    public MyButtonRectangle(Context context) {
        super(context, null); //Pass null for AttributeSet Params
    }
}

Than create new MyButtonRectangle as any other view in Android:

MyButtonRectangle btn = new MyButtonRectangle(this);
btn.setId(R.id.someId); // you need to add dummy id in ids.xml in values folder.
btn.setText("Button");
btn.setBackgroundColor(getResources().getColor(R.color.colorName));

LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT); // Replace LinearLayout with your base layout like RelativeLayout,  FrameLayout,  etc.

btn.setLayoutParams(lp);
Arnab Jain
  • 247
  • 1
  • 5
  • Thank you for your answer! I also tought like this. But it has this constructor: ButtonRectangle(Context context, AttributeSet attrs). Can you help with this: I cannot figure out what I should send for AttributeSet attrs? – Joe Richard Feb 01 '16 at 13:14
  • Extend the class in your android app and add a new constructor to read only context. Check my edited answer in few mins – Arnab Jain Feb 01 '16 at 13:15
  • I have tried use custom class as you suggested. But it calls parent(ButtonRectangle)'s contructor. The contructor needs AttributeSet attrs. – Joe Richard Feb 01 '16 at 13:22
  • As I stated replace ButtonRectangle btn = new ButtonRectangle(this); to MyButtonRectangle btn = new MyButtonRectangle(this); – Arnab Jain Feb 01 '16 at 13:23
  • I understood that I should create custom class instance. But I have error inside MyRectangleButton – Joe Richard Feb 01 '16 at 13:26
  • Ok. Give me 5 mins. I am booting my PC. Will test and update my Answer in few mins. – Arnab Jain Feb 01 '16 at 13:26
  • 1
    Am sorry there seems no easy way to do this. You will have to import the whole library as a module and manually modify the ButtonRectangle Java file to support single parameter constructor. Or follow this : http://stackoverflow.com/questions/29379150/how-to-pass-attributeset-as-a-parameter – Arnab Jain Feb 01 '16 at 13:34
  • Thank you for your time and attention!) – Joe Richard Feb 01 '16 at 13:35
  • Check my edited comment. Please upvote and accept my answer to show support. – Arnab Jain Feb 01 '16 at 13:36
  • Unfortunately, this way is also not working. Anyways, thank you for your help:) I will try to come up with some solution. – Joe Richard Feb 01 '16 at 13:45
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/102251/discussion-between-arnab-jain-and-joe-richard). – Arnab Jain Feb 01 '16 at 14:08
  • Hello, Check my updated answer. It should do the job. – Arnab Jain Feb 02 '16 at 02:56