I'm trying to display a persistent message at the bottom of my application(through out all activities) when there's no internet connection, and the message disappears once there's a network connection
I thought about using and application dialog, which I was able to do, but then I realised the background was not clickable, I need users to still have access to navigate pages/menu
something as seen in the picture below
What I tried with a custom alert dialog
NoInternetDialog.java
public class NoConnectionDialog {
public void showDialog(Activity activity, String msg){
final Dialog dialog = new Dialog(activity);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setCancelable(false);
dialog.setContentView(R.layout.no_internet_connection_dialog);
Window window = dialog.getWindow();
WindowManager.LayoutParams wlp = window.getAttributes();
wlp.gravity = Gravity.BOTTOM;
wlp.flags &= ~WindowManager.LayoutParams.FLAG_DIM_BEHIND;
window.setAttributes(wlp);
TextView text = (TextView) dialog.findViewById(R.id.text_dialog);
text.setText(msg);
// Button dialogButton = (Button) dialog.findViewById(R.id.btn_dialog);
// dialogButton.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
// }
// });
dialog.show();
}
}
the XML
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/zxing_viewfinder_laser"
android:elevation="4dp"
android:orientation="vertical"
android:padding="#dimen/_8sdp"
app:elevation="4dp">
<TextView
android:id="#+id/text_dialog"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/no_internet_connection"
android:textColor="#color/colorWhite"
android:textSize="#dimen/dimen_16" />
</LinearLayout>
and how I call it
NoConnectionDialog alert = new NoConnectionDialog();
alert.showDialog(this, "No Internet Connection");
What do you guys suggest?
Don't use a Dialog, just add that layout to your Activity's layout and set the visibility based on whether the user has an internet connection. This will avoid all of the issues you'll run into trying to do this with a Dialog. If you have multiple Activitys, you could use a common superclass for them that includes this functionality.
Related
I'm trying to show a tutorial when user runs the app for the first time, by using dialog boxes. However, the "back" and "next" buttons aren't visible on smaller screens and/or resolutions, so the user can't proceed with the tutorial.
I tried making the dialog box a ScrollView but it didn't help, buttons are still invisible. What's the best way to resolve this? Thanks in advance!
EDIT: here's an example of the first dialog shown to user:
AlertDialog.Builder w1 = new AlertDialog.Builder(this);
LayoutInflater w1Inflater = LayoutInflater.from(this);
View w1Layout = w1Inflater.inflate(R.layout.dialog, null);
ImageView image1 = (ImageView) w1Layout.findViewById(R.id.my_image);
image1.setImageResource(R.drawable.ic_launcher);
w1.setView(w1Layout);
w1.setTitle("Welcome");
w1.setMessage(Html.fromHtml("Before you start using the app, get familiar with the User Interface!"));
w1.setCancelable(false);
w1.setPositiveButton("Next", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch3();
num = 3;
((ViewGroup)w1Layout.getParent()).removeView(w1Layout);
}
});
And here's the dialog.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<ImageView
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center"
android:id="#+id/my_image"/>
</LinearLayout>
</ScrollView>
I am building an music player. Whenever the user will click on a ImageView, a dialog will come. The dialog will have a switch in it. I wanted to say that whenever the user switches on the switch to on, I wanted to change the ContentView of MainActivity.java to a custom layout file. How can I do that.
Any suggestions would be accepted..
Thanks
I would add a callback to your Dialog that you simply invoke to set your content, something like
class MyDialog(val switchChangedCallback: (Boolean) -> Unit) : DialogFragment() ...
When you change the switch, invoke the callback and handle the result in the Activity:
val dialog = MyDialog(switchChangedCallback = { isOn ->
if (isOn) {
setContentView(R.layout.abc)
} else {
setContentView(R.layout.def)
}
})
dialog.show(supportFragmentManager, MyDialog::class.java.name)
You may need to check that the callback survives app rotation!
You can try below code...
Dialogue :
Dialog dialogue = new Dialog(MainActivity.this);
dialogue.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialogue.setCancelable(false);
dialogue.setContentView(R.layout.dialogue);
Switch mySwitch = (Switch) dialogue.findViewById(R.id.MYSWITCH);
Button btnClose = (Button) dialogue.findViewById(R.id.CLOSEBTN);
mySwitch.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if(isChecked){
setContentView(R.layout.layout1);
}
else{
setContentView(R.layout.layout2);
}
}
});
btnClose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialogue.dismiss();
}
});
dialogue.show();
dialogue.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:layout_height="match_parent">
<Switch
android:id="#+id/MYSWITCH"
android:text="Change Layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<Button
android:id="#+id/CLOSEBTN"
android:text="Close"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
as your question says you need a switch inside dialogue , so we need to create a custom dialogue layout and apply it to dialogue !
Coming from VB world I learned today that dialogs are not modal in Java. I have the task of converting a program we have written in VB.net for windows based Motorola devices to Java using Android Studio for Android Motorola devices.
I am trying to make it as similar to forms in windows.
I am at a form that has that has several messageboxes that prompt for input.
I tried doing java but noticed that dialogs don't always wait for response.
I have read that using dialogfragments can accomplish what I am trying to do.
Here is the XML that I had for the first dialog.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorBackgroundGray"
>
<TextView
android:id="#+id/theText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Holding Text"
android:textAlignment="center"
android:textColor="#color/colorBlack"
android:textSize="30dp" />
<Button
android:id="#+id/buttonYes"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_below="#id/message"
android:layout_margin="8dp"
android:onClick="onClickYes"
android:text="Yes"
android:textSize="26dp" />
<Button
android:id="#+id/buttonNo"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_below="#id/message"
android:layout_margin="8dp"
android:onClick="onClickNo"
android:text="No"
android:textSize="26dp" />
</RelativeLayout>
Here is how I was calling the dialog.
final Dialog dialog = new Dialog(ReturnAreaActivity.this, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
dialog.setContentView(R.layout.dialog_location);
dialog.setTitle("New Location Setup");
TextView message = (TextView) dialog.findViewById(R.id.theText);
message.setText("Is this location a Pick Bin");
Button buttonYes = (Button) dialog.findViewById(R.id.buttonYes);
Button buttonNo = (Button) dialog.findViewById(R.id.buttonNo);
dialog.show();
Having question on what changes would need to be done to turn the dialog into a dialogFragment. I appreciate and clarity anyone can shed. Also the calling of the dialogFragments would be done during a loop
Thanks
First extend DialogFragment and use AlertDialog.Builder to create the dialog inside method onCreateDialog.
public class MyDialogFragment extends DialogFragment {
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
LayoutInflater inflater = getActivity().getLayoutInflater();
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setView(inflater.inflate(R.layout.dialog_location, null))
.setMessage("Is this location a Pick Bin")
.setPositiveButton("Yes", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Handle "yes" button click here.
}
})
.setNegativeButton("No", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// Handle "no" button click here.
}
});
return builder.create();
}
}
Then you can show DialogFragment by using show() method:
DialogFragment newFragment = new MyDialogFragment();
newFragment.show(getSupportFragmentManager(), "MyDialogFragment")
Small advise - don't use dp in your widget textSize, use sp. You can read the explanation here.
I am displaying image in the dialog box which contains the zoom view as well. Here image is placed and zoom is working fine without any issue but problem is image is always placed in the top of the dialog and if i scroll down it comes to middle then zoom happens. I dont want like this instead image should display in the middle and i need to do the zoom.
I am using dynamic imageview and for zoom using chrisbanes photoview library. If i remove the library the image is placed in the center.
#Override
public void onClick(View v) {
Dialog builder = new Dialog(listdisplay, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
builder.requestWindowFeature(Window.FEATURE_NO_TITLE);
builder.getWindow().setBackgroundDrawable(
new ColorDrawable(Color.BLACK));
builder.setCanceledOnTouchOutside(false);
builder.setOnDismissListener(new DialogInterface.OnDismissListener() {
#Override
public void onDismiss(DialogInterface dialogInterface) {
//nothing;
}
});
ImageView imageView = new ImageView(listdisplay);
Glide.with(finalConvertView.getContext()).load(Limage).fitCenter()
.diskCacheStrategy(DiskCacheStrategy.ALL).into(imageView);
DisplayMetrics metrics = new DisplayMetrics();
listdisplay.getWindowManager().getDefaultDisplay().getMetrics(metrics);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(metrics.widthPixels,
metrics.heightPixels);
imageView.setLayoutParams(lp);
//imageView.setScaleType(ImageView.ScaleType.CENTER);
p = new PhotoViewAttacher(imageView);
builder.addContentView(imageView, lp);
builder.show();
}
});
i tried with scaletype for imageview as center but image is placed in top of the dialog.
Use Custom dialog for you own dialog design
final Dialog dialog = new Dialog(context);
dialog.setContentView(R.layout.custom);
dialog.setTitle("Title...");
// set the custom dialog components - text, image and button
TextView text = (TextView) dialog.findViewById(R.id.text);
text.setText("Android custom dialog example!");
ImageView image = (ImageView) dialog.findViewById(R.id.image);
image.setImageResource(R.drawable.ic_launcher);
Button dialogButton = (Button) dialog.findViewById(R.id.dialogButtonOK);
// if button is clicked, close the custom dialog
dialogButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
dialog.show();
Layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginRight="5dp" />
<TextView
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#FFF"
android:layout_toRightOf="#+id/image"/>/>
<Button
android:id="#+id/dialogButtonOK"
android:layout_width="100px"
android:layout_height="wrap_content"
android:text=" Ok "
android:layout_marginTop="5dp"
android:layout_marginRight="5dp"
android:layout_below="#+id/image"
/>
</RelativeLayout>
this is my last desperate attempt to fix this problem.
i am using dialogfragment to make my dialog window MyDialog. the window is supposed to show a score I have sent with and the user can then write in their name and confirm. i am then supposed to be able to retrieve their name.
My first problem is that the edittext that are supposed to show the score, doesn't want to show the score i set the edittext to be (it will only show new text if it was in the xml file). I can however still retrieve the correct score(text) afterwards from the same edittext.
The second problem is that the text that the user writes in can't be retrieved since the edittext isn't updating. However the text that was in the field from the xml file can be retrieved.
It seems like the edittext doesn't want to refresh after they are made in the xml file.
the dialog class (imports taken out):
public class MyDialog extends DialogFragment
{
private EditText scoreEdit;
private EditText userEdit;
private DialogClickListener callback;
public interface DialogClickListener
{
public void onFinishedClick();
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
try
{
callback = (DialogClickListener) getActivity();
}catch(ClassCastException e)
{
throw new ClassCastException("called class must implement interface");
}
}
public static MyDialog newInstance(int title)
{
MyDialog frag = new MyDialog();
Bundle args = new Bundle();
args.putInt("tittel",title);
frag.setArguments(args);
return frag;
}
#Override
public Dialog onCreateDialog(Bundle savedInstanceState)
{
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
LayoutInflater inflater = getActivity().getLayoutInflater();
View inflatedView = inflater.inflate(R.layout.dig_lay, null);
scoreEdit = (EditText) inflatedView.findViewById(R.id.dialogScoreEdit);
userEdit = (EditText) inflatedView.findViewById(R.id.dialogUsernameEdit);
scoreEdit.setText("score supposed to change");
// scoreEdit (edittext) is not changing
dialog.setView(inflater.inflate(R.layout.dig_lay, null));
dialog.setTitle("Title");
dialog.setPositiveButton("reg1", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface dialog, int whichButton)
{
System.out.println("new text supposed to be: " + userEdit.getText().toString());
//old text from xml shows
callback.onFinishedClick();
}
});
setCancelable(false);
return dialog.create();
}
}
the XML file for the dialog window:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/dialogLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp">
<TextView
android:text="score"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<EditText
android:id="#+id/dialogScoreEdit"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:hint="score here"
android:inputType="none"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif">
<TextView
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText
android:id="#+id/dialogUsernameEdit"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="retrive this"
android:inputType="none"/>
</LinearLayout>
</LinearLayout>
picture of the dialogwindow: http://i.stack.imgur.com/EvwSe.png
I have tried calling onResume() and putting the new score in. but the edittext still doesn't change.
I appreciate any answer and sorry for any bad English :)
It looks like there are a few issues there. The ones I have noticed are that when you set your view you are reinflating the view, versus setting it to the view you have already set up when you do
dialog.setView(inflater.inflate(R.layout.dig_lay, null));
you want to set that instead to
dialog.setView(inflatedView);
Also, since we can't see the actual data you are putting into the EditText, we can't see if that might be null, etc.
Finally, I never see you run a scoreEdit.getText(), so you wouldn't be retrieving any changes the user made to the edittext.