Saturday 15 June 2013

android - How to change TextView Text -



android - How to change TextView Text -

i'm making custom toast, i've xml file custom toast layout named ctoast_view.xml

class="snippet-code-html lang-html prettyprint-override"><?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:orientation="horizontal" > <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@android:drawable/checkbox_on_background" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="" /> </linearlayout>

the main.xml file

class="snippet-code-html lang-html prettyprint-override"><?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" > <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="133dp" android:text="button" /> </relativelayout>

and ctoast main class

class="snippet-code-html lang-html prettyprint-override">package com.example.customtoast; import android.app.activity; import android.content.context; import android.os.bundle; import android.util.log; import android.view.gravity; import android.view.view; import android.view.view.onclicklistener; import android.widget.button; import android.widget.linearlayout; import android.widget.textview; import android.widget.toast; public class ctoast extends activity{ private context mcontext; private button mbutton; private linearlayout ctoastview; private textview toasttextview; @override protected void oncreate(bundle savedinstancestate) { // todo auto-generated method stub super.oncreate(savedinstancestate); setcontentview(r.layout.main); mcontext = this; toasttextview = (textview) findviewbyid(r.id.textview1); mbutton = (button) findviewbyid(r.id.button1); mbutton.setonclicklistener(new onclicklistener() { @override public void onclick(view v) { // todo auto-generated method stub showtoast(); } }); } private void showtoast() { // todo auto-generated method stub toast mtoast = new toast(getapplicationcontext()); mtoast.setgravity(gravity.center_vertical, 0, 0); toasttextview.settext("message!!"); mtoast.setview(getlayoutinflater().inflate(r.layout.ctoast_view,null)); mtoast.setduration(toast.length_long); mtoast.show(); } }

now got nullpointerexception on line

class="snippet-code-html lang-html prettyprint-override">toasttextview.settext("message!!");

i think because can't reference view in different layout file set setcontentview. how can dinamically set text of custom toast layout?

you need way:

private void showtoast() { view view = getlayoutinflater().inflate(r.layout.ctoast_view,null); toasttextview = (textview) view.findviewbyid(r.id.textview1); toasttextview.settext("message!!"); toast mtoast = new toast(getapplicationcontext()); mtoast.setview(view); mtoast.setgravity(gravity.center_vertical, 0, 0); mtoast.setduration(toast.length_long); mtoast.show(); }

inflate toast content view , textview when create toast.

android

No comments:

Post a Comment