I have 2 fragments. First one is a checklist for live electrical work(safety check list)
, and a second fragmet which is also a checklist (non- live electrical work).
What I'm doing is getting the user to take a screen shot of each fragment when they click "next". This is achieved by getting the user to click the "next" button which will take a screen shot of the current fragment, then loading the next fragment(goes from liveWorkFragment -> nonLiveWorkFragment). Same thing on the second fragment. The problem I am having is that the first fragment takes a screen shot no problem, it gets saved in the correct folder etc, but when I click next on the second fragment, it takes the screenshot, but when I go to look at the picture(fragment2 screenshot is called "SafetyCheckList2.png"), it's the same picture as fragment1. I have no idea why.
This is the code to take a screen shot for fragment1:
View.OnClickListener clickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(v.getId() == R.id.btnNext)
{
try
{
View u = view.findViewById(R.id.liveWorkLayout).getRootView();
u.setDrawingCacheEnabled(true);
Bitmap b = u.getDrawingCache();
//Setup the directory
File mediaFolder = new File(context.getExternalFilesDir(null)+"/ArcFlash/CheckListMedia");
if(!mediaFolder.exists())
mediaFolder.mkdirs();
File fileName = new File(context.getExternalFilesDir(null)+"/ArcFlash/SafetyCheckList1.png");//CheckListMedia
FileOutputStream out = new FileOutputStream(fileName);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
Toast.makeText(context, "Saved Picture", Toast.LENGTH_SHORT).show();
new AlertDialog.Builder(context)
.setTitle("Continue")
.setMessage("You cannot return to this page after clicking next.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
FragmentManager fm = getFragmentManager();
FragmentTransaction transaction = fm.beginTransaction();
Fragment newFragment = new NonLiveWorkFragment();
transaction.replace(R.id.mainLayout, newFragment);
//transaction.addToBackStack("first");
transaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
transaction.commit();
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
}
catch(Exception ex)
{
handler.Log(ex.getMessage(), "onClick (LiveWorkFragment.java)", context);
}
}
}
};
Code for fragment2:
View.OnClickListener clickListener = new View.OnClickListener()
{
#Override
public void onClick(View v)
{
if(v.getId() == R.id.frag2Next)
{
try
{
View u = view.findViewById(R.id.nonLiveWorkLayout).getRootView();
u.setDrawingCacheEnabled(true);
Bitmap b = u.getDrawingCache();
File fileName = new File(context.getExternalFilesDir(null)+"/ArcFlash/SafetyCheckList2.png");
FileOutputStream out = new FileOutputStream(fileName);
b.compress(Bitmap.CompressFormat.PNG, 90, out);
out.close();
Toast.makeText(context, "Saved Picture", Toast.LENGTH_LONG).show();
//Until I figure out how the heck to draw with a silly(being PG 13 here..) fragment,
//I'm going to use an activity as the final page
new AlertDialog.Builder(context)
.setTitle("Continue")
.setMessage("You cannot return to this page after clicking next.")
.setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
Intent signatures = new Intent();
signatures.setClass(getActivity(), Signature.class);
startActivity(signatures);
}
})
.setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// do nothing
}
})
.show();
}
catch(Exception ex)
{
handler.Log(ex.getMessage(), "onClick Next (NonLiveWorkFragment.java)", context);
Toast.makeText(context, "Couldn't load next page.", Toast.LENGTH_LONG).show();
}
}
}
};
Related
i tried to delete an item in sharedPreferences, in recyclerView adapter. but that item doesn't disappear till I go back from the activity and come back. I need it to disappear soon after being deleted.
Method
public boolean RemoveFromWantToRead(AllBooksActivityModel book) {
ArrayList<AllBooksActivityModel>books = getWantToReadBooks();
if (null != books){
for (AllBooksActivityModel b: books){
if (b.getBookID() == book.getBookID()){
if (books.remove(b)){
//We create a 'if' loop here is if only the thing happened, the loop actions.
Gson gson = new Gson();
SharedPreferences.Editor editor = sharedPreferences.edit();
editor.remove(WANT_TO_READ_BOOKS);
editor.putString(WANT_TO_READ_BOOKS,gson.toJson(books));
editor.commit();
return true;
}
} }
}return false;
}
This is how I tried to delete an item.
holder.BtnDltTxt.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
builder.setMessage("Do you want to delete this massage");
builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
String EleName = allBooksActivityModels.get(position).getBookName();
if( Ulitls.getInstance(context).RemoveFromCurrenlyReading(allBooksActivityModels.get(position))){
Toast.makeText(context, EleName + " Removed", Toast.LENGTH_SHORT).show();
notifyDataSetChanged();
}else{
Toast.makeText(context, "Something Wrong Happens, Try again", Toast.LENGTH_SHORT).show();
}
}
});
builder.setNegativeButton("No", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
}
});
builder.create().show();
}
});
I would like to make an input method which is used only for SoftKeyboard. My how to make popup onkey event in input method.
I am creating Dialog but here is problem you see my logcat:
09-14 11:16:54.349: E/MessageQueue-JNI(7172): at android.inputmethodservice.KeyboardView.detectAndSendKey(KeyboardView.java:824)
Softkeyboard.java
Here java code
public void onKey(int primaryCode, int[] keyCodes) {
if (primaryCode == -2) {
// add this to your code
dialog = builder.create();
Window window = dialog.getWindow();
WindowManager.LayoutParams lp = window.getAttributes();
lp.token = mInputView.getWindowToken();
lp.type = WindowManager.LayoutParams.TYPE_APPLICATION_ATTACHED_DIALOG;
window.setAttributes(lp);
window.addFlags(WindowManager.LayoutParams.FLAG_ALT_FOCUSABLE_IM);
// end addons
builder.show();
}
Thanks for advance..
You need to have ACTION_MANAGE_OVERLAY_PERMISSION permission to open/display Alert onkey event in input method.
Before you set your custom Keyboard Check for Overlay Permission.
final boolean overlayEnabled = Settings.canDrawOverlays(MainActivity.this);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && !overlayEnabled) {
openOverlaySettings();
}
#TargetApi(Build.VERSION_CODES.M)
private void openOverlaySettings() {
final Intent intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + getPackageName()));
try {
startActivityForResult(intent, RC_OVERLAY);
} catch (ActivityNotFoundException e) {
Log.e("MainActivity", e.getMessage());
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case RC_OVERLAY:
final boolean overlayEnabled = Settings.canDrawOverlays(this);
if (overlayEnabled) {
preferenceManager.setBooleanPref(IS_CYBER_BULLING_ON, true);
Intent intent = new Intent(MainActivity.this, ImePreferences.class);
startActivity(intent);
overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
} else {
// switchCyberBulling.setChecked(false);
}
// Do something...
break;
}
}
Then inside your SoftKeyboard.java class, put code for alert dialog & set alert type of "TYPE_APPLICATION_OVERLAY".
AlertDialog.Builder builder = new AlertDialog.Builder(this)
//set icon
.setIcon(android.R.drawable.ic_dialog_alert)
//set title
.setTitle("Warning!")
//set message
.setMessage("This is alert dialog!")
//set positive button
.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what would happen when positive button is clicked
dialogInterface.dismiss();
}
})
//set negative button
.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogInterface, int i) {
//set what should happen when negative button is clicked
dialogInterface.dismiss();
}
});
AlertDialog alertDialog = builder.create();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY);
}else{
alertDialog.getWindow().setType(WindowManager.LayoutParams.TYPE_SYSTEM_ALERT);
}
alertDialog.show();
Do not forget for Draw Overlay Permission. Hope this helps you. :)
I've got a huge problem. When I had AlertBox everything was OK, but I would change it to custom dialog box with pretty good graphic.
With AlertBox it was displaying the current scores and highscore.
When I changed it to Custom Dialog box it's showing nothing.
CustomDialogClass.java
public class CustomDialogClass extends Dialog
{
public CustomDialogClass(Context context) {
super(context);
// TODO Auto-generated constructor stub
/** It will hide the title */
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_dialog);
}
}
GameActivity.java (fragments with custom dialog box)
#Override
public void surfaceDestroyed(SurfaceHolder holder) {
surfaceCreated = false;
stopDrawingThread();
}
public void customizeDialog() {
int highest = PrefUtil.getHighestScore(this);
String text = null;
if (currentPoint > highest) {
highest = currentPoint;
PrefUtil.setHighestScore(this, currentPoint);
} else {
}
text = "Current Points: " + currentPoint + "\nThe Best Score: " + highest;
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Game Over");
builder.setMessage(text);
builder.setPositiveButton("Try Again", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
playSwooshing();
restart();
}
});
builder.setNegativeButton("Exit Game", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Intent intent = new Intent(GameActivity.this,Bye.class);
startActivity(intent);
playSwooshing();
finish();
}
});
builder.setCancelable(false);
alertDialog = builder.show();
}
and the other fragment:
private void onGameOver() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (!isFinishing()) {
soundPool.play(soundIds[SOUND_DIE], 0.5f, 0.5f, 1, 0, 1);
CustomDialogClass customizeDialog = new CustomDialogClass(GameActivity.this);
customizeDialog.show(); }
}
});
}
Where is a problem? Can someone fix it?
Now it's showing only my layout file, without any data.
Thanks!
It's not displaying your data because you're not setting your data. In your middle code fragment where you're creating your dialog using a Builder you're not using your custom dialog, so apparently that code is no longer being called. Likewise in your last fragment where you do create a custom dialog, you're not setting any the data.
See the documentation:
If you want a custom layout in a dialog, create a layout and add it to an AlertDialog by calling setView() on your AlertDialog.Builder object.
By default, the custom layout fills the dialog window, but you can still use AlertDialog.Builder methods to add buttons and a title.
i want to open a dialog box that gives me two option.
1- Choose file from SD Card
2- Take a snapshot from Camera
Right now i am using this code
receipt.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
showDialog(RECEIPT_DIALOG_ID);
}
});
protected Dialog onCreateDialog(int id) {
Dialog dialog = null;
AlertDialog.Builder builder = new Builder(this);
case RECEIPT_DIALOG_ID:
builder.setTitle("Choose your file");
dialog = builder.create();
return dialog;
}
Now, how can i add these two options.
Best Regards
Try this it provides both the options
final CharSequence[] items = {"Camera", "Memory Card"};
builder.setTitle(R.string.pic_option);
builder.setIcon(R.drawable.camera_icon);
builder.setItems(items, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item) {
launchCamera(item);
}
});
builder.create();
builder.show();
and the Function launchCamera(item) is here
public void launchCamera(int id){
switch (id) {
case 0:
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
((Activity)getParent()).startActivityForResult(cameraIntent, 1888);
break;
case 1:
Intent intent = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
((Activity)getParent()).startActivityForResult(intent, 2);
break;
default:
break;
}
}
Use this directly in your method.It will show 2 buttons on dialog.
Dialog dialog;
dialog = new Dialog(getParent());
dialog.setContentView(R.layout.camdialog);
dialog.setTitle(" Select the Image");
dialog.setCancelable(true);
sdCard = (Button) dialog.findViewById(R.id.sdCard);
camDialog = (Button) dialog.findViewById(R.id.camDialog);
dialog.show();
dialog.getWindow().setLayout(LayoutParams.FILL_PARENT, 200);
try this...
new AlertDialog.Builder(menu)
.setTitle("Choose your file")
.setMessage("Your msg")
.setPositiveButton("Choose file from SD Card",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
// Your code
}
})
.setNegativeButton("Take a snapshot from Camera",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int whichButton) {
//Your code
}
}).show();
I'm using this pieces of code, in the activity:
public void carregaListaDemanda(){
setContentView(R.layout.listaviewdemanda);
lstDem = (ListView) findViewById(R.id.listViewDemanda);
DemandaAdapter adapter = new DemandaAdapter(ctx,
bancodedados.getAllDem(), this);
lstDem.setAdapter(adapter);
lstDem.setItemsCanFocus(true);
teste=0;
}
and in the adapter:
public void onClick(View v) {
AlertDialog alertDialog = new AlertDialog.Builder(ctx).create();
alertDialog.setTitle("Do you wanna delete?");
alertDialog.setIcon(R.drawable.icon);
alertDialog.setMessage("if 'yes' the demand '"
+ dem.getNr_demanda() + "' will be deleted!");
alertDialog.setButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
// if yes delete, and REFRESH the screen
DemandaDAO dbHelper;
try {
dbHelper = new DemandaDAO(ctx);
dbHelper.DeleteDem(dem);
} catch (FileNotFoundException e) {
e.printStackTrace();
}}
});
alertDialog.setButton2("Cancel",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,
int which) {
return;
}
});
alertDialog.show();
}
});
I want to refresh the listview after deleting a demand, but it results in a forceclose if I call the method in activity again.
call adapter.notifyDataSetChanged() , it Notifies the attached View that the underlying data has been changed and it should refresh itself.