Android repeatable dialog buttons - android

I'm building a dialog that lets you click the picture multiple times, and each time you click it it changes the picture.
final Dialog dialog = new Dialog(ViewCase.this);
dialog.setContentView(R.layout.viewcase_largeimage);
dialog.setCancelable(true);
dialog.setCanceledOnTouchOutside(true);
dialog.setTitle(name);
// show enlarged image
currPic = 1;
final ImageView imageViewLarge1 = (ImageView) dialog
.findViewById(R.id.imageViewViewCasePhotoLarge1);
imageViewLarge1.setImageBitmap(photoBitmap1);
imageViewLarge1
.setOnClickListener(new ImageView.OnClickListener() {
public void onClick(View view) {
switch (currPic) {
case 0:
imageViewLarge1
.setImageBitmap(photoBitmap1);
currPic++;
case 1:
imageViewLarge1
.setImageBitmap(photoBitmap2);
currPic++;
case 2:
imageViewLarge1
.setImageBitmap(photoBitmap3);
currPic = 0;
}
}
});
// shows the dialog
dialog.show();
}
This is my on click listener, and I can have allow for one click that changes to the second picture, but it stops after that. Any way to make the button click repeatable?

In switch block you should always use break; after every case. Switch doesn't stop executing when it finds the right case, it goes forward and executes every case. Maybe this can be the problem, you need to try it.

Related

Calling .getBackground() in universal onClick method

I want to include many buttons in my app, which can play a sound by clicking, so I included an OnClick event.
#Override
public void onClick(View view) {
int id = view.getId();
final MediaPlayer mediaPlayer = new MediaPlayer();
switch (id) {
case R.id.whisteling_bird:
stopandPlay(R.raw.whisteling_bird, mediaPlayer);
break;
default:
break;
}
}
But now I have the following problem:
I also want to change the Alpha value of the button by using
.getBackground().setAlpha(64);
But what do I have to write before .getBackground()?
I donĀ“t want to write this
final Button whisteling_bird = (Button) view.findViewById(R.id.whisteling_bird);
whisteling_bird.setOnClickListener(this);
whisteling_bird.getBackground().setAlpha(64);
for every single button. What can I do?
In your onClick(), below id line,
put view.getBackground().setAlpha(64);
it will set every clicked view's alpha to 64. But you will also need to reset it somewhere for safety.

Runtime onClicklLstener android

Just wondering can anyone solve this problem about adding listeners to imagebuttons at runtime. I'm presuming it has got something to do with the "this" parameter passed to setOnClickListener as we are already in a onClickListener.
My fragment implements onclicklistener. The onClick methods work for imagebuttons known at compile time, just not for the ones that are defined after inflating a prompt view. What seems to happen is the prompt layout seems to be recreated and added onto the back stack.
Basically the onclicklistener for mWhatsappshare,mEmail share does not react as I might expect. I put a stackoverflow error in the onclick method previously and when I clicked these imagebuttons my application did not crash. This means that the listener is in fact not registered (or at least not correctly) at
mwhatsapp.setonclicklistener(this)
By the way, I do not want to set separate listener like mWhatsapp.setonclicklistner(new View.onClickListener()){ for each imagebutton as it is too cumbersome and I want each listener to be handled in onclick().
Thank you
mOtherShare.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
removeColorFilters();
mOtherShare.setImageResource(R.drawable.other_pill_pressed);
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialoglayout = inflater.inflate(R.layout.share_prompt,null);
mWhatsappShare = (ImageButton) dialoglayout.findViewById(R.id.ib_whatsapp_share);
mEmailShare = (ImageButton) dialoglayout.findViewById(R.id.ib_email_share);
mFlickrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_flickr_share);
mTumblrShare = (ImageButton) dialoglayout.findViewById(R.id.ib_tumblr_share);
final AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(dialoglayout);
dlg = builder.show();
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
}
});
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
case R.id.ib_whatsapp_share:
sendIntent("com.whatsapp");
break;
case R.id.ib_email_share:
sendIntent("android.email");
break;
case R.id.ib_flickr_share:
sendIntent("com.yahoo.mobile.client.android.flickr");
break;
case R.id.ib_tumblr_share:
sendIntent("com.tumblr");
break;
}
}
``
On this block of code:
mWhatsappShare.setOnClickListener(this);
mEmailShare.setOnClickListener(this);
mFlickrShare.setOnClickListener(this);
mTumblrShare.setOnClickListener(this);
this is referring to the new OnClickListener you are creating - mOtherShare.setOnClickListener(new View.OnClickListener() and NOT your fragment's OnClickListener
To use the outer onClick, change this to YourFragment.this

android imageView

I'd like to do such thing.... There is a Relative layout with splash.PNG as a background.... there are five ImageViews: #drawable\1.png, #drawable\2.png, #drawable\3.png, #drawable\4.png and #drawable\5.png...
1-is clickable and visible;
2-5 - are invisible and clickable="false"
by clicking once Imageview 2 becomes visible but unclickable, and then by clicking all 2-5 appears, then by clicking last fifth time 2-5 again becomes invisible....
As for me, such construction works with one invisible ImageView:
final ImageView iv36 = (ImageView) findViewById(R.id.yabl3skr);
iv36.setOnClickListener( new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
final ImageView iv37 = (ImageView)findViewById(R.id.yab3);
iv37.setVisibility(1);
iv37.setClickable(true);
iv37.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v33) {
// TODO Auto-generated method stub
iv37.setVisibility(View.GONE);
iv37.setClickable(false);
}
});
}
});
Need any ideas how to do it in a good way with five imageViews?
I don't know if I fully get it, but what I think you're saying is that you have five ImageViews that all perform conditional logic when clicked. If that's the case, what I normally do is implement single OnClickListener for all of the images. You can set an initial state on all of your images and then do a switch on the id of the View being passed to enable and show which ever views you want to show.
final ImageView iv33 = (ImageView) findViewById(R.id.iv33);
final ImageView iv34 = (ImageView) findViewById(R.id.iv34);
final ImageView iv35 = (ImageView) findViewById(R.id.iv35);
final ImageView iv36 = (ImageView) findViewById(R.id.iv36);
final ImageView iv37 = (ImageView) findViewById(R.id.iv37);
OnClickListener imageClickListener = new OnClickListener() {
public void onClick(View v) {
// Initial state
iv33.setClickable(false);
iv34.setClickable(false);
iv35.setClickable(false);
iv36.setClickable(false);
iv37.setClickable(false);
iv33.setVisibility(View.GONE);
iv34.setVisibility(View.GONE);
iv35.setVisibility(View.GONE);
iv36.setVisibility(View.GONE);
iv37.setVisibility(View.GONE);
switch(v.getId())
{
case R.id.iv33:
// show and hide what you would like...
break;
case R.id.iv34:
// show and hide what you would like...
break;
case R.id.iv35:
// show and hide what you would like...
break;
case R.id.iv36:
// show and hide what you would like...
break;
case R.id.iv37:
// show and hide what you would like...
break;
}
}
};
iv33.setOnClickListener(imageClickListener);
iv34.setOnClickListener(imageClickListener);
iv35.setOnClickListener(imageClickListener);
iv36.setOnClickListener(imageClickListener);
iv37.setOnClickListener(imageClickListener);

Null Pointer on an attempt to call a button

I have a null pointer on a button that I need to take me to a new layout when pushed. I have the code set as:
((Button) findViewById(R.id.analyzee)).setOnClickListener(btnClick);
inside a method that uses conditional statements.
It is a basic face detect app. If faces are not found, I do this:
if (facesFound < 1) {
mFlipper.setDisplayedChild(2);
mTheMessage = (TextView) findViewById(R.id.falsemessage);
mThePicture = (ImageView) findViewById(R.id.false_view);
mTheMessage.setText(R.string.noFaceOne);
mThePicture.setImageBitmap(bitmap565);
return;
if faces are found, I draw a box on the face, and do this:
mFlipper.setDisplayedChild(1);
mTheMessage.setText(R.string.noFaceFive);
mThePicture.setImageBitmap(bitmap565);
My issue lies here though, I use this method to run when buttons are clocked:
private final View.OnClickListener btnClick = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.scan_box:
openCamera();
break;
case R.id.crop_face:
final ProgressDialog dialog = ProgressDialog.show(Main.this, "",
"Cropping photo", true);
dialog.show();
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
dialog.dismiss();
}
}, 3000);
cropFace();
break;
So, my issue lies in this:
In one of my layouts in the flipper, the button that rests on the layout will need to give the user the option to snap a new picture if there are no faces found. The other layout will need the button (upon click) to have the faces cropped and the results to be sent to another layout.
The issue I am facing is where the code:
((Button) findViewById(R.id.crop_face)).setOnClickListener(btnClick);
needs to be placed in order for the program to release the button has been clicked, it calls the case in my switch statement, and runs the crop face_method.
I try putting it in the if statement where I set the image View and text View, but I get a null pointer on that line I am declaring my button on.
The buttons I have on my main menu work fine, as they are in my onCreate method, but I don't know where to play this button command, and also where I need to place my reopen Camera command.
Thanks!
I simply was using the wrong button ID...sorry for the confusion =x

DialogPreference shouldn't close if no option selected

I have a DialogPreference and I want to avoid the user from closing it when pressing "OK", "Cancel", etc.
How should I do that?
EDIT:
I tried to reach the OK button to disable when the dialog is created. But I couldn't make it :(
The solution is quite easy. Overwrite showDialog and set your own click listener to the buttons you want to intercept.
#Override
protected void showDialog(Bundle bundle) {
super.showDialog(bundle);
Button pos = ((AlertDialog) getDialog()).getButton(DialogInterface.BUTTON_POSITIVE);
pos.setOnClickListener(...);
}
In your click listener you can do the validation you want.
A tweak could be to create a custom dialog where you define your own buttons (OK and Close).
public class YourClass implements OnClickListener {
private Button DialogButton;
private Dialog dialog;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.MainLayout);
/* Your code... */
DialogButton = (Button) findViewById(R.id.DialogButtonId);
DialogButton.setOnClickListener(this);
}
public void onClick(View v) {
switch (v.getId()) {
case R.id.DialogButtonId:
LayoutInflater inflater = LayoutInflater.from(YourClass.this);
final View inflay = inflater.inflate(R.layout.DialogLayout, (ViewGroup) findViewById(R.id.RootIdOfDialogLayout));
TextView YourTextView = (TextView) inflay.findViewById(R.id.TextViewId);
Button cancel = (Button) inflay.findViewById(R.id.CancelButtonId);
cancel.setOnClickListener(YourClass.this);
Button ok = (Button) inflay.findViewById(R.id.OkButtonId);
ok.setOnClickListener(YourClass.this);
dialog = new Dialog(YourClass.this);
dialog.setContentView(inflay);
dialog.setTitle(getString(R.string.TitleStringId));
dialog.show();
break;
case R.id.CancelButtonId:
/* Checking if the user selected an option if true call dialog.dismiss() */
break;
case R.id.OkButtonId:
/* Here handle your preferences (e.g. putString(String key, String value)) */
/* Checking if the user selected an option if true call dialog.dismiss() */
break;
}
}
}
Check out http://developer.android.com/reference/android/content/SharedPreferences.Editor.html in order to handle your preference in onClick. I didn't test this code just wrote it to show you how you could solve it!
The dialog stays open until you call dialog.dismiss();. In that case you'll have to create your drop-down-menu, polls or what ever you want to display in your layout file. After pressing ok or cancel you should check if the user made a choice, and parse that choice into your preferences. (check link above)
Rgds
Layne
You could try opening it again.
Why would you want to prevent users to close the dialog? Users should be able to have 'full' control of their device.
You can see the source code of DialogPreferences here:
https://android.googlesource.com/platform/frameworks/base/+/master/core/java/android/preference/DialogPreference.java
And then, copy most of it to your code, modifying the code as needed.
How about overriding the onDismiss() method and implementing a canExit() method with the validations you want to occcur? E.g. :
public class MyDialogPref extends DialogPreference {
#override public void onDismiss(DialogInterface dialog) {
if (canExit()) {
super.onDismiss(dialog);
}
}
...
}
A good UI should have a default selection/option already selected (the previously user-entered options or a program default).
Presenting a dialog asking for a change in options without any indication of what you already have is bad UI design.
This way if the user clicks Cancel, nothing changes and they saw what the option selected was. If they make no change and click OK then nothing really changes either.
Software is supposed to make doing specific tasks easier, not force the user to process the apps logic themselves.

Categories

Resources