Has anyone know how to create a dialogbox like the picture showing above?
Rounded corner.
Transparent background.
No title, buttons, border.
Fade-in -- delay for 5 seconds -- fade out.
*I have seen toast, popup window, dialog, alert dialog, which of these best suit the above? :)
It would be nice if some code snippet could be provided, I'm fairly new to android :)
For custom dialog check http://www.c-sharpcorner.com/UploadFile/2fd686/androd-dialogs/
private void createCustomDialog(){
//Create a dialog object
final Dialog dialog = new Dialog(MainActivity.this);
//Set its layout
dialog.setContentView(R.layout.custom_dialog_layout);
//Set the title
dialog.setTitle("This is custom layout");
//Make it cancelable
dialog.setCancelable(true);
//We need to dismiss the dialog so we add a listener to the ok button
dialog.findViewById(R.id.okButton).setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
dialog.dismiss();
}
});
dialog.show();
}
}
For the dark alpha background you can create a drawable. Below code will give you a semi transparent background with round corners.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item>
<shape android:shape="rectangle">
<gradient
android:startColor="#AA000000"
android:endColor="#AA000000"
android:angle="-90"
android:type="linear"
/>
<corners android:radius="10dp" />
</shape>
</item>
</layer-list>
For the auto hide part you can use
Animation anim = new AlphaAnimation(1,0);
anim.setDuration(300);
anim.setStartOffset(5000);
anim.setInterpolator(new LinearInterpolator());
anim.setFillAfter(false);
myView.startAnimation(anim);
It is not a problem at all. Just create 9nth patch drawable with delays and fading and put it as bg for the dialog.
Try this
Create XML with with your desired Content and then set transparent image to that
I am providing you image, use this
and the
Declare field of type PopupWindow.
PopupWindow popup;
inflate your layout here
View v = inflatter.inflate(R.layout.yourlayout, null);
set your layout to the popup window
v1.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
int height1 = v1.getMeasuredHeight();
popup= new PopupWindow(v, (int) (width * 0.8), height1, true);
popup.showAtLocation(mainlayout, Gravity.CENTER, 0, 0);
mainlayout is your activity view group
this is the piece of code that I have used in my app.
Example I have used something like this in my app
Custom Toast would do everything for you, just prepare your xml and set it to Toast, here is a sample:
public class CustomToast {
public CustomToast(Context ctx, CharSequence text) {
LayoutInflater inflater = LayoutInflater.from(ctx);
View layout = inflater.inflate(R.layout.toast_layout, null);
TextView txt = (TextView) layout.findViewById(R.id.toastText);
txt.setText(text);
Toast myToast = new Toast(ctx.getApplicationContext());
myToast.setGravity(Gravity.CENTER_VERTICAL, 0, 100);
myToast.setDuration(Toast.LENGTH_SHORT);
myToast.setView(layout);
myToast.show();
}
}
Related
I just installed the Android R (API 30) image in my emulator to try my app, and it crashed when trying to set the Background color of a Toast.
Toast toast = Toast.makeText(ctxt, msg, duration);
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
toast.show();
This is really strange as in Android Q (API 29) works perfectly.
My build.gradle updated for Android R (API 30)
compileSdkVersion 30
buildToolsVersion "30.0.1"
Is there a new way to do it??
Since Android 11, custom toasts/ toast modifications are deprecated, according to Google to "protect users". Hence why your app in Android 30 is not able to display custom toasts.
From Android Developers documentation:
Custom toast views are deprecated. Apps can create a standard text toast with the makeText(android.content.Context, java.lang.CharSequence, int)
The only way I have found of showing custom toasts from API 30 onwards is by creating them ad hoc.
XML LAYOUT
Customize as needed
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".main_activity">
<!--Ad hoc toast Textview-->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="12dp"
android:layout_margin="18dp"
android:background="#drawable/ad_hoc_toast_background"
android:textColor="#1e1e1e"
android:gravity="center"
android:visibility="gone"
android:layout_alignParentBottom="true"
android:id="#+id/ad_hoc_toast_textview"
tools:text="Temporary message bla bla bla ..."/>
</RelativeLayout>
TOAST BACKGROUND (ad_hoc_toast_background.xml)
Customize as needed
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<shape
xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="rectangle">
<size
android:width="220dp"
android:height="100dp"/>
<corners
android:radius="25dp"
/>
<solid
android:color="#e6ffffff"
/>
</shape>
</item>
</selector>
Define the show_ad_hoc_toast() method
private void show_ad_hoc_toast(final TextView ad_hoc_toast_textview, String text){
//Set the text
ad_hoc_toast_textview.setText(text);
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(0f, 1f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation){}
#Override public void onAnimationRepeat(Animation animation){}
#Override public void onAnimationEnd(Animation animation){
//After 2250 millis -> hide the toast
new CountDownTimer(2250, 1) {
public void onTick(long millisUntilFinished){}
public void onFinish() {hide_ad_hoc_toast(ad_hoc_toast_textview);}
}.start();
}
});
//Make the view visible
ad_hoc_toast_textview.setVisibility(View.VISIBLE);
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
Define the hide_ad_hoc_toast() method
private void hide_ad_hoc_toast(final TextView ad_hoc_toast_textview){
//Create alpha animation
AlphaAnimation animation1 = new AlphaAnimation(1f, 0f);
//Set duration
animation1.setDuration(300);
//Set that the animation changes persist once the animation finishes
animation1.setFillAfter(true);
//Set on AnimationEnd Listner
animation1.setAnimationListener(new Animation.AnimationListener() {
#Override public void onAnimationStart(Animation animation) { }
#Override public void onAnimationRepeat(Animation animation) { }
#Override public void onAnimationEnd(Animation animation) {
//Make the view gone
ad_hoc_toast_textview.setVisibility(View.GONE);
}
});
//Start animation
ad_hoc_toast_textview.startAnimation(animation1);
}
Call the method from your code when needed
//Find ad_hoc_toast textview
TextView ad_hoc_toast_textview = findViewById(R.id.ad_hoc_toast_textview);
//Define the text to be shown
String text = "This is the custom toast message"
//Show the ad_hoc toast
show_ad_hoc_toast(ad_hoc_toast_textview, text);
RESULT
You can check before to custumized toast
Toast toast = Toast.makeText(ctxt, msg, duration);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.R) {
View view = toast.getView();
view.setBackgroundColor(0xFF303030);
TextView tview = view.findViewById(android.R.id.message);
tview.setTextColor(Color.WHITE);
}
toast.show();
#pvalle & #Aayush Panda, It works for me in Android 11. Please check below code
public static void showCenterToastMessage(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View layout = inflater.inflate(R.layout.custom_toast,null);
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText(msg);
text.setPadding(20,0,20,0);
text.setTextSize(18);
text.setTextColor(Color.WHITE);
Toast toast = new Toast(context);
toast.setGravity(Gravity.CENTER_VERTICAL, 0, 0);
toast.setDuration(Toast.LENGTH_LONG);
layout.setBackgroundColor(Color.DKGRAY);
toast.setView(layout);
toast.show();
}
layout is as follows
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/text"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
The solution with setting a custom view on Toast is deprecated for API level 30
Documentation says
This method was deprecated in API level 30. Custom toast views are
deprecated. Apps can create a standard text toast with the
makeText(android.content.Context, java.lang.CharSequence, int) method,
or use a Snackbar when in the foreground. Starting from Android
Build.VERSION_CODES#R, apps targeting API level Build.VERSION_CODES#R
or higher that are in the background will not have custom toast views
displayed.
There is a walkaround though which still works and is not deprecated
Toast.makeText(applicationContext,
HtmlCompat.fromHtml("<font color='red'>custom toast message</font>", HtmlCompat.FROM_HTML_MODE_LEGACY),
Toast.LENGTH_LONG).show()
Html color tag can also be <font color='#ff6347'>
WindowManager interface can be an alternative to the toast after the Android 11 limitations.
https://developer.android.com/reference/android/view/WindowManager
But you just need user permission to display custom messages over the apps.
I am developing android application and i am trying to apply some roll effect from top of screen for popup window but don't know how to achieve but currently i am adding some other animation effect
this is my popup window function code
private void loadingPopup() {
LayoutInflater inflater = getActivity().getLayoutInflater();
final View layout = inflater.inflate(R.layout.profile_popup, null);
final PopupWindow windows = new PopupWindow(layout , 450,650,true);
windows.setFocusable(false);
windows.setTouchable(true);
windows.setOutsideTouchable(true);
windows.setAnimationStyle(R.style.AnimationPopup);
layout.post(new Runnable() {
public void run() {
windows.showAtLocation(layout, Gravity.TOP, 0, 0);
}
});
name = (TextView)layout.findViewById(R.id.name);
profilepicture =(ImageView)layout.findViewById(R.id.profileimage);
String sname = profilelistdb.get(0).get("pname");
name.setText(sname);
String imagename = profilelistdb.get(0).get("pimage");
String totalurl = imageurl+imagename;
imageLoader1.DisplayImage(totalurl, profilepicture);
btnClosePopup = (Button) layout.findViewById(R.id.btn_close_popup);
btnClosePopup.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
windows.dismiss();
}
});
}
This is Style.xml
<style name="AnimationPopup">
<item name="#android:windowEnterAnimation">#anim/appear</item>
</style>
appear.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:shareInterpolator="false" >
<translate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromYDelta="50%p" android:toYDelta="0%p"
android:duration="#android:integer/config_longAnimTime"/>
</set>
My Requirement:-
My Popup window should come from top with roll kind of thing and whenever user click close button it should hide popup with reverse roll effect could you please help me how to achieve this.
If you don't understand about what i am trying to say about roll
just check this link and see 30 seconds
https://www.youtube.com/watch?v=KSHVSswMUng this is what exactly i need.
You could use this library. It's for View Animations in android, and works very well. And then use this:
YoYo.with(Techniques.RollIn)
.duration(700)
.playOn(findViewById(R.id.edit_area));
YoYo.with(Techniques.RollOut)
.duration(700)
.playOn(findViewById(R.id.edit_area));
Or experiment with it yourself, to see what looks the best.
Edit: Not sure how to achieve that particular roll-effect that you have in mind now that I see a video of it, but if you can't find it, I'm sure you'll find something in this library that will look good. Good luck.
I have two images which I want to fade between causing a glowing on and off effect. This should run all the time like an animation, not just when the button is pressed.
Here are the two images:
This was working well before but after some hardware/android OS updates my animation is really jumpy. Here is the Animation XML I was using:
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" >
<item android:drawable="#drawable/bottom_bar_add_dark"/>
<item android:drawable="#drawable/bottom_bar_add" android:duration="500" />
</animation-list>
I have looked high and low and cannot find an answer to this.
Edit
Here is the code that creates the image view and sets all its resources:
public ImageView findDevicesButton(){
bottomButton = new ImageView(this);
int id = bottomButton.generateViewId();
bottomButton.setId(id);
if(currentapiVersion >= 11){
bottomButton.setImageResource(R.drawable.animationxmladddevice);
//Background image
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
saveButtonAnimation = (AnimationDrawable)bottomButton.getDrawable();
saveButtonAnimation.setEnterFadeDuration(1000);
saveButtonAnimation.setExitFadeDuration(1000);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}else{
bottomButton.setImageResource(R.drawable.bottom_bar_add);
bottomButton.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
bottomButton.setBackgroundResource(R.drawable.bottom_bar);
bottomButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
findDevicesAlertBuilder();
}
});
}
return bottomButton;
}
This is the back background image:
All together it should look like this with the center button glowing:
I'm trying to implement this scenario:
I get my images
I add them dynamically to ScrollView
For each Image I add OnClick listener, so when I click the image, I get a Dialog showing up with the image full screen (basically, something like ThickBox in JQuery)
My code:
decodetByteFullSize = getImageFromApi();
decodedByte=scaleToFitWidth(decodetByteFullSize, photoScroll.getWidth());
String DESIREDTEXT = GetStringFromApi();
ImageView resultImage = new ImageView(this);
resultImage.setImageBitmap(decodedByte);
resultImage.setPadding(0, 0, 0, 10);
resultImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Dialog imagePopup = new Dialog(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.image_layout, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(DESIREDTEXT);
ImageView iv = (ImageView)view.findViewById(R.id.tabsImage);
iv.setImageBitmap(decodetByteFullSize);
imagePopup.setContentView(view);
imagePopup.show();
}
});
photoScroll.addView(resultImage);
The question - how to pass DESIRED TEXT and decodetByteFullSize parameters to onClick event, as it also is dynamical and different for each image?
Thanks in advance!
final ImageView resultImage = new ImageView(this);
resultImage.setTag(DESIRED TEXT);
resultImage.setImageBitmap(decodedByte);
resultImage.setPadding(0, 0, 0, 10);
resultImage.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Dialog imagePopup = new Dialog(MainActivity.this);
View view = getLayoutInflater().inflate(R.layout.image_layout, null);
TextView tv = (TextView) view.findViewById(R.id.tabsText);
tv.setText(resultImage.getTag());
imagePopup.setContentView(view);
imagePopup.show();
}
});
Use the keyword final on the variables you want to be able to access in the onClickListener.
If by ScrollView you actually meant ListView, you probably don't want to create a new listener each time, and instead put it in the tag as Digvesh suggested.
The tag of a View can be used as a map, using a resource id as a key: resultImage.setTag(R.id.text, GetStringFromApi());, resultImage.setTag(R.id.image, decodeByteFull);. The values can then be retrieved using resultImage.getTag(R.id.text) & resultImage.getTag(R.id.image);
If you have a View named text & image somewhere in your xml then you don't need to do anything else, but if you want unique ids for this purpose you can make ids in a ids.xml file in your res\values directory:
<?xml version="1.0" encoding="utf-8"?>
<resources>
<item type="id" name="text" />
<item type="id" name="image" />
</resources>
I've created a class that generates a PopupWindow based on parameters fed into it. I believe this will suit my needs better than manipulating XML-based content for my PopupWindow.
Creating the window and it's content seems to go through smoothly - it's actually getting that content to appear on the screen that I haven't been able to manage yet. The problem is that I haven't been able to find an example of PopupWindow code in use that doesn't rely on the LayoutInflater function to place it on the screen. As my PopupWindow isn't generated from an XML file, I can't use LayoutInflater to place it on the screen.
Something else I should probably explain is that my PopupWindow-generating class is in it's own file. i.e. It is not a subclass of an Activity file. I've done it this way so that I can easilly copy my custom-PopupWindow class to any future projects I might develop.
Here is the basic layout of my class:
class myPopup extends Object {
public myPopup(parameters){
ViewGroup winBody;
// "winbBody" will be the content of the PopupWindow.
// Code that fills and adjusts "winBody" based on the parameters goes here.
int width = //Determined by parameters.
int height = //Determined by parematers.
PopupWindow pw = new PopupWindow(winBody, width, height, true);
//This is as far as I seem to get before getting stuck.
}
}
I gather that I am supposed to somehow use the PopupWindow function "showAtLocation", but I am unclear what parameters I am supposed to use for this. Could someone please tell me how to get my popup window to appear on the screen? Hopefully in the very center of it. :)
Try something like that
you may define the whole popup window as an activity and just make its background disapear, so it will look like a popup window:
<style name="MyTransparentPopup">
<item name="android:windowBackground">#android:color/transparent</item>
<item name="android:background">#android:color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowNoTitle">true</item>
</style>
public class PopupWindowActivity extends Activity {
PopupWindow popUp;
LinearLayout layout;
TextView tv;
LayoutParams params;
LinearLayout mainLayout;
Button but;
boolean click = true;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
popUp = new PopupWindow(this);
layout = new LinearLayout(this);
mainLayout = new LinearLayout(this);
tv = new TextView(this);
but = new Button(this);
but.setText("Show popup");
but.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if (click) {
popUp.showAtLocation(mainLayout, Gravity.CENTER, 0, 0);
popUp.update(0, 0, 300, 80);
click = false;
} else {
popUp.dismiss();
click = true;
}
}
});
params = new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
layout.setOrientation(LinearLayout.VERTICAL);
tv.setText("Hello popup");
layout.addView(tv, params);
popUp.setContentView(layout);
mainLayout.addView(but, params);
setContentView(mainLayout);
}
}