Here I use this Activatedialog method in onPostExecute inside registration >activity , and need to start a login activity when the alert dialog dissmiss ..however I tried to do this, but I get error.
public void Activatedialog(String jsonmsg, final String name)
{
final AlertDialog.Builder alert = new AlertDialog.Builder(ct);
alert.setTitle("Activate Account");
alert.setMessage(jsonmsg);
// Set an EditText view to get user input
final EditText input = new EditText(ct);
alert.setView(input);
alert.setCancelable(true);
Log.d("MYLOG","before +ve button click");
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
String code = input.getText().toString();
Log.d("MYLOG","before try");
try{
List<NameValuePair> params = new ArrayList<NameValuePair>();
params.add(new BasicNameValuePair("u",name ));
params.add(new BasicNameValuePair("code",code ));
JSONObject json = jsonParser.makeHttpRequest(ACTIVATION_URL,"POST", params);
Log.d("MYLOG","after request for json");
stcode = json.getJSONObject("activate").getInt("statuscode");
stmsg = json.getJSONObject("activate").getString("statusmessage");
Log.d("MYLOG","after json parsing : json data is "+stmsg+"stcodeis "+stcode);
if(stcode == 1)
{
dialog.dismiss();
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Intent i = new ("LOGIN");startActivity(i);![i get this error][1]
Toast.makeText(ct, " "+stmsg, Toast.LENGTH_SHORT).show();
}
else if(stcode == 0)
{
Log.d("MYLOG"," in check json data is "+stmsg+" stcodeis "+stcode);
Toast.makeText(ct, " "+stmsg+" "+stcode, Toast.LENGTH_SHORT).show();
}
}
catch(JSONException e)
{
e.printStackTrace();
Toast.makeText(ct, ""+e, Toast.LENGTH_LONG).show();
}
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int whichButton)
{
// Canceled.
Toast.makeText(ct, "Your Account is not Activated.Login to Activate", Toast.LENGTH_LONG).show();
}
});
alert.show();
}
}
To start an Activity, the intent must have following parameters. A context and target activity. Lets start activity "Login.java" .
Intent i = new Intent(getApplicationContext(), Login.class);
startActivity(i);
you have created intent incorrectly.
1> add the relevant activity you want to open say Loginby adding new activity to the project.
2>create the intent as below
Intent intent = new Intent(getApplicationContext(), Login.class);
3>start activity using the intent created
startActivity(i);
You can do something like this:
AlertDialogManager alert = new AlertDialogManager(StartActivity.this);
AlertDialogManager constructor:
public void AlertDialogManager(final Context context) {
activity = (Activity) context;
}
and you can launch the activity like this:
Intent intent = new Intent(activity , Login.class);
activity.startActivity(intent);
Related
I am creating an auto caller app in which i have used a button which takes data from arrayList, iterate through the arraylist and ask user on each element if want to do next call or pause on that for this i have used alert dialog, but the loop is not getting pause on each element it went to the last element and show alert dialog for that contact number.
private List<ContactEntity> mContactsList = new ArrayList<>();
private ContactEntity c;
private int i = 0;
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
if (id == R.id.start_call) {
Toast.makeText(this, "Call Started" + mContactsList.size(), Toast.LENGTH_LONG).show();
startAutoCall();
}
return super.onOptionsItemSelected(item);
}
private void startAutoCall() {
if (mContactsList.isEmpty()) {
Toast.makeText(this, "Import Contacts ", Toast.LENGTH_LONG).show();
} else {
c = mContactsList.get(i);
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
while(i < mContactsList.size()){
c = mContactsList.get(i);
Log.d(TAG, "startAutoCall: " + c.getId());
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Auto Dialer Start");
builder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toast.makeText(MainActivity.this, "hello", Toast.LENGTH_LONG).show();
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:" + c.getPersonContactNumber()));
startActivity(intent);
}
});
AlertDialog dialog = builder.create();
dialog.show();
i++;
}
}
}
It's not a good idea to build alert dialog inside a loop. Instead, create one and update the value for each element in the list on button click. Override onClickListener for the dialog so that you can control the flow better.
Here's a snippet in kotlin.
if(mContactList.isNotEmpty()){
c = mContactList.get(i)
val builder = AlertDialog.Builder(this)
builder.setTitle("Auto Dialer Start")
builder.setMessage("Your message here...")
builder.setPositiveButton("OK", null)
val dialog = builder.create()
dialog.setOnShowListener{ dialogInterface->
val btnOk = dialog.getButton(AlertDialog.BUTTON_POSITIVE)
btnOk.setOnClickListener{
//Your code here...
if(i == mContactList.size-1){
dialogInterface.dismiss()
}else {
i++
c = mContactList.get(i)
}
}
}
dialog.show()
}
Here let me explain my issue need to add volley inside alertdialog which i have done successfully its like pinging the entered Url its working fine but what I need to do is in onErrorResponse method alert dialog gets hide suppose when user enter wrong url it will go to onErrorResponse method in this my alertdialog gets hide need to stay awake the dialog even when error gets occurred so far what I have tried is:
This is my code:
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
alert.setTitle("Add Self Hosted URL "); //Set Alert dialog title here
alert.setMessage("Enter The Url Here"); //Message here
final EditText input = new EditText(context);
alert.setView(input);
ConnectivityManager cn = (ConnectivityManager) PingingActivity.this.getSystemService(Context.CONNECTIVITY_SERVICE);
NetworkInfo nf = cn.getActiveNetworkInfo();
if (nf != null && nf.isConnected()) {
final ProgressDialog progressDialog=new ProgressDialog(this);
progressDialog.hide();
Toast.makeText(getApplicationContext(), "Network Available", Toast.LENGTH_LONG).show();
alert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
public void onClick(final DialogInterface dialog, int whichButton) {
progressDialog.show();
progressDialog.setMessage("Pinging Please Wait");
final String srt = input.getEditableText().toString();
RequestQueue queue = Volley.newRequestQueue(getApplicationContext());
StringRequest stringRequest = new StringRequest(Request.Method.GET, srt,
new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("resp", response);
SharedPreferences settings = getSharedPreferences(PingingActivity.id, 0); // 0 - for private mode
SharedPreferences.Editor editor = settings.edit();
editor.putString("Url", srt);
editor.putBoolean("URLflag", true);
editor.apply();
Intent intent = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(intent);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
// alert.show();
// input.setText(srt);
//final String srt = input.getEditableText().toString();
String str = error.toString();
if (str != null) {
Toast.makeText(getApplicationContext(), "Please Check the Entered URL", Toast.LENGTH_SHORT).show();
// progressDialog.cancel();
}
}
});
// Add the request to the RequestQueue.
queue.add(stringRequest);
stringRequest.setRetryPolicy(new DefaultRetryPolicy(5000,
DefaultRetryPolicy.DEFAULT_MAX_RETRIES,
DefaultRetryPolicy.DEFAULT_BACKOFF_MULT));
Toast.makeText(context, srt, Toast.LENGTH_LONG).show();
}
});
alert.setNegativeButton("CANCEL", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
dialog.cancel();
}
}).setIcon(R.drawable.ic_info_black_24dp).show(); //End of alert.setNegativeButton
/* Alert Dialog Code End*/
}
else {
showAlertDialog("No Network", "Please Check Your Network Connectivity", true);
}
even in onerrorresponse need to display my alertdialog how can i achieve this. It may be a dumb question but as a beginner am struggling with this can anyone help me
Create a bool method returning the status , call it
alert.setPositiveButton("OK", new DialogInterface.OnClickListener()
return true from response and false from onError()
if the response return success start your desired Activity , otherwise let the alert dialog show.
I am having a dialog which must execute some code on canceled. I set a negative button but its not executing when dialog is canceled from back button.
here is my code.
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setTitle("Licence expired");
builder.setCancelable(false);
builder.setMessage("Your licence for " + url
+ " has been expired, Please renew it or select another server");
builder.setPositiveButton("Renew now",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Open url in webview
Intent intent = new Intent(MainActivity.this,
WebActivity.class);
intent.putExtra("ServerDomain", url);
startActivity(intent);
/*
* URL domain; try { domain = new URL(url);
* intent.putExtra("ServerDomain", domain.getHost());
* startActivity(intent); } catch (MalformedURLException
* e) { e.printStackTrace(); }
*/
/*
* String url = "http://www.google.com"; Intent i = new
* Intent(Intent.ACTION_VIEW);
* i.setData(Uri.parse(url)); startActivity(i);
*/
}
});
builder.setNegativeButton("Switch Server",
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
showServerList();
}
});
builder.create().show();
Create a cancel listner for dialog.
builder.setOnCancelListener(new OnCancelListener() {
#Override
public void onCancel(DialogInterface dialog) {
// TODO Auto-generated method stub
showServerList();
}
});
I used the android alertdialog in order to redirect to a url, the redirection should go according to user choice, here is the code:
final CharSequence[]stringArray = {"1" ,"2" , "3"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (stringArray.toString().equals("1")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st));
startActivity(intent);
}
else if (stringArray.toString().contains("2")) {
Intent intent = new Intent(Intent.ACTION_VIEW,
Uri.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
but when I click 1 or 2 there is no redirection to the url
what is wrong with the code?
You are checking your arrays items which is not valid way to implement. You need to check for selected item's id which you can get from onclick method only.
As the array count starts from "0" so can check your selected item with "0 to 2" where your 0 will be considered as "1" selected and so on.
Check out below code:
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (item == 0)
// if (stringArray.toString().equals("1"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st));
startActivity(intent);
} else if (item == 1)
// else if (stringArray.toString().contains("2"))
{
Intent intent = new Intent(Intent.ACTION_VIEW, Uri
.parse(st2));
startActivity(intent);
}
}
});
AlertDialog alert = builder.create();
alert.show();
Please use following code:-
final CharSequence[] stringArray = { "1", "2", "3" };
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Press selected name");
builder.setItems(stringArray, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int position) {
String st = "https://www.youtube.com/watch?v=x9QyKNQ0uVc";
String st2 = "https://www.youtube.com/";
if (position == 0) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st));
startActivity(intent);
}
else if (position == 1) {
Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(st2));
startActivity(intent);
}
else if (position == 2) {
// write some code
}
}
});
AlertDialog alert = builder.create();
alert.show();
I have one DialogBox with 2 buttons. If we click the +ve button, it attempts to open the Dialing Pad; else if we click the -ve button it closes the dialog. When I click the +ve button it shows the null exception. If the same code executes without the DialogBox, it is fine.
Here is my code:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
I've given permissions in the manifest file as <uses-permission android:name="android.permission.CALL_PHONE"/>
Try this..
Intent call = new Intent(Intent.ACTION_DIAL);
call.setData(Uri.parse("tel:phonenumber");
startActivity(call);
Remove the #Overrride line.
Like this:
callDialog.setPositiveButton("Call Now", new android.DialogInterface.
OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
}
----UPDATE-----
Since you havent posted any logcat, its hard to know where it crashes. Try this block:
AlertDialog.Builder callDialog = new AlertDialog.Builder(this);
callDialog.setTitle("My title");
callDialog.setMessage("My message");
callDialog.setPositiveButton("Call", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
Intent dial = new Intent();
dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
}
});
callDialog.setNegativeButton("Cancel", null);
callDialog.show();
Try the following---
Intent dial = new Intent(Intent.ACTION_DIAL);
//dial.setAction("android.intent.action.DIAL");
try {
dial.setData(Uri.parse("tel:9951037343"));
startActivity(dial);
} catch (Exception e) {
Log.e("Calling", "" + e.getMessage());
}
startActivity(dial);
Use
Intent callIntent = new Intent(Intent.ACTION_CALL);
instead of this
Intent callIntent = new Intent(Intent.ACTION_CALL);
callIntent.setAction("android.intent.action.DIAL"); // but this line is an optional one
callIntent.setData(Uri.parse("tel:044 6000 6000"));
startActivity(callIntent);
use this code......
String number = "tel:" + phonenumber;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
give permission in manifest: <uses-permission android:name="android.permission.CALL_PHONE" />
updated.....
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.menu_icon_pager).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
new AlertDialog.Builder(MainActivity.this)
.setIcon(android.R.drawable.ic_dialog_alert)
.setTitle("Exit")
.setMessage(
"Texting from dialog")
.setNegativeButton(android.R.string.cancel, null)
.setPositiveButton(android.R.string.ok,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
// Exit the activity
String number = "tel:"+phonenumber ;
Intent intent = new Intent(Intent.ACTION_CALL, Uri
.parse(number));
startActivity(intent);
}
})
.show();
}
});
}
Actually I called the AlertDialog from another Activity,so I need the context of called
Activity's Context reference.So I passed the Context as a parameter to Called Activity
Here is Called Activity's code
if(position == 1)new CalledByActivity().dailingDialogBox(param1,
param2,context);//Here I'm passing the context as param
Here CalledBy activity's implemented function
public void dailingDialogBox(final String param1,String param2,final Context context){
Here I implemented in +ve button click
Intent dail = new Intent(Intent.ACTION_DIAL);
dail.setData(Uri.parse("tel:"+station_Number));
context.startActivity(dail);//Here the "context" is reference of called Activity's context