Android Syntax error, insert "}" to complete MethodBody [closed] - android

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 8 years ago.
Improve this question
i have created a program for selecting the date using OnDateListner method.I have completed the program but it shows a syntax error.I have crated an Xmlfile by placing the button On button click a DatePickerDialog appears from which you can select a date.After making the selection, and clicking the Done button, a toast will appear with the selected date.
the coresponding code is being attached .
The error is shown when i am trying to close OnClickListner method
public class MainActivity extends Activity {
//Declaring a button and the DatePickerDialog
private Button btnDatePicker;
DatePickerDialog _date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDatePicker = (Button) findViewById(R.id.button1);
//Setting an OnclickListener on the Button
btnDatePicker.setOnClickListener(new OnClickListner(){
public void onClick(View v)
{
//Creating an object of DatePickerDialog incontext of the Mainactivity
//dateCallback is called which defined below
_date=new DatePickerDialog(MainActivity.this, dateCallback, 2012, 10, 12);
//Showing the DatePickerDialog
_date.show();
}
});
//Setting OnDateSetListener on the DatePickerDialog
private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(MainActivity.this, "The date is : " + dayOfMonth+"/"+ ++monthOfYear +"/"+ year, Toast.LENGTH_LONG).show();
}
};

Below is your complete class. You were not closing onCreate Method of Activity. Every method must starts with a starting brace {and must close with a closing brace }.
import junit.framework.Test;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.DatePickerDialog.OnDateSetListener;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.Toast;
public class MainActivity extends Activity {
// Declaring a button and the DatePickerDialog
private Button btnDatePicker;
DatePickerDialog _date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDatePicker = (Button) findViewById(R.id.button1);
// Setting an OnclickListener on the Button
btnDatePicker.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// Creating an object of DatePickerDialog incontext of the
// Mainactivity
// dateCallback is called which defined below
_date = new DatePickerDialog(Test.this, dateCallback, 2012, 10,
12);
// Showing the DatePickerDialog
_date.show();
}
});
}
// Setting OnDateSetListener on the DatePickerDialog
private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
Toast.makeText(
MainActivity.this,
"The date is : " + dayOfMonth + "/" + ++monthOfYear + "/"
+ year, Toast.LENGTH_LONG).show();
}
};
}

public class MainActivity extends Activity {
//Declaring a button and the DatePickerDialog
private Button btnDatePicker;
DatePickerDialog _date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnDatePicker = (Button) findViewById(R.id.button1);
//Setting an OnclickListener on the Button
btnDatePicker.setOnClickListener(new OnClickListner(){
public void onClick(View v)
{
//Creating an object of DatePickerDialog incontext of the Mainactivity
//dateCallback is called which defined below
_date=new DatePickerDialog(MainActivity.this, dateCallback, 2012, 10, 12);
//Showing the DatePickerDialog
_date.show();
}
});
}
//Setting OnDateSetListener on the DatePickerDialog
private DatePickerDialog.OnDateSetListener dateCallback = new OnDateSetListener()
{
public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth) {
Toast.makeText(MainActivity.this, "The date is : " + dayOfMonth+"/"+ ++monthOfYear +"/"+ year, Toast.LENGTH_LONG).show();
}
};

Related

How do I call my Date Picker from a button press?

I am trying to create an app that allows me to pick a date and a table is populated with appointments on that day. I am having troubles with getting the date picker to show.
I have taken a look at this https://developer.android.com/guide/topics/ui/controls/pickers and created a date picker from this code. I want to call that date picker from a button press found in the AddAppointmentActivity class.
Here is my Appointment class:
package com.example.dentdevils.ui.HomeLinks;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.fragment.app.DialogFragment;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import com.example.dentdevils.R;
import com.example.dentdevils.ui.HomeActivity;
import java.util.Calendar;
public class AddAppointmentActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_appointment);
// Date label
TextView dateLabel = (TextView) findViewById(R.id.txtDateLabel);
// Date Picker Button Functionality
Button datePicker = (Button) findViewById(R.id.btnDatePicker);
datePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
// Back Button Functionality
Button btnBack = (Button) findViewById(R.id.btnBack);
btnBack.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent launchHomepage = new Intent(AddAppointmentActivity.this, HomeActivity.class);
startActivity(launchHomepage);
}
});
}
}
class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
final Calendar calendar = Calendar.getInstance();
int year = calendar.get(Calendar.YEAR);
int month = calendar.get(Calendar.MONTH);
int day = calendar.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int month, int dayOfMonth) {
// Get date set by user and display appointments in table for the date chosen.
}
public void showDatePickerDialog() {
DialogFragment newFragment = new com.example.dentdevils.ui.HomeLinks.DatePickerFragment();
newFragment.show(getFragmentManager(), "datePicker");
}
}
I did have the classes in separate files but was testing different things hence why they are in the same file (I will move them back out again to separate files after I have managed to call the date picker).
My question is, how would I call the date picker dialog from the button press?
datePicker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
});
You don't need create an extra dialog fragment for date picker. Just create DatePickerDialog object and call show().
Example:
val calendar = Calendar.getInstance()
val dialog = DatePickerDialog(context,
DatePickerDialog.OnDateSetListener { view, year, month, dayOfMonth ->
// TODO
}, calendar.get(Calendar.YEAR), calendar.get(Calendar.MONTH), calendar.get(Calendar.DAY_OF_WEEK_IN_MONTH))
dialog.show()

That I have wrong with this code? Trying to show a datepicker on a edittext

That I have wrong with this code?
I date indicates that the variable has to be marked as final, but if I do it still does not work, shoot me error: Insert app indexed code api.
I'm trying that through a EditText I display a datepicker
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
Here code
package com.example.cesar.mybankaccess.view;
import android.app.DatePickerDialog;
import android.content.DialogInterface;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import com.example.cesar.mybankaccess.R;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class PromocionesFormulario extends AppCompatActivity{
Calendar myCalendar;
EditText fechaInicio;
DatePickerDialog.OnDateSetListener date;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_promociones_formulario);
fechaInicio = (EditText) findViewById(R.id.fechaInicio);
myCalendar = Calendar.getInstance();
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
// TODO Auto-generated method stub
myCalendar.set(Calendar.YEAR, year);
myCalendar.set(Calendar.MONTH, monthOfYear);
myCalendar.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabel();
}
};
fechaInicio.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new DatePickerDialog(PromocionesFormulario.class, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabel() {
String myFormat = "MM/dd/yy"; //In which you need put here
SimpleDateFormat sdf = new SimpleDateFormat(myFormat, Locale.US);
fechaInicio.setText(sdf.format(myCalendar.getTime()));
}
}
Make DatePickerDialog.OnDateSetListener date final. And in,
new DatePickerDialog(PromocionesFormulario.class, date, myCalendar.get(Calendar.YEAR), myCalendar.get(Calendar.MONTH),
myCalendar.get(Calendar.DAY_OF_MONTH)).show();
Pass PromocionesFormulario.this instead of PromocionesFormulario.class.
The problem here is, the type of PromocionesFormulario.class is Class. But new DatePickerDialog() requires Context object as first parameter.
UPDATE
As Msp mentioned, you also need to change that. Context is given with .this,
not .class
If you're trying to make the object final, clear this line:
DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() { //...
and make it:
date = new DatePickerDialog.OnDateSetListener() { //...
then update your initialization, like:
final DatePickerDialog.OnDateSetListener date;
other than that, your question is not obvious.

Android: DatePicker value saved in a variable doesn't go in onCreate

I have a rather simple issue but this is bugging me out!
I am trying to build an app in which I have 2 DatePickers, periodFrom and periodTo.
Basically what I'm trying to do is let the user pick dates on both cases and then calculate the difference between dates in days. I already know how to get the difference, how to get the date from the DatePicker. The problem is I don't know where I should get the value from the DatePicker, as my onCreate intializes the listener for the DatePickers but it doesn't store the set dates in the global variables, only the current date which it gets from Calendar.getInstance();
Here is my code for the MainActivity, maybe you can enlighten me on this issue I'm having. Thank you!
MainActivity.class
package com.endtech.utilitycalculator;
import android.app.DatePickerDialog;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.DatePicker;
import android.widget.EditText;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
private EditText periodFrom, periodTo;
private Calendar mCalendarFrom = Calendar.getInstance();
private Calendar mCalendarTo = Calendar.getInstance();
private long from, to;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
periodFrom = (EditText) findViewById(R.id.periodFrom);
periodTo = (EditText) findViewById(R.id.periodTo);
initListeners();
from = mCalendarFrom.getTimeInMillis(); //This is current time, not the set time
to = mCalendarTo.getTimeInMillis(); //This is current time, not the set time
}
private void initListeners() {
setDateFrom();
setDateTo();
}
private void setDateFrom() {
final DatePickerDialog.OnDateSetListener date = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarFrom.set(Calendar.YEAR, year);
mCalendarFrom.set(Calendar.MONTH, monthOfYear);
mCalendarFrom.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelFrom();
}
};
periodFrom.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date, mCalendarFrom.get(Calendar.YEAR),
mCalendarFrom.get(Calendar.MONTH), mCalendarFrom.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelFrom() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodFrom.setText(sdf.format(mCalendarFrom.getTime()));
}
private void setDateTo() {
final DatePickerDialog.OnDateSetListener date2 = new DatePickerDialog.OnDateSetListener() {
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
mCalendarTo.set(Calendar.YEAR, year);
mCalendarTo.set(Calendar.MONTH, monthOfYear);
mCalendarTo.set(Calendar.DAY_OF_MONTH, dayOfMonth);
updateLabelTo();
}
};
periodTo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
new DatePickerDialog(MainActivity.this, date2, mCalendarTo.get(Calendar.YEAR),
mCalendarTo.get(Calendar.MONTH), mCalendarTo.get(Calendar.DAY_OF_MONTH)).show();
}
});
}
private void updateLabelTo() {
String mFormat = "dd/MM/yyyy";
SimpleDateFormat sdf = new SimpleDateFormat(mFormat, Locale.GERMANY);
periodTo.setText(sdf.format(mCalendarTo.getTime()));
}
}
The problem is you are getting the dates before selecting them.
Simply Create a button (when you click on the button then the process of converting difference into days get done). then on button click get the values of dates and then convert it into days.
Let me know if you find some problem by commenting below.

Android DatePicker and Button issues

I'm trying to have a DatePicker dialog display when a user clicks on a button that says "Set your birthday", and what I need then is for the button to then display the user's selection back in the button. I have tried some of the solutions listed on this site with a TextView, but all to no avail. The DatePicker is displaying just fine, but I can't get the data to show up on the Button. I've tried these solutions: DatePicker not updating Textview in Android Display datepickers value in a textview Android: DatePicker and DatePicker Dialog How to transfer the formatted date string from my DatePickerFragment?
Here's my code:
First the DatePickerFragment:
public class DatePickerFragment extends DialogFragment implements DatePickerDialog.OnDateSetListener {
TheListener listener;
public interface TheListener {
public void returnDate(String date);
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
return new DatePickerDialog(getActivity(), this, year, month, day);
}
#Override
public void onDateSet(DatePicker view, int year, int monthOfYear, int dayOfMonth) {
Calendar c = Calendar.getInstance();
c.set(year, monthOfYear, dayOfMonth);
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
String formattedDate = sdf.format(c.getTime());
if (listener != null) {
listener.returnDate(formattedDate);
}
}
}
Now, the Fragment that is calling it:
public class SignUpFragment extends Fragment implements MyBirthday.TheListener {
private Button btnBirthday;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_signup, container, false);
return view;
}
#Override
public void onResume() {
super.onResume();
btnBirthday = (Button) getActivity().findViewById(R.id.signup_birthday_button);
btnBirthday.setText("Set your birthday");
btnBirthday.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogFragment picker = new DatePickerFragment();
picker.show(getFragmentManager(), "datePicker");
}
});
}
#Override
public void returnDate(String date) {
btnBirthday.setText(date);
}
}
Any and all help will be greatly appreciated. If this is a duplicate question, I'm sorry for that, but I couldn't find a way to get my program to behave in the way I would like for it to. I couldn't find a solution on here.
Thanks in advance.
Try this. This will show you how to render the information in a textview, but you can transfer that to a button as well. You can find the full source code on http://www.ahotbrew.com/android-datepicker-example/
package com.ahotbrew.datepicker;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.support.v4.app.DialogFragment;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.DatePicker;
import android.widget.TextView;
import java.util.Calendar;
public class MainActivity extends ActionBarActivity {
public static TextView SelectedDateView;
public static class DatePickerFragment extends DialogFragment
implements DatePickerDialog.OnDateSetListener {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the current date as the default date in the picker
final Calendar c = Calendar.getInstance();
int year = c.get(Calendar.YEAR);
int month = c.get(Calendar.MONTH);
int day = c.get(Calendar.DAY_OF_MONTH);
// Create a new instance of DatePickerDialog and return it
return new DatePickerDialog(getActivity(), this, year, month, day);
}
public void onDateSet(DatePicker view, int year, int month, int day) {
SelectedDateView.setText("Selected Date: " + (month + 1) + "-" + day + "-" + year);
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
SelectedDateView = (TextView) findViewById(R.id.selected_date);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void showDatePickerDialog(View v) {
DialogFragment newFragment = new DatePickerFragment();
newFragment.show(getSupportFragmentManager(), "datePicker");
}
}

android-Intent and OnClickListener

Brand new to android and facing a weird problem.Check out the code below
FirstActivity.java:
package experiment.on.it;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class FirstActivity extends Activity implements Button.OnClickListener {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button timeDatePicker = (Button) findViewById(R.id.timeDatePickerBtn);
Button customSpinner = (Button) findViewById(R.id.spinnerBtn);
timeDatePicker.setOnClickListener(this);
customSpinner.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.timeDatePickerBtn:
startActivity(new Intent(this, TimeDatePicker.class));
case R.id.spinnerBtn:
startActivity(new Intent(this, CustomSpinner.class));
}
}
}
As I/you expected that by clicking on the two buttons i.e.
timeDatePicker
customSpinner
a new Activity will starts (TimeDatePicker and CustomSpinner respectively).
Our expectation is correct. But the problem i'm facing that when i click on the timeDatePicker the second activity (CustomSpinner) starts. But if i press the emulator's back button (i.e. the device back button) the first activity(TimeDatePicker) appears. I cant find any keyword to describe this kind of problem. So writing this boring question. Any help will be greatly appreciated from a beginner.(...And i think the answer will be pretty easy and small, which i couldnt get. :-P) The Activities code are given below.
TimeDatePicker.java:
package experiment.on.it;
import java.util.Calendar;
import android.app.Activity;
import android.app.DatePickerDialog;
import android.app.Dialog;
import android.app.TimePickerDialog;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.TextView;
import android.widget.TimePicker;
public class TimeDatePicker extends Activity {
private TextView mDateDisplay;
private Button mPickDate;
private int mYear;
private int mMonth;
private int mDay;
private TextView mTimeDisplay;
private Button mPickTime;
private int mhour;
private int mminute;
static final int TIME_DIALOG_ID = 3;
static final int DATE_DIALOG_ID = 2;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timedatepicker);
mDateDisplay = (TextView) findViewById(R.id.date);
mPickDate = (Button) findViewById(R.id.datepicker);
mTimeDisplay = (TextView) findViewById(R.id.time);
mPickTime = (Button) findViewById(R.id.timepicker);
// Pick time's click event listener
mPickTime.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(TIME_DIALOG_ID);
}
});
// PickDate's click event listener
mPickDate.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
showDialog(DATE_DIALOG_ID);
}
});
final Calendar c = Calendar.getInstance();
mYear = c.get(Calendar.YEAR);
mMonth = c.get(Calendar.MONTH);
mDay = c.get(Calendar.DAY_OF_MONTH);
mhour = c.get(Calendar.HOUR_OF_DAY);
mminute = c.get(Calendar.MINUTE);
}
// -------------------------------------------update
// date----------------------------------------//
private void updateDate() {
mDateDisplay.setText(new StringBuilder()
// Month is 0 based so add 1
.append(mDay).append("/").append(mMonth + 1).append("/")
.append(mYear).append(" "));
showDialog(TIME_DIALOG_ID);
}
// -------------------------------------------update
// time----------------------------------------//
public void updatetime() {
mTimeDisplay.setText(new StringBuilder().append(pad(mhour)).append(":")
.append(pad(mminute)));
}
private static String pad(int c) {
if (c >= 10)
return String.valueOf(c);
else
return "0" + String.valueOf(c);
}
// Datepicker dialog generation
private DatePickerDialog.OnDateSetListener mDateSetListener = new DatePickerDialog.OnDateSetListener() {
public void onDateSet(DatePicker view, int year, int monthOfYear,
int dayOfMonth) {
mYear = year;
mMonth = monthOfYear;
mDay = dayOfMonth;
updateDate();
}
};
// Timepicker dialog generation
private TimePickerDialog.OnTimeSetListener mTimeSetListener = new TimePickerDialog.OnTimeSetListener() {
public void onTimeSet(TimePicker view, int hourOfDay, int minute) {
mhour = hourOfDay;
mminute = minute;
updatetime();
}
};
#Override
protected Dialog onCreateDialog(int id) {
switch (id) {
case DATE_DIALOG_ID:
return new DatePickerDialog(this, mDateSetListener, mYear, mMonth,
mDay);
case TIME_DIALOG_ID:
return new TimePickerDialog(this, mTimeSetListener, mhour, mminute,
true);
}
return null;
}
}
CustomSpinner.java:
package experiment.on.it;
import android.app.Activity;
import android.os.Bundle;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
public class CustomSpinner extends Activity {
String[] items = { "this", "is", "a", "really", "really2", "really3",
"really4", "really5", "silly", "list" };
#Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.spinnerlayout);
Spinner spin =(Spinner)findViewById(R.id.sPinner);
ArrayAdapter adapter = new ArrayAdapter(this,R.layout.radio,R.id.txt,items);
spin.setAdapter(adapter);
}
}
The problem can be solved putting break in every case of the switch
public void onClick(View v) {
switch (v.getId()) {
case R.id.timeDatePickerBtn:
startActivity(new Intent(this, TimeDatePicker.class));
break;
case R.id.spinnerBtn:
startActivity(new Intent(this, CustomSpinner.class));
break;
}
}
try this.
You are missing break; statement in your switch case block.
So, thought the first activity starts and is in your backstack, it is immediately pushed down by the second one due to the missing break and the back button behaves as it should and return you to the the first one.
I think, you missed 'break' in switch~case.
You're missing break in your switch case, along with default. Since your switch case is taking the value of the spinner class, when you end the spinner class by pressing back button, the time-date picker class pops up.

Categories

Resources