How do I show text and a button? (android) -
i'm working on app generates random quote (from switch) when press button. goes next screen, displays textview. part works. want button under this, send user main screen (i plan on having 2 other buttons come later). however, app displaying either text or button, not both.
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_comp_out); intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); // create text view textview textview = new textview(this); textview.settextsize(40); textview.settext(message); button button = new button(this); button.settext("main menu"); button.settextcolor(color.parsecolor("#ff0000")); button.setheight(75); // set text view activity layout setcontentview(textview); setcontentview(button); }
this code i'm using create text output , button. string message quote. code making button fills whole screen though.
<linearlayout 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:orientation="vertical" android:background="@color/orange"> <button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginend="5dp" android:layout_marginstart="5dp" android:layout_margintop="5dp" android:textcolor="@color/blue" android:text="@string/button_menu" /> </linearlayout>
without java code making button, using this, wasn't making button either. (also background wasn't being set orange, thats not big deal) ideas on how put button under text?
you have 3 setcontentview()
instructions (the second , third override first one, why see button - 1 define in code - , don't orange background, anymore).
there must 1.
this instruction used set activity (or fragment) layout should contain both components.
try this:
protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_comp_out); textview textview = (textview) findviewbyid(r.id.text1); button button = (button) findviewbyid(r.id.button1); // add click handler button - choose 1 of possible methods (see link @ end of answer). // following instructions should go inside click handler. intent intent = getintent(); string message = intent.getstringextra(mainactivity.extra_message); }
activity_comp_out.xml
<linearlayout 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:orientation="vertical" android:background="@color/orange" > <textview android:id="@+id/text1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginend="5dp" android:layout_marginstart="5dp" android:layout_margintop="5dp" android:textcolor="@color/blue" android:text="@string/text_menu" /> <button android:id="@+id/button1" android:layout_width="match_parent" android:layout_height="50dp" android:layout_marginend="5dp" android:layout_marginstart="5dp" android:layout_margintop="5dp" android:textcolor="@color/blue" android:text="@string/button_menu" /> </linearlayout>
several possible methods handle view (not buttons) clicks illustrated here: http://www.tutorialspoint.com/android/android_event_handling.htm
Comments
Post a Comment