5

How to open PopUp Window when EditText is long pressed where user can type or paste content and than can press ok which then place the entered text in the EditText and i don't want user to use enter key.

My main motive is that user can see whole text at once as EditText size is small in my apk.

Note:- I'm beginner in android programming and this is my first question. :)

4 Answers 4

3

What I understood with your Question. My answer is

  • You can open a pop up window on long press of the Edit Text.
  • Copy your Edit Text- text to popup window -edit text.
  • On press "ok" copy the text of pop up window edit text back to your basic text box.

attaching the code for your reference

Popup window xml -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/LinearLayout1"
    android:layout_width="250dp"
    android:layout_height="250dp"
    android:background="#FFFFE0"
    android:orientation="vertical" 
    android:paddingLeft="7dip" 
    android:paddingTop="7dip"
    android:paddingRight="7dip"
    android:paddingBottom="7dip">

    <EditText
       android:id="@+id/edit_pop"
       android:layout_width="match_parent"
       android:layout_height="120dp"
       android:ems="10"/>

   <Button
      android:id="@+id/btok"
      android:layout_width="match_parent"
      android:layout_height="wrap_content"
      android:text="OK" />

</LinearLayout>

Main activity -

public class MainActivity extends Activity implements OnLongClickListener
{
  EditText vedt=null,edPop;
  Button btOk=null;
  @Override
  protected void onCreate(Bundle savedInstanceState) 
  {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    vedt = (EditText) findViewById(R.id.editText1);
    vedt.setOnLongClickListener(this); 
}

@Override
public boolean onLongClick(View v) 
{

    LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE);
    View popupView = layoutInflater.inflate(R.layout.popup, null);
    final PopupWindow popupWindow = new PopupWindow(popupView,android.view.ViewGroup.LayoutParams.WRAP_CONTENT,android.view.ViewGroup.LayoutParams.WRAP_CONTENT);
    edPop = (EditText)popupView.findViewById(R.id.edit_pop);
    btOk  = (Button)popupView.findViewById(R.id.btok);
    edPop.requestFocus();
    edPop.setText(vedt.getText().toString());
    popupWindow.showAtLocation(popupView, Gravity.CENTER,5,5);
    popupWindow.setOutsideTouchable(false);
    popupWindow.setFocusable(true);
    popupWindow.update();
    btOk.setOnClickListener(new OnClickListener()
    {
        @Override
        public void onClick(View v) 
        {
            vedt.setText(edPop.getText().toString());
            popupWindow.dismiss();
        }
    });

    return false;

}

}

1
  • Thanks for your feedback,it worked for me and solved my problem. :)
    – M10
    Aug 4, 2015 at 11:31
3

welcome to SO . You have to do some research before asking a Question in SO. please provide related info which will help us also finding answer

as an answer to your Question, you can use setOnclickListener to attach an onclick listner to your EditText inorder to trigger an Event when the user clicks on EditText.

for a popup which includes an EditText where user can enter input

you can use a Dialog with your own xml layout

take a look at this page for creating and using a dialog

1

Most of the stuff can be found on the android API page as what Dev have said. However, this is an example slightly edited from the documentation to help you to get started.

dialog.xml

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<EditText
    android:id="@+id/text"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="16dp"
    android:layout_marginLeft="4dp"
    android:layout_marginRight="4dp"
    android:layout_marginBottom="4dp"
    android:hint="Enter Text Here" />
</LinearLayout>

Place this in your activity.java

@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
    AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
    // Get the layout inflater
    LayoutInflater inflater = getActivity().getLayoutInflater();

    // Inflate and set the layout for the dialog
    // Pass null as the parent view because its going in the dialog layout
    builder.setView(inflater.inflate(R.layout.dialog, null))
    // Add action buttons
           .setPositiveButton("Submit", new DialogInterface.OnClickListener() {
               @Override
               public void onClick(DialogInterface dialog, int id) {
                   // Do something
               }
           })
           .setNegativeButton("Cancel", new DialogInterface.OnClickListener() {
               public void onClick(DialogInterface dialog, int id) {
                   LoginDialogFragment.this.getDialog().cancel();
               }
           });      
    return builder.create();
}

PS. I have not complied this code so there might be errors, but that's the concept

0
Write inside your activity:  

@Override
    public void handleOnLongClick(View v, int elementActionCode) {
        if (ORDER_SUMMARY_POPUP == elementActionCode) {
            View orderSummaryLayout = _initiatePopupWindow();
            if(orderSummaryLayout != null)
                _setOrderSummaryValues(orderSummaryLayout);
        }

private View _initiatePopupWindow() {
        int xCoOrdinate = 25;
        int yCoOrdinate = 25;
        int xWindowPerc = 10;
        int yWindowPerc = 50;
        Display display = getWindowManager().getDefaultDisplay();
        int width = display.getWidth();  
        int height = display.getHeight();
        View layout = null;

        xCoOrdinate = ((width*xWindowPerc)/100);
        yCoOrdinate = ((height*yWindowPerc)/100);
        width = width - xCoOrdinate;
        height = height - yCoOrdinate;

        try {
            // We need to get the instance of the LayoutInflater
            LayoutInflater inflater = (LayoutInflater) this
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
            layout = inflater.inflate(R.layout.order_summary_popup,
                    (ViewGroup) findViewById(R.id.orderPopupWindow));
            popupWindow = new PopupWindow(layout, width, height,
                    true);
            popupWindow.showAtLocation(layout, Gravity.AXIS_X_SHIFT,
                    (xCoOrdinate/2), (yCoOrdinate/2));

        } catch (Exception ex) {
            Log.e(TAG, "Error while initiation of the Order Summary Popup."  +    ex.getMessage());
        }
        return layout;
    }


private void _setOrderSummaryValues(View layout) {
        try {

            ImageView btnClosePopup = (ImageView) layout.findViewById(R.id.btn_close_popup1);
            btnClosePopup.setOnClickListener(new UUIHandlers.UListener(this,ORDER_SUMMARY_POPUP_CLOSE));

            TextView totGrossAmt = (TextView) layout.findViewById(R.id.totalGrossAmt1);
            totGrossAmt.setText(String.valueOf(df.format(totalGrossAmount)));
            totGrossAmt.setText(CommonUtils.getSystemCurrency(this, totGrossAmt, true));

            TextView totDiscAmt = (TextView) layout.findViewById(R.id.totalDiscAmt1);
            totDiscAmt.setText(String.valueOf(df.format(totalDiscAmount)));
            totDiscAmt.setText(CommonUtils.getSystemCurrency(this, totDiscAmt, true));

            TextView totTaxableAmt = (TextView) layout.findViewById(R.id.totalTaxableAmt1);
            totTaxableAmt.setText(String.valueOf(df.format(totalTaxableAmount)));
            totTaxableAmt.setText(CommonUtils.getSystemCurrency(this, totTaxableAmt, true));

            TextView totTaxAmt = (TextView) layout.findViewById(R.id.totalTaxAmt1);
            totTaxAmt.setText(String.valueOf(df.format(totalTaxAmount)));
            totTaxAmt.setText(CommonUtils.getSystemCurrency(this, totTaxAmt, true));

            TextView totBilAmt = (TextView) layout.findViewById(R.id.totalOrderAmount);
            totBilAmt.setText(String.valueOf(df.format(totalOrderAmount)));
            totBilAmt.setText(CommonUtils.getSystemCurrency(this, totBilAmt, true));



            TableLayout orderSummaryTable = (TableLayout) layout
                    .findViewById(R.id.orderSummaryPopupTable);


        } catch (Exception e) {
            Log.e(TAG, "Error while setting Bill Summary Popup values. " + e.getMessage());
        }
    }



Pop up xml file:     

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/orderPopupWindow"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:layout_margin="3dp"
    android:background="@drawable/popup_window_background"
    android:gravity="top"
    android:orientation="vertical"
    android:weightSum="100" >

    <TableLayout
        android:id="@+id/orderSummaryTable"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="top"
        android:orientation="vertical" >

        <!-- Order Summary Header -->

        <TableRow
            android:id="@+id/discAmtRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="5dp" >

            <TextView
                android:id="@+id/txtView"
                style="@style/textview_style"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_weight=".99"
                android:gravity="center"
                android:text="@string/order_popup_header"
                android:textStyle="bold" />

            <ImageView
                android:id="@+id/btn_close_popup1"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:layout_gravity="right"
                android:layout_weight=".01"
                android:src="@drawable/popup_cancel" />
        </TableRow>

        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />
    </TableLayout>

    <!-- Displaying the Tax Wise Amount -->

<!--     <RelativeLayout -->
<!--         android:layout_width="fill_parent" -->
<!--         android:layout_height="wrap_content" > -->

        <!-- creating Horizontal scroll for the element -->

<!--         <HorizontalScrollView -->
<!--             android:id="@+id/billHorizontalScrollView" -->
<!--             android:layout_width="fill_parent" -->
<!--             android:layout_height="wrap_content" > -->

            <ScrollView
                android:id="@+id/orderScrollView"
                android:layout_width="fill_parent"
                android:layout_height="wrap_content" >

                <TableLayout
                    android:id="@+id/orderSummaryPopupTable"
                    android:layout_width="fill_parent"
                    android:layout_height="wrap_content"
                    android:layout_marginLeft="2dp"
                    android:orientation="vertical" 
                    android:weightSum="100">

                    <!-- table header for the sku details -->
                    <TableRow
                        android:id="@+id/orderSummaryPopupRow2"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/gross_amt_label2"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalGrossAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>
                    <TableRow
                        android:id="@+id/orderSummaryPopupRow"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/disc_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalDiscAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>



                    <TableRow
                        android:id="@+id/orderSummaryPopupRow3"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/taxable_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalTaxableAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>

                     <TableRow
                        android:id="@+id/orderSummaryPopupRow4"
                        android:layout_width="fill_parent"
                        android:layout_height="wrap_content" >

                        <TextView
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:text="@string/total_tax_amt_label"
                            android:textColor="@color/highlight_text_color" />

                        <TextView
                            android:id="@+id/totalTaxAmt1"
                            style="@style/textview_style"
                            android:layout_height="wrap_content"
                            android:layout_weight="0.5"
                            android:layout_marginRight="1dp"
                            android:gravity="right"
                            android:text="@string/initial_gross_amt" />
                    </TableRow>



<!--                     <TableRow -->
<!--                         android:id="@+id/orderSummaryPopupRow5" -->
<!--                         android:layout_width="fill_parent" -->
<!--                         android:layout_height="wrap_content" > -->

<!--                         <TextView -->
<!--                             style="@style/textview_style" -->
<!--                             android:layout_height="wrap_content" -->
<!--                             android:layout_weight="0.5" -->
<!--                             android:text="@string/credit_note_amt_lable" -->
<!--                             android:textColor="@color/highlight_text_color" /> -->

<!--                         <TextView -->
<!--                             android:id="@+id/creditNoteAmt" -->
<!--                             style="@style/textview_style" -->
<!--                             android:layout_height="wrap_content" -->
<!--                             android:layout_weight="0.5" -->
<!--                             android:layout_marginRight="1dp" -->
<!--                             android:gravity="right" -->
<!--                             android:text="@string/initial_gross_amt" /> -->
<!--                     </TableRow> -->

                </TableLayout>
            </ScrollView>
<!--         </HorizontalScrollView> -->
<!--     </RelativeLayout> -->

    <TableLayout
        android:id="@+id/orderSummaryNetAmt"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:gravity="bottom"
        android:orientation="vertical" >

        <!-- Total Bill Amount -->

        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />

        <TableRow
            android:id="@+id/totalOrderAmountRow"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_margin="2dp" >

            <TextView
                android:id="@+id/totalOrderAmountText"
                style="@style/textview_style"
                android:layout_height="wrap_content"
                android:layout_marginLeft="1dp"
                android:layout_weight="1"
                android:text="@string/total_order_amt_label"
                android:textColor="@color/highlight_text_color"
                android:textStyle="bold" />

            <TextView
                android:id="@+id/totalOrderAmount"
                style="@style/textview_style"
                android:layout_height="wrap_content"
                android:layout_marginRight="1dp"
                android:layout_weight="1"
                android:gravity="right"
                android:text="@string/initial_gross_amt"
                android:textStyle="bold" />
        </TableRow>
        <View
            android:id="@+id/sku_search_bottom_seperator"
            style="@style/seperator_style"
            android:layout_height="1dp"
            android:layout_marginBottom="3dp"
            android:layout_marginTop="3dp" />
    </TableLayout>

</LinearLayout>

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.