'Creating Custom Dialog box (android example error?)
Been trying to create customer dialog box and I copied source code from android website. I worked my way round a few things now I'm really stuck. I don't know what LoginFragment is in the code:
...
public class start extends Activity{
Button buttonx;
final Context context = this;
LayoutInflater mInflater;
@Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
}
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(context);
// Get the layout inflater
//LayoutInflater inflater = getActivity().getLayoutInflater();
mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Inflate and set the layout for the dialog
// Pass null as the parent view because its going in the dialog layout
builder.setView(mInflater.inflate(R.layout.login, null))
// Add action buttons
.setPositiveButton(R.string.signin, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int id) {
// sign in the user ...
}
})
.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
LoginDialogFragment.this.getDialog().cancel();
}
});
return builder.create();
}
When I //comment the line out the line out it says @Override is unnecessary which doesnt solve anything. Sorry for noob question
Solution 1:[1]
Take a look in this post on android developers, was useful to me many times.
http://developer.android.com/guide/topics/ui/dialogs.html
here is a simples example also:
http://www.mkyong.com/android/android-custom-dialog-example/
Solution 2:[2]
I guess you might have figured it out that LoginFragment
is a fragment, so you have to create a new class and extend it with Fragment
class. There you may put your code onCreateDialog()
. And then follow the steps as how to show it.
You will use this code to show it -
DialogFragment newFragment = new LoginFragment();
newFragment.show(getSupportFragmentManager(), "login");
refer the link rochasdv has shared, you will get a better idea. Ask me if missing anything.
Solution 3:[3]
Create a layout file for the view you want to show in the dialog. Suppose you want a textview and a button in a dialog:
dialog_box.xml
<?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="wrap_content">
<TextView
android:id="@+id/textview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="17sp"
android:layout_margin="20dp"/>
<Button
android:id="@+id/button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textSize="17sp"
android:layout_margin="20dp"/>
and in you java file:
Dialog d = new Dialog(this);
d.setTitle("No Places found");
d.setContentView(R.layout.dialog_box);
d.getWindow().getAttributes().width = WindowManager.LayoutParams.FILL_PARENT;
d.show();
Good luck
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
Solution | Source |
---|---|
Solution 1 | rochasdv |
Solution 2 | Darpan |
Solution 3 | The Bat |