Je ne peux pas ajouter un OnClicklistener à mai Constraint Layout. Il retourne toujours nul.OnClickListener renvoie null pour ConstraintLayout
J'ai definded un 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);
}
je peux régler la TextView et ImageView. Mais pour le clickableArea il renvoie null.
Dans mon autre classe, j'en crée une instance pour définir les élémets.
@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);
}
});
et le fichier XML:
<?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" />
Comme dit befor: Je peux définir chaque image et le texte. Mais pour le clickableArea j'obtiens un pointeur nul. Pour ConstraintLayout, j'utilise "compile 'com.android.support.constraint: constraint-layout: 1.0.2'" Y at-il une version plus récente?
est complex_butt on_clickable_area_new une instance de ConstraintLayout? –
poster votre mise en page xml –