kmalmur
kmalmur

Reputation: 2839

Android: how to make a custom button with LinearLayout inside?

I've got the following piece of layout which is already used as a row of a ListView:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/schedule_row_content"
    android:background="@drawable/item_selector" android:layout_width="fill_parent" android:layout_height="60dp">
    <TextView android:gravity="center_vertical|center_horizontal" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:textColor="#000000" android:id="@+id/labelLessonNumber" android:text="1" android:layout_width="27dp"></TextView>
    <LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="wrap_content" android:paddingLeft="10px" android:layout_weight="1" android:layout_gravity="center_vertical">
        <TextView android:layout_height="wrap_content" android:textColor="#000000" android:id="@+id/labelSubject" android:layout_width="fill_parent" android:textAppearance="?android:attr/textAppearanceMedium" android:text="Text"></TextView>
        <TextView android:layout_height="wrap_content" android:textColor="#555555" android:layout_width="wrap_content" android:id="@+id/labelTime" android:text="00:00"></TextView>
    </LinearLayout>
    <LinearLayout android:layout_width="wrap_content" android:layout_height="fill_parent">
        <TextView android:gravity="center" android:width="80dp" android:layout_gravity="center_vertical" android:layout_height="wrap_content" android:textColor="#000000" android:layout_width="wrap_content" android:id="@+id/labelGroup" android:textAppearance="?android:attr/textAppearanceSmall" android:text="Klasa Ia"></TextView>
    </LinearLayout>    
</LinearLayout>

I want to use this layout to create a custom button (the same way it work inside the ListView). Is there any container that captures onClick events from the inside views?

Upvotes: 2

Views: 5596

Answers (3)

Zia Ur Rahman
Zia Ur Rahman

Reputation: 1880

No need to put anything into button, you need the clickable Layout infact, so for this purpose just use this.

<LinearLayout
android:clickable="true"
android:background="@android:drawable/btn_default"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
    android:id="@+id/image"
    android:layout_alignParentLeft="true"
    android:layout_centerVertical="true"
    android:src="@android:drawable/ic_input_add"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content">
</ImageView>
<TextView
    android:id="@+id/text"
    android:text="Sample text"
    android:textAppearance="?android:attr/textAppearanceButton"
    android:layout_toRightOf="@id/image"
    android:layout_centerVertical="true"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"></TextView>
</LinearLayout>

so the android:clickable="true" will make the Layout clickable and will behave like a button :) Hope this helpful.

Upvotes: 1

tludek
tludek

Reputation: 133

I'm not sure whether you may use your layout as custom button because the clickable flag of it should be set to true. If you do so it will capture onClick event and you will have problems with ListView. Try to set android:clickable="true" to your layout and use include tag:

<include android:id="@+id/your_layout"
     layout="@layout/your_layout"
 android:layout_height="wrap_content"
 android:layout_width="fill_parent"/>

Upvotes: 0

R.daneel.olivaw
R.daneel.olivaw

Reputation: 2679

If all you need is to click on the button then a simple and straight solution will be to take a linear layout and set it background to a custom selector drawble, then simply set an onclick listener of this linear layout in your java code.

Custom Selector link 1

Custom Selector link 2

In your case we can have a common on click listener for all list items but differentiate by setting different tag objects for all list item buttons.

I hope it helps..

Upvotes: 1

Related Questions