0

I have a text box that takes the users input then launches this link or launches a reddit app (depending on what the user chooses in the chooser that appears). For some reason when you click the textbox to start typing, the chooser comes up without leaving the user a chance to even start typing. Why is this happening and how can I make it so that the chooser only comes up after the user submits their text?

public void searchBar(View view)
{
    EditText et = (EditText)findViewById(R.id.editTextSearchBar);
    String input = et.getText().toString();
    String goTo = "http://reddit.com/r/"+input;
    Uri webpage = Uri.parse(goTo);
    Intent webIntent = new Intent(Intent.ACTION_VIEW,webpage);
    startActivity(webIntent);
}

<EditText
     android:id="@+id/editTextSearchBar"
     android:layout_width="match_parent"
     android:layout_height="wrap_content"
     android:ems="10"
     android:hint="@string/redditHint"
     android:inputType="textUri"
     android:imeOptions="actionGo"
     android:onClick="searchBar"
    />

I understand that this code does not account for the instance of the user not entering any text but hitting enter anyway. For all intensive purposes, lets assume the user does enter some text.

gsamaras
  • 71,951
  • 46
  • 188
  • 305

1 Answers1

0

This because you are not doing it right. When the user clicks on the edit text your searchBar function is called which in turns calls startActivity -> Application selection dialog

Have a look at here which handles the return button on the keyboard. I hope that this explains

Community
  • 1
  • 1
hoomi
  • 1,882
  • 2
  • 16
  • 17
  • what I am getting from that is I need an onClickListener instead of using onClick in the XML? Either way the method will end up calling startActivity -> Application selection dialog so what is the difference? Your comment about the return button, the user cant even get to the keyboard, becasue the instant they click the textbox the dialog appears as opposed to the keyboard. – user3749443 Jun 24 '14 at 19:09
  • What was saying was that you do not need to have onClick listener. just remove the onClick attribute from the xml since you do not need to listen for click events – hoomi Jun 24 '14 at 21:37
  • if I dont implement the onClick listener (which I currently dont have) and now you are saying remove onClick in the XML, how will this method ever get called? – user3749443 Jun 25 '14 at 15:42
  • Have you had a chance to look at the link I sent you? – hoomi Jun 25 '14 at 15:45
  • yes but the info in that link is different than the problem I am having. That question pertains to handling keyboard return/enter key action, while my problem happens even before the user gets to use the keyboard – user3749443 Jun 25 '14 at 16:15
  • What you want is that user types something in the EditText and then after they are done, your app launches the browser with your predefined url + userinput. The problem with your code is that when you click on the EditText the `searchBar` function is called which automatically launches the browser. In order to test that remove `android:onClick="searchBar"` and see that when you click on the EditText the keyboard pops up and you can enter a text. – hoomi Jun 26 '14 at 13:54