简体   繁体   中英

Android-How to create custom dialog/alert

HI I have a game level screen..when touch a level to play I need to give instruction alert/popup window(scrolable) for it.

It should be customized..likes popup in angry-bird..

own background image no borders with only ok button

Please help me Im about to publish this to GOOGLEPLAY...

@Override
protected Dialog onCreateDialog(int id) {
    // TODO Auto-generated method stub
    switch (id) {

    case 0:

        final Dialog mDialog = new Dialog(this);
        mDialog.setContentView(R.layout.customdialog);
        mDialog.setTitle("Custom Dialog");
        mDialog.show();
        mDialog.setCancelable(false);
        final EditText txtUser = (EditText)mDialog.findViewById(R.id.txtUserName);


        Button btnButtonOK = (Button)mDialog.findViewById(R.id.btnOK);
        Button btnButtonCancel = (Button)mDialog.findViewById(R.id.btnCancel);

        btnButtonOK.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                String user = txtUser.getText().toString();
                if (!user.equals(""))
                    {
                    Toast.makeText(getApplicationContext(),
                        user, Toast.LENGTH_LONG).show();
                mDialog.dismiss();
                    }
            }
        });



        btnButtonCancel.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                mDialog.dismiss();
            }
        });


        break;
}

you can make your owne desing with: progress_layer-xml

<?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:orientation="vertical" 
   android:layout_marginLeft="10dp"
   android:layout_marginRight="10dp">

   <TextView
       android:id="@+id/textView1"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
            android:text="@string/layer_folien"
         android:textAppearance="?android:attr/textAppearanceLarge" />

        <ProgressBar
            android:id="@+id/progressBar1"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <TextView
            android:id="@+id/textView2"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/layer_gesamnt"
            android:textAppearance="?android:attr/textAppearanceLarge" />

        <ProgressBar
            android:id="@+id/progressBar2"
            style="?android:attr/progressBarStyleHorizontal"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content" />

        <Button
            android:id="@+id/dialog_cancel"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_gravity="center"
            android:text="@string/layer_button" />

    </LinearLayout>

and de Code to call it

final Dialog dialog = new Dialog(PechaKuchaTimerActivity.this);
dialog.show();

final ProgressBar pr_fol = (ProgressBar)  dialog.findViewById(R.id.progressBar1);
final ProgressBar pr_ges = (ProgressBar)  dialog.findViewById(R.id.progressBar2);
final Button button_layerCancel = (Button) dialog.findViewById(R.id.dialog_cancel);

i think you need code here is the one

mydialog.xml

<?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="wrap_content"
    android:orientation="vertical" >

        <Button
            android:id="@+id/ok"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="Ok"
            android:layout_gravity="center"
             />

</LinearLayout>

.

Dialog dialog=new Dialog(mContext);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.mydialog);
dialog.getWindow().getAttributes().width = LayoutParams.FILL_PARENT;
dialog.getWindow().setBackgroundDrawableResource(R.drawable.dialogbackground);

Button mOkButton=(Button)dialog.findViewById(R.id.ok);


mOkButton.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {

    //do here what you want to do with ok button click

    }
});

Here dialogbackground is the background image

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM