-3

I am trying to create a dialog box with Textview in android like:

Image]

For creating the Textview, I am using following function object.settype(textviewobject) but there is no success.

halfer
  • 19,471
  • 17
  • 87
  • 173
Developer_99
  • 125
  • 1
  • 6
  • 16

6 Answers6

2
            final Dialog dialog = new Dialog(this);
            dialog.setContentView(R.layout.dialog);
            TextView text1 = 
                      (TextView)dialog.findViewById(R.id.text1);
            Button proceed =
            (Button)dialog.findViewById(R.id.button);

            proceed.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View view) {
                   dialog.dismiss();

                }
            });

            dialog.show();

dialog.xml

    <?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">

<TextView
    android:id="@+id/text1"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Text1"
    android:layout_centerInParent="true"/>

  <Button
    android:id="@+id/button"
    android:layout_width="match_parent"
    android:layout_height="wrap_content" 
    android:text="Proceed"
    android:layout_alignParentBottom="true"/>


 </RelativeLayout>

I hope this helps

Abhishek Jaiswal
  • 618
  • 6
  • 17
0

Use alertdialog like this, and create another layout for dialog:

final AlertDialog.Builder builder = new AlertDialog.Builder(context);
                LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                @SuppressLint("InflateParams") final View alertlayout = inflater.inflate(R.layout.notif_msg_dialog, null);

                TextView text_main=(TextView)alertlayout.findViewById(R.id.notif_msg);
                text_main.setText("YOUR TEXT");


                builder.setView(alertlayout);
                packsizeDialog=builder.create();
                packsizeDialog.show();

Here, notif_msg_dialog.xml is layout which has textview

Divyesh Patel
  • 2,536
  • 2
  • 13
  • 29
0

Just use AlertDialog.Builder and put your text in setMessage it will display

        AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
        builder.setMessage("your text");
        builder.create();
        builder.show();
Saurabh Padwekar
  • 3,590
  • 1
  • 27
  • 36
0

You can get help from following post http://stackoverflow.com/questions/10903754/input-text-dialog-android

singh.indolia
  • 1,252
  • 15
  • 25
0

Use this:-

    private void fn_showAlertDialog() {
        new AlertDialog.Builder(YourActivity.this)
            .setTitle("Title of your dialog")
            .setMessage("Text that you want to show.")
            .setCancelable(false)
            .setPositiveButton(android.R.string.yes, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //do your task
                    dialog.cancel();
                }
            })
            .setNegativeButton(android.R.string.no, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int which) {
                    //do your task
                    dialog.cancel();
                }
            })
            .setIcon(android.R.drawable.ic_dialog_alert)
            .show();
}
priyanka kataria
  • 377
  • 3
  • 17
0

custom_dialog.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="80dp"
    android:background="#3E80B4"
    android:orientation="vertical" >

    <TextView
        android:id="@+id/txt_dia"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:layout_margin="10dp"
        android:text="Do you realy want to exit ?"
        android:textColor="@android:color/white"
        android:textSize="15dp"
        android:textStyle="bold"/>


    <LinearLayout
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="center"
        android:background="#3E80B4"
        android:orientation="horizontal" >

        <Button
            android:id="@+id/btn_yes"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="Yes"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />

        <Button
            android:id="@+id/btn_no"
            android:layout_width="100dp"
            android:layout_height="30dp"
            android:layout_marginLeft="5dp"
            android:background="@android:color/white"
            android:clickable="true"
            android:text="No"
            android:textColor="#5DBCD2"
            android:textStyle="bold" />
    </LinearLayout>

</LinearLayout>

You have to extends Dialog and implements OnClickListener

public class CustomDialogClass extends Dialog implements
    android.view.View.OnClickListener {

  public Activity c;
  public Dialog d;
  public Button yes, no;

  public CustomDialogClass(Activity a) {
    super(a);
    // TODO Auto-generated constructor stub
    this.c = a;
  }

  @Override
  protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    requestWindowFeature(Window.FEATURE_NO_TITLE);
    setContentView(R.layout.custom_dialog);
    yes = (Button) findViewById(R.id.btn_yes);
    no = (Button) findViewById(R.id.btn_no);
    yes.setOnClickListener(this);
    no.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    switch (v.getId()) {
    case R.id.btn_yes:
      c.finish();
      break;
    case R.id.btn_no:
      dismiss();
      break;
    default:
      break;
    }
    dismiss();
  }
}

How to call this ?

CustomDialogClass cdd = new CustomDialogClass(MainActivity.this);
cdd.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
cdd.show();
nzala
  • 562
  • 7
  • 10