Reputation: 25
I can't add an OnClicklistener to may Constraint Layout. It always returns null.
I have definded a ComplexButton:
public class ComplexButton{
private TextView title;
private TextView subTitle;
private ImageView icon;
private ConstraintLayout clickableArea;
public ComplexButton(ConstraintLayout layout) {
title = (TextView) layout.findViewById(R.id.textview_title);
subTitle = (TextView) layout.findViewById(R.id.textview_button_subtitle);
icon = (ImageView) layout.findViewById(R.id.imageview_button_icon);
clickableArea = (ConstraintLayout) layout.findViewById(R.id.complex_button_clickable_area_new);
}
I can set the TextView and ImageView. But For the clickableArea it returns null.
In my other class I make an Instance of it to set the elemets.
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.NotOfInterrest, container, false);
ConstraintLayout complexButtonLayout= (ConstraintLayout) view.findViewById(R.id.buttonHotspotFinder_new);
ComplexButton complexButton= new ComplexButton(complexButtonLayout);
complexButton.setTitle( getString(R.string.tab_find_hotspot) );
complexButton.setSubtitle( "Browse hotspots on our map" );
complexButton.setImageResource( R.drawable.show_hotspots_map );
complexButton.setOnClickListener( new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent intent = new Intent(getContext(), NotOfInterrest.class);
startActivity(intent);
}
});
And the XML File:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@drawable/complex_button_layout"
android:clickable="true"
android:focusable="false"
android:focusableInTouchMode="false"
android:id="@+id/complex_button_clickable_area_new">
<ImageView
android:id="@+id/imageview_button_icon"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.027"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:srcCompat="@drawable/buy_hotspot_subscription" />
<ImageView
android:id="@+id/imageview_right_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="@drawable/ic_keyboard_arrow_right_black_48dp" />
<TextView
android:id="@+id/textview_title"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textColor="@color/colorPrimary"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/imageview_button_icon"
app:layout_constraintRight_toLeftOf="@+id/imageview_right_arrow"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="@+id/textview_button_subtitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_marginTop="8dp"
android:text="TextView"
android:textSize="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toRightOf="@+id/imageview_button_icon"
app:layout_constraintRight_toLeftOf="@+id/imageview_right_arrow"
app:layout_constraintTop_toBottomOf="@+id/textview_title"
app:layout_constraintVertical_bias="0.0" />
As said befor: I can set every image and text. But for the clickableArea i get null pointer.
For ConstraintLayout I use "compile 'com.android.support.constraint:constraint-layout:1.0.2'" Is there a newer version ?
Upvotes: 0
Views: 2785
Reputation: 1082
You are trying to find ConstraintLayout
in side your ConstraintLayout
//Here layout is already an instance of your ConstraintLayout and you are again finding a ConstraintLayout in that
clickableArea = (ConstraintLayout) layout.findViewById(R.id.complex_button_clickable_area_new);
You have already find the ContraintLayout and that you are passing to the Constructor of ComplexButton
class here
ConstraintLayout complexButtonLayout= (ConstraintLayout) view.findViewById(R.id.buttonHotspotFinder_new);
ComplexButton complexButton= new ComplexButton(complexButtonLayout);
Do this instead
public class ComplexButton {
private TextView title;
private TextView subTitle;
private ImageView icon;
private ConstraintLayout clickableArea;
public ComplexButton(ConstraintLayout layout) {
title = (TextView) layout.findViewById(R.id.textview_title);
subTitle = (TextView)
layout.findViewById(R.id.textview_button_subtitle);
icon = (ImageView) layout.findViewById(R.id.imageview_button_icon);
//because layout is your constraint layout
clickableArea = layout;
}
}
Upvotes: 2