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();
}
});
}
}
Related
I am working on an application where I would like to do a search via EditText. I have a few buttons and I would like to make it so that when text is entered into the EditText field, it automatically searches for the Text in that button. I'm using navigation_drawer, so the code is again a bit more complicated. I just wanted to ask if it's possible to search the text of a button and if so how.
What I want to do is that the text that is entered into EditText will act as a search, that is, the buttons with text that match the text that is in EditText will show up and the rest will disappear.
fragment_morning.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
android:id="#+id/fragment_morning"
tools:context="com.example.mcdonaldscoupons.MorningFragment">
<EditText
android:id="#+id/text_input_search"
android:layout_width="260dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="125dp"
android:hint="Zadej název kupónu..."
android:imeOptions="actionSearch"
android:inputType="text"
android:maxLength="100"
android:maxLines="1" />
<Button
android:id="#+id/morning1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_input_search"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:text="McMuffin"
android:textAllCaps="false" />
<Button
android:id="#+id/morning2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/morning1"
android:layout_alignParentStart="true"
android:layout_alignParentLeft="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginStart="10dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="5dp"
android:layout_marginEnd="10dp"
android:layout_marginRight="10dp"
android:textAllCaps="false"
android:text="McMuffin Menu" />
</RelativeLayout>
MorningFragment.java
package com.example.mcdonaldscoupons;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.Toast;
import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;
public class MorningFragment extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_morning, container, false);
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
editText = view.findViewById(R.id.text_input_search);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
//Here goes code, right?
}
});
view.findViewById(R.id.morning1).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Typ kupónu");
builder1.setMessage("Jak chceš kupón zobrazit?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Kód (Jen u tabletu)",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Kód");
builder1.setMessage("Zadej prosím tento kód:\n132 900 23");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Hotovo!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "GRATULUJU a přeji dobrou chuť :P",Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton(
"Nefunguje",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Smůla :/\nMůžeš zkusit McDonald's aplikaci!",Toast.LENGTH_LONG).show();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
builder1.setNegativeButton(
"Kupón z aplikace",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Coming soon...",Toast.LENGTH_SHORT).show();
}
});
builder1.setNeutralButton(
"Zrušit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
view.findViewById(R.id.morning2).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Typ kupónu");
builder1.setMessage("Jak chceš kupón zobrazit?");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Kód (Jen u tabletu)",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
AlertDialog.Builder builder1 = new AlertDialog.Builder(getContext());
builder1.setTitle("Kód");
builder1.setMessage("Zadej prosím tento kód:\n132 852 34");
builder1.setCancelable(true);
builder1.setPositiveButton(
"Hotovo!",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "GRATULUJU a přeji dobrou chuť :P",Toast.LENGTH_SHORT).show();
}
});
builder1.setNegativeButton(
"Nefunguje",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Smůla :/\nMůžeš zkusit McDonald's aplikaci!",Toast.LENGTH_LONG).show();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
builder1.setNegativeButton(
"Kupón z aplikace",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
Toast.makeText(getContext(), "Coming soon...",Toast.LENGTH_SHORT).show();
}
});
builder1.setNeutralButton(
"Zrušit",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
}
});
}
}
List<String> textContent = new ArrayList();
Button button1 = findViewById(R.id.morning1);
Button button2 = findViewById(R.id.morning2);
textContent.clear();
textContent.add(button1.getText());
textContent.add(button2.getText());
And then in your
#Override
public void afterTextChanged(Editable s) {
if(textContent.contains(editText.getText().toString().trim().toLowerCase()))
//do your work
}
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
Hello I m newbie in Android development.
So i practiced with one sample which is sending ussd code to operator such as recharging bill. I am okay with that.
But my main problem is to show dialog box when clicking button1 after filling PIN code to examine the user correct or not.
I don't know how to add and what to add.
My codes are -
for MainActivity.java
package com.messagealert;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
[MENTION=439709]override[/MENTION]
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
}
[MENTION=439709]override[/MENTION]
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;
}
}
for activity_main.xml
<RelativeLayout xmlns:android="h t t p://schemas. android. com/apk/res/android"
xmlns:tools="h t t p:// schemas. android. com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_marginTop="22dp"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="number"
android:maxLength="6"
android:hint="Enter 6 numbers here"
>
<requestFocus />
</EditText>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_marginTop="31dp"
android:layout_centerHorizontal="true"
android:text="#string/button1" />
</RelativeLayout>
Please guide what i need to put & where exactly to show a dialog box "Is that your PIN code - xxxxxx" & Yes or No
Sorry for my bad English.
With Best Regards,
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(this);
dlgAlert.setMessage("PIN VERIFICATION");
dlgAlert.setTitle("Is that your PIN code -"+pin );
dlgAlert.setPositiveButton("OK", null);
dlgAlert.setCancelable(true);
dlgAlert.create().show();
dlgAlert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
dlgAlert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
editText1 .setText(" ");
}
});
For this you have to add alert box Just like the below code
new AlertDialog.Builder(this)
.setTitle("Delete entry")
.setMessage("Are you sure you want to delete this entry?")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// continue with delete
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.setIcon(android.R.drawable.ic_dialog_alert)
.show();
or try this
AlertDialog.Builder builder1 = new AlertDialog.Builder(context);
builder1.setMessage("Write your message here.");
builder1.setCancelable(true);
builder1.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
builder1.setNegativeButton("No",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
Just add dialog in your button click only
EditText editText1 =(EditText) findViewById(R.id.editText1);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
//call dialog here and on dialog ok press start your activity
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
Use code below, this will help you:
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
dialog();
}
});
public void dialog()
{
AlertDialog.Builder dialog = new Builder(con);
dialog.setTitle("Message:");
dialog.setMessage("Is that your PIN code - xxxxxx");
dialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
dialog.dismiss();
}
});
dialog.show();
}
use variable global if needed.
use an alertDialog for that inside you button onClick
AlertDialog.Builder dialog = new Builder(con);
dialog.setTitle("Confirm PIN");
dialog.setMessage("You have entered pincode as"+pin+"\n Please Confirm to Continue");
dialog.setNegativeButton("Edit Pin", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.setPositiveButton("Confirm", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String encodedHash = Uri.encode("#");
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
dialog.dismiss();
}
});
dialog.show();
Firstly, I am very thank you for your answers.
Sorry i can't vote up your answers because i haven't enough reputations.
By adapting MSS's answers i changed a bit at MainActivity.java
package com.messagealert;
import android.net.Uri;
import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
public class MainActivity extends Activity {
final Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new OnClickListener(){
public void onClick (View v){
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
AlertDialog.Builder dlgAlert = new AlertDialog.Builder(context);
dlgAlert.setTitle("PIN VERIFICATION");
dlgAlert.setMessage("Is that your PIN code -" + pin );
dlgAlert.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
String encodedHash = Uri.encode("#");
EditText editText1 =(EditText) findViewById(R.id.editText1);
String pin = editText1.getText().toString();
String ussd = "*" + "124" + pin + encodedHash;
Intent button1 = new Intent(android.content.Intent.ACTION_CALL, Uri.parse("tel:" + ussd));
startActivity(button1);
}
});
dlgAlert.setNegativeButton("NO",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
EditText editText1 =(EditText) findViewById(R.id.editText1);
editText1 .setText(" ");
}
});
dlgAlert.setCancelable(true);
dlgAlert.create().show();
};
});
}}
You guys did a great help on me!
With Regards,
I created a DialogFragment that looks like this:
package com.name.test;
import android.app.AlertDialog;
import android.app.Dialog;
import android.app.DialogFragment;
import android.content.DialogInterface;
import android.os.Bundle;
import android.text.Editable;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.EditText;
import android.widget.Toast;
import com.name.test.MainActivity.DialogType;
public class AlertDialogFragment extends DialogFragment {
public static DialogType type;
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
View v = getActivity().getLayoutInflater().inflate(R.layout.search_fragment, null);
final EditText searchField = (EditText)v.findViewById(R.id.searchEditTxt);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
switch(type) {
case SEARCH:
builder.setView(inflater.inflate(R.layout.search_fragment, null))
.setTitle("Search Dialog")
.setPositiveButton("Search", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
// Toast.makeText(getActivity().getBaseContext(), "Search Dialog", Toast.LENGTH_SHORT).show();
String tempString = searchField.getText().toString();
System.out.println(tempString);
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
case PREFERENCES:
builder.setView(inflater.inflate(R.layout.preferences_fragment, null))
.setTitle("Preferences")
.setPositiveButton("Save", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the search button
Toast.makeText(getActivity().getBaseContext(), "Preferences", Toast.LENGTH_SHORT).show();
}
})
//.setView(searchField)
.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// logic for the cancel button
AlertDialogFragment.this.getDialog().cancel();
}
});
break;
default:
break;
}
return builder.create();
}
public static AlertDialogFragment newInstance(DialogType dialogType) {
type = dialogType;
return new AlertDialogFragment();
}
}
In the XML file for this fragment I have this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:tag="search_fragment"
android:orientation="vertical" >
<EditText
android:id="#+id/searchEditTxt"
android:hint="#string/search_menu_hint"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I'm trying to target that EditText in the DialogFragment so when the user types something and clicks on Save or OK I can store its value. I can't seem to be able to target the EditText properly. My System.out.println(tempString); is empty. What am I doing wrong? Thanks much.
try to change
builder.setView(inflater.inflate(R.layout.search_fragment, null));
to
builder.setView(v);
You just need to set your inflated view to your builder.
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