Adding a Textview to a relative layout in Android -
so have researched everywhere no else seems have problem because know how program, anyways i'm trying add textview relative layout in android app, no matter cannot text , buttons have in layout. here code activity:
public class enterapp extends activity { int marblesinjar=0; int targetmarblesinjar=0; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); intent intent= getintent(); targetmarblesinjar= intent.getintextra("message_marbles",0); relativelayout marble= new relativelayout(this); textview textview = new textview(getapplicationcontext()); textview.settextsize(20); textview.settext("your marble jar full when has "+ targetmarblesinjar + " marbles. have " + marblesinjar + " marbles."); textview.settextcolor(color.black); marble.addview(textview); this.setcontentview(r.layout.activity_enter_app); // show button in action bar. setupactionbar(); }
here code xml relative layout
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:id="@+id/relativelayout" android:layout_width="match_parent" android:layout_height="wrap_content" 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=".enterapp" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/button2" android:layout_alignbottom="@+id/button2" android:layout_marginright="16dp" android:layout_toleftof="@+id/button2" android:onclick="addmarble" android:text="@string/addmarble" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_alignparentright="true" android:layout_marginbottom="82dp" android:layout_marginright="22dp" android:onclick="lossmarble" android:text="@string/losemarble" /> </relativelayout>
you have relativelayout
don't need
relativelayout marble= new relativelayout(this);
this creates new relativelayout
. create textview
, add layout
after inflate
setcontentview()
public class enterapp extends activity { int marblesinjar=0; int targetmarblesinjar=0; relativelayout marble;
@override
public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); ... setcontentview(r.layout.activity_enter_app); textview textview = new textview(this); textview.settextsize(20); textview.settext("your marble jar full when has "+ targetmarblesinjar + " marbles. have " + marblesinjar + " marbles."); textview.settextcolor(color.black); marble = (relativelayout) findviewbyid(r.id.relativelayout); // id gave root layout marble.addview(textview);
Comments
Post a Comment