crear un LinearLayout clickeable con Android

7:53 , , 0 Comments

Si necesitamos que un linearlayout sea clickeable en una aplicación realizada en Android, lo podemos realizar de la siguiente manera.

en la estructura XML del LinearLayout se deberá poner la propiedad android:clickable="true"
y la propiedad android:background="?attr/selectableItemBackground" se le asigna el comportamiento de un selector clickeable

Código xml del LinearLayout clickeable
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/youLineLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:gravity="center_horizontal"
    android:orientation="horizontal"
    android:clickable="true"
    android:background="?attr/selectableItemBackground">

En el documento .java debermos referir al layout y escuchar los clicks/touchs que se hagan en el, como si un botón normal se tratase.

LinearLayout btnOne = (LinearLayout) findViewById(R.id.youLineLayout);
btnOne.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View view) {
    //click sentences
    }
});

webserveis

Some say he’s half man half fish, others say he’s more of a seventy/thirty split. Either way he’s a fishy bastard.

0 comentarios: