0

I'm new in developing android and I'm having trouble making a TextView show a DatePicker after the Textview is touched.

The code of the XML is here

<?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="match_parent"
    app:layout_behavior="@string/appbar_scrolling_view_behavior"
    tools:context="dfx.com.asistentepredicaciondfx.MainActivity"
    tools:showIn="@layout/app_bar_main">

    <TextView
        android:id="@+id/textView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        app:layout_constraintRight_toLeftOf="@+id/textView2"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintRight_creator="1"
        android:layout_marginEnd="18dp"
        app:layout_constraintTop_toBottomOf="@+id/textView2" />

    <TextView
        android:id="@+id/textView2"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Dia"
        tools:layout_constraintTop_creator="1"
        tools:layout_constraintBottom_creator="1"
        app:layout_constraintBottom_toTopOf="@+id/textView3"
        android:layout_marginTop="67dp"
        tools:layout_constraintLeft_creator="1"
        android:layout_marginBottom="66dp"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView3"
        app:layout_constraintVertical_bias="1.0" />

    <TextView
        android:id="@+id/textView3"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Horas"
        android:layout_marginStart="158dp"
        app:layout_constraintBaseline_toBaselineOf="@+id/textView4"
        tools:layout_constraintBaseline_creator="1"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintLeft_toLeftOf="parent" />

    <TextView
        android:id="@+id/textView4"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="0"
        tools:layout_constraintTop_creator="1"
        android:layout_marginStart="40dp"
        android:layout_marginTop="150dp"
        tools:layout_constraintLeft_creator="1"
        app:layout_constraintTop_toTopOf="parent"
        app:layout_constraintLeft_toRightOf="@+id/textView3" />

</android.support.constraint.ConstraintLayout>

I want the TextView2 to show the DatePciker.

I would appreciate if someone can help me with an example what i have to do. I'm almost sure I need to create another class but i dont know what to put in the class, thats the thing i need more help. I don't know Android to much but i know a little bit of Java. Try to explain as simple as possible please because english is also not my main language.

Java code for the XML

package dfx.com.asistentepredicaciondfx;

import android.app.Activity;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;

import java.util.Calendar;

/**
 * Created by Diego Utreras on 2/3/2018.
 */

public class ContentMain extends Activity implements View.OnClickListener {
    TextView dia;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.content_main);

        TextView dia = (TextView) findViewById(R.id.textView2);
        dia.setOnClickListener(this);
    }


    @Override
    public void onClick(View view) {
        new DatePickerDialog(ContentMain.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                //DO SOMETHING


                dia.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear+1)+"/"+String.valueOf(year));

            }
        }, 2015, 02, 26).show();

    }
}
Diego Utreras
  • 101
  • 5
  • 13
  • Can you post your java code for this layout – Psypher Apr 07 '18 at 02:28
  • 1
    Possible duplicate of https://stackoverflow.com/q/39916178/6559031 – Devil10 Apr 07 '18 at 02:34
  • Possible duplicate of [How to set two or more DatePicker and Display it on TextView?](https://stackoverflow.com/questions/39517971/how-to-set-two-or-more-datepicker-and-display-it-on-textview) – Mocking Apr 07 '18 at 03:17
  • I have placed the Java code for the xml but I'm almost sure its wrong. Thats where I need help – Diego Utreras Apr 07 '18 at 03:38
  • Possible duplicate of [How to show DatePickerDialog on Button click?](https://stackoverflow.com/questions/39916178/how-to-show-datepickerdialog-on-button-click) – Jyoti JK Apr 07 '18 at 04:28

2 Answers2

0

add this on onclick

    new DatePickerDialog(ContentMain.this, R.style.DialogTheme, new DatePickerDialog.OnDateSetListener() {
            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
                //DO SOMETHING

                dia.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear+1)+"/"+String.valueOf(year));

            }
        }, 2015, 02, 26).show();

and R.style.DialogTheme is shown below add it in it in style.xml

       <style name="DialogTheme" parent="Theme.AppCompat.Light.Dialog">
      <item name="colorAccent">@color/colorPrimary</item>
       </style>
Nikhil Biju
  • 645
  • 7
  • 8
  • I did what you say but now it says cannot resolve symbol dia. Also can you explain a little bit more the DialogTheme part, I don't understand it to much. – Diego Utreras Apr 07 '18 at 04:57
  • dia is the text view that is updated on date set as in the questian.On Date set gets called when you select date you can do whatever you want withe the date i just updated textview(dia) wtith the selected date.Dialog theme is used just to add the theme colour for dialog just add that piece of code in style.xml – Nikhil Biju Apr 07 '18 at 04:59
  • i know dia is the one that should be updated in this line of code dia.setText(String.valueOf(dayOfMonth)+"/"+String.valueOf(monthOfYear+1)+"/"+String.valueOf(year)); but when I have this line in the onclick method it says that it can't find the symbol. – Diego Utreras Apr 07 '18 at 05:05
  • declare the variable above onCreate you declared 'dia' inside onCreate it cannot be accessed . delcare it below the line: public class ContentMain extends Activity implements View.OnClickListener { – Nikhil Biju Apr 07 '18 at 05:14
  • I did all what you say but when I run the app in the emulator it doesn't show anything when i click in the TextView2. Do I need to do something perhaps with the activity main or the problem is only in the contentmain class. I will update my Java code of the contentmain class – Diego Utreras Apr 07 '18 at 12:58
0

Try using EditTest with android:inputType="date" this works for me.

   <EditText
        android:id="@+id/editText"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:ems="10"
        android:inputType="date" />
anoopknr
  • 3,177
  • 2
  • 23
  • 33