Create Instance of TextView - android

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.

Related

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

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.

Recovery fields from a form in a array

In my current Android project, I am adding a layout file for an activity composed by a list of EditText fields and a Button in the end.
I am looking for a way to, when the button is clicked, the method associated to the click retrieve all ths fields and store them in a array of Objects (something like Object object[]).
Anyone knows if this is possible and how to do that?
You have to get a reference to each of the EditTexts and then get their current value, one by one.

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.

Android: Using my own customized layout to fill a scrollview

I've been learning and building an Android application from scratch and I've hit a wall.
I read on the android developer page about customizable UI components and figured I could use those to contain messages for a chat page in my application.
I have made the XML files but I have no idea how to call those layout files from my code.
At the moment I read my messages from an array and then go through a loop making a TextView and setting the necessary properties for it through code, then adding it to a RelativeLayout within a scrollView. This all works fine but I want my messages to display more information (such as time and if the recipient has received/read it), this is where the new XML files come in, they have the field and layout already set up, so I figured I could just call those, set some variables and be done with it.
As I've stated before, the problem is that I have no idea how to reference the layout files inside my code. I've already read all the articles on the android developer page concerning building your own UI elements and I've also googled my fair share, but apparently I'm either the only one who has thought of doing it this way, or I can't find the right keywords for it. So now I'm asking you guys. Thanks ahead for any answers you guys can give me.
Edit:
I've been doing some research, and I think what I need to do is write my own adapter to work with my own class of messages and my own XML layout for those messages. However I have no idea where to begin with this, since I can't find any good documentation on how to write your own adapters.
You could do the following, but that will create a new TextView ignoring whatever layout you had:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = new TextView(this);
textView.setText(message);
scrollView.addView(textView);
textView.setText(timeAndDate);
scrollView.addView(textView);
I've never personally used ScrollView, so I'm not 100% sure on the above functionality, but it matches my experience with LinearLayout and RelativeLayout.
Alternatively, since you have your customr layout already, you would do:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
TextView textView = (TextView)findViewById(R.id.textView);
textView.setText(messageTimeDate);
scrollView.addView(textView);
That's essentially how you could handle it. I'm not 100% sure if you could add textView more than once in either implementation. If that throws an exception, you could use an array list of text views, such as:
ScrollView scrollView = (ScrollView)findViewById(R.id.scrollView);
ArrayList<TextView> textView = new ArrayList<TextView>();
textView.add((TextView)findViewById(R.id.textView));
textView.get(index).setText(messageTimeDate);
scrollView.addView(textView.get(index).getId());
Wherever I put textView for the id, you could use message or timeDate, and make two of each thing I referenced, then.
For those interested in how I did it (and if you are running in to the same problem yourself):
For every list I wanted to create with my own random set of data I created my own adapter which extends BaseAdapter. By simply overriding the getView method it's possible to inflate and fill the different parts of your listitems.
I used this tutorial to help me with creating my own adapters.

Linking two EditText boxes for unit conversion

I'm trying to come up with a scalable way to link two edit text boxes together for unit conversion. The user would enter a value in either of the boxes and the converted value would show up in the other.
I would normally just make it so when one EditText was edited, it would get the input, pass it through a method to convert the value and set the text of the other field.
The problem is I have a lot of these pairs and each pair is used to convert a different kind unit. If I used a TextWatcher for each box, you can see where that would start to get out of hand.
I thought about extending the TextWatcher so I could pass it the EditText View and its partner's view, but I'm not sure how to pass it what kind of conversion method needs to be used. I could assign an int to each kind of conversion and use a switch, but that doesn't seem like a good solution to me.
Is there a better way?
This is how I would do it. Create a class, say PartnerEditTextInfo ,which contains a reference number and a EditText obj. Attach this as a tag to every EditText in your app. Set the editText in the PartnerEditTextInfo to the partner editText and have a unique reference number. Thus every EditText can get a hold of its partner.
Extend the EditText class and over ride onTextChanged() method to call a common conversion method(This can be a static class static method) . You can get the partner editText by getting the tag object of the editText whose text has changed. Then ,based on which pairs of editText(based on reference number) is changed apply required conversion formula and do the setText on both the editTexts in the pair.
Caution- You need have a way to make sure you wont get into a infinite loop, have some sort of flag to differentiate changes to editText's text made by user and made by the conversion method.

Categories

Resources