Thinh Vu
Thinh Vu

Reputation: 61

Custom layout of button in android

How to custom layout of a button? I want to create a button with layout like this:

<LinearLayout
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:orientation="vertical">
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="aaa" />
    <TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="bbb" />
</LinearLayout>

Upvotes: 0

Views: 410

Answers (3)

Cornel
Cornel

Reputation: 314

You can create a custom "button" from a View element by adding in xml to your view android:onClick="myFunction" and in code declare your function void myFunction(View v){ .. }.

Or declare click listeners on your View in code

LinearLayout myLinearLayout = (LinearLayout) findViewById(R.id.myLinearButton); 
myLinearLayout.setOnClickListener(new View.OnClickListener(){
@Override
public void onClick(View v) {
    myFunction(v);
}
});

void myFunction(View v){
//do something awesome here
}

This is the simplest way I think. Or you can extend a Button class, add new XML styles for different button states (activated, highlighted, normal) http://developer.android.com/guide/topics/ui/controls/button.html

Upvotes: 0

JstnPwll
JstnPwll

Reputation: 8695

The best and proper way is to create a subclass of Button, and inflate your custom XML layout in the constructor. This way the custom button is reusable.

More information can be found here.

Upvotes: 2

Seshu Vinay
Seshu Vinay

Reputation: 13588

Give that LinearLayout an id. Add an onClickListener to that. and you got a button with that layout.

Upvotes: 0

Related Questions