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);
Related
I want to add and remove some views frequently like
Recycler view,textview,seekbar,customview,imagebutton etc.!!!!
I know following methode to perform it
Add all view to layout and just play with Visibility.GONE, Visible and other..
using layout params add and remove view...
Use View.inflater to add and remove predefined XML views(I'm using)
So question is
1.Is there any other method to do it?
2. Which one you prefer and why?
When you make Visibility.GONE all the views and associated resources such as image, sound files will be kept in the memory and if you have a lot if resources that may slow down your application. I think better way would be to use Fragments.
Check out this link. Hope this helped
I am new to android programming am trying to learn it.
So basically there are two ways of creating whatever is going to be visible on the screen :-
We create views and view group objects inside layout.xml files. And then as the need be we access those views that are already existing
in the layout through our java programs by accessing their ids ( as
r.java….). So basically when we start a particular activity, we
set the content to be displayed corresponding to that activity using
setContentView method, to which we pass the layout.xml file, inside
which we have defined the different views and view groups to be
displayed on the screen.
The second way is we create these views dynamically though our java programs and then set them as the content on the screen using
setContentView again.
Now the above is my basic understanding. Please let me know if the above needs correction.
Now what I want to understand here is :-
Is there a way that using the first method itself, we can do the
vice-versa, as in say instead of fetching the the views through their
ids from the layout.xml files, can we already have a predefined
layout.xml file with different views having ids, and now through our
java programs can we just access those views though their ids and set
their values, something like say (in javascript) :-
document.getElementById(“someTextBoxId”).value= “some calculated
value from java code here”
Thanks.
The app I am working on requires a dynamic layout that displays a HorizontalScrollView of profiles. A profile is simply a RelativeLayout with a picture and some text. Since I get the data from a data file, I need to create a Relative layout for each of profiles. At first I created each RelativeLayout programmatically in a for loop and then added it to the parent view. This works, but I don't want to do it this way. I want to use different layout files based on the screen size of the device, etc.
Then I thought. Well, what if I had a Layout with just one profile on it? My code could get that one profile with findViewById() and then create new ones based off of it! In other words:
// get the layout
profileLayout = (LinearLayout) findViewById(R.id.profileLayout);
// get the first profile in the layout
originalProfile = (RelativeLayout) findViewById(R.id.profile1);
// make copy of profile
temporaryProfile = originalProfile;
// make changes to the this profile and add it back
profileLayout.addView(temporaryProfile);
Of course, this doesn't work because this is java and temporaryProfile is now a reference to originalProfile. So is there any way to make a copy of this RelativeLayout? I'm aware of LayoutInflater, but I still don't understand how it works. There is also Object.clone().
Not so much. Sounds like your particular case could benefit from a ListView of some kind and an adapter (see resources like this for info). If not, then a LayoutInflater is the best answer as it does precisely what you want. Acquiring one you can then use it to "inflate" any view you've defined in your XML layout files and do whatever you want with it.
Here's a great discussion of the inflater.
you can make a copy of your inflator with a new context. Layout Inflater - Android Developers
Why not simply use an include in the xml file for the layout that you want.
I need XML for creating layouts in my Android apps and what I wanted to know is the following. Can I implement some logic in XML? For instance, I wanted to position my text exactly "(fill_parent - (the width of the image))/2"... something like this.
you cant do that directly in xml file. if u want to position ur text exactly means just set the positons of the text, check properities of text.
setContentView(R.layout.main);
tv=(TextView) findViewById(R.id.textView1);
main.xml
contains textview with id textview1.
like this u can acess the textview of ur xml file..
Not inside the XML. The usual way this is done is to locate the views with findViewById() in onCreate() after the setContentView(), then adjust the relevant properties programmatically.
Edit: after re-reading the example in the question, my advice seems too general (towards modifying any properties on a view). For calculated layout positions you are better achieving these kinds of results with relative layouts or some of the specialized view containers.
I'm reading the book 'Hello, Android'. In the Sudoku example, it uses a options menu. It needs a MenuInflater that we use to read the menu definition from XML and turns it into a real view.
To use button, textview, or many other views, I don't need to inflate them.
My question is, in what situations, I need inflaters? Why doesn't Android treat menus like other views?
You need an inflater at every place that you want to dynamically create a view out of an XML file.
Activity layouts are automatically inflated when you call setContentView() as they're always required.
But when the menu is required — which is only when the user first presses the Menu button — the XML-defined layout needs to be manually inflated.
Similarly, if you have a ListView, you don't know in advance what rows will exist, so we have to inflate a View from XML for each row in the list, as they're required.
Inflaters are mainly used for parsing Xml layout into view objects. As mentioned above the inflation is needed for making a link between the UI defined in the Xml for manipulating and making developer.
Whenever UI Updation is needed we need inflation and UI Updation is done through view object and developer can dynamically create view and add to existing view.
Hence inflation helps developer to change behaviour of UI in xml layout according to specified condition in a program.
With inflation, we are able define controllers in MVC for each xml layout where xml is view.
Menu is also a view it has to inflated In certain code such setContentView(specifiedLayout) includes inflation
But in earlier version it was not like this it was like setContextView(getInflater().inflate(specifiedLayout))
for ease of programming,android developers have incorporated inflation in setContentview() and there are lot scenarios like add view to layout addView(),etc..In most cases inflation has incorporated in code that why most of beginner does know inflation concept and has difficulties in understanding inflation in android.