I am a new android dev. I want to use a same TextView in all activity. But, i don't know how to declare TextView as global Variable & How can i use this to show text in activity. Please help me. The code is simple. Thank for every one.
2
-
if your layouts will be stored in xml file then you can use them anywhere in your application. You just need to inflate them and they wil be available to you.Prateek– Prateek2012-12-29 11:51:53 +00:00Commented Dec 29, 2012 at 11:51
-
Oh man! it's just not you think. I need to Change color with RadioButton & resize text too. then i want to known a code.T2terBKK– T2terBKK2012-12-29 11:54:49 +00:00Commented Dec 29, 2012 at 11:54
Add a comment
|
4 Answers
Write your XML code of text view with id as:
<TextText
android:id="@+id/textview"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
Than in your activity declare it before onCreate() method as:
public static TextView textview = (TextView) findViewByID(Your ID);
Than this will be accessible to all calsses.
Comments
Make it a singleton. Or just keep one public static reference.
public class MyReference {
public static TextView myTextView = new TextView();
}
and then you can use it anywhere by calling MyReference.myTextView
2 Comments
T2terBKK
It mean i must create class for constant variable ? Because i have many activity in my app are use this textView. Before i use this, i have to import class too ?
Vinze
Well I don't know about Android, but that's basic Java... And you don't have to create a class for this, you can put it in any meaningful one. But first I would encourage you to read any Java for beginner book :)
I think this Link this is helpfull to
http://alinberce.wordpress.com/2012/02/20/android-edittext-with-custom-font-and-clear-button/
you have to create Editbox just One time & use anywhere in Application
weather use in Layout file or use Dynamically in java Class