Mattia Mancina
Mattia Mancina

Reputation: 119

Android onClick on Linear Layout

I have following code:

<?xml version="1.0" encoding="utf-8"?>    
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/list_item"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="10dp"
    android:orientation="horizontal"
    android:clickable="true"
    android:onClick="openGithubUrl">

    <ImageView
        android:layout_width="100dp"
        android:layout_height="100dp"
        android:src="@mipmap/github_icon"/>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:id="@+id/github_name"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />
        <TextView
            android:id="@+id/github_url"
            android:layout_width="match_parent"
            android:layout_height="wrap_content" />

    </LinearLayout>


</LinearLayout>

I want to add a onCLick listener on LinearLayout(@+id/list-item). Is this possible? After the onClick fired I would like to get the content of the child Textview @+id/github_url.

Upvotes: 0

Views: 3180

Answers (1)

hsm59
hsm59

Reputation: 2074

Yes you can set an onClickListener to the linear layout, in your use case, listItem.setOnClickListener() in the activity.

LinearLayout listItem;
TextView tvGithubUrl;


tvGithubUrl = (TextView) findViewById(R.id.github_url)
listItem = (LinearLayout) findViewById(R.id.list_item)

listItem.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            //Get the github_url text here
        tvGithubUrl.getText().toString();
        }
    });

Upvotes: 1

Related Questions