Linearlayout with 2 textViews to simulate a button

Asked

Viewed 103 times

0

My goal is to have one Button with 2 TextViews inside. So I researched the easiest way is to have a LinearLayout and inside place the TextViews and place the property android:clickable="true".

The problem is that I can’t 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");
}

To Exception that 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’ve already put the android:onClick="OnClick" within the tag of LinearLayout and I keep getting the exception.

  • 1

    My problem is in Java the private way. It has to be public.

  • 1

    Or protected if you prefer.

  • Where have you rewritten this method onClick @porthfind? and can you be clearer on what you’re trying to achieve? maybe an illustration?

1 answer

0

Hello!

You can add the click programmatically!

To do this you must add an id to your Linearlayout and instantiate in your class:

Follow 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’ve helped!

Greetings!

  • 1

    Consider using android:background="?android:attr/selectableItemBackground" in Linearlayout. The Java part can be done more simply by creating an object for Linearlayout and using a traditional cast (just to simplify).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.