m4n07
m4n07

Reputation: 2287

Alert Dialog spacing issue with textview and edit text

I'm not able to see "MY DATA" in my AlertDialog as it gets overlapped by the EditText .

           Context context = MyActivity.this;
           AlertDialog.Builder alert = new AlertDialog.Builder(context);

            alert.setTitle(" NEW TITLE");
            alert.setMessage("MESSAGE 1");

            final TextView tx = new TextView(this);
            tx.setText("MY DATA");
            alert.setView(tx);

            // Set an EditText view to get user input   
              final EditText input = new EditText(this); 
              alert.setView(input);


            alert.setPositiveButton(R.string.alert_dialog_ok, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked OK so do some stuff */
                }
            });

            alert.setNegativeButton(R.string.alert_dialog_cancel, new DialogInterface.OnClickListener() {
                public void onClick(DialogInterface dialog, int whichButton) {

                    /* User clicked Cancel so do some stuff */
                }
            });

            alert.create();
            alert.show();

Upvotes: 3

Views: 5714

Answers (4)

Joseph Earl
Joseph Earl

Reputation: 23432

You can only set one view with setView so it is not that "MY DATA" is not visible - is is not there at all since it is being overwritten by your EditText. Swap around the order in which you call setView to see what I mean.

To have multiple views as the custom view you'll need to make a ViewGroup such as LinearLeayout, add the EditText and TextView to this group, and then set the group as your custom Dialog view.

// Create a ViewGroup to hold the other views
LinearLayout group = new LinearLayout(this);
group.setOrientation(LinearLayout.VERTICAL);

// Header for the TextView
final TextView txHeader = new TextView(this);
txHeader.setText("TEXTVIEW HEADER");
group.addView(txHeader);

// Add the textview to the group
final TextView tx = new TextView(this);
tx.setText("MY DATA");
group.addView(tx);

// Header for the EditText
final TextView inputHeader = new TextView(this);
inputHeader.setText("EDIT-TEXT HEADER");
group.addView(inputHeader);

final EditText input = new EditText(this); 
group.addView(input);

// Set the group as the custom dialog view
alert.setView(group);

Upvotes: 1

kannappan
kannappan

Reputation: 2250

try this code

"LayoutInflater factory = LayoutInflater.from(nextScreen.this); final View textEntryView = factory.inflate(R.layout.text_entry, null);
final AlertDialog.Builder meeting=new AlertDialog.Builder(nextScreen.this); meeting.setView(textEntryView).setTitle("Ok"); meeting.setPositiveButton("Create", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {
                    TextView txt=(TextView) textEntryView.findViewById(R.id.username_edit);
                    Toast.makeText(nextScreen.this, txt.getText().toString(), Toast.LENGTH_SHORT);
                                        }
            });
            meeting.setNegativeButton("Cancel", new DialogInterface.OnClickListener() {

                @Override
                public void onClick(DialogInterface dialog, int which) {


                }
            });
            meeting.create();
            meeting.show();"

and also in the .xml file

<EditText
    android:id="@+id/username_edit"
    android:layout_height="wrap_content"
    android:layout_width="fill_parent"
    android:layout_marginLeft="20dip"
    android:layout_marginRight="20dip"
    android:scrollHorizontally="true"
    android:autoText="false"
    android:capitalize="none"
    android:gravity="fill_horizontal"
    android:singleLine="true"/>

Upvotes: 0

2red13
2red13

Reputation: 11227

I suggest to use a custom dialog, define a xml layout e.g:

 <?xml version="1.0" encoding="utf-8"?>

<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android" 
android:id="@+id/plausi" 
android:layout_width="fill_parent" 
android:layout_height="wrap_content" 
android:layout_gravity="center_vertical" 
android:background="@color/tree_normal_back" 
android:padding="3dip">
<TextView 
    android:id="@+id/plausi_tv" 
    android:layout_width="fill_parent" 
    android:textSize="22dip"
    android:padding="2dip"
    android:layout_height="wrap_content" 
    android:background="@color/tree_normal_back" 
    android:textColor="@color/tree_normal_font" 
    android:minLines="4"/>
<EditText 
    android:layout_below="@+id/plausi_tv"
    android:id="@+id/plausi_et" 
    android:layout_width="fill_parent" 
    android:textSize="22dip"
    android:padding="2dip"
    android:layout_height="wrap_content" 
    android:textColor="@color/tree_input_font"/>
</RelativeLayout>    

and build the dialog like:

LayoutInflater mInflater = LayoutInflater.from(tab3.this);
                     AlertDialog.Builder builder = new AlertDialog.Builder(tab3.this);
                     builder.setTitle("Plausibilitätscheck");

                     View convertView = mInflater.inflate(R.xml.plausi, null);
                     RelativeLayout rl = (RelativeLayout) convertView.findViewById(R.id.plausi);
                     final EditText et = (EditText) convertView.findViewById(R.id.plausi_et);
                     final TextView tv = (TextView) convertView.findViewById(R.id.plausi_tv);
                     tv.setText(Html.fromHtml(vorgabe));
                     et.setInputType(InputType.TYPE_CLASS_NUMBER | InputType.TYPE_NUMBER_FLAG_DECIMAL | InputType.TYPE_NUMBER_FLAG_SIGNED);
                     et.setText(GlobalVars.form_adapter.DATA[clicked_index]);
                     et.addTextChangedListener(new TextWatcher(){
                        @Override
                        public void afterTextChanged(Editable arg0) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void beforeTextChanged(CharSequence arg0,
                                int arg1, int arg2, int arg3) {
                            // TODO Auto-generated method stub

                        }

                        @Override
                        public void onTextChanged(CharSequence arg0,
                                int arg1, int arg2, int arg3) {
                            // TODO Auto-generated method stub

                        }

                     });
                     builder.setPositiveButton("OK", new DialogInterface.OnClickListener(){
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                              // TODO Auto-generated method stub

                            }

                        } 
                     });
                     builder.setView(rl);


                     final AlertDialog alert = builder.create();
                     et.setOnFocusChangeListener(new View.OnFocusChangeListener() {
                            @Override
                            public void onFocusChange(View v, boolean hasFocus) {
                                if (hasFocus) {
                                    alert.getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_VISIBLE);
                                }
                            }
                        }); 
                     alert.show();

Upvotes: 0

mudit
mudit

Reputation: 25536

Try this:

LinearLayout layout = new LinearLayout(this);
layout.setOrientation(Vertical);

            final TextView tx = new TextView(this);
            tx.setText("MY DATA");
            layout.addView(tx);

            // Set an EditText view to get user input   
              final EditText input = new EditText(this); 
              layout.addView(input);

 alert.setView(layout);

Upvotes: 5

Related Questions