java - How can I switch activities when clicking a date using a DatePicker in Android Studio? -
i new android studio, , know how switch activity after clicking date using datepicker? or, @ least, make text appear on screen after clicking date. have read documentation datepicker , looked @ tutorials , reason, still lost on this. advice appreciated.
(edit) alright, did more work , found out missing onclicklistener. problem text date changes if click on border of datepicker , not actual date.
currently, code follows:
activity.xml
<?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" tools:context="com.example.wes19_000.working_on_it.mainmenu"> <datepicker android:id="@+id/datepicker2" android:layout_width="wrap_content" android:layout_height="wrap_content" tools:layout_editor_absolutex="18dp" tools:layout_editor_absolutey="16dp" tools:ignore="missingconstraints" android:clickable="true" /> <textview android:id="@+id/textview2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/datetext" tools:layout_editor_absolutex="162dp" tools:layout_editor_absolutey="461dp" tools:ignore="missingconstraints" /> </android.support.constraint.constraintlayout>
mainmenu.java
import android.support.v7.app.appcompatactivity; import android.os.bundle; import android.view.view; import android.widget.datepicker; import android.widget.textview; public class mainmenu extends appcompatactivity implements view.onclicklistener { private datepicker datepicker; private textview datetext; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main_menu); datepicker = (datepicker) findviewbyid(r.id.datepicker2); datepicker.setonclicklistener(this); datetext = (textview)findviewbyid(r.id.textview2); } @override public void onclick(view v) { int day = datepicker.getdayofmonth(); int month = datepicker.getmonth(); int year = datepicker.getyear(); string date = (month + 1) + "/" + day + "/" + year; datetext.settext(date); } }
datepicker
supports listener, need initialize date picker in order use listener.
datepicker.init(2000,6,15, new datepicker.ondatechangedlistener(){...});
see documentation more details.
Comments
Post a Comment