Felipe Caldas
Felipe Caldas

Reputation: 2503

Custom Component can't trigger listeners off

I have the following Custom Component:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="@dimen/bottom_actionbar_item_height"
android:orientation="horizontal"  
android:gravity="center">

    <Button
        android:id="@+id/btnLogin"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/botton_button_selector"
        android:drawableLeft="@drawable/balloon_overlay_close"            
        android:text="@string/bottonbar_earned" 
        android:layout_weight="1" android:clickable="true" android:focusable="true"
        android:layout_marginRight="10dp"/>

    <Button
        android:id="@+id/btnLogin2"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:background="@drawable/botton_button_selector"
        android:drawableLeft="@drawable/balloon_overlay_close"
        android:layout_marginRight="10dp"
        android:text="@string/bottonbar_inprogress" android:focusable="true" android:clickable="true" />        

     <Button
        android:id="@+id/btnLogin3"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:background="@drawable/botton_button_selector"
        android:drawableLeft="@drawable/balloon_overlay_close"
        android:text="@string/bottonbar_redeemed"
        android:layout_marginRight="10dp"
        android:layout_weight="1" android:focusable="true" android:clickable="true"/>

</LinearLayout>

I have my main.xml which has a main RelativeLayout, with many other layouts on it, and one of them is my custom component. Custom component is shown correctly, but no listener is being invoked. My Custom Component class has the following code:

public class BottomActionBar extends LinearLayout implements OnClickListener{

private LayoutInflater mInflater;
private LinearLayout mBarView;

public BottomActionBar(Context context, AttributeSet attrs) {
    super(context, attrs);

    mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mBarView = (LinearLayout) mInflater.inflate(R.layout.bottom_actionbar, null);
    addView(mBarView);

    Button bt1 = (Button) mBarView.findViewById(R.id.btnLogin);
    bt1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(getContext(), "test", Toast.LENGTH_SHORT);
        }
    });
}

@Override
public boolean onTouchEvent(MotionEvent evt) {
    System.out.print("test");
    return super.onTouchEvent(evt);
}

public void onClick(View v) {
    System.out.print("test");       
}

}

Now, when testing this, nothing shows up! I also tried inflating the component in my main layout:

mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
    mBarView = (LinearLayout) mInflater.inflate(R.layout.bottom_actionbar, null);

    Button bt1 = (Button) mBarView.findViewById(R.id.btnLogin);
    bt1.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT);                
        }
    });

What am I doing wrong?

Thanks everyone

Upvotes: 0

Views: 502

Answers (1)

Dhanesh Budhrani
Dhanesh Budhrani

Reputation: 190

You forgot to add ".show()" at the end of the Toast line in the onClick() method! :)

Toast.makeText(getApplicationContext(), "test", Toast.LENGTH_SHORT).show();

Upvotes: 1

Related Questions