I need to create a date picker from a fragment, the problem is that the datepicker that I have seen (examples) are generated from an Activity, and it turns out that it is not the same for fragments, I need to store a period of time and in the fragment I have 2 button
and 2 textview
one button
and one textview
for the start of the date and the rest for the end of the date, these text views will then be stored in the database in the form integer
... but that's another thing...
my objective is:
generate the data picker in the fragment
get a start date and a closing date (ie a period of time) (in each button respectively there is a TextView that will load those dates)
ConfiguracionPeriodoF.java (this is the snippet)
package company.viral.organizadorjec.FracmentPopUp;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.icu.util.Calendar;
import android.icu.util.TimeZone;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.CalendarView;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.PopupWindow;
import android.widget.RelativeLayout;
import android.widget.TextView;
import company.viral.organizadorjec.R;
import static android.content.Context.LAYOUT_INFLATER_SERVICE;
public class ConfiguracionPeriodoF extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// mandamos a cargar la imagen del fragment
View view = inflater.inflate(R.layout.fragment_configuracion_periodo, container, false);
//creamos las variables que van a interactuar con el layout
//TextView
TextView inifecha = (TextView)view.findViewById(R.id.fechaini);
TextView finalfecha = (TextView)view.findViewById(R.id.fechafinal);
//botones
Button btnini = (Button)view.findViewById(R.id.btnfragfecha);
Button btnfinal = (Button)view.findViewById(R.id.btnfragfinal);
return view;
}
}
fragment_configuration_period.xml
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="company.viral.organizadorjec.FracmentPopUp.ConfiguracionPeriodoF">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Inserte periodo de trabajo"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/textView2"
android:gravity="center"
android:textSize="24sp"
tools:textColor="@android:color/holo_blue_dark" />
<Space
android:layout_width="match_parent"
android:layout_height="36dp" />
<EditText
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:ems="10"
android:id="@+id/etnombreperiodo"
android:hint="Nombre del periodo a insertar"
android:gravity="center"
android:layout_width="match_parent" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Inicio"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnfragfecha"
android:layout_weight="1"
style="@android:style/Widget.Button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fechaini"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="1"
tools:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="36dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:text="Final"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/btnfragfinal"
android:layout_weight="1"
style="@android:style/Widget.Button" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/fechafinal"
android:gravity="center"
android:textSize="24sp"
android:layout_weight="1"
tools:textColor="@android:color/holo_blue_dark" />
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="36dp" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Agregar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/agregarperiodo"
android:layout_weight="1"
style="@android:style/Widget.Button"/>
<Button
android:text="Editar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/editarperiodo"
android:layout_weight="1"
style="@android:style/Widget.Button"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
Easy... what you should do is implement this in your onclick of the fragment corresponding to the date you want to capture (that is, one for each button only modifies the corresponding variables for the second button...
Make some changes.
Call this function in your click event of the btnini button for example.
Ready so you will get the current date, I hope it will serve you.