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");
}
Related
ok, I have a custom adapter which will display exercise, time, and whether the exercise is selected or not. When the user clicks on the clock icon inside every element in the adapter a dialogbox will show up, in which the user can modify the time for exercise. Once the time is entered the arraylist containing objects of type exercise will be updated. so the exerciseTime for that exercise object inside the list will be updated. Then the list will be refresh and the new exercise time will be displayed. but that is not the case. I can get the time to be updated. when I first create the arraylist of exercises, which I get from string.xml resource file, the default exercise time is 30min. but after the user updates, is still reflecting the time. any help apprciated.
entry_item.xml for custom adapter
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backroundColor">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="3dp"
android:layout_marginBottom="3dp">
<ImageView
android:id="#+id/item_icon_imageview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.10"
android:src="#drawable/ic_running_exercise"/>
<TextView
android:id="#+id/exercise_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.40"
android:textSize="20sp"
android:textColor="#color/textColor"
android:text="Running"/>
<TextView
android:id="#+id/exercise_time_textview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:textSize="18sp"
android:textColor="#color/textColor"
android:text="30 min"/>
<ImageView
android:layout_gravity="center"
android:id="#+id/set_time_imagview"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:src="#drawable/ic_timer_black_24dp"/>
<CheckBox
android:id="#+id/exercise_checkBox"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.05"
android:buttonTint="#color/textColor"/>
</LinearLayout>
</RelativeLayout>
custom adapter
public class WorkoutAdapter extends ArrayAdapter<Exercise> {
public WorkoutAdapter(Context context, int num, ArrayList<Exercise> exerciseList) {
super(context, 0, exerciseList);
// allExercises = exerciseList;
}
#NonNull
#Override
public View getView(final int position, View convertView, ViewGroup parent) {
// Get the data item for this position
final Exercise exercise = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.entry_item, parent, false);
}
// Lookup view for data population
ImageView exerciseImageview = convertView.findViewById(R.id.item_icon_imageview);
TextView exerciseNameTextview = convertView.findViewById(R.id.exercise_textview);
TextView exerTimeTextview = convertView.findViewById(R.id.exercise_time_textview);
final ImageView setTimeImageview = convertView.findViewById(R.id.set_time_imagview);
final CheckBox exerciseCheckbox = convertView.findViewById(R.id.exercise_checkBox);
exerciseNameTextview.setText(exercise.getExerciseName());
exerciseImageview.setImageResource(exercise.getExerciseImage());
//selected exercise checkbox
exerciseCheckbox.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(exerciseCheckbox.isChecked()) {
exercise.setSelected(true);
}
else {
exercise.setSelected(false);
}
}
});
setTimeImageview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setTimerDialogBox(position);
Toast.makeText(getContext(), position+"", Toast.LENGTH_LONG).show();
}
});
return convertView;
}
/**
* alert dialogbox for add new item to listview
*/
private void setTimerDialogBox(final int position) {
// Creating alert Dialog with one Button
final AlertDialog.Builder alertDialog = new AlertDialog.Builder(getContext(), R.style.dialogBox);
alertDialog.setTitle("Set Time");
final EditText input = new EditText(getContext());
alertDialog.setView(input);
alertDialog.setIcon(R.drawable.ic_timer_black_24dp1);
alertDialog.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
long time = Long.parseLong(input.getText().toString());
Workout.exerciseList.get(position).setExerciseTime(time);
notifyDataSetChanged();
Toast.makeText(getContext(),"Time added", Toast.LENGTH_SHORT).show();
}
}).create();
alertDialog.setNegativeButton("CANCEL",
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
}).create();
alertDialog.show();
}
}
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();
This question already has answers here:
findViewById in Fragment
(37 answers)
Closed 6 years ago.
I'm making an android app and I have a button that won't react when I press it. The button is supposed to bring up an alertdialog box. I'm very confused as I have an OnClickListener and the dialog box is instantiated.
Here is the code from the Java file:
public class tab1Expenses extends Fragment {
Button btnEx;
public class button extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tab1expense);
btnEx = (Button)findViewById(R.id.btnEx);
btnEx.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);
AlertDialog.Builder add = new AlertDialog.Builder(button.this);
add.setView(view);
add.setCancelable(true)
.setTitle("Enter Expense:")
.setMessage("Expense Name:")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
Dialog dialog = add.create();
dialog.show();
}
});
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1expense, container, false);
return rootView;
}
}
And here is my XML file that is related to the java file:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.ojemz.expensetracker.tab1Expenses$button"
android:background="#android:color/darker_gray">
<Button
android:text="Add Expense"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnEx"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:width="800dp"
android:textAppearance="#style/TextAppearance.AppCompat.Widget.Switch"
android:background="#android:color/black"
android:textColor="#android:color/holo_green_light" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="17dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<!-- TextView and other stuff -->
</LinearLayout>
</ScrollView>
You're doing it wrong!
The button class extending Activity has no reason to exist here.
If you simply want to get some onClick callbacks just do the following:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab1expense, container, false);
btnEx = (Button)rootView.findViewById(R.id.btnEx);
btnEx.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
View view = LayoutInflater.from(button.this).inflate(R.layout.add_ex, null);
AlertDialog.Builder add = new AlertDialog.Builder(tab1Expenses.this.getActivity());
add.setView(view);
add.setCancelable(true)
.setTitle("Enter Expense:")
.setMessage("Expense Name:")
.setPositiveButton("Add", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
Dialog dialog = add.create();
dialog.show();
}
});
return rootView;
}
Don't forget to remove your "button" inner class.
Side note
You should rename your tab1Expenses Fragment class to Tab1Expenses in order to distinguish between instances and classes.
Please refer to Java naming conversions.
I'm making an app for a school project and I'm stuck on one area.
I want to use an okay button to dismiss the pop up window.
If you have any suggestions or if there is a better way to implement popups please share them.
package xyz.ashraf.whoisdelasalle;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
public class Pop extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.5));
}
}
Java code^
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#FAFAFA">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="John Baptist de La Salle was a French priest, educational reformer, and founder of the Institute of the Brothers of the Christian Schools. He is a saint of the Roman Catholic Church and the patron saint of teachers."
android:id="#+id/textView"
android:background="#FAFAFA"
android:textColor="#000000"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:layout_marginRight="15dp"
android:layout_marginLeft="15dp"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/okButton_who"
android:textColor="#00E676"
android:background="#FAFAFA"
android:layout_below="#+id/textView"
android:layout_alignRight="#+id/textView"
android:layout_alignEnd="#+id/textView"
android:layout_marginTop="50dp"/>
</RelativeLayout>
</ScrollView>
XML^
Thanks in advance!
Not sure if this would work, but you can try:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.popwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.5));
Button okButton = (Button) findViewById(R.id.okButton_who);
okButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
Or else you could use Dialogs or DialogFragments as akadouri suggested.
It looks like you're looking for Dialogs or DialogFragments.
I guess you could use AlertDialog to do this.
AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
builder.setTitle(title); // If you want a header title
builder.setCancelable(false); // If you want to stop user dismissing with back button
builder.setMessage(message); // Message to user
//If you want custom layout (not needed)
LayoutInflater inflater = mActivity.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.pin_dialog, null);
builder.setView(dialogView)
builder.setPositiveButton(positiveLable, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something
}
});
builder.setNegativeButton(negativeLable, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int whichButton) {
//Do something, or remove if you dont want negative button
}
});
builder.show();
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();
}
});
}