I'll keep this simple. I want to set an ImageView in an AlertDialog to an image in an array of Drawables The image that I would like to set to the ImageView can be retrieved by accessing the mImages[position].
Here is a brief explanation (complete explanation below):
In short- I need a way to pass an image from my main activity to a dialog then on Cancel dismiss the dialog and on confirm set the system wallpaper (to the image passed from the main activity) then finish(); the activity.
Here's a complete explanation:
The user is presented with a Gallery and an ImageView above the Gallery that shows a larger preview of the image that has focus in the Gallery.
The images displayed in the Gallery are setup using:
// setup wallpaper array
private void findWallpapers() {
mThumbs = new ArrayList<Integer>(24);
mImages = new ArrayList<Integer>(24);
final Resources resources = getResources();
final String packageName = getApplication().getPackageName();
addWallpapers(resources, packageName, R.array.wallpapers);
}
// setup array defining all wallpapers & define thumbnails
private void addWallpapers(Resources resources, String packageName, int list) {
final String[] extras = resources.getStringArray(list);
for (String extra : extras) {
int res = resources.getIdentifier(extra, "drawable", packageName);
if (res != 0) {
final int thumbRes = resources.getIdentifier(extra + "_small",
"drawable", packageName);
if (thumbRes != 0) {
mThumbs.add(thumbRes);
mImages.add(res);
}
}
}
}
After "Set Wallpaper" Button is pressed, an AlertDialog should open with another preview of the image that had focus in the Gallery. The AlertDialog will contain a TextView with instructions, the preview of the image we are proposing to set as the wallpaper, an "Okay" Button and a "Cancel" Button. Pressing the "Okay" Button will set the image from the ImageView preview as the system wallpaper via an InputStream.
Thanks again!
private void selectWallpaper(int position) {
Toast.makeText(getBaseContext(), "Select Wallpaper", Toast.LENGTH_SHORT)
.show();
if (mIsWallpaperSet) {
return;
}
mIsWallpaperSet = true;
Context context = this;
// CharSequence text = "Wallpaper Set!";
// int duration = Toast.LENGTH_LONG;
InputStream stream = getResources().openRawResource(
mImages.get(position));
final Dialog accept = new Dialog(context);
accept.setContentView(R.layout.confirm);
accept.setTitle("Please Confirm");
TextView instructions = (TextView) accept.findViewById(R.id.textView1);
instructions.setText("Would you like to set this as your wallpaper?");
ImageView wallpreview = (ImageView) accept
.findViewById(R.id.imageView1);
wallpreview.createFromStream(stream, "test");
// SETUP cancel (no btn) listener
Button cancelbtn = (Button) accept.findViewById(R.id.button2);
cancelbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
accept.dismiss();
}
});
// SETUP Yes Btn listener
Button okbtn = (Button) accept.findViewById(R.id.button1);
okbtn.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// dismiss being used as placeholder, actually setting wallpaper
// will be added
accept.dismiss();
}
});
accept.show();
}
{
;
}
You can use the layout inflator to inflate your alert dialog layout and assign it to the View of the alert dialog in your main activity. You can use findViewById() to set the onClickListeners listeners for those buttons. That will ensure that your onClickListener can see mImages.
final ArrayList<Integer> mImages=new ArrayList<Integer>();
final ImageView v=findViewById(R.id.imageView);
AlertDialog d=new AlertDialog.Builder(Main.this).create();
d.setView(getLayoutInflater().inflate(R.layout.dialogView,findViewById(R.id.ROOT_LAYOUT_IN_XML_ID),false));
((ImageView)d.findViewById(R.id.YOUR_VIEW_ID)).setImageResource(mImages.get(?));
((Button)d.findViewById(R.id.cancelButton)).setOnClickListener(new OnClickListener()
{
d.dismiss();
});
((Button)d.findViewById(R.id.acceptButton)).setOnClickListener(new OnClickListener()
{
v.setImageResource(mImages.get(?));
d.dismiss();
});
Related
Code that is not working
public void fade(View v){
ImageView pic1 = (ImageView) findViewById(R.id.pic1);
ImageView pic2 = (ImageView) findViewById(R.id.pic2);
pic1.animate().alpha(0f).setDuration(2000);
pic2.animate().alpha(1f).setDuration(2000);
}
Not quite sure what you are asking for, but if you want to change the image back and forth onClick, you could try modifying the code I wrote below.
// set a boolean value
private boolean firstImageShown = true;
public void clickFunction(View view) {
ImageView firstImage = (ImageView) findViewById(R.id.firstImage);
if ((firstImage != null) && (firstImageShown)) {
firstImage.setImageResource(R.drawable.sample_image2);
firstImageShown = false;
} else {
if (firstImage != null) firstImage.setImageResource(R.drawable.sample_image1);
firstImageShown = true;
}
}
//Suppose you have two images rose_1 and rose_2 and you want to switch back
//and forth between these on a button press.
//Set a static variable
private static int switcherPressedCount = 2;
//Map the onClick attribute of the button to a function say "switcherPressed".
//"imageView1" is the id attribute of the image which first gets loaded in the
//app, in this example, it is the attribute of rose_1.
public void switcherPressed(View view){
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
if(switcherPressedCount%2 == 0) {
imageView.setImageResource(R.drawable.rose_2);
switcherPressedCount = 3; //to avoid reaching maximum int limit.
}
else {
imageView.setImageResource(R.drawable.rose_1);
switcherPressedCount = 2; //to avoid reaching maximum int limit.
}
}
im trying to build share dialog.
in this share dialog I have facebook,whatsapp,mail and more.
im trying the make the background of every view change on touch to indicate the toch.
so far no problem.
the problem is that I also want to let the user move is finger into other option and when he do that the previous background return to his original color and the new view background changes.
i just cant get the hover event at all, and I couldn't trigger other onTouchEvent as long as the first one is still alive.
this is my code so far:
public class customDialogFragment1 extends DialogFragment {
public customDialogFragment1() {
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
postShareUrl=getActivity().getResources().getString(R.string.servicePostShareUrl);
id=getArguments().getString("body");
postTitle=getArguments().getString("subject");
Intent sendIntent = new Intent(android.content.Intent.ACTION_SEND);
sendIntent.setType("text/plain");
Dialog dialog = new Dialog(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity(),android.R.style.Theme_Translucent_NoTitleBar);
LayoutInflater inflater = (LayoutInflater) getActivity()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.share_title_layout, null);
// Set title divider color
TextView txtTitle= (TextView) v.findViewById(R.id.share_title);
Typeface tf = Typeface.createFromAsset(getActivity().getResources().getAssets(),
"fonts/OpenSansHebrew-Bold.ttf");
txtTitle.setTypeface(tf);
View layout=inflater.inflate(R.layout.custom_share_layout_inner,null);
builder.setView(layout);
ImageView facebook= (ImageView) layout.findViewById(R.id.imgFaceebook);
ImageView whatsapp= (ImageView) layout.findViewById(R.id.imgWhatsapp);
ImageView more= (ImageView) layout.findViewById(R.id.imgMore);
ImageView mail= (ImageView) layout.findViewById(R.id.imgMail);
facebook.setOnClickListener(imageClickListener);
whatsapp.setOnClickListener(imageClickListener);
more.setOnClickListener(imageClickListener);
mail.setOnClickListener(imageClickListener);
List activities = getActivity().getPackageManager().queryIntentActivities(sendIntent, 0);
Context context=(Activity)getActivity();
for(int i=0;i<activities.size();i++) {
ResolveInfo appPacageName = (ResolveInfo) activities.get(i);
Log.i("pacageName", appPacageName.toString());
if (appPacageName.toString().contains("com.facebook.composer")) {
shareCheckList[0] = appPacageName;
} else if (appPacageName.toString().contains("whatsapp")) {
shareCheckList[1] = appPacageName;
} else if (appPacageName.toString().contains("mail")) {
shareCheckList[2] = appPacageName;
}
}
dialog.setContentView(layout);
Window window = dialog.getWindow();
window.setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
window.setLayout(MainActivity.screenWidth-90,350);
return dialog;
}
and this is my ontouch listener :
private View.OnClickListener imageClickListener=new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(android.content.Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT,getArguments().getString("subject"));
intent.putExtra(Intent.EXTRA_TEXT,getArguments().getString("body"));
switch (v.getId()){
case R.id.imgFaceebook:
if(!shareCheckList[0].toString().isEmpty()){
intent.setClassName(shareCheckList[0].activityInfo.packageName, shareCheckList[0].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgWhatsapp:
intent.setClassName(shareCheckList[1].activityInfo.packageName, shareCheckList[1].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
}
break;
case R.id.imgMail:
try{
intent.setClassName(shareCheckList[2].activityInfo.packageName, shareCheckList[2].activityInfo.name);
((Activity)getActivity()).startActivity(intent);
break;
case R.id.imgMore:
CustomDialogFragment2 cdf=new CustomDialogFragment2();
Bundle bundle = new Bundle();
bundle.putString("body",id);
bundle.putString("subject", postTitle);
cdf.setArguments(bundle);
cdf.show(getActivity().getFragmentManager(), "customDialogFragment2");
break;
}
}
};
If I understood right, you should use state list:
http://developer.android.com/guide/topics/resources/drawable-resource.html#StateList
Just create xml drawable file for every ImageView, and set for each state item with source you need.
I have next and previous button for changing the image. When activity launch, image comes from previous activity. I use bundle object for getting image on my current activity. Actually 2 images use for pass it on bundle(image_a_inner and image_a_outer). One image overlap on second image and set on custom view. Now i want to when any image comes from bundle then i press next button or previous button then according to position image will be change. For example, images like A_Z alphabet. When i press on D image then it display on my activity using bundle and when i press next button then E image will be shown or when i press previous button then C image will be shown. Below is my code.
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int imageRes1 = extras.getInt("picture1");
int imageRes2 = extras.getInt("picture2");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(imageRes1, imageRes2);
btn_next = (Button) findViewById(R.id.btn_next);
// btn_next.setOnClickListener(this);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_a_inner||imageRes2==R.drawable.img_a){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index++;
}
else if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index++;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_d_inner, R.drawable.img_d);
index++;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_e_inner, R.drawable.img_e);
index++;
}
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
// btn_prev.setOnClickListener(this);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
initializeMP();
playsound.start();
if(imageRes1==R.drawable.img_b_inner||imageRes2==R.drawable.img_b){
mDrawingView.setShape(R.drawable.img_a_inner, R.drawable.img_a);
index--;
}
else if(imageRes1==R.drawable.img_c_inner||imageRes2==R.drawable.img_c){
mDrawingView.setShape(R.drawable.img_b_inner, R.drawable.img_b);
index--;
}
else if(imageRes1==R.drawable.img_d_inner||imageRes2==R.drawable.img_d){
mDrawingView.setShape(R.drawable.img_c_inner, R.drawable.img_c);
index--;
}
});
}
If you have lists for all images in advance, declare them as arrays.
Otherwise, you can pass them using bundle with getIntArray() and putIntArray().
Now, you have lists of images like this,
// These are can be declared as member or static variables.
int[] innerPictures = {R.drawable.image_a_inner, R.drawable.image_b_inner, ...}
int[] pictures = {R.drawable.image_a, R.drawable.image_b, ...}
or
int[] innerPictures = extras.getIntArray("innerPictures");
int[] pictures = extras.getIntArray("pictures");
And you need the index of image to be displayed at the first time, it can be also passed as a extra
int displayingIndex = extra.getInt("pictureIndex"); // it has to be member variable to use inside of listener
So code is like below,
private DrawingView mDrawingView;
Bundle extras = getIntent().getExtras();
int[] innerPictures = ...
int[] pictures = ...
displayingIndex = extra.getInt("pictureIndex");
mDrawingView = (DrawingView) findViewById(R.id.drawing_view);
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
btn_next = (Button) findViewById(R.id.btn_next);
btn_next.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex + 1 == innerPictures.length) return;
displayingIndex++;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
}
});
btn_prev = (Button) findViewById(R.id.btn_prev);
btn_prev.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
initializeMP();
playsound.start();
if (displayingIndex == 0) return;
displayingIndex--;
mDrawingView.setShape(innerPictures[displayingIndex], pictures[displayingIndex]);
});
}
Sorry for bad indentation.
I have button on (CustomDilaog activity) when clicked show custom dialog with password edittext and OK button and cancel button , if you put correct password it open another activity (Text activity), till now every things work fine ,
i have question with two parts.
Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss
Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .
(CustomDilaog activity):
public class CustomDilaog extends Activity {
final Context context = this;
private Button button;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView tv=(TextView)findViewById(R.id.introclusion_tv1);
tv.setTypeface(FontFactory.getBFantezy(getBaseContext()));
TextView tv1=(TextView)findViewById(R.id.introclusion_tv2);
tv1.setTypeface(FontFactory.getBFantezy(getBaseContext()));
tv1.setText(Html.fromHtml(getString(R.string.introclusion)));
button = (Button) findViewById(R.id.button1);
// add button listener
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
// custom dialog
final Dialog dialog = new Dialog(context,R.style.cust_dialog);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.custom);
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
Typeface font = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
text.setTypeface(font);
text.setText("write password :");
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
Typeface font1 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
dialogButton.setTypeface(font1);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
EditText password = (EditText) dialog.findViewById(R.id.password);
////
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
Intent intent = new Intent(CustomDilaog.this,Text.class);
startActivity(intent);
}
else{
// get your custom_toast.xml layout
LayoutInflater inflater = getLayoutInflater();
View layout = inflater.inflate(R.layout.custom_toast,
(ViewGroup) findViewById(R.id.custom_toast));
// set a dummy image
ImageView image = (ImageView) layout.findViewById(R.id.image_toast);
image.setImageResource(R.drawable.ic_launcher);
// set a message
TextView text = (TextView) layout.findViewById(R.id.text_toast);
text.setText("Wrong password");
// Toast...
Toast toast = new Toast(getApplicationContext());
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
toast.setView(layout);
toast.show();
}
}
}
});
Button dialogButtonCancell = (Button) dialog.findViewById(R.id.cancel);
Typeface font11 = Typeface.createFromAsset(getAssets(), "BFantezy.ttf");
dialogButtonCancell.setTypeface(font11);
dialogButtonCancell.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
}
});
}
}
call dialog.dismiss() at the beginning of dialogButton click listener:
dialogButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
EditText password = (EditText) dialog.findViewById(R.id.password);
if( password.getText().toString().length() > 0 ) {
...
}
});
Part one : when im in (Text activity) and press back button to return to (CustomDilaog activity) , still the dialog show over it , how to let it dismiss
Simply call finish() in your CustomDialogActivity when you start the new Activity. It looks like your Intent is calling this same Activity though so I'm a little confused on that.
if( password.getText().toString().length() > 0 ) {
if( password.getText().toString().equals("test")) {
Intent intent = new Intent(CustomDilaog.this,Text.class);
startActivity(intent);
Part two : after dialog fired , if i dont write password and just click OK button with edittext empty it has no response , how to let this click just dismiss dialog without no action ( which is open (Text activity) if wrote correct password .
Add an else statement to your first if and inside it put dialog.dismiss()
Need some advice again.
I am 99% done with this module I'm working on, although I'm stuck at the last hurdle. I'm dynamically publishing a button on the screen at runtime. This button will take it to a new activity view when pressed. I got that working perfectly.
However, I have another button which randomly changes the view so it effectively needs to reset itself, if that makes sense. What's happening is each time the button is clicked (the dynamic one) it then adds another button to the stack. Effectively I have buttons each time I click running down the screen. Is my logic wrong or is there a way to check and prevent the button to show each time? i.e. Just once...Below is the code.
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
EDIT: How about this? Add a new class field member (a class variable, not a method's local variable) that indicates whether the button was actually added in the layout.
private boolean buttonShown = false; /* Here changed */
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (buttonShown == false) { /* Here changed */
Button myButton = new Button(this);
myButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(myButton);
final Button myButton1 = myButton;
myButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
buttonShown = true; /* Here changed */
} /* Here changed */
break;
}
}
Edited again: Instead of local variable myButton, I used class field member pressMeButton.
private Button pressMeButton;
public void ButtonOnClick(View v) {
Random rnd = new Random();
int randomListIndex = rnd.nextInt(2);
Animation myFadeInAnimation = AnimationUtils.loadAnimation(Page1.this, R.anim.fadein);
int firstRun = 0;
switch (randomListIndex) {
case 0:
//get the image your going to muck with
image = (ImageView) findViewById(R.id.cardImageView);
//set the image with what it should be
image.setImageResource(R.drawable.storm);
//apply the transition effect so it looks correct
image.startAnimation(myFadeInAnimation);
button = (Button)findViewById(R.id.dynamicButton);
button.setText("Need another question?");
if (pressMeButton == null) {
pressMeButton = new Button(this);
pressMeButton.setText("Press Me");
LinearLayout layout = (LinearLayout) findViewById(R.id.nextPageContainer);
layout.addView(pressMeButton);
}
/* If the pressMeButton is already in the layout, all you need to do is just changing the onClickListener. */
pressMeButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View arg0) {
String activityName = "Storm";
Intent intent = new Intent(Page1.this, Page2.class);
intent.putExtra(ACTIVITYNAME,activityName);
startActivity(intent);
}
});
break;
}
}
Your code is fine but the way it is it will always add a button to the screen. Whats the desired behavior your looking for? If its a conditional thing than you need to account for that in the onclick call.
EDIT:
So what you need to do then is to create some kind of flag or id for the button and set it as the button's view.tag like this:
Button b = new Button(context);
b.setTag(flagTag);
Then when you want to check if the button exists you check it like:
if((LinearLayout) findViewById(R.id.nextPageContainer))
.findViewById(tagFlag)==null){
Button b = new Button(context);
b.setTag(flagTag);
}else{
//do nothing or something :p
}