Hi guys I have an exit button on my main activity XML however I need it so when the user clicks it, it opens a dialogue to confirm the exit, if yes then exit. if no take them back.
here's my code so far, don't have any of that part however as I don't know where to start.
package com.tradingsoftwarelimited.labelprinter10;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Sets Edit and Data Button to hidden
Button hiddenButton = (Button) findViewById(R.id.editButton);
hiddenButton.setVisibility(View.INVISIBLE);
Button hiddenButton1 = (Button) findViewById(R.id.dataButton);
hiddenButton1.setVisibility(View.INVISIBLE);
//Acknolages settings button
Button showButton = (Button) findViewById(R.id.settingsButton);
//if clicked
showButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//If clicked show Edit and data Button
Button showButton = (Button) (findViewById(R.id.editButton));
showButton.setVisibility(View.VISIBLE);
Button showButton1 = (Button) (findViewById(R.id.dataButton));
showButton1.setVisibility(View.VISIBLE);
}
});
}
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getSupportActionBar().hide();
return true;
}
};
;
private void confirmDialog(Context context){
final AlertDialog alert = new AlertDialog.Builder(
new ContextThemeWrapper(context,android.R.style.Theme_Dialog))
.create();
alert.setTitle("Alert");
alert.setMessage("Do you want to exit ?");
alert.setIcon(R.drawable.warning_icon);
alert.setCancelable(false);
alert.setCanceledOnTouchOutside(false);
alert.setButton(DialogInterface.BUTTON_POSITIVE, "Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
finish();
}
});
alert.setButton(DialogInterface.BUTTON_NEGATIVE, "No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
alert.dismiss();
}
});
alert.show();
}
Call this method in
#Override
public void onBackPressed() {
super.onBackPressed();
confirmDialog(getApplicationContext());
}
of your Activity
Try like this:
Button exitButton = (Button) findViewById(R.id.exit_button);
exitButton..setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builde = new AlertDialog.Builder(this);
builde.setMessage(
"Want Exit?")
.setPositiveButton("Yes ", dialogClickListeners)
.setNegativeButton("Cancel", dialogClickListeners).show();
}
});
DialogInterface.OnClickListener dialogClickListeners = new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int which)
{
switch (which)
{
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Invite = (ImageButton) findViewById(R.id.imageButton5);
final DialogInterface.OnClickListener dialogClickListeners = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case DialogInterface.BUTTON_POSITIVE:
finish();
break;
case DialogInterface.BUTTON_NEGATIVE:
break;
}
}
};
Invite.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
AlertDialog.Builder builde = new AlertDialog.Builder(MainActivity.this);
builde.setMessage(
"Want Exit?")
.setPositiveButton("Yes ", dialogClickListeners)
.setNegativeButton("Cancel", dialogClickListeners).show();
}
});
}
Related
In my app, I have music playing when the app starts. If the user goes to settings they can change the volume or turn the music on and off, however I do not know how to recall the information from the radio buttons to turn the music on or off in other activities. I know if I turn the music off in the main activity under onCreate it will listen to the radio buttons, but I want music playing when the app starts and when you resume the activity it inputs the info from the radio buttons.
This is the code of my main activity
import androidx.annotation.RequiresApi;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Intent svc = new Intent(this, music.class
);
startService(svc);
}
public void onPlayPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), Game.class);
startActivity(myIntent);
}
public void onSettingsPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), Settings.class);
startActivity(myIntent);
}
public void onHowtoPressed(View view) {
Intent myIntent = new Intent(getBaseContext(), HowtoPlay.class);
startActivity(myIntent);
}
#Override
public void onResume() {
super.onResume();
}
#Override
public void onDestroy() {
super.onDestroy();
Intent svc = new Intent(this, music.class);
stopService(svc);
}
}
and this is the code of my Settings activity
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.Intent;
import android.media.AudioManager;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.SeekBar;
import android.widget.Toast;
public class Settings extends AppCompatActivity {
private RadioGroup radioGroup;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_settings);
final Intent svc = new Intent(this, music.class
);
RadioButton rdb = (RadioButton) findViewById(R.id.on);
rdb.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((RadioButton) v).isChecked();
// Check which radiobutton was pressed
if (checked){
startService(svc);
}
else{
stopService(svc);
}
}
});
RadioButton rdb2 = (RadioButton) findViewById(R.id.off);
rdb2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean checked = ((RadioButton) v).isChecked();
// Check which radiobutton was pressed
if (checked){
stopService(svc);
}
else{
startService(svc);
}
}
});
final AudioManager audioManager = (AudioManager)getSystemService(Context.AUDIO_SERVICE);
int maxVolume = audioManager.getStreamMaxVolume(AudioManager.STREAM_MUSIC);
int curVolume = audioManager.getStreamVolume(AudioManager.STREAM_MUSIC);
SeekBar volControl = (SeekBar)findViewById(R.id.seekBar);
volControl.setMax(maxVolume);
volControl.setProgress(curVolume);
volControl.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
#Override
public void onStopTrackingTouch(SeekBar arg0) {
}
#Override
public void onStartTrackingTouch(SeekBar arg0) {
}
#Override
public void onProgressChanged(SeekBar arg0, int arg1, boolean arg2) {
audioManager.setStreamVolume(AudioManager.STREAM_MUSIC, arg1, 0);
}
});
}
public void onMainMenuPressed(View view){
Intent myIntent=new Intent(getBaseContext(),MainActivity.class);
startActivity(myIntent);
}
public void onResumePressed (View view){
Intent myIntent=new Intent(getBaseContext(),Game.class);
myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
myIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
startActivity(myIntent);
finish();
}
} ```
Use SharedPreferences with simple boolean flag.
You should use onCheckedChangeListener on the radio group rather than onClickListener on individual radio button, like so:
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(...) {
getSharedPreferences("prefs", 0).edit().putBoolean("music", rdb.isChecked()).apply();
}
});
Then inside other activity:
boolean music = getSharedPreferences("prefs", 0).getBoolean("music", true);
Then check value of the flag to determine whether or not to start the music service.
My program opens a DialogFragment class while running in MainActivity.java
I want to be able to click on the "neutral button" of that dialog and open a new activity, SensorDataDisplay.java
I am having trouble finding the right way to reference the Context in my button's onClick.
package com.august.customtisensortagclient;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import java.util.ArrayList;
public class GetInfoDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final String thisWhichGetInfoDialog = ((MainActivity)getActivity()).getWhichGetInfoDialog();
final ArrayList<String> thisScannedDevicesArrayList =
((MainActivity)getActivity()).getScannedDevicesArrayList();
final int thisIsInLeftConnectedDeviceDisplay = ((MainActivity)getActivity()).getIsInLeftConnectedDeviceDisplay();
final int thisIsInRightConnectedDeviceDisplay = ((MainActivity)getActivity()).getIsInRightConnectedDeviceDisplay();
int thisIsInThisConnectedDeviceDisplay = 0;
if (thisWhichGetInfoDialog == "Left") {
thisIsInThisConnectedDeviceDisplay = thisIsInLeftConnectedDeviceDisplay;
} else if (thisWhichGetInfoDialog == "Right")
thisIsInThisConnectedDeviceDisplay = thisIsInRightConnectedDeviceDisplay;
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(thisWhichGetInfoDialog + " Sensor Info");
builder.setMessage("MAC Address: " + thisScannedDevicesArrayList.get(thisIsInThisConnectedDeviceDisplay));
builder.setNeutralButton("View Data", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent(?????, SensorDataDisplay.class);
myIntent.putExtra("key", "TEST VALUE"); //Optional parameters
?????.startActivity(myIntent);
}
});
builder.setNegativeButton("Done", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
return builder.create();
}
}
DialogFragment has getActivity() and getContext() methods (which it inherits from Fragment), both will work in your case. If you're having trouble accessing these methods from the anonymous class (which shouldn't be the case), you can use the GetInfoDialog.this.getActivity() syntax.
getActivity() returns the Activity the fragment is attached to
builder.setNeutralButton("View Data", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent myIntent = new Intent(getActivity(), SensorDataDisplay.class);
myIntent.putExtra("key", "TEST VALUE"); //Optional parameters
getActivity().startActivity(myIntent);
}
});
I'm currently learning android app development and on the very beginning phase. I built this game and now i want to set store and display high score as Textview on my screen. I'd really appreciate if anyone could help me out. thanks!
package com.princeghimire.clickmeter;
import android.content.DialogInterface;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.os.CountDownTimer;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity{
TextView tv_time, tv_clicks;
Button b_start, b_click;
CountDownTimer timer;
int time = 10;
int clicks = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setLogo(R.drawable.iconnn);
getSupportActionBar().setDisplayUseLogoEnabled(true);
tv_time = (TextView) findViewById(R.id.tv_time);
tv_clicks = (TextView) findViewById(R.id.tv_clicks);
b_start = (Button) findViewById(R.id.b_start);
b_click = (Button) findViewById(R.id.b_click);
b_start.setEnabled(true);
b_click.setEnabled(false);
timer = new CountDownTimer(10000, 1000) {
#Override
public void onTick(long millisUntilFinished) {
time--;
tv_time.setText("Time: " + time);
}
#Override
public void onFinish() {
b_start.setEnabled(true);
b_click.setEnabled(false);
tv_time.setText("Time: 10" );
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Time's Up Buddy!");
builder.setMessage("Your Score Is: " + clicks);
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int which) {
dialogInterface.dismiss();
// finish();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
};
b_click.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
clicks++;
// switchh.start();
tv_clicks.setText("Your Clicks: " + clicks);
}
});
b_start.setOnClickListener(new View.OnClickListener(){
public void onClick (View v){
timer.start();
b_start.setEnabled(false);
b_click.setEnabled(true);
clicks = 0;
time = 10;
tv_time.setText("Time: " + time);
tv_clicks.setText("Your Clicks: " + clicks);
}
});
}
public void onBackPressed()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to EXIT?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
finish();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
}
Basically this is the procedure:
Create instance of SharedPreferences and SharedPreferences.Edit SharedPreferences sharedPref = .... AND SharedPreferences.Edit editor = .....
Call this in where your game ends if (sharedPref.getInt("highscore",0) < clicks) { editor.putInt("highscore",clicks).apply() }
The above code will first check the last score you saved in "highscore" key and if your current clicks is higher it will replace it with it, and if this is the first game so the default value will be taken as 0 so the first game's score will be saved as highscore unless its 0 again.
I've made almost the same game, its Taplay and on PlayStore btw.
Good Luck.
You can use SharedPreferences, I suggest you to use Sqlite if you want to keep track of the user game statistics.
Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am new to programming in java, i'm creating a simple application with multiple buttons within one activity. The problem that I am having is that buttons only work in sequence, one button will carry out its job only after another has been pressed, any help would be appreciated.
Code below:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Settings button start
Button settingsButton = (Button) findViewById(R.id.btnSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSettings();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
// Settings button end
// Set A Button start
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneA();
}
});}
public void setzoneA(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
// Set A Button end
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneB();
}
});}
public void setzoneB(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
}
// Set B Button end
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
Because you set listeners for button have dependency, All the button initiation and listeners should be in onCreate().
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Settings button start
Button settingsButton = (Button) findViewById(R.id.btnSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSettings();
}
});
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneA();
}
});
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneB();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
}
public void setzoneA() {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
}
public void setzoneB() {
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
You need to set all your onClick() listeners within the onCreate() superfunction. Your current solution only sets the listeners when the previous button is clicked.
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
// Settings button start
Button settingsButton = (Button) findViewById(R.id.btnSettings);
settingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startSettings();
}
});
// Set A Button start
Button setAButton = (Button) findViewById(R.id.btnSetA);
setAButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneA();
}
});
// Set B Button start
Button setBButton = (Button) findViewById(R.id.btnSetB);
setBButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view) {
setzoneB();
}
});
}
public void startSettings() {
Intent launchSettings = new Intent(this, SettingsScreen.class);
startActivity(launchSettings);
// Settings button end
}
public void setzoneA(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
// Set A Button end
}
public void setzoneB(){
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("Zone Set");
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Ok",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
//dismiss the dialog
}
});
}
// Set B Button end
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
I just picked up the android SDK and eclipse and decided to write a simple dialog that pops up when you click a button, however I found out that the showDialog() method has been deprecated and that the DialogFragment was the best way to go about making one, so here it is:
package net.learn2develop.dialog;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.FragmentManager;
import android.view.View;
import android.widget.Button;
public class DialogWindow extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_window);
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
class MyDialog extends DialogFragment {
FragmentManager fm = getFragmentManager();
public MyDialog() {
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
//using the builder class
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Simple Dialog")
.setPositiveButton("nothing", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getBaseContext(), "ok then", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getBaseContext(), "cancel", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
MyDialog md = new MyDialog();
md.show(md.fm, "dialog");
}
});
}
}
The Debugger shows the error at:
md.show(md.fm, "dialog");
On further inspection, the fm variable is null according to the variables tab in the debugger, why is this so and is there a more efficient solution to this? I am seriously new to android, sorry if this is an easy question.
You should use an AlertDialog in this case, not a DialogFragment. Here is how I would do it:
public class DialogWindow extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.dialog_window);
Button btn = (Button) findViewById(R.id.btn_dialog);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
createDialog.show();
}
});
}
public Dialog createDialog() {
//using the builder class
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage("Simple Dialog")
.setPositiveButton("nothing", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//getBaseContext() is not advised (I can't remember why right now but I know I read it. You may want to read more that method but for now getActivity will work for your Toast.
//Toast.makeText(getActivity(), "ok then", Toast.LENGTH_SHORT).show();
}
})
.setNegativeButton("cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
//Toast.makeText(getActivity(), "cancel", Toast.LENGTH_SHORT).show();
}
});
return builder.create();
}
}
DialogFragments should be used in conjunction with other Fragments, in the case of a single activity it would be best to use an AlertDialog.