Hello guys iam realy Confused i spent over 17 hours try to fix this Problem but
No hope
Am try to show Custom Dialog with > AlertDialog.Builder when i Click Button but when i run the App and click the button it gives me this Exception
i tried ButterKnife #OnClick Anotation and Casting the Item Inside the Dialog Function like Button item = findviewById(R.id.ItemID);
i search for questions has same problem like mine but useless till i found
this question Multiple injections but i found that its really very old May 29, 2014
The Exception
05-23 21:15:56.630 8769-8769/com.w4ma.soft.tamenly E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.w4ma.soft.tamenly, PID: 8769
java.lang.NullPointerException: Attempt to invoke virtual method 'android.text.Editable android.widget.EditText.getText()' on a null object reference
at com.w4ma.soft.tamenly.View.CreateandShow.ShowThePost.ShowCommentDialog(ShowThePost.java:133)
at com.w4ma.soft.tamenly.View.CreateandShow.ShowThePost.onClick(ShowThePost.java:159)
at com.w4ma.soft.tamenly.View.CreateandShow.ShowThePost_ViewBinding$1.doClick(ShowThePost_ViewBinding.java:52)
at butterknife.internal.DebouncingOnClickListener.onClick(DebouncingOnClickListener.java:22)
at android.view.View.performClick(View.java:5647)
at android.view.View$PerformClick.run(View.java:22462)
at android.os.Handler.handleCallback(Handler.java:754)
at android.os.Handler.dispatchMessage(Handler.java:95)
at android.os.Looper.loop(Looper.java:163)
at android.app.ActivityThread.main(ActivityThread.java:6221)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:904)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:794)
Here is my code
ShowThePost.java
#Nullable #BindView(R.id.priceSuggested) EditText txtSuggestprice;
#Nullable #BindView(R.id.txtnotes) EditText txtNotes;
#Nullable #BindView(R.id.btnClose) Button btnclose;
#Nullable #BindView(R.id.btnCommentDone) Button btndone;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_show_the_post);
ButterKnife.bind(this);
}
public void ShowCommentDialog() {
final AlertDialog.Builder alert = new AlertDialog.Builder(context);
view = getLayoutInflater().inflate(R.layout.create_comment,null);
alert.setCancelable(false);
alert.setView(view);
final AlertDialog dialog = alert.create();
// final Dialog dialog = new Dialog(context);
// dialog.setContentView(R.layout.create_comment);
// dialog.setCancelable(false);
// dialog.setTitle("This is Dialog");
// EditText txtSuggestprice = findViewById(R.id.priceSuggested);
// EditText txtNotes = findViewById(R.id.txtnotes);
// Button btnDonee = (Button)findViewById( R.id.btnCommentDone);
// Button btnClosee =(Button)findViewById(R.id.btnClose);
//
// btndone.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
//
//
// }
// });
// btnclose.setOnClickListener(new View.OnClickListener() {
// #Override
// public void onClick(View v) {
// dialog.dismiss();
//
// }
// });
final String Notes = txtNotes.getText().toString();
final String Description = txtSuggestprice.getText().toString();
alert.setPositiveButton("Done", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
Toasty.success(context,"Done" + Notes + Description, Toast.LENGTH_LONG).show();
}
}).setNegativeButton("Close", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
btndone.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Done", Toast.LENGTH_SHORT).show();
}
});
btnclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "Closed", Toast.LENGTH_SHORT).show();
dialog.dismiss();
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
dialog.show();
}
#OnClick(R.id.fabComment)
public void ShowDialog(){
ShowCommentDialog();
}
CreatComment.xml this is the Custom Dialog which should Inflate to the Dialog
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="#dimen/_12sdp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Comment"
android:textSize="#dimen/_22sdp"
android:textColor="#000"
android:textAlignment="center"
android:layout_margin="#dimen/_9sdp"
android:typeface="serif"/>
<View
android:layout_width="match_parent"
android:layout_height="#dimen/_1sdp"
android:background="#color/lightgray"
android:padding="#dimen/_4sdp"/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_4sdp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/priceSuggested"
android:layout_margin="#dimen/_5sdp"
android:hint="Suggest Price"
android:inputType="number"
android:typeface="serif"/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/_4sdp">
<EditText
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="#dimen/_5sdp"
android:hint="Notes"
android:id="#+id/txtnotes"
android:typeface="serif"/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Done"
android:background="#drawable/btncommentstyle"
android:id="#+id/btnCommentDone"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Close"
android:layout_marginTop="#dimen/_4sdp"
android:background="#drawable/btncommentstyle"
android:id="#+id/btnClose"/>
</LinearLayout>
</android.support.v7.widget.CardView>
The exception is caused by this line:
final String Notes = txtNotes.getText().toString();
As described in the reason, the txtNotes member is null, which means that the binding did not work (probably the view with txtnotes id was not found).
From what I see, you call ButternKnife.bind() for the activity which has the activity_show_the_post layout as content view. But the view with txtnotes id is actually in the create_comment layout, of which ButterKnife knows nothing about, and doesn't even exist at that time. Thus, the binding fails, no reference to txtnotes is created and you later get a NPE.
You need to bind the views for create_comment.xml too
view = getLayoutInflater().inflate(R.layout.create_comment,null);
Butterknife.bind(view,this);
alert.setCancelable(false);
alert.setView(view);
final AlertDialog dialog = alert.create();
Related
I'm trying to implement an Exposed Dropdown menu on an AlertDialog with a custom layout defined in dialog_main_createremote.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<com.google.android.material.textfield.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Name"
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox"
android:layout_marginTop="10dp">
<com.google.android.material.textfield.TextInputEditText
android:id="#+id/dialog_main_textinput"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</com.google.android.material.textfield.TextInputLayout>
<com.google.android.material.textfield.TextInputLayout
style="#style/Widget.MaterialComponents.TextInputLayout.OutlinedBox.ExposedDropdownMenu"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:hint="Category">
<AutoCompleteTextView
android:id="#+id/dialog_main_dropdown"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:inputType="none"/>
</com.google.android.material.textfield.TextInputLayout>
</LinearLayout>
As per Material Design Guidelines, items need a layout for the dropdown menu to appear, so I created the following in the dropdown_item.xml file:
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:ellipsize="end"
android:maxLines="1"
android:textAppearance="?attr/textAppearanceSubtitle1" />
Finally, I started to generate the AlertDialog with a MaterialAlertDialogBuilder, then defined and instantiated a LayoutInflater which I used to get dialog's custom view and get access to the setAdapter method, but after setting the adapter and showing the dialog all I can get is an Outlined TextInputLayout without any dropdown menu.
So that's it, thanks in advance for any suggestions/solutions, also, as you may have noticed, I'm new to android development, so, if possible and of course, if you have time in your hands, elaborate your answer or point to a specific resource where I could get more info.
MainActivity.java:
public class MainActivity extends AppCompatActivity {
private RemoteListViewModel remoteListViewModel;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
RecyclerView recyclerView = findViewById(R.id.main_recyclerview);
recyclerView.setLayoutManager(new LinearLayoutManager(this));
recyclerView.setHasFixedSize(true);
final RemoteAdapter adapter = new RemoteAdapter();
recyclerView.setAdapter(adapter);
remoteListViewModel = new ViewModelProvider(this).get(RemoteListViewModel.class);
remoteListViewModel.getAllRemotes().observe(this, new Observer<List<Remote>>() {
#Override
public void onChanged(#Nullable List<Remote> remotes) {
adapter.setRemotes(remotes);
}
});
ExtendedFloatingActionButton createButton = findViewById(R.id.create_efab);
createButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
MaterialAlertDialogBuilder builder = new MaterialAlertDialogBuilder(MainActivity.this);
LayoutInflater inflater = LayoutInflater.from(MainActivity.this);
final View inflatedView = inflater.inflate(R.layout.dialog_main_createremote, null);
AutoCompleteTextView actv = inflatedView.findViewById(R.id.dialog_main_dropdown);
String categories[] = getResources().getStringArray(R.array.categories);
actv.setAdapter(new ArrayAdapter(MainActivity.this, R.layout.dropdown_item, categories));
builder.setView(R.layout.dialog_main_createremote);
builder.setTitle("Set control params");
builder.setNegativeButton("Nope", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.setPositiveButton("Okey", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
});
}
}
Hi Guys please recommend a solution for me.
when preparing an alert dialogue, see the error i get
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.techlinemobile.foodbasket, PID: 22724
java.lang.NullPointerException: Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)' on a null object reference
at com.techlinemobile.foodbasket.ItemDetails.showAddedToCartDialogue(ItemDetails.java:162)
at com.techlinemobile.foodbasket.ItemDetails.access$300(ItemDetails.java:25)
at com.techlinemobile.foodbasket.ItemDetails$1.onClick(ItemDetails.java:131)
at android.view.View.performClick(View.java:6291)
at android.view.View$PerformClick.run(View.java:24931)
at android.os.Handler.handleCallback(Handler.java:808)
at android.os.Handler.dispatchMessage(Handler.java:101)
at android.os.Looper.loop(Looper.java:166)
at android.app.ActivityThread.main(ActivityThread.java:7529)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run(Zygote.java:245)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:921)
see the method
private void showAddedToCartDialogue() {
//before inflating the custom alert dialog layout, we will get the current activity viewgroup
ViewGroup viewGroup = findViewById(android.R.id.content);
//then we will inflate the custom alert dialog xml that we created
View dialogView = LayoutInflater.from(this).inflate(R.layout.added_to_cart_dialog, viewGroup, false);
//Now we need an AlertDialog.Builder object
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//setting the view of the builder to our custom view that we already inflated
builder.setView(dialogView);
//finally creating the alert dialog and displaying it
AlertDialog alertDialog = builder.create();
final Button buttonContinue = (Button)alertDialog.getWindow().findViewById(R.id.buttonContinue);
final Button buttonViewCart = (Button)alertDialog.getWindow().findViewById(R.id.buttonViewCart);
buttonContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart
Intent it = new Intent(ItemDetails.this, MainActivity.class);
startActivity(it);
}
});
buttonViewCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart
Intent it = new Intent(ItemDetails.this, ShoppingCartActivity.class);
startActivity(it);
}
});
alertDialog.show();
}
see the layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/colorPrimary">
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_centerInParent="true"
android:background="#drawable/ic_success" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/added_to_cart"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Headline" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="#string/successfully_added_long"
android:textAlignment="center"
android:textAppearance="#style/TextAppearance.AppCompat.Medium" />
<Button
android:id="#+id/buttonContinue"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/button_background"
android:text="#string/continue_shopping"
android:textColor="#color/colorPrimary" />
<Button
android:id="#+id/buttonViewCart"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="15dp"
android:background="#drawable/button_background"
android:text="#string/view_cart"
android:textColor="#color/colorPrimary" />
</LinearLayout>
</LinearLayout>
use
Button buttonContinue = dialogView.findViewById(R.id.buttonContinue);
Button buttonViewCart = dialogView.findViewById(R.id.buttonViewCart);
instead of
Button buttonContinue = (Button)alertDialog.getWindow().findViewById(R.id.buttonContinue);
and create dailoge at the end like this.
alertDialog= alertDialogBuilder.create();
alertDialog.show();
alertDialog.getWindow().setLayout(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Move the line alertDialog.show() to right under builder.create(). Or you can use builder.show() method which will create and show the dialog immediately.
The error reported that you set listener on null object. Your dialog is not visible so that findViewById return null. Variables buttonContinue and buttonViewCart refer to null value.
//before inflating the custom alert dialog layout, we will get the current activity viewgroup
ViewGroup viewGroup = findViewById(android.R.id.content);
//then we will inflate the custom alert dialog xml that we created
View dialogView = LayoutInflater.from(this).inflate(R.layout.added_to_cart_dialog, viewGroup, false);
//Now we need an AlertDialog.Builder object
AlertDialog.Builder builder = new AlertDialog.Builder(this);
//setting the view of the builder to our custom view that we already inflated
builder.setView(dialogView);
//finally creating the alert dialog and displaying it
AlertDialog alertDialog = builder.create();
alertDialog.show(); <---------- show dialog here
final Button buttonContinue = (Button)alertDialog.getWindow().findViewById(R.id.buttonContinue);
final Button buttonViewCart = (Button)alertDialog.getWindow().findViewById(R.id.buttonViewCart);
buttonContinue.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart
Intent it = new Intent(ItemDetails.this, MainActivity.class);
startActivity(it);
}
});
buttonViewCart.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d(TAG, "before adding card");
//--------------------------------
//load data to cart
Intent it = new Intent(ItemDetails.this, ShoppingCartActivity.class);
startActivity(it);
}
});
So the title explains it all. I have a dialogfragment that currently pops up and I want to add a do not show checkbox to it and then obviously implement that check and not show if it was checked. I know there is a .setSingleChoiceItems, but I am not entirely sure on what would be going in there as it isn't really an item I would add somewhere. But then again I could probably be wrong as I am just getting into Android development.
Dialogfragment java
public class WifivsDataDialog extends DialogFragment {
#
Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
// Use the Builder class for convenient dialog construction
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.dialog_box)
.setPositiveButton(R.string.WiFi, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// FIRE ZE MISSILES!
}
})
.setNegativeButton(R.string.Cell_Data, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
// User cancelled the dialog
}
});
// Create the AlertDialog object and return it
return builder.create();
}
}
Here is the code calling it in my MainActivity.java
WifivsDataDialog myDiag = new WifivsDataDialog();
myDiag.show(getFragmentManager(), "dialog_layout");
myDiag.setCancelable(false);
The accepted answer works, but it doesn't look like the normal material design system dialog, which is what I wanted.
Here's an option using a custom view layout to hold just the text and checkbox (with padding that matches material design), and allow the AlertDialog to manage the buttons.
no_location_dialog.xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="24dp"
android:paddingRight="12dp">
<TextView
android:id="#+id/no_location_text"
style="?android:attr/textAppearance"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="14dp"
android:paddingBottom="6dp"
android:text="#string/main_nolocation"
android:textSize="16sp"
android:autoLink="all"
android:linksClickable="true"></TextView>
<CheckBox
android:id="#+id/location_never_ask_again"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/main_never_ask_again" />
</LinearLayout>
And the code for setting up the AlertDialog:
private Dialog createNoLocationDialog() {
View view = getActivity().getLayoutInflater().inflate(R.layout.no_location_dialog, null);
CheckBox neverShowDialog = (CheckBox) view.findViewById(R.id.location_never_ask_again);
neverShowDialog.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Save the preference
PreferenceUtils.saveBoolean(getString(R.string.preference_key_never_show_location_dialog), isChecked);
}
});
Drawable icon = getResources().getDrawable(android.R.drawable.ic_dialog_map);
DrawableCompat.setTint(icon, getResources().getColor(R.color.theme_primary));
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity())
.setTitle(R.string.main_nolocation_title)
.setIcon(icon)
.setCancelable(false)
.setView(view)
.setPositiveButton(R.string.rt_yes,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
startActivityForResult(
new Intent(Settings.ACTION_LOCATION_SOURCE_SETTINGS),
REQUEST_NO_LOCATION);
}
}
)
.setNegativeButton(R.string.rt_no,
new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
// Ok, I suppose we can just try looking from where we
// are.
mMapFragment.mController.onLocation();
}
}
);
return builder.create();
}
It looks like this:
Here's the full code in a commit on Github - https://github.com/OneBusAway/onebusaway-android/commit/98135b66b0be5b73187e0148bef5d308c42a9fbe.
The DialogFragment class
public class WifivsDataDialog extends DialogFragment implements View.OnClickListener {
private CheckBox checkBox;
private Button button1;
private Button button2;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View mainView = inflater.inflate(R.layout.wifi_dialog, container);
checkBox = (CheckBox) mainView.findViewById(R.id.checkBox);
button1 = (Button) mainView.findViewById(R.id.button1);
button2 = (Button) mainView.findViewById(R.id.button2);
button1.setOnClickListener(this);
button2.setOnClickListener(this);
checkBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton compoundButton, boolean isChecked) {
// Store the isChecked to Preference here
SharedPreferences sharedPref = getActivity().getPreferences(Context.MODE_PRIVATE);
SharedPreferences.Editor editor = sharedPref.edit();
editor.putBoolean("DONT_SHOW_DIALOG", isChecked);
editor.commit();
}
});
return mainView;
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.button1:
// Do wifi stuff here
break;
case R.id.button2:
// Do cellular stuff here
break;
}
}
}
The Layout xml wifi_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:padding="20dp"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Some information to be displayed to human"
android:id="#+id/textView" android:textSize="30dp"/>
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Do now show this dialog again."
android:id="#+id/checkBox"/>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content" android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="WiFi"
android:id="#+id/button1" android:layout_gravity="center_horizontal"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CELL"
android:id="#+id/button2"/>
</LinearLayout>
</LinearLayout>
And on your Activity or where ever you display the dialog, check the preference set by user before displaying the dialog. Something like below.
SharedPreferences sharedPref = getPreferences(Context.MODE_PRIVATE);
boolean dontShowDialog = sharedPref.getBoolean("DONT_SHOW_DIALOG", false);
if (!dontShowDialog) {
new WifivsDataDialog().show(getFragmentManager(), "WiFi");
}
i have some problems with my layout, this is the capture of my layout inflater
the layout is to big and the button are in wrong place, i don't use a title but there is a black title background in there
i just want to make it smaller like piano tiles layout
can anyone help me to fix this?
this is my layout.xml data that will show inflater layout in menu.java
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#drawable/backgroundblankwhite"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/textinflateexit"
android:singleLine="true" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquityes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquitno" />
</LinearLayout>
</LinearLayout>
and this is my menu.java that have a button to display my inflate layout
public class menu extends Activity {
private Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(context);
openDialog.setContentView(R.layout.inflatequitapp);
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// if this button is clicked, close
// current activity
menu.this.finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
#Override
public void onBackPressed() {
// do nothing.
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}
I have change a bit variable to check at my end..please do change variables,class to match at your end...
MainActivity.java
///---////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button exit = (Button) findViewById(R.id.but);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(MainActivity.this);
openDialog.setContentView(R.layout.main);
openDialog.setTitle("Confirm Exit");
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
Dialog xml file..
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#ffffff"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:singleLine="true"
android:text="Are You Sure!!" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="Yes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="No" />
</LinearLayout>
</LinearLayout>
output:
you have to used below code for dialog
private void forgotPasswordDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.dialog_forgot_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.PauseDialog);
final AlertDialog dialog = builder.create();
dialog.setView(dialogView);
dialog.show();
final EditText edtUserNameDialog = (EditText) dialogView.findViewById(R.id.edtUserNameDialog);
edtUserNameDialog.setText(edtUserName.getText());
final Button btnSubmit = (Button) dialogView.findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!FieldsValidator.validateUsername(edtUserNameDialog)) {
//forgotPasswordDialog();
} else if (!checkDMSID()) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
} else {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
forgotPassword(edtUserNameDialog.getText().toString());
dialog.dismiss();
}
}
});
final Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
}
});
}
I get this strange error, when I activate my clickListener
Attempt to invoke virtual method 'void android.widget.Button.setOnClickListener(android.view.View$OnClickListener)'
on a null object reference
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (Build.VERSION.SDK_INT < 16) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DISMISS_KEYGUARD);
setContentView(R.layout.activity_main);
WebView webView = (WebView) findViewById(R.id.webView);
webView.loadData("test ", "text/html", "utf-8");
webView.loadUrl("https://www.google.de/");
webView.getSettings().setDomStorageEnabled(true);
PrefUtils.setKioskModeActive(true, getApplicationContext());
}
#Override
public void onBackPressed() {
Context context = getApplicationContext();
CharSequence text = "password to deactivate mode!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.dialog_signin);
myDialog.setCancelable(false);
password = (EditText) myDialog.findViewById(R.id.password);
myDialog.show();
// Error probably because of this
Button lbtn = (Button)findViewById(R.id.loginButton);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (password.getText().toString().equals("123")) {
Log.d("myapp", "test1");
} else {
Log.d("myapp", "test2");
}
}
});
}
so basically a dialog textfield windows appears, when I click on the back button. Inside this I check if the password of 123 is correct or not.
Here is my dialog_signin.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
android:id="#+id/password"
android:inputType="textPassword"
android:layout_width="200dp"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="16dp"
android:fontFamily="sans-serif" />
<!--android:hint="#string/password"-->
<Button
android:id="#+id/loginButton"
android:layout_width="200dp"
android:layout_height="30dp"
android:background="#color/red"/>
</LinearLayout>
and this is my activity_main.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:keepScreenOn="true"
tools:context=".MainActivity">
<WebView
android:id="#+id/webView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</WebView>
</RelativeLayout>
You are attempting for find the Button in your Activity or Fragment layout, not in the Dialog:
Button lbtn = (Button)findViewById(R.id.loginButton);
should be
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
You need to get the Button from the view it is in. If you use findViewById without any view reference then it would try to find the view in you activity xml in this case activity_main.xml. loginButton is not in this xml but in the dialog you have created that's why you are getting NPE. So, change
Button lbtn = (Button)findViewById(R.id.loginButton);
to
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
Replace your code with this :
#Override
public void onBackPressed() {
Context context = getApplicationContext();
CharSequence text = "password to deactivate mode!";
int duration = Toast.LENGTH_SHORT;
Toast.makeText(context, text, duration).show();
myDialog = new Dialog(this);
myDialog.setContentView(R.layout.dialog_signin);
myDialog.setCancelable(false);
password = (EditText) myDialog.findViewById(R.id.password);
myDialog.show();
// Error probably because of this
Button lbtn = (Button)myDialog.findViewById(R.id.loginButton);
lbtn.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
if (password.getText().toString().equals("123")) {
Log.d("myapp", "test1");
} else {
Log.d("myapp", "test2");
}
}
});