Show GIF using Glide in AlertDialog - android

I want to show GIF using Glide in ImageView, and set this ImageView in AlertDialog.
ImageView gifImageView = new ImageView(context);
Glide.with(context).load(recorder.getPictureFile()).asGif().into(gifImageView);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(context);
share_dialog.setMessage("Live Message Saved");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
share_dialog.show();
But it only shows Message and PositiveButton.
I can't understand why, what should I do to fix it ?
Update:2017/09/23
I create another project to test this issue, here is the code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_main = (Button)findViewById(R.id.button_main);
imageView_main = (ImageView)findViewById(R.id.imageView_main);
GlideDrawableImageViewTarget imageViewMainTarget = new GlideDrawableImageViewTarget(imageView_main);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewMainTarget);
button_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ImageView gifImageView = new ImageView(MainActivity.this);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(gifImageView);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewTarget);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(MainActivity.this);
share_dialog.setMessage("GIF Test");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
share_dialog.show();
}
});
}
After running this project.
GIF can display in imageView_main, but it can't be shown on AlertDialog's gifImageView !
Why ?

I solved it, according to the answer of Niranj Patel.
How to put an image in an AlertDialog?? Android
Here is my code.
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button_main = (Button)findViewById(R.id.button_main);
imageView_main = (ImageView)findViewById(R.id.imageView_main);
GlideDrawableImageViewTarget imageViewMainTarget = new GlideDrawableImageViewTarget(imageView_main);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewMainTarget);
button_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater factory = LayoutInflater.from(MainActivity.this);
View view = factory.inflate(R.layout.sample, null);
ImageView gifImageView = (ImageView)view.findViewById(R.id.dialog_imageview);
GlideDrawableImageViewTarget imageViewTarget = new GlideDrawableImageViewTarget(gifImageView);
Glide.with(MainActivity.this).load(R.raw.test).into(imageViewTarget);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(MainActivity.this);
share_dialog.setMessage("GIF Test");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(view);
share_dialog.show();
}
});
}

Do it like this
ImageView gifImageView = new ImageView(context);
AlertDialog.Builder share_dialog = new AlertDialog.Builder(context);
share_dialog.setMessage("Live Message Saved");
share_dialog.setPositiveButton("Done", null);
share_dialog.setView(gifImageView);
Glide.with(context).load(recorder.getPictureFile()).asGif().into(gifImageView);
share_dialog.show();
put your load GLIDE after setting the view.

Related

Can't get an ImageView in custom AlertDialog within an custom AlertDialog, updated

sry for my english..
In my app from Home_Activity, i click on an Button and a custom AlertDialog shows up. Within this AlertDialog(DialogAdd), i click on an ImageView say iv1 (or ImageButton - both didn't work) and another custom AlertDialog opens(Choose) - so far so good. Now i have my 2nd AlertDialog opened and see, say 4 little pictures. When i click on one of these pictures, the second AlertDialog should close and the ImageView (iv1) should change to that choosen icon from AlertDialog 2. I got everything running but not to change this ImageView/ImageButton...
I seperated this behaviour into a little app..
Home_Activity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class Home_Activity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.btn_showDialog);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
DialogAdd addEntrieActivity = new DialogAdd(Home_Activity.this);
addEntrieActivity.createDialogAdd(Home_Activity.this);
}
});
}
}
DialogAdd
import android.content.Context;
import android.support.v7.app.AlertDialog;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
public class DialogAdd implements View.OnClickListener, Choose.Choose_Interface {
private LayoutInflater inflater;
private View view;
private Context context;
private AlertDialog dialog;
private String selectetSmile = null;
private ImageView ib_dialog_smile;
public DialogAdd(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.dialog_line, null);
ib_dialog_smile = (ImageView) view.findViewById(R.id.ib_dialog_smile);
ib_dialog_smile.setOnClickListener(this);
}
public void createDialogAdd(Context context) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
#Override
public void onClick(View v) {
if(v.getId() == R.id.ib_dialog_smile){
Choose choose = new Choose(context);
choose.createDialogChoose();
}
}
#Override
public void selectedSmile(String smile) {
selectetSmile = smile;
if(selectetSmile.equals("oh")){
ib_dialog_smile.setImageResource(R.drawable.oh);
}else if(selectetSmile.equals("oh_nooo")){
ib_dialog_smile.setImageResource(R.drawable.oh_nooo);
}else if(selectetSmile.equals("oh_what")){
ib_dialog_smile.setImageResource(R.drawable.oh_what);
}else if(selectetSmile.equals("oh_yes")){
ib_dialog_smile.setImageResource(R.drawable.oh_yes);
}
}
}
Choose
public class Choose {
public interface Choose_Interface {
void selectedSmile(String flag);
}
public Choose_Interface mCallback;
private View view;
private Context context;
private LayoutInflater inflater;
private AlertDialog dialog;
private ImageView oh, oh_nooo, oh_what, oh_yes;
public Choose(final Context context) {
this.context = context;
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view = inflater.inflate(R.layout.choose, null);
mCallback = new DialogAdd(context);
oh = (ImageView) view.findViewById(R.id.oh);
oh.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh");
Toast.makeText(context, "oh klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_nooo = (ImageView) view.findViewById(R.id.oh_nooo);
oh_nooo.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_nooo");
Toast.makeText(context, "oh_nooo klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_what = (ImageView) view.findViewById(R.id.oh_what);
oh_what.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_what");
Toast.makeText(context, "oh_what klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
oh_yes = (ImageView) view.findViewById(R.id.oh_yes);
oh_yes.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mCallback.selectedSmile("oh_yes");
Toast.makeText(context, "oh_yes klicked", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
}
public void createDialogChoose() {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
builder.setView(view);
dialog = builder.create();
dialog.getWindow().setBackgroundDrawableResource(android.R.color.transparent);
dialog.show();
}
}
i've tried it with invalidate ib_dialog_smile, i used an ImageButton and also ImageView, AppCompatImageView, setImageDrawable, setImageResource.. but the image didnt get updated.
Can someone please help me out to change ib_dialog_smile image?
Thank you!
I think your code is a little complicated. You don't need seperate classes to create two custom AlertDialogs. Just create two XML layouts each for every dialog and style them the way you want (in the first XML layout put your "chosen" ImageView and the button and in the second the four images).
Let's call them layout_1 (chosen icon) and layout_2 (four options)
then in your home_activity inflate those in a view var and choose them as alertDialog views like this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//Creating the first dialog.
AlertDialog.Builder dialogOneBuilder = new AlertDialog.Builder(this);
View layout1 = getLayoutInflater().inflate(R.id.layout_1, null);
dialogOneBuilder.setView(layout1)
AlertDialog dialogOne = dialogOneBuilder.show();
//Finding the chosen icon and button image from dialog 1
final ImageView chosenIcon = (ImageView)layout1.findViewById(R.id.chosen_icon);
Button chooseButton = (Button)layout1.findViewById(R.id.choose_button);
//On button click
chooseButton.setOnClickListener(new View.OnClickListener(){
//Creating the second dialog.
AlertDialog.Builder dialogTwoBuilder = new AlertDialog.Builder(this);
View layout2 = getLayoutInflater().inflate(R.id.layout_2, null);
dialogTwoBuilder.setView(layout2)
AlertDialog dialogTwo = dialogTwoBuilder.show();
//finding the ImageViews of the four choices
ImageView img1 = (ImageView)layout2.findViewById(R.id.img1);
ImageView img2 = (ImageView)layout2.findViewById(R.id.img2);
ImageView img3 = (ImageView)layout2.findViewById(R.id.img3);
ImageView img4 = (ImageView)layout2.findViewById(R.id.img4);
//Add them in a LinkedList helps to shrink your code
LinkedList<ImageView> choices = new LinkedList<>();
choices.add(img1);
choices.add(img2);
choices.add(img3);
choices.add(img4);
//Now set ONE ClickListener for all choices
for(int i = 0; i < choices.size(); i++){
choices.get(i).setOnClickListener(new View.OnClickListener(){
//change your icon to the chosen image
chosenIcon.setImageDrawable(choices.get(i).getDrawable());
//And close the second dialog.
dialogTwo.dismiss();
});
}
});
}
Worked perfectly for my voting & commenting dialog. Hope this helped you and the code was readable.

Picasso not loading image, and no error logged

My usecase is to load images from a URL, and I am using picasso, in the following way to acheive that.
ll = (LinearLayout) findViewById(R.id.tasklayout);
ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView);
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
What I see is a 800 * 800 whitespace with no image in it. Looking at the logs, there doesn't seem to any problem. Atleast picasso doesn't record anything.
I replicate your problem with button click and it is working. You can as well check the your path is correct
downloadButton = (Button)findViewById(R.id.download_button);
downloadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout relativeLayout = (RelativeLayout) findViewById(R.id.relative);
ImageView imageView = new ImageView(PicassoDownloadActivity.this);
Picasso.with(PicassoDownloadActivity.this).load(Helper.imageDownloadPath).into(imageView);
relativeLayout.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
});
Picasso won't typically give errors when something goes wrong, i suggest doing something like this:
ll = (LinearLayout) findViewById(R.id.tasklayout);
final ImageView imageView = new ImageView(this);
Picasso.with(this).load(taskFieldDao.data).into(imageView, new Callback() {
#Override
public void onSuccess() {
ll.addView(imageView, new LinearLayout.LayoutParams(800, 800));
}
#Override
public void onError() {
Log.e("Picasso","Image load failed);
}
});

how to display image on imageview dynamically from drawalble folder

public class Serchresult extends Activity implements OnClickListener {
ImageView imageView1;
String Status;
String Reason;
TextView status;
TextView reason;
ImageView statusicon;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.act_serchresult);
Intent intent = getIntent();
Status = intent.getExtras().getString("Status");
Reason = intent.getExtras().getString("Reason");
imageView1 = (ImageView) findViewById(R.id.searchstatus_imgBack);
imageView1.setOnClickListener(this);
status = (TextView) findViewById(R.id.status);
reason = (TextView) findViewById(R.id.reason);
statusicon = (ImageView) findViewById(R.id.imageView1);
reason.setText(Reason.replace("null", ""));
if (reason.equals("ACCEPTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.accept_icon);
} else if (reason.equals("REJECTED")) {
AQuery aq = new AQuery(getApplicationContext());
statusicon.setImageResource(R.drawable.reject_icon);
}
else {
// reason.setCompoundDrawables(null, null, null, null);
statusicon.setImageResource(0);
}
status.setText(Status.replace("null", ""));
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v.getId() == R.id.searchstatus_imgBack) {
finish();
}
}
}
here is my code i want display image from drawable folder accept and reject icon .i am getting ACCEPTED and REJECTED from intent on the basis of this response i want to display image in image view i have apply condition but image is not visible please help me where am doing wrong
Your code seems working. But still you can try following code to display image. Make sure your if-else conditions are working
Bitmap defBitmap = BitmapFactory.decodeResource(getResources(),R.drawable.accept_icon);
statusicon.setImageBitmap(defBitmap);
Maybe the Bitmap is too big for the heap, try use BitmapFactory.Options to reduce the size.
BitmapFactory.Options op = new BitmapFactory.Options();
op.inSampleSize = 4;
statusicon.setImageBitmap(BitmapFactory.decodeResource(getResources(), R.drawable.accept_icon, op));

change background of views on hover inside dialog

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.

Admob ad on custom Dialog

I am trying to implement Admob ads on app. What I want to do is to ad a banner ad on a custom Dialog. I have tried everything but can't find the solution.
I have made a custom xml for the dialog. When adding admob on xml, it won't run. So I tried do it programmatically. But still can't make it work.
public void OnClickButton(View paramView)
{
int btn_id = paramView.getId();
if (btn_id == R.id.hint_field)
{
//set up dialog
if (hint != null && hint.length()>0) {
final Dialog dialog = new Dialog(Activity.this);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
//ad loading
dialog.setContentView(R.layout.custom_dialog);
RelativeLayout layout = (RelativeLayout)findViewById(R.id.dialog_l);
layout.addView(ad);
AdRequest r = new AdRequest();
ad.loadAd(r);
dialog.setTitle("Σχετικά με την λέξη :");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.hint_text);
text.setText(hint);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
// now that the dialog is set up, it's time to show it
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
}
}
On a button click I display a custom Dialog. In the OnCreate method of my Activity I have made the adView
AdView ad = new AdView(this, AdSize.BANNER, "a15xxxxxxxxxxx");
I get a NullPointerException at : layout.addView(ad);
Any ideas ?
Thanks in advance!
Problem solved! I had to inflate the custom dialog interface first. The code below is the working code!
public void OnClickButton(View paramView)
{
int btn_id = paramView.getId();
if (btn_id == R.id.hint_field)
{
//set up dialog
if (hint != null && hint.length()>0) {
final Dialog dialog = new Dialog(Activity.this, R.style.Theme_New);
dialog.requestWindowFeature(Window.FEATURE_LEFT_ICON);
dialog.setContentView(R.layout.custom_dialog);
LayoutInflater inflater =(LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View main = inflater.inflate(R.layout.custom_dialog, null);
dialog.setContentView(main);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
LinearLayout linear = (LinearLayout)main.findViewById(R.id.ad_layout);
ad = new AdView(this, AdSize.IAB_MRECT, "a15xxxxxxxx");
AdRequest request = new AdRequest();
Set<String> keywords = new HashSet<String>();
keywords.add("game");
request.setKeywords(keywords);
linear.addView(ad);
ad.loadAd(request);
dialog.setTitle("Title :");
dialog.setCancelable(true);
//set up text
TextView text = (TextView) dialog.findViewById(R.id.hint_text);
text.setText(hint);
//set up button
Button button = (Button) dialog.findViewById(R.id.Button01);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
dialog.dismiss();
}
});
// now that the dialog is set up, it's time to show it
lp.width = width;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
dialog.show();
dialog.setFeatureDrawableResource(Window.FEATURE_LEFT_ICON, R.drawable.ic_launcher);
dialog.getWindow().setAttributes(lp);
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
dialog.getWindow().getAttributes().height = LayoutParams.WRAP_CONTENT;
}
}
}

Categories

Resources