I have created a AlertDialogue. I want that when I open the App Dialogue will Show. Which method should I try? I tried onStart method but its doesn't works.
Here is the code:
#Override
protected void onStart() {
super.onStart(); AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setCancelable(false);
builder.setTitle("Quiz");
builder.setMessage("Are You Sure?");
builder.setPositiveButton("ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(),"Ok was Clicked",Toast.LENGTH_SHORT).show();
finish();
}
})
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getApplicationContext(),"Ok was Clicked",Toast.LENGTH_SHORT).show();
finish();
}
});
}
On the Navigation Drawer I called a method . That method is created with a Alert Dialouge . When i click the item in Navigation Drawer its Shows 3-4 times continuously. i want that it will show only one time .
here is the code :
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
int id = item.getItemId();
if (id == R.id.con_id) {
convertPointsToMoney();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
I want to add a Facebook group link on the navigation item. When I click on that item the browser will open and show the site.
Put the AlertDialogueinside onCreate()
Related
I want to start a new activity when a user clicks on Nav Item named as a logout. I am not finding any suitable syntax or a proper way to do it
navController.addOnDestinationChangedListener(new NavController.OnDestinationChangedListener() {
#Override
public void onDestinationChanged(#NonNull NavController controller, #NonNull NavDestination destination, #Nullable Bundle arguments) {
int id=destination.getId();
if(id==R.id.nav_tools){
finish();
}
}
});
Try below code for open new activity from nav item click
if(id==R.id.nav_tools){
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
}
Intent settingIntent = new Intent(HomeActivity.this, SettingActivity.class);
startActivity(settingIntent);
}
This question already has answers here:
Custom Dialog Fragment
(2 answers)
Closed 4 years ago.
From Google Developer UI guide
You can accomplish a wide variety of dialog designs—including custom layouts and those described in the Dialogs design guide—by extending DialogFragment and creating a AlertDialog in the onCreateDialog() callback method.
For example, here's a basic AlertDialog that's managed within a DialogFragment:
What?
public class ResetAll extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.reset)
.setPositiveButton(R.string.reset2, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// code
}
});
return builder.create();
}
}
Use here the Class?
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// USE HERE! DIALOG
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.action_settings) {
// USE HERE! DIALOG
ResetAll dialog = new ResetAll();
dialog.show(getSupportFragmentManager(), "restFragmentDialog");
// restFragmentDialog is a unique tag name that the system uses to save and restore the fragment state when necessary.
// The tag also allows you to get a handle to the fragment by calling findFragmentByTag().
}
return super.onOptionsItemSelected(item);
}
From Google UI link mentioned on question.
Showing a Dialog
When you want to show your dialog, create an instance of your
DialogFragment and call show(), passing the FragmentManager and a tag
name for the dialog fragment.
You can get the FragmentManager by calling getSupportFragmentManager()
from the FragmentActivity or getFragmentManager() from a Fragment.
I am trying to develop a small app as a part of my exam. Basically, I have a button that goes to a webpage and I want an alert dialog to pop up when the button is clicked and before the web page opens. This is what I have so far:
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ButterKnife.bind(this);
webViewAndroidWeekly();
}
private void webViewAndroidWeekly (){
Button btnWebpage = findViewById(R.id.btnwebpage);
btnWebpage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.addCategory(Intent.CATEGORY_BROWSABLE);
intent.setData(Uri.parse("http://androidweekly.net/"));
startActivity(intent);
}
});
}
#Override
protected void onStop() {
super.onStop();
Toast.makeText(this, "Thank you for using this app! :)", Toast.LENGTH_SHORT).show();
finish();
}
}
The button works and it opens the webpage so that part is set. I created a separate java class for the alert dialog and tried to implement it to work but I can't figure it out.
Dialog class:
public class InternetWarningDialog extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setIconAttribute(android.R.attr.alertDialogIcon);
builder.setTitle("Go to WebPage");
builder.setMessage("Are you sure you want to proceed to webpage? This may incur additional charges based on your mobile data plan!");
builder.setCancelable(false);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
getActivity().finish();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alertDialog = builder.create();
return alertDialog;
}
}
create this method in mainActivity ...... Call this showDialog() method in your main Activity where you want
public void showDialog(){
InternetWarningDialog exampleDialog = new InternetWarningDialog();
exampleDialog.show(getFragmentManager(),"Internet Dialog");
}
If its works accept the answer
You have created the AlertDialog, but to get the Alert dialog view use the method show() on AlertDialog.Builder as shown below
AlertDialog alertDialog = builder.create();
builder.show();
I cannot see any call for AlertDialog. You need to add the call for AlertDialog along with AlertDialog.show() and then open the webpage if the user clicks on Yes.
AlertDialog alertDialog = builder.create();
builder.show();
A familiar question has already been asked before. Please refer this
open a dialog when i click a button
I've created a DialogFragment [implemented as AlertDialog with OnCreateDialog(Bundle)].
The DialogFragment asks for the user to input a project name (String) via an EditText box, and I'm trying to pass this back to the MainActivity.
In MainActivity, I use a Toast to check if the String has indeed been passed along. This check fails as nothing is passed. However, if I hardcode the String in my DialogFragment, it works. It leads me to suspect that there is a problem with the way I try to locate the EditText object, but I'm not sure what my mistake is.
MainActivity.java
public class MainActivity extends AppCompatActivity implements NewProjectDialog.NewProjectDialogListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//initialize the two on screen buttons
//onclick listeners are attached to activity_main.xml
Button newProject = (Button) findViewById(R.id.new_project);
Button browseProjects = (Button) findViewById(R.id.browse_projects);
}
#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 newProject(View view) {
//Open up the DialogFragment that prompts user for the title
DialogFragment newFragment = new NewProjectDialog();
newFragment.show(getFragmentManager(), "New Project Dialog");
}
#Override
public void onDialogOK(String projectTitle) {
//Toast.makeText(MainActivity.this, projectTitle, Toast.LENGTH_SHORT).show();
}
#Override
public void onDialogCancel() {
//user pressed cancel in NewProjectDialog
Toast.makeText(MainActivity.this, "Cancelled", Toast.LENGTH_SHORT).show();
}
public void browseProjects(View view){
Toast.makeText(MainActivity.this, "Browse Projects", Toast.LENGTH_SHORT).show();
}
NewProjectDialog.java
public class NewProjectDialog extends DialogFragment{
/* The activity that creates an instance of this dialog fragment must
* implement this interface in order to receive event callbacks.
* Each method passes the DialogFragment in case the host needs to query it. */
public interface NewProjectDialogListener {
public void onDialogOK(String projectTitle);
public void onDialogCancel();
}
// Use this instance of the interface to deliver action events
NewProjectDialogListener mListener;
// Override the Fragment.onAttach() method to instantiate the NoticeDialogListener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// Verify that the host activity implements the callback interface
try {
// Instantiate the NoticeDialogListener so we can send events to the host
mListener = (NewProjectDialogListener) activity;
} catch (ClassCastException e) {
// The activity doesn't implement the interface, throw exception
throw new ClassCastException(activity.toString()
+ " must implement NoticeDialogListener");
}
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder newProjectDialog = new AlertDialog.Builder(getActivity());
//prevent dialog from closing
setCancelable(false);
//set dialog title
newProjectDialog.setTitle(R.string.new_project_title);
//inflate view so that findViewbyId on the next line works
View view = View.inflate(getActivity(),R.layout.new_project_dialog, null);
//Link tempEdit object to the text-edit box so we can retrieve data from it below upon button click
final EditText tempEdit = (EditText)view.findViewById(R.id.project_title);
//set the view
newProjectDialog.setView(R.layout.new_project_dialog);
//set OK button
newProjectDialog.setPositiveButton(R.string.ok, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
//Toast.makeText(getActivity(), tempEdit.getText().toString(), Toast.LENGTH_SHORT).show();
mListener.onDialogOK(tempEdit.getText().toString());
}
});
//set cancel button
newProjectDialog.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
mListener.onDialogCancel();
}
});
// Create the AlertDialog object and return it
return newProjectDialog.create();
}
You're right, the problem is your taking an EditText from a different view. Because your layout is being inflated twice.
Try using setView(View) instead of the one with the layout resource id.
i want to show my menu with action bar, but my menu won't display, this is my source code :
public class EpolicyMainActivity extends TabActivity {
/** Called when the activity is first created. */
public void onCreate(Bundle savedInstanceState) {
//hide title bar
BasicDisplaySettings.toggleTaskBar(EpolicyMainActivity.this, false);
//show status bar
BasicDisplaySettings.toggleStatusBar(EpolicyMainActivity.this, true);
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
Resources res = getResources(); // Resource object to get Drawables
TabHost tabHost = getTabHost(); // The activity TabHost
TabHost.TabSpec spec; // Resusable TabSpec for each tab
Intent intent; // Reusable Intent for each tab
// Create an Intent to launch an Activity for the tab (to be reused)
intent = new Intent().setClass(this, LoginActivity.class);
spec = tabHost.newTabSpec("Login").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_home))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, NABActivity.class);
spec = tabHost.newTabSpec("NAB").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_nab))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, ContactActivity.class);
spec = tabHost.newTabSpec("Contact").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_contact))
.setContent(intent);
tabHost.addTab(spec);
intent = new Intent().setClass(this, AgenActivity.class);
spec = tabHost.newTabSpec("Agen").setIndicator("",
res.getDrawable(R.drawable.epolicy_menu_xml_agen))
.setContent(intent);
tabHost.addTab(spec);
for (int i = 0; i < tabHost.getTabWidget().getChildCount(); i++){
tabHost.getTabWidget().getChildAt(i).setPadding(0,0,0,0);
tabHost.getTabWidget().getChildTabViewAt(i).setBackgroundDrawable(null);
}
tabHost.setCurrentTab(0);
}
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return super.onCreateOptionsMenu(menu);
}
public boolean onOptionsItemSelected(MenuItem item) {
super.onOptionsItemSelected(item);
switch (item.getItemId()) {
case R.id.menu_bantuan:
Intent itAbout = new Intent(EpolicyMainActivity.this, EpolicyBantuan.class);
itAbout.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itAbout);
break;
case R.id.menu_exit:
dialogExit();
break;
case R.id.menu_logout:
dialogSignOut();
break;
}
return true;
}
public void dialogSignOut()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah anda ingin sign-out?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent itSignOut = new Intent(EpolicyMainActivity.this, LoginActivity.class);
itSignOut.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSignOut);
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
public void dialogExit()
{
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Apakah anda ingin keluar?")
.setCancelable(false)
.setPositiveButton("Ya", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent itSplashEnd = new Intent(EpolicyMainActivity.this, SplashOutActivity.class);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
itSplashEnd.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(itSplashEnd);
finish();
}
})
.setNegativeButton("Tidak", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
#Override
public void onBackPressed() {
dialogExit();
}
this is my menu.xml :
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="#+id/menu_bantuan"
android:title="#string/menu_bantuan"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText"/>
<item android:id="#+id/menu_exit"
android:title="#string/menu_exit"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText" />
<item android:id="#+id/menu_logout"
android:title="#string/menu_logout"
android:orderInCategory="100"
android:showAsAction="ifRoom|withText" />
in my main layout, i'm using header, is this giving effect to my menu, so my menu won't display or anything else?
I was under the assumption that you should always first call super.
public boolean onCreateOptionsMenu(Menu menu)
{
super.onCreateOptionsMenu(menu);
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu, menu);
return true;
}
I am assuming your menu.xml has an appropriate menu closing tag. Do you have this xml file stored in your res/menu folder? Can you verify that onCreateOptionsMenu is called?
On Android 2.3 and lower you will have to press the menu button, whereas in later distributions it should be displayed in the title bar. Since you are using a TabActivity I assume you are writing an app for Android 2.3.
This question might be useful for the onMenuItemSelected() method:
Merge TabActivity menu with contained Activities menus
For Activities within you TabActivity: How to create an optionsMenu in an Android's TabActivity
Perhaps when you press the menu button, the options menu of the specific tab activity that you are in (e.g. LogInActivity) is called, and not that of its parent. Try putting this code in every subactivity:
public boolean onCreateOptionsMenu(Menu menu)
{
return getParent().onCreateOptionsMenu(menu);
}
Google decided to avoid using menu at all. Please read this article http://android-developers.blogspot.ru/2012/01/say-goodbye-to-menu-button.html
If device doesn't have hardware menu button and you set targetSDK version > 10 user does not have ability to use menu. As short term solution you can set targetSDK = 10 as long term solution consider using ActionBar http://developer.android.com/guide/topics/ui/actionbar.html