LinearLayout with 2 textViews to simulate a button

0

My goal is to have a Button with 2 TextView s inside. For what I researched the easiest way is to have a LinearLayout and within put the TextView s and put the android:clickable="true" property.

The problem is that I can not simulate the button action because I get an exception. Can someone help me?

<LinearLayout
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="#D8D8D8"
    android:orientation="vertical"
    android:clickable="true">

    <TextView
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:id="@+id/daily"
        android:text="@string/daily"
        android:textSize="14dp"
        android:gravity="center_vertical|left"
        android:onClick="OnClick"/>

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/hora"
        android:textSize="10dp"
        android:text="Hora"
        android:gravity="center_vertical|left"/>
</LinearLayout>

In Java, I have this:

private void OnClick(View view){
    DialogFragment newFragment = new PickerFragment();
    newFragment.show(this.getFragmentManager(), "timePicker");
}

The% w /% I'm getting is:

Process: com.converter.android.dailyhoroscope, PID: 7679
java.lang.IllegalStateException: Could not find method OnClick(View) in a parent or ancestor Context for android:onClick attribute defined on view class android.widget.TextView with id 'dailyHoroscope'                                                                                        at android.view.View$DeclaredOnClickListener.resolveMethod(View.java:4479)                                                                                        at android.view.View$DeclaredOnClickListener.onClick(View.java:4443)                                                                                        at android.view.View.performClick(View.java:5198)                                                                                        at android.view.View$PerformClick.run(View.java:21147)                                                                                       at android.os.Handler.handleCallback(Handler.java:739)                                                                                        at android.os.Handler.dispatchMessage(Handler.java:95)                                                                                        at android.os.Looper.loop(Looper.java:148)                                                                                        at android.app.ActivityThread.main(ActivityThread.java:5417)                                                                                        at java.lang.reflect.Method.invoke(Native Method)                                                                                        at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:726)                                                                                      at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616)

I already put Exception within android:onClick="OnClick" of tag and I still get the exception.

    
asked by anonymous 06.12.2015 / 16:12

1 answer

0

Hello!

You can add the click programmatically!

For this you must add an id to your LinearLayout and instantiate in your class:

Here's an example:

xml

<LinearLayout
    android:id="@+id/liner_layout_id"
    android:layout_marginTop="10dp"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginLeft="20dp"
    android:layout_marginRight="20dp"
    android:background="#D8D8D8"
    android:orientation="vertical"
    android:clickable="true"
    android:background="?android:attr/selectableItemBackground">

Java :

 LinearLayout objetoClicavel = ((LinearLayout)view.findViewById(R.id.liner_layout_id));
 objetoClicavel.setOnClickListener(new View.OnClickListener() {
             @Override
             public void onClick(View v) {
                 DialogFragment newFragment = new PickerFragment();
                 newFragment.show(this.getFragmentManager(), "timePicker");
              }
   });

I hope I have helped!

Greetings!

    
09.12.2015 / 15:54