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.
Related
I have db with name of countries(for example).
My first goal is dynamically add textView for (each country) to linearLayout (android:layout_width="match_parent" android:layout_height="match_parent"). In fact adding is not a problem, but problem if the orientation of layout is horizontal and there is to much countries, but the width of layout is not enough to show all of them and they are hide!
So i need to add textViews dynamically, but if there is not enough space for new textView - add it in the next line or create new linearLayout.
I need to create new textView for each country cos than i want to make clickListener for each of them, and there is second goal...
the second goal is delete some of that textView by clicking on it, and the other textView must relocate to that empty space.
Hope that my explanation is clear :) if not i will try another one.
so, i have idea how to add textView: every time when i add new textView - count the length of all previously added ones with this one and compare it with length of linearLayout, if the length of all textViews is less - add to this linLayout, else - create new lin layout and add textView there.
I think this could work, it it looks ugly :)
I hope there are must be more simply and pretty solution!
Talking about dynamically deleting textViews from layout - I have no idea how to do this correctly.
So I will be glad any solutions and ideas, thanks!
EDIT
here is example how I want it looks like in the end:
LinearLayout is usually used for fixed childs.
For dynamic and large number of childs, you should use ListView or RecyclerView, which is exactly designed for what you described.
More than that, ListView and RecyclerView can reuse the child views, i.e. TextView for your case, which is needed, because view objects are heavy to create and keep in memory.
Edit:
Given your image and replies in comment. I would suggest you to use TextView with ClickableSpan, and set update the whole Text on click.
You can check ClickableSpan in the link below.
http://www.programcreek.com/java-api-examples/index.php?api=android.text.style.ClickableSpan
You can use flowlayout for that. Link for flowlayout is https://github.com/ApmeM/android-flowlayout. Just inflate your views inside flowlayout. You don't have to do any calculation when inflating. If there is space available for textviews then they will be added horizontally else in the next line.
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 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.
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'm trying to learn how to build apps for Android.
The first simple app, which will become a component of a bigger app I hope to build, is to have a button on the screen where, when tapped, it adds something new to the view.
For instance:
Imagine a layout that only has a button:
[Create!]
When that button is pressed, the view gets a new row added to it:
[Create!]
A Something!
Upon subsequent presses, more rows are added
[Create!]
A Something!
A Something!
And so on.
I've made a LinearLayout and placed the button in it, and have attached a click listener to it. That all works great. What I can't figure out is how to get a handle on the LinearLayout in the onClick function with which I'll addView() the new TextView that says "A Something!"
Am I on the right track? What basic thing am I missing? Thanks!
I think you are approaching this the wrong way. You should look into ListView and SimpleArrayAdapter. This will put the elements into a list format that users will be more familiar with. Google has some good examples that use this (like their Notepad example). Especially if you are new to Android, you should look at their demos to get you through the basics. you can find them here
This is from memory, so it may not be exactly right.
In your layout, you'll want to give the LinearLayout an id.
<
LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/namehere"
... >
Then in your code you'll use findViewById to get a reference to it.
LinearLayout ll = (LinearLayout) findViewById(R.id.namehere);
ll.addView(...);