Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to open popup window on EditText long press in Android studio

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. :)

like image 855
M10 Avatar asked Aug 04 '15 06:08

M10


1 Answers

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;

}

}

like image 87
Kamleshwer Purohit Avatar answered Oct 13 '22 20:10

Kamleshwer Purohit