I was trying to make a custom button in Android that contains more than one components, to put it straight forward what button contains in form of a layout, here it is:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="@drawable/button"
android:clickable="true"
android:focusable="true">
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/imageView4"
android:background="@drawable/button1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button2"
android:id="@+id/textView2" />
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/imageView5"
android:background="@drawable/button4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub-description"
android:id="@+id/textView3" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/button5" />
</LinearLayout>
</LinearLayout>
Question: how to use this as a button in an Activity with all sort of button pressed and focused customisation? (till now all i have worked around is only with this UI file XML)
You have 2 options in XML or Code.
XML:
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:clickable="true"
>
<ImageView
android:layout_width="15dp"
android:layout_height="15dp"
android:id="@+id/imageView5"
android:background="@drawable/button4"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="sub-description"
android:id="@+id/textView3" />
<ImageView
android:layout_width="25dp"
android:layout_height="25dp"
android:background="@drawable/button5" />
</LinearLayout>
In code .java use:
LinearLayout.setClickable(true);
And remember in both cases use onClick like a simple button.
You'll have to set the LinearLayout to clickable. You can either do this in the XML with
android:clickable="true"
Or in code with
yourLinearLayout.setClickable(true);
yourLinearLayout.setOnClickListener(
new View.OnClickListener() {
@Override
public void onClick(View v) {
// DO STUFF!
}
}
);
Cheers!
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.