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();
}
}