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.
Related
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.
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
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.
I want to create a pocket reference application. So, much of the content would be texts, linkbuttons and images.
I wonder where is a good place to put all of the contents. I could place it hard-coded on the source code, So, when a user click a linkbutton, a new view will be opened and in the source code I specify the TextView and setText, etc. But I think it's not a good idea. Can I put the content in an xml file or in a database? Which one is better for this case?
I see that we are encouraged to put layout in main.xml. But, from what I read, the xml layout is static, what if I want to put some TextView, but I don't know how many TextView would be displayed, because the content would be loaded dynamically/programmatically?
Thank you.
Not sure it this is what you meant:
You can initialize your application ui by an android xml file layout.
to inflate, you use this method.
in your activity's onCreate()-Method or even later, you can then get the TextViews or whatever you want by calling findViewById(R.id.textview). Note that this method will search all over the layout xml file for the specified id and though blocks the ui thread while searching. if your textview is very near at bottom and many other elements come before it, this can take some time.
if you want to build your own layout dynamically, you have to do this programmatically of course.
for general layout declaring, refer this tutorial on android dev guide.
You could write the textView in a xml layout and inflate it dynamically in the activity as many times you want
View view = getLayoutInflater().inflate(R.layout.scroll_project, null);
//then add the view in linear layout as
layout.add(view);
I have an Activity with a listView with few options and a button at the bottom of the screen. The listView is just to configurate some options so, when i click in any of the items in the list its needed to let the user choose between some options (in some cases i'll use another list to show the options, in other cases i'll let the user write in an editText view) to make the configuration.
It's recommendable to create new Activities to show this options or can i choose other way? I was thinking about loading a new .XML in the same Activity but im not sure if this is "a good practice".
Something like that:
Activity{
setContentView() --> The main XML
setOnItemClickListener{
switch between item's Id's and setContentView() depending on the item;
}
}
Thanxs!
#EDIT
I also have a question about declaring new classes. I've seen some tutorials declaring a custom Adapter class inside the main Activity. So, once again, is that a good way of doing things? :D
AFAIK, you can not use the setContentView() more than one time. It makes the conflict. But you cna achieve it using view's visibility change. That is you have invisible the current ListView and make visible the next view what you want to show.
You do not have to create a new Activity. For settings that are set through list of checkboxes or through a radio button selection, please check Android documentation for
AlertDialog
AlertDialog.Builder
Very easy and simple (and visualy acceptable) way to set some setting in your current Activity. Also, you can put some .xml in the DialogBuilder (through setView() function) and customize your Dialog that way (also possible to put EditText widget in the dialog to get some string).