How to open popup on EditText for long press in android studio

How to open PopUp Window when EditText pressed for a long time, when user can enter or paste content and can press ok and then put the entered text in EditText , and I don’t want the user to use the enter key.

My main motive is that the user can see all the text at once, since the size of the EditText small in my apk.

Note: - I am starting to program in android, and this is my first question. :)

+5
source share
4 answers

What I understood with your Question. My answer

  • You can open a pop-up window by long pressing Edit Text.
  • Copy the text edit text into the -edit popup.
  • When you click "ok", copy the text of the pop-up window, edit the text back to the main text box.

adding code for your reference

Xml popup -

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

Primary 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
source

welcome to SO. You should do some research before asking a question in SO. provide relevant information to help us find the answer

as the answer to your question, you can use setOnclickListener to attach the onclick listner to your EditText inorder to trigger an event when the user clicks EitText.

for a popup that includes an EditText where the user can enter input

you can use dialog with your own xml layout

look at the page for creating and using dialogue

+2
source

Most of the material can be found on the Android API page, as Dev said. However, this is an example slightly edited from the documentation to help you 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> 

Put it 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 did not execute this code so that there might be errors, but what a concept

0
source
 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> 
0
source

All Articles