Alert Dialog Does not apper - java

When i press the Button the alert dialog does not appear here is the code:
Please could you tell me whats wrong with it, and what i can do to fix it.
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_item);
Button bDR = (Button) findViewById(R.id.bDR);
bDR.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(DeleteRenameList.this);
builder.setCancelable(true);
builder.setMessage("Would you like to delete or rename the list?");
builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog alert = builder.create();
alert.show();
}
});
Plese Help

Try this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.row_item);
Button bDR = (Button) findViewById(R.id.bDR);
bDR.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new
AlertDialog.Builder(DeleteRenameList.this).create();
builder.setCancelable(true);
builder.setMessage("Would you like to delete or rename the list?");
builder.setNegativeButton("Delete", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
builder.setNeutralButton("Rename", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
AlertDialog.show();
}
});

Try the following format:
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends Activity {
Context context = this;
Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonAlert);
// add button listener
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
context);
// set title
alertDialogBuilder.setTitle("Your Title");
// set dialog message
alertDialogBuilder
.setMessage("Would you like to delete or rename the list?")
.setCancelable(false)
.setPositiveButton("Delete",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
})
.setNegativeButton("Rename",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
}
});
// create alert dialog
AlertDialog alertDialog = alertDialogBuilder.create();
// show it
alertDialog.show();
}
});
}
}
Source

Related

This is my first Android program using Java, I am trying to use a alert dialog, but the app keeps on closing

So belove is my code, in the design i have one textview with the ID t. I am using android 5.0 to build and i have the required SDK(android 5 and 5.1).
package com.example.alertdialogbox;
import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.app.AlertDialog;
import android.os.Bundle;
import android.content.DialogInterface;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView t1=findViewById(R.id.t);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
AlertDialog.Builder b1=new AlertDialog.Builder(this);
b1.setTitle("Alert Dialog");
b1.setMessage("Do you want to continue?");
b1.setCancelable(false);
b1.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked YES");
}
});
b1.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked NO");
}
});
b1.setNeutralButton("CANCEL", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
t1.setText("Clicked CANCEL");
}
});
AlertDialog d=b1.create();
d.show();
}
}
Sorry if my question looks silly :)
Thank you
call TextView t1=findViewById(R.id.t); after setContentView(R.layout.activity_main);

How do I copy and paste text from EditText of alertdialog to EditText of my activity, on button click?

I searched but the questions I could see dealt with just copy, or copying to clipboard, or just pasting. Specifically what I want (in 1 button click, the PositiveButton in AlertDialog) is to copy the text entered by user in the EditText of my alertdialog to the EditText of my Activity.
Can you tell me how to do this please? Here is the code I am using and trying to fix:
//when user touches on "commentname" edittext we want the alertdialog to open
commentname.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if(event.getAction() == MotionEvent.ACTION_UP) {
AlertDialog.Builder builder = new AlertDialog.Builder(NewContact.this);
builder.setTitle("Ur Comment:");
//start the following xml file/ layout
View viewInflated = LayoutInflater.from(NewContact.this).inflate(R.layout.comment_text_pop_up, null, false);
builder.setView(viewInflated);
// Set up the buttons
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
//we want to copy the text entered in "input", in the alertdialog,
//and paste it in commentname
commentname.setText(alertdialog_edittext.getText().toString());
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
alertdialog_edittext = (EditText) dialog.findViewById(R.id.input);
dialog.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE);
dialog.show();
return true;
}
return false;
}
});
i wrote this simple code example for you to do it.
just add method to setText on Your edittext in your Activity:
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
when user click in dialog get text from edittext dialog and pass using this method:
setTextFromDialog(YouEditTextValueX);
here code example:
import android.content.DialogInterface;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends AppCompatActivity {
private EditText myEditText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button ShowDialog = findViewById(R.id.showdialog_id);
myEditText = findViewById(R.id.editText_id);
ShowDialog.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder alert = new AlertDialog.Builder(MainActivity.this);
final EditText edittext = new EditText(MainActivity.this);
alert.setTitle("Title");
alert.setMessage("Message");
alert.setView(edittext);
alert.setPositiveButton("Set text", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
String YouEditTextValueX = edittext.getText().toString();
if(YouEditTextValueX.length() > 0){
//this line for call method and pass the text
setTextFromDialog(YouEditTextValueX);
}
}
});
alert.setNegativeButton("Exit", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// what ever you want to do with No option.
}
});
alert.show();
}
});
}
private void setTextFromDialog(final String textFromDialog){
myEditText.setText(textFromDialog);
}
}
hope this help you

Android AlertDialog with a common message for 3 buttons

Please help me with my code
I need to show an Android AlertDialogBox with a common message for 3 buttons, i.e Yes, No, And Cancel. As in this I have used simple Android AlertDialog with a message for individual 3 buttons, i.e. Yes, No and Cancel.
So I think there is need of function to be call.
The layout and Java part are given below
Layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.talent.simpledialogbox.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="/hello_world"
android:id="#+id/textView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Yes"
android:layout_above="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:id="#+id/Yes"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_above="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:id="#+id/Cancel"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="No"
android:layout_above="#+id/textView"
android:layout_alignLeft="#+id/textView"
android:layout_alignStart="#+id/textView"
android:id="#+id/No"/>
</RelativeLayout>
Java Part
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
builder.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
builder.show();
}
});
}
}
Assign AlertDialog object and assign it to the buider.create()
package com.talent.simpledialogbox;
import android.os.Bundle;enter code here
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.support.annotation.RequiresPermission;
import android.view.Menu;
import android`enter code here`.view.View;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
Button Yes,No, Cancel;
Object myObject = new Object();
RequiresPermission.Read FunctionCalling;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Yes = (Button)findViewById(R.id.Yes);
No = (Button)findViewById(R.id.No);
Cancel= (Button)findViewById(R.id.Cancel);
Yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Delete...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
No.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm No)...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
Cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Confirm Cancel...");
builder.setMessage("Are you sure you want delete this?");
builder.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(getApplicationContext(), "You clicked on YES", Toast.LENGTH_SHORT).show();
}
});
builder.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on Cancel", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
AlertDialog alertDialog = builder.create();
alertDialog.show();
}
});
}
}

Android Pich zoom Image how add ImageButton listner

I have Code Image with Pich zoom
package com.androidtutorialpoint;
import com.androidtutorialpoint.imageview.PhotoView;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
public class MainActivity extends Activity {
private ViewPager mViewPager;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mViewPager = new HackyViewPager(this);
setContentView(mViewPager);
mViewPager.setAdapter(new SamplePagerAdapter());
}
static class SamplePagerAdapter extends PagerAdapter {
private static int[] sDrawables = {
R.drawable.ic_launcher,R.drawable.ic_launcher,
};
#Override
public int getCount() {
return sDrawables.length;
}
#Override
public View instantiateItem(ViewGroup container, int position) {
PhotoView photoView = new PhotoView(container.getContext());
photoView.setImageResource(sDrawables[position]);
// Now just add PhotoView to ViewPager and return it
container.addView(photoView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
return photoView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((View) object);
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
}
}
I have add imageButton listner ale marker with function switching to a new activity
My Image button lister have Code
public class MainActivity extends Activity {
private ImageView mainBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mainBtn = (ImageView) findViewById(R.id.button);
mainBtn.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openAlert(v);
}
});
}
private void openAlert(View view) {
AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(MainActivity.this);
alertDialogBuilder.setTitle(this.getTitle()+ " ");
// set positive button: Yes message
alertDialogBuilder.setPositiveButton("więcej",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// go to a new activity of the app
Intent positveActivity = new Intent(getApplicationContext(), PositiveActivity.class);
startActivity(positveActivity);
}
});
// set negative button: No message
alertDialogBuilder.setNegativeButton("wyjście",new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int id) {
// cancel the alert box and put a Toast to the user
dialog.cancel();
Toast.makeText(getApplicationContext(), "Mapa Budnik",
Toast.LENGTH_LONG).show();
}
});
AlertDialog alertDialog = alertDialogBuilder.create();
// show alert
alertDialog.show();
}
}
My Ask how to add a tag to the image Button and when you click the trigger Activity?
When my Image Button is in layaut.xml there is invisible.please help me

Nothing happens when I click on the "Check For BusyBox" button

I made an application that checks for root using RootTools. Now, I added an option to check for BusyBox availability. When I click on "Check For BusyBox", nothing happens, though the "Check For Root" is working perfectly. I don't understand why is it happening. Please help!
package com.maverick.checkforroot;
import com.stericson.RootTools.RootTools;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView Manufacturer = (TextView) findViewById(R.id.Manufacturer);
String Manu = android.os.Build.MANUFACTURER;;
Manufacturer.setText(Manu);
TextView tv1 = (TextView) findViewById(R.id.tv1);
String Model = android.os.Build.MODEL;
tv1.setText(Model);
TextView Product = (TextView) findViewById(R.id.Product);
String Pro = android.os.Build.PRODUCT;;
Product.setText(Pro);
Button Root = (Button) findViewById(R.id.Root);
Root.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isAccessGiven()) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Congratulations!");
builder.setMessage("You Have Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
else {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setIcon(R.drawable.ic_launcher);
builder.setTitle("Oops!");
builder.setMessage("No Root Access!");
builder.setPositiveButton("OKAY", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
Button BusyBox = (Button) findViewById(R.id.BusyBox);
BusyBox.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (RootTools.isBusyboxAvailable()) {
Toast.makeText(MainActivity.this,"No BusyBox!", Toast.LENGTH_LONG).show();
} else {
Toast.makeText(MainActivity.this,"BusyBox Is Available!", Toast.LENGTH_LONG).show();
}
}
});
}
});
}}
All the code concerning the BusyBox button (inclusive the definition of its OnClickListener) is written inside the Root buttons OnClickListener code. Move the code and it should work for you.

Categories

Resources