How to display a messgae on top of each activity? - java

I am checking the internet connection in my application, when the internet is not accessible I want to display a small popup message to the user that you're offline.
I tried to solve the issue by using AlertDialog and AlertDialog.Builder, and I have also searched through different solution on the internet but no solution resolve my issue. I am trying to do this by the following method.
AlertDialog.Builder builder = new AlertDialog.Builder(context);
LayoutInflater inflater = LayoutInflater.from(context);
View alertdialog = inflater.inflate(R.layout.nointernetdialogue, null);
builder.setView(alertdialog);
AlertDialog dialog = builder.create();
dialog.getWindow().setType(WindowManager.LayoutParams.TYPE_TOAST);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
WindowManager.LayoutParams alertTop = dialog.getWindow().getAttributes();
alertTop.gravity = Gravity.TOP | Gravity.START;
alertTop.x = 100;
alertTop.y = 100;
dialog.show();
The result I want.

Create BaseActivity which shall implement Broadcast Receiver regarding network connectivity checks. Whenever network connectivity goes off, show the SnackBar/Alert.
Let all your other activities extend this Base activity.

Alert dialog looks like a over kill just have a TextView and show and hide it on network change.

Try with this example Click here
here i am using BroadcastReceiver to find the Wifi and Mobile data status
By this you can access the Network state anywhere un the App

You should create a BaseActivity that checks internet connection and shows alert dialog if needed. All activities that you want to show alert dialog should extended from BaseActivity.

You can work with top SnackBar , there is the code
Snackbar snack = Snackbar.make(findViewById(android.R.id.content), "Online", Snackbar.LENGTH_LONG);
View view = snack.getView();
FrameLayout.LayoutParams params =(FrameLayout.LayoutParams)view.getLayoutParams(); params.gravity = Gravity.TOP;
view.setLayoutParams(params);
snack.show();

Related

Android: Getting Resource ID before AlertDialog is created and shown?

I'm currently working on letting a user tap an image that will then display an AlertDialog with a zoomed in version of that image. I'm not too sure if the AlertDialog is the best way to go about this but it's my solution for now.
I'm finding myself struggling getting the Resource ID for an ImageView before I show the alert dialog. Here is the exact piece of code where the error is occurring (The comments are failed fix attempts):
mPhotoView.setOnClickListener(new View.OnClickListener() {
#SuppressLint("ResourceType")
#Override
public void onClick(View view) {
if(mPhotoView != null){
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = requireActivity().getLayoutInflater();
dialog.setView(inflater.inflate(R.layout.zoomed_image, null)).create();
//medImg.setImageResource(dialog.getContext().getResources().getLayout(R.layout.zoomed_image).getIdAttributeResourceValue(R.id.ivZoomedImg));
//ivZoomedImg = getActivity().findViewById(R.id.ivZoomedImg);
//ivZoomedImg = view.findViewById(R.id.ivZoomedImg);
Bitmap bm = PictureUtils.getScaledBitmap(mPhotoFile.getPath(), getActivity());
ivZoomedImg.setImageBitmap(bm); // <- null reference
dialog.show();
}
}
});
Specifically, I can't seem to figure out a way to access the R.id.ivZoomedImg that should have been instantiated through the LayoutInflator? I'm not even sure if that's possible to be honest, though I don't see why it wouldn't. Does anyone know what would be the best way to go around this?
Some things to note:
R.layout.zoomed_image is just an XML layout containing a single ImageView with the id of R.id.ivZoomedImg
mPhotoView is the image being clicked on to have it displayed bigger
The fix involved simply instantiating an independent View where you can pull the ImageView resource Id from!
Lines of coded added:
View v = inflater.inflate(R.layout.zoomed_image, null);
ivZoomedImg = v.findViewById(R.id.ivZoomedImg);
dialog.setView(v).create();

'Running something in background inside a dialog

I have some codes like this:
ArrayList<Teacher> teachers = subs.get(position).getTeachers();
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.dialog_layout, null);
builder.setView(v);
ListView listViewDialog = (ListView) v.findViewById(R.id.dialog_lv);
TesAdapter adapter1 = new TesAdapter(MainActivity.this, teachers);
listViewDialog.setAdapter(adapter1);
adapter1.notifyDataSetChanged();
listViewDialog.setDivider(null);
builder.show();
This operation takes a lot of time. So, I am trying to show a ProgressDialog while it is loading. then to hide the progressbar and show this AlertDialog.
I have tried these:
How to show progress dialog in Android?
How can I run code on a background thread on Android?
But none of them worked :( any solution?

How to display drop-down list-view below particular view in android?

I have one filter button.I need to open expandable/dropdown filter same as given below image.I tried popup menu and Listpopup window but,I did not get any success.
Thanks in advance
you can make use of fragment which will be below the filter button,layout will include the ui you expect i.e upper arrow and expandable view or any view you are using..
After clicking on filter just visible the fragment and with fragment transition,for the effect you can use animation too...
This i already done,you can refer carwale app used car section..click on filter button one same dilog will come..
for any thing let me know further..
I did code by the help of above comments. but the code looks as below.
final View popUpView = getLayoutInflater().inflate(R.layout.exp, null);
listPopupWindow = new PopupWindow(popUpView, LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, true);
expandableListView= (ExpandableListView) popUpView.findViewById(R.id.expandableListView);
btnticker.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
expandableListDetail = ExpandableListDataPump.getData();
expandableListTitle = new ArrayList<String>(expandableListDetail.keySet());
expandableListAdapter = new CustomExpandableListAdapter(MainActivity.this, expandableListTitle, expandableListDetail);
expandableListView.setAdapter(expandableListAdapter);
listPopupWindow.showAsDropDown(v);
}
});

Android: How to add a layer on top no matter how many activities started?

I want to play a gif animation and this view will move on the screen every second, but if I start an activity I need to add this view again and the position will change.
See the attached image below. I need to stick this over every view. Any way to do it?
Create one BaseActivity for all activities in your app to show Overlay View or Layout.
you can inherit BaseActivity on other Activity
You can add gif supported views or layouts
for example i added overlay_layout in my showOverlay method.
you can call showOverlay method where ever you want to show and you can remove with removeOverlay with conditions.
Please note that showOverlay and removeOverlay should be in BaseActivity
void showOverlay(){
LayoutInflater inflater = activity.getLayoutInflater();
layout = inflater.inflate(R.layout.overlay_layout, null);
WindowManager.LayoutParams params = new WindowManager.LayoutParams();
params.gravity = Gravity.BOTTOM;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.type = WindowManager.LayoutParams.TYPE_APPLICATION;
final WindowManager mWindowManager = (WindowManager);
activity.getApplicationContext().getSystemService(Context.WINDOW_SERVICE);
mWindowManager.addView(layout, params);
}
void removeOverlay(){
windowManager.removeView(view);
}
set this view into WindowManager and it will always show on top

Need to show a popup/Dialog on different activities

I have an app that uses three Activities. I've created in the first one a Thread that checks the connection with the server and when the app cant reach the server it shows a Popup.
The thing is that when I go from the Activity1 to the Activity2 and I lose the connection, I'm getting a WindowManager$BadTokenException.
I've tried with PopupWindow and with AlertDialog but I have the same problem, I can't give them the current Activity.
Alert Dialog:
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
builder1.setMessage("Se ha hecho el cierre diario, es necesario reiniciar la aplicaciĆ³n.");
builder1.setCancelable(true);
builder1.setPositiveButton("Ok",
new DialogInterface.OnClickListener()
{
public void onClick(DialogInterface dialog, int id)
{
java.lang.System.exit(0);
}
});
AlertDialog alert11 = builder1.create();
alert11.show();
PopupWindow:
final Activity context = Activity_Start.this;
final boolean Reset = reset;
// Inflate the popup_layout.xml
LinearLayout viewGroup = (LinearLayout) context.findViewById(R.id.popup_mensaje_error);
LayoutInflater layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View popup_error = layoutInflater.inflate(R.layout.mensaje_error, viewGroup, false);
// Creating the PopupWindow
final PopupWindow popupW_error = new PopupWindow(context);
popupW_error.setContentView(popup_error);
In both cases I have the same error and I'm almost 100% sure that is cause getApplicationContext() is no enough to get what the app needs.
Can someone help me? Thanks!!
Android documents suggests to use getApplicationContext();
but it will not work instead of that use your current activity while instantiating AlertDialog.Builder or AlertDialog or Dialog...
Try
AlertDialog.Builder builder1 = new AlertDialog.Builder(Activity_Start.this);
instead of
AlertDialog.Builder builder1 = new AlertDialog.Builder(getApplicationContext());
And also use
final Context context = Activity_Start.this;
instead of
final Activity context = Activity_Start.this;
The dialog is tied to this particular activity, and not the entire application. Generally use Activity.this when creating objects whose scope is just the activity. Use Application context when you are creating objects whose scope is beyond the current activity.
Hope this helps!

Categories

Resources