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.
Related
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.
I'm trying to get the text from editText and input it into my database. The problem I am having is a NullPointerException once i try to submit the text. Any help would be appreciated.
ERROR:
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
This is from my MainActivity:
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.action_add_task:
final EditText text = (EditText)findViewById(R.id.editText);
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
return true;
}
return super.onOptionsItemSelected(item);
}
my custom_view.xml:
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter a task"
android:id="#+id/editText" />
my activity_main.xml:
<android.support.design.widget.TabLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/tablayout"
android:background="#color/colorPrimaryDark"
android:minHeight="?attr/actionBarSize"
>
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:backgroundTint="#android:color/darker_gray"
android:visibility="visible">
<ListView
android:id="#+id/lstTask"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:text="#string/tasks"
android:textSize="30sp" />
</android.support.v4.view.ViewPager>
Well since I cant see the xml for MainActivity layout, here it's what most likely it's causing the NPE.
final EditText text = (EditText)findViewById(R.id.editText);
Your searching your edit text in you activity view hierarchy when (assuming from what is shown in the Dialog creation) you should be searching it in the Dialog view hierarchy.
.setView(R.layout.custom_view)
There you are setting that view as the view of the dialog and thats probably what is causing the exception. Try fining the reference to the edit text in the onClick method like this:
AlertDialog dialog = new AlertDialog.Builder(this)
.setTitle("Add New Task")
.setMessage("what do you want to do next")
.setView(R.layout.custom_view)
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
String task;
task = text.getText().toString();
dbHelper.insertNewTask(task);
if(getFragmentRefreshListener()!=null) {
getFragmentRefreshListener().onRefresh();
}
loadTaskList();
}
})
.setNegativeButton("Cancel",null)
.create();
dialog.show();
Here is what fixed my problem:
I changed this line:
final EditText text = ((AlertDialog)dialog).findViewById(R.id.editText);
To this:
final EditText text = (EditText) ((AlertDialog)dialog).findViewById(R.id.editText);
I added an Action button to show about Alert Dialog for my app that I'm developing.... I just fetched one xml file for that Action Dialog popup.
I used this xml as Alert Dialog popup. using below java code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="300dp"
android:layout_height="300dp"
android:weightSum="1">
<TextView
android:id="#+id/info"
android:layout_width="277dp"
android:layout_height="wrap_content"
android:text="Click the links below to contact us"
android:layout_marginTop="13dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/wa"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/wa"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/info"
android:layout_centerHorizontal="true" />
<TextView
android:id="#+id/fb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/fb"
android:textSize="30dp"
android:layout_marginTop="8dp"
android:layout_below="#+id/wa"
android:layout_centerHorizontal="true" />
</RelativeLayout>
java code :
public void info(MenuItem item) {
View v = LayoutInflater.from(MainActivity.this).inflate(R.layout.alertdiag, null);
TextView waf = (TextView) findViewById(R.id.wa);
TextView fbf = (TextView) findViewById(R.id.fb);
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
builder.setMessage("About");
builder.setView(v);
builder.setCancelable(false);
builder.setPositiveButton("Okay", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
Now I can see those TextViews in my main activity as alert Dialog. But I can't click the links. Any solution????
Thank you in advance.....
Use AlertDialog.setView() to display a custom layout containing your hyperlink TextView in the message area.
E.g. Make a hyperlink_layout.xml containing a TextView with id = hyperlink_text
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.
I would like to know how to customize theme/style of AlertDialog using sdk api 10. I know how to do that from 11 onwards but not how to do it on the 10.
XML Layout file
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
style="#style/dialog_theme" >
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/disclaimerText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/disclaimer"
android:padding="5dp"/>
<CheckBox
android:id="#+id/checkDisclaimer"
style="?android:attr/textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="#string/agree" />
</LinearLayout>
</ScrollView>
JAVA Resource file
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//Setting Dialog Title
builder.setTitle("Disclaimer");
//Setting Dialog Message
builder.setMessage(R.string.disclaimer);
View view = (View) LayoutInflater.from(this).inflate(R.layout.layout_disclaimer, null);
builder.setView(view);
You could try this:
View myView = View.inflate(this, R.layout.customize_dialog, null);
AlertDialog.Builder builder=new AlertDialog.Builder(this);
builder.setView(myView);
builder.setTitle("Customize dialog");
builder.setCancelable(false);
AlertDialog alert=builder.create();
alert.show();
You have to create a layout in res/layout/ called customize_dialog.xml,
you can also add to "myView" other view object.
To add tipical Alert button:
builder.setPositiveButton("Ok",new OnClickListener(){
public void onClick(DialogInterface dialog, int id){
//action to do
dialog.dismiss();
}
});
builder.setNegativeButton("Close",new OnClickListener(){
public void onClick(DialogInterface dialog, int id){
//action to do
dialog.dismiss();
}
});
I hope it helps you.