android - How to make button come down after edit text field? -


i have piece of code shows static text, edit text search box , submit button submit query .

the code kind of looks :

public class myactivity extends activity {      textview textview;     private edittext edittext;     button button;      @override     protected void oncreate(bundle savedinstancestate) {         super.oncreate(savedinstancestate);         setcontentview(r.layout.activity_mine);         textview = (textview) findviewbyid(r.id.textview1);         textview.settext("enter search string :");         addkeylistener();         addlisteneronbutton();     }      //implement method     public void addkeylistener() {         // edit text component         edittext = (edittext) findviewbyid(r.id.searchtext);         // add key listener keep track user input         edittext.setonkeylistener(new onkeylistener() {         public boolean onkey(view v, int keycode, keyevent event) {              // if key down , "enter" pressed             if ((event.getaction() == keyevent.action_down)                 && (keycode == keyevent.keycode_enter)) {                  // display floating message                 toast.maketext(recipeactivity.this,                     edittext.gettext(), toast.length_long).show();                 return true;             } else if ((event.getaction() == keyevent.action_down)                 && (keycode == keyevent.keycode_9)) {                  // display floating message                 toast.maketext(recipeactivity.this,                     "aboriginal text!", toast.length_long).show();                 return true;             }             return false;         }      });     }      //implement button     public void addlisteneronbutton() {         button = (button) findviewbyid(r.id.button1);         button.setonclicklistener(new onclicklistener() {             @override             public void onclick(view arg0) {                /* capture search string here */             }         });     } } 

the activity screen comes :

enter image description here

edit : xml layout:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     xmlns:tools="http://schemas.android.com/tools"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:paddingbottom="@dimen/activity_vertical_margin"     android:paddingleft="@dimen/activity_horizontal_margin"     android:paddingright="@dimen/activity_horizontal_margin"     android:paddingtop="@dimen/activity_vertical_margin"     tools:context=".recipeactivity" >        <textview            android:id="@+id/textview1"           android:layout_width="wrap_content"           android:layout_height="wrap_content">       </textview>        <edittext         android:id="@+id/searchtext"         android:layout_width="match_parent"         android:layout_height="wrap_content"         android:label="@string/search_label"          android:hint="@string/search_hint" >         <requestfocus />       </edittext>        <button         android:id="@+id/button1"         android:layout_width="wrap_content"         android:layout_height="wrap_content"         android:text="submit" />  </relativelayout> 

as can see submit button overlapping edit text search box. how can make button element come down ?

it better use linearlayout android:orientation="vertical" in xml layout because it's cheaper relativelayout render.

solution linearlayout

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent"     android:orientation="vertical">    <textview        android:id="@+id/textview1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"/>    <edittext     android:id="@+id/searchtext"     android:layout_width="match_parent"     android:layout_height="wrap_content"/>    <button     android:id="@+id/button1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:text="submit" />  </linearlayout> 

if have use relativelayout:

<relativelayout xmlns:android="http://schemas.android.com/apk/res/android"     android:layout_width="match_parent"     android:layout_height="match_parent">    <textview        android:id="@+id/textview1"       android:layout_width="wrap_content"       android:layout_height="wrap_content"/>    <edittext     android:id="@+id/searchtext"     android:layout_width="match_parent"     android:layout_height="wrap_content"     android:layout_below="@id/textview1"/>    <button     android:id="@+id/button1"     android:layout_width="wrap_content"     android:layout_height="wrap_content"     android:layout_below="@id/searchtext"     android:text="submit" />  </relativelayout> 

see relativelayout documentation further informations.


Comments