Function to Open Calendarview by clicking on Relayoutlayout

Asked

Viewed 565 times

0

I have a screen where I would like the user to click and open the system’s native calendar. Only I would like the CalendarView was opened even if you clicked on RelativeLayout that covers it. I already gave an ID to the RelativeLayout, but as I open the CalendarView.

inserir a descrição da imagem aqui

Java code to get the date:

    @Override
    public void onSelectedDayChange(CalendarView calendarView, int years, int month, int dayOfMonth) {
           Toast.makeText(getApplicationContext(),dayOfMonth + "/" + month + "/" + years, Toast.LENGTH_LONG).show();
        }
    });

XML code

    <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:card_view="http://schemas.android.com/apk/res-auto"
    android:id="@+id/linearLayout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <android.support.v7.widget.CardView
        android:id="@+id/card_view_1"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:layout_gravity="center"
        android:layout_margin="5dp">

        <RelativeLayout
            android:id="@+id/calendar"
            android:layout_width="match_parent"
            android:layout_height="match_parent">

            <TextView
                android:id="@+id/textView37"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_alignParentLeft="true"
                android:layout_alignParentStart="true"
                android:layout_centerVertical="true"
                android:layout_marginLeft="24dp"
                android:layout_marginStart="24dp"
                android:text="@string/datapgto"
                android:textAppearance="?android:attr/textAppearanceMedium" />

            <EditText
                android:id="@+id/editText4"
                android:layout_width="110dp"
                android:layout_height="wrap_content"
                android:layout_alignParentEnd="true"
                android:layout_alignParentRight="true"
                android:layout_centerVertical="true"
                android:layout_marginEnd="16dp"
                android:layout_marginRight="16dp"
                android:ems="10"
                android:inputType="date" />

        </RelativeLayout>
    </android.support.v7.widget.CardView>

  • This date is to be given only through the calendar or can be given by the keyboard?

  • Through the calendar only. When I click open the calendar, then select the date and it inserts into the field the date I chose.

  • @Bruno. I have doubts that his edition translates what the AP wants to ask. In the original nothing indicates that he can not open the Calendar.

  • @ramaral, good that Bruno edited. Because that was my same question. Maybe I didn’t know how to express myself.

  • I could have said it in my answer. I got the idea that she had answered the question. Do you have the code for the method chamarCalendar()? Where is this Calendarview?

  • @ramaral I don’t have the method yet. I was looking here on the internet some tutorials and saw the same code I posted up there. In the tutorial only had this. And his worked properly, the only difference is that he had a button on the screen to call the calendar. In my case I have a whole Relativelayout.

  • I can’t be sure but in principle the code that should put in the method chamarCalendar will be the one in the onClickListener from the button you spoke of.

  • 1

    I will contact the tutorial owner to better understand what he did there. As I said before, the same code I posted there he did. Thanks for your help, fellas.

Show 3 more comments

1 answer

1

To intercept the "click" on Relativelayout add a OnClickListener() to him:

layout = (RelativeLayout) findViewById(R.id.calendar);
layout.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Toast.makeText(MainActivity.this, "Layout clicked", Toast.LENGTH_SHORT).show();
        chamarCalendar();
    }
});

If you want to click on Editetext call the calendar instead of the keyboard add this:

editText = (EditText) findViewById(R.id.editText4);
editText.setOnTouchListener(new View.OnTouchListener() {
    @Override
    public boolean onTouch(View v, MotionEvent event) {
        Toast.makeText(MainActivity.this, "EditText clicked", Toast.LENGTH_SHORT).show();
        chamarCalendar();
        return true;
    }
});

The code shall be placed in the method onCreate() of Activity

  • Ramaral I could not do. Can you explain me better and in more detail ? Thank you.

  • You’re the one who has to go into more detail. What do you mean, "I couldn’t do it"?

  • Ta cara, be calm. Relax. I explain it better. When I put this code in my project, it has a local variable called editText created. Also the method call Legend() do not have to create ?

  • The method call Legend() should be created by you and put in it the code that (I suppose) you already have to call the calendar. You have to declare the variables layout and editText, may do so on the line itself or in the Activity.

  • Thanks man..

Browser other questions tagged

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