Insert LongPress from listview - android

I tried to introduce long press.
this time using a alertdialog to the 2 options there, play and delete.
so I would like to offer longpress for delete and touch for play. How can I do?
code:
#Override
public void onVideoSelected(final String uri, String mimeType) {
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.AppCompatAlertDialogStyle);
builder.setTitle("Select");
// builder.setMessage("Lorem ipsum dolor ....");
builder.setItems(new CharSequence[]
{getString(R.string.play_video), getString(R.string.remove_video)},
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
Intent intent = new Intent(Main2Activity.this, MainActivity.class);
intent.putExtra("url", uri);
startActivity(intent);
break;
case 1:
File file = new File(uri);
file.delete();
Main2Activity.this.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File(uri))));
break;
}
}
});
builder.create().show();
}

Related

android studio through spring mvc

For android studio, I am doing a module where I have to take an image as well as all type files and store into our server. (i.e my sql I am using common backend.) Through Spring MVC, I am doing this project. I am not able to save the file location to mysql.
We have to save only the uploaded location, but that location is our server.
I tried to take the image from mobile, but after taking the image it is not saving. I also granted permission to externalsd.
my code is:
//image selection
private void selectImage() {
Dialog dialog = new Dialog(getActivity());
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle("Choose Image Source");
builder.setItems(new CharSequence[] { "Gallery", "Camera" },
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog,
int which) {
switch (which) {
case 0:
Intent intent = new Intent(
Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
Intent chooser = Intent
.createChooser(
intent,
"Choose a Picture");
startActivityForResult(
chooser,
ACTION_REQUEST_GALLERY);
break;
case 1:
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(
cameraIntent,
ACTION_REQUEST_CAMERA);
break;
default:
break;
}
}
});
builder.show();
dialog.dismiss();
}

alertdialog redirection to url

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();

How to show sequential dialog popups?

I'm having some trouble with displaying multiple popups. Right now I have an AlertDialog that pops up with an EditView for the user to put in the name of the file they want to make, which I would then pass into a File object and then a Writer and a new Dialog is supposed to pop up asking the user if they want to launch the music player.
However, as things are now, after I press 'Ok' on the first AlertDialog, absolutely nothing happens. I'm not sure what I'm doing wrong. Any help? Here is my code.
//naming the playlist
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Exporting Playlist");
alert.setMessage("Enter the name of the playlist!");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
name = input.getText().toString() + ".m3u";
popup = true;
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
//after the playlist is named, put songs into file
if (popup){
popup = false;
final File list = new File(mp3folderPath + name);
FileWriter writer;
BufferedWriter write;
ArrayList<String> playlist = new ArrayList<String>();
Log.d("poo", "mAdapter count: "+mAdapter.getCount());
for (int i=0; i < mAdapter.getCount(); i++) {
playlist.add(mAdapter.getItem(i));
}
Log.d("poo", playlist.toString());
//write the songs to the m3u playlist
writer = new FileWriter(list);
write = new BufferedWriter(writer);
for (int i = 0; i<playlist.size(); i++){
String[] name = playlist.get(i).split(" : ");
Log.d("poo", name[0]);
write.append(name[0]+"\n");
}
write.close();
//popup window
CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exported playlist!");
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
finish();
}
else {
finish();
}
}
});
builder.show();
}
}
To show sequential popup, the conditions and code for consecutive popup(s) would have to be reachable from one to the other.
AlertDialog1 has to contain the code which would show AlertDialog2...
Try something like this:
//naming the playlist
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setTitle("Exporting Playlist");
alert.setMessage("Enter the name of the playlist!");
// Set an EditText view to get user input
final EditText input = new EditText(this);
alert.setView(input);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//check if the name is not null
name = input.getText().toString() + ".m3u";
//Now instead of popup = true;
//call func to name the playlist and next dialog
callNextDialog();
}
});
alert.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
// Canceled.
}
});
alert.show();
//after the playlist is named, put songs into file
// if (popup){
// popup = false;
// }
public void callNextDialog(){
final File list = new File(mp3folderPath + name);
FileWriter writer;
BufferedWriter write;
ArrayList<String> playlist = new ArrayList<String>();
Log.d("poo", "mAdapter count: "+mAdapter.getCount());
for (int i=0; i < mAdapter.getCount(); i++) {
playlist.add(mAdapter.getItem(i));
}
Log.d("poo", playlist.toString());
//write the songs to the m3u playlist
writer = new FileWriter(list);
write = new BufferedWriter(writer);
for (int i = 0; i<playlist.size(); i++){
String[] name = playlist.get(i).split(" : ");
Log.d("poo", name[0]);
write.append(name[0]+"\n");
write.close();
//popup window
CharSequence choices[] = new CharSequence[] {"Launch Music Player", "Quit"};
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Exported playlist!");
builder.setItems(choices, new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
if (which == 0) {
Intent intent = new Intent(MediaStore.INTENT_ACTION_MUSIC_PLAYER);
startActivity(intent);
finish();
}
else {
finish();
}
}
});
builder.show();
}
}
AlertDialog.show() doesn't wait for the dialog to go away. It returns immediately. That means ALL of the logic of what to do after the user makes a choice has to go in the onClick function of the dialog's positive button.
Basically, everything in your if(popup) code needs to be in the onClick handler

How to use phone number in alert dialog android?

I want to use default phone number and pass that in alert dialog.How it's possible. I have tried,but it force close.Please solve my issue. Thanks in advance. My coding is as follows:
case R.id.menu_settings:
String phoneNo ="123456789";
AlertDialog.Builder alert = new AlertDialog.Builder(this);
alert.setMessage("Do you want to call us?"+phoneNo);
alert.setPositiveButton("Ok", new DialogInterface.OnClickListener() {
String uri = "123456789";
public void onClick(DialogInterface dialog, int whichButton) {
Intent intent = new Intent(Intent.ACTION_CALL);
intent.setData(Uri.parse(uri));
startActivity(intent);
}
});
alert.setNegativeButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// TODO Auto-generated method stub
finish();
}
});
alert.show();
return true;
}
return false;
}
The Uri should be in the format tel:########
String uri = "tel:123456789";
This is happening because at least 1 of 2 reasons.
You do not have the permission <uses-permission android:name="android.permission.CALL_PHONE"/> in your manifest
Your Uri is in an incorrect format. Change it from
String uri = "123456789" to
String uri = "tel:123456789"

context menu not working properly

please have a look at the code below...
i am creating a context menu and implementing the method onContextItemSelected but the problem is that when i press the reply item...the dialog on the delete case also appears and the activity also starts twice...means all the cases executes...the delete the reply and the forward case...what happens to be the problem please help
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId())
{
case R.id.reply:
{
Intent i = new Intent();
String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(2);
i.putExtra("number", s2);
// Log.v("number", s2);
i.setClass(this, CreateMessage.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
}
case R.id.delete:
{
final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(1);
dba = new DBAdapter(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Log.v("", "You Clicked " + s);
dba.delete("messages", "id=?", new String[] { s });
populate();
dba.close();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
}
case R.id.forward:
{
Intent i = new Intent();
String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(4);
// Log.v("message", s3);
i.putExtra("message", s3);
i.setClass(this, CreateMessage.class);
startActivity(i);
}
default:
return super.onContextItemSelected(item);
}
}
and here is the error from the logcat that is displayed in the logcat...
03-30 09:13:28.439: E/WindowManager(2273): Activity sms.app.Displayer has leaked window com.android.internal.policy.impl.PhoneWindow$DecorView#44eb95c8 that was originally added here
sms.app.Displayer is the class in which i am implementing the context menu..
and here is the code of the context menu file..
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:id="#+id/reply" android:title="Reply"></item><item
android:id="#+id/forward"
android:title="Forward">
</item>
<item android:id="#+id/delete" android:title="Delete Message">
</item>
</menu>
add breaks after your case statements
Edit:
You fall thru to the following case blocks because you are missing the breaks. Either add breaks to you case statements (and handle the return value later) or directly add return true instead of the breaks.
Once check this code a minor change in your onContextItemSelected method::
#Override
public boolean onContextItemSelected(MenuItem item) {
// TODO Auto-generated method stub
AdapterContextMenuInfo info = (AdapterContextMenuInfo) item.getMenuInfo();
switch(item.getItemId()){
case R.id.reply:
Intent i = new Intent();
String s2 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(2);
i.putExtra("number", s2);
// Log.v("number", s2);
i.setClass(this, CreateMessage.class);
// i.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
startActivity(i);
break;
case R.id.delete:
final String s = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(1);
dba = new DBAdapter(this);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage("Are you sure you want to delete?")
.setCancelable(false)
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Log.v("", "You Clicked " + s);
dba.delete("messages", "id=?", new String[] { s });
populate();
dba.close();
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
AlertDialog alert = builder.create();
alert.show();
break;
case R.id.forward:
Intent i = new Intent();
String s3 = (String) ((Cursor) getListView().getItemAtPosition(info.position))
.getString(4);
// Log.v("message", s3);
i.putExtra("message", s3);
i.setClass(this, CreateMessage.class);
startActivity(i);
break;
}
return true;
}

Categories

Resources