Android - Re-use TextView showing current date time in multiple activities - android

I have some activities displaying the current date and time.
I want to make only one text view (layout) txtTime showing time and it will be updated each second by a handler in main activity.
Then others views of other activities can re-use the text view txtTime by including.
(I want only 1 handler updating date/time value and all app activities can display this value)
In fact, I don't know how to create/access a text view like that txtTime.
As usual I find a text view by (Activity).findViewById(R.id...).
But since it does not belongs to any activity I got stuck there.
My pb:
How to use a TextView as an application circle object which doesn't belongs to any activity?
How to change its text value(Date or Time)?
Do you have any suggestion/solution for me?
Thanks,
# The reponse of Remy:
You want something like that : static final TextView appTimeText = new TextView(appContext);, And in the handler appTimeText.setText("Text"); Now when you create the latout in any activity simple add it. – Remy

Tell me if I understand you currectly, you dont want to make your textview inside an any activity, because you want to use it in all the life circle of the application?
So if im understand you currect you want the application context.
Then you can retrievegetResources()
Im hope im understand you properly.

If you want other activities to get the text view from the 'txtTime' layout then <include #layout/txtTime' /> in the other layout.

Related

Display items (TextView, ImageView etc) on timers in Android

I'm fairly new to android and was wondering if in my activity I can display different text views for a certain amount of seconds before they vanish, replaced with a different text view.
Is there any way I can handle these events within an activity? I am creating a simple game and would like the Views in the activity to display after one another.
I could not find any help online (may have been searching for the wrong thing)
You can definitely use a timer variable to keep track of time and then use the setVisibility() method to set the textview visible(View.VISIBLE) or invisible(View.INVISIBLE).
For example:
TextView textView=(TextView)findViewById(R.id.text);
textView.setVisibility(View.INVISIBLE);

Create Instance of TextView

I am new to the android eclipse and I have a very small question. I use a Text View and a Text Field in my application. TextView just says "Enter a number below". Then, I am going to make a button so that if the user clicks on it, it makes some things given the inserted value of the textfield. Question is, do I have to say TextView mytextview = (TextView)findViewById(R.id.tview);
on my Main Activity to create an instance of it? Because, there is no data in it unlike Text Field, it just exists. Thanks a lot
There is no need to do that if you don't need to interact with the TextView itself, getting a reference to the TextView is only used if you need to manipulate it in some way or get data from it.

Loading XML layout

I have a problem loading previously created layout. I would like to load it and change text on buttons inside, then show it to the user. It will be quiz question and I have to show it many times during one activity. I don't want to create new class for my layout.
What do I have to use? I read something about Inflate class, but I think it is used only to create new classes. I tried setContentView() method, but app stops when method doing this load starts:
LinearLayout layout = (LinearLayout) findViewById(R.id.CapitalQuestionLayout);
setContentView((View) layout);
Can someone give some hints?
Try using the layout field not the id field, when you call from the R class, like so :
LinearLayout layout = (LinearLayout) findViewById(R.layout.CapitalQuestionLayout);
setContentView((View) layout);
For creating a "Quiz" app your basic requirement is:
a layout which has a TextView for Question and 4 Buttons for options.
a set of questions; pretty obvious :-).
You can create a Custom Class - Questions that will hold Text for a question and its associated options (as Strings).
Now, whenever you want to display a new question with different text for buttons just do the following:
If user clicks on right answer then display a Right-Answer-Activity to the user (that has a next-question-Button).
When user clicks on next-question-button you can display Question-Activity and populate the layout-views with a randomly picked question-object's attributes (i.e. Question's text and options' text).
Hope this helps.
This is quite common in Android apps. Do your fields/buttons have an id? For the parent activity, you mostly do
Button someButton = (Button) findViewById(R.id.the_id_to_the_button_you_want_to_change);
This allows you to do things like
someButton.setText("What is the ultimate answer to life, the universe, and everything?");
Look up the Andorid documentation if you want to do other things like set the color. If you have a viewgroup of some sort (RelativeLayout / LinearLayout / etc), you can specify that specific one
Button someButton = (Button)viewGroup.findViewById(R.id.awesomely_named_button_identifier);
The above someOtherButton is used more often with inflated viewGroups

Android: Show real time stamp on list view items

In my application, i have a ListView with a Custom adapter containing data like an image, 3-5 TextViews.
This data is fetched from a web server, until now i am showing something
This item is posted on Oct 10th 2012
but i want to change it to something like
This item is posted 6 days ago
So, its something like refreshing the ListView every 1 min, I dont want to use adapter.NotifyDataSetChanged() or something like that, which reCreates the whole ListView with the new data.
I just want that TextView alone to change in background and get displayed on the UI.
Thank You
One option would be to create a custom text view by extending the android TextView class and put a time task in it which update the text view message every one minute.
you can also use the
http://developer.android.com/reference/android/widget/TextView.html#onVisibilityChanged(android.view.View, int) to start and stop the timer task.
I don't think that is supported out of the box.
Since it seems you already have a Date/Calendar at hand just convert it to relative time string with a function like this: http://www.zachstronaut.com/posts/2009/01/20/php-relative-date-time-string.html
If you want real-time updates, I think your best bet is to start a background thread giving it the ListView and update the visible items periodically, however you may want to update all of them to prevent inconsistencies.
For Reference i suggest you to use below thing for change in ListView.
1.You need to get Row View for Particular item in which you want to change Textview.
2.Now from step 1 i believe you have your row view so apply change in it's child .

findViewByTag within a dialog

I've got a custom dialog layout that has two EditText fields and I've initially set the visibility to GONE for both (in the layout XML). In the dialog onCreate I want to do a findViewByTag to locate one of the two EditText fields so I can switch visibility to VISIBLE. Everything works find in the dialog if I switch visibility in the XML but I don't know how to get a reference to the dialog's main View from within the dialog so I can call findViewByTag.
I am inflating the layout in the dialog class's onCreate because that's how the example I found did it. I'm willing to change that if necessary to get the reference in the caller and set visibility before showing the dialog if that's the best way to do it.
Still pretty new to Android so any tips on how best to handle custom dialogs is appreciated.
I'm going to assume this example from outside of a view class.
Dialog amazingDialog = new Dialog(context);
amazingDialog.setContentView(R.layout.amazingdialogcontentview)
MyAmazingView view = (MyAmazingView)amazingDialog.findViewById(R.id.amazingview);
TextView tv = (TextView)amazingDialog.findViewById(R.id.textview);
I'm not sure precisely what your use case is, so there may be a better way to do this if you have access to some member variables you could initialize in onCreate, but if you don't:
You could try
View parent = myDialog.findViewById(R.id.parentId)
to get a known parent view of those EditTexts, and then call
parent.findViewWithTag(myTag)
to find your EditText.
Looking at the way you've phrased your question, and the fact you said you're new at Android, are you familiar with the difference between IDs and Tags?
An ID is a resource number assigned to an item (e.g., a View) by Android when you tell it to give something a name. You'd declare, in your XML:
<TextView android:id="#+id/myTextView"/> <!--with other parameters as necessary-->
And then you'd use
TextView tv = (TextView)findViewById(R.id.myTextView);
to find that TextView.
A Tag is an object that you can attach to a View (which I am pretty sure you can't do by XML), either for finding it later or for persisting some interesting information about it to use whenever you might next look it up (like a data object associated with its contents). So, you might say:
tv.setTag(myInterestingData);
so that you could later look up myInterestingData just by having a reference to tv.
After much reading and trial and error, I've concluded that the only way to do this is to use multiple EditText in the XML, all with visibility="gone". Then, in the Java code, have an if or switch to lookup and show the control either by tag or by ID. I was just trying to force too much abstraction into the Dialog class. With the multiple EditText I can use the class for multiple dialogs instead of having one class for each dialog.

Categories

Resources