title: OnClickListener inside custom alertdialog Android
body: <p>I am a self-taught beginner and appreciate patience! Thanks!</p>
  
  <p>In Eclipse, I made a custom alertdialog with its own xml file ("custom_dialog") and it is called "usernamealert".</p>
  
  <p>I want an alert to pop-up if the user hasn't entered a username yet (ie, username.length == 0).</p>
  
  <p>Inside this layout I have a textView ("What is your name?"), editText and button ("usernameButton").</p>
  
  <p>Before putting in the onclicklistener for the button, everything worked. This was my (relevant) Java:</p>
  
  <pre><code>LayoutInflater inflater = getLayoutInflater();
  View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)     getCurrentFocus());
  AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
  usernamebuilder.setView(dialoglayout);
  
  AlertDialog usernamealert = usernamebuilder.create();
  </code></pre>
  
  <p>When I put the onclicklistener in, it broke! Where should I have put it?</p>
  
  <p>(the following is what I had tried... all in my OnCreate)</p>
  
  <pre><code>LayoutInflater inflater = getLayoutInflater();
  View dialoglayout = inflater.inflate(R.layout.custom_dialog, (ViewGroup)getCurrentFocus());
  AlertDialog.Builder usernamebuilder = new AlertDialog.Builder(this);
  usernamebuilder.setView(dialoglayout);
  
  
  Button usernameButton = (Button) usernamealert.findViewById(R.id.usernameButton);
  usernameButton.setOnClickListener(new OnClickListener() {
  
  @Override
  public void onClick(View v) {  
  //store username in sharedprefs
  usernamealert.dismiss();
  
  
  
  }
  });
  </code></pre>
  
  <p>After the code I said:</p>
  
  <pre><code>if (username.length() == 0) {
              usernamealert.show();
          }
  </code></pre>
  
  <p>Again, it worked before I started messing with the button!!</p>