Can I implement some logic in XML? - android

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.

Related

Is it possible to make a sample android view (Card View) in XML and store it in a a variable then use it again and again?

I want to use a cardView having some internal structure at multiple places. Is there a way to make a cardView in an extra layout XML file and then store it in a variable programmatically and later use that variable to show that cardView where ever I want by adding it dynamically?
I want to make that cardView sample in XML as it much easier to edit there.
Currently, when I try above method I get an error "view already has a parent" for obvious reasons. I can definitely copy paste code but i would like to know if there is any smart way.
Any help will be appreciated. Let me know if you need any other detail or find it hard to understand my question.
you can do it.......
have a separate xml for your card view...........let say card.xml
in your main/activity xml make/select a parent layout (Linear / Relative) where you want to place it......or may be different view..
private void addMyCard(){
ParentLayoutType parent=(ParentLayoutType) findViewById(R.id.idGivenToParent);
View yourCardViewName= LayoutInflater.from(this).inflate(R.layout.card,null);
parent.addView(yourCardViewName);
}
Hope it helps

How dynamically add/delete textViews to/from linearLayout?

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.

How to dump as XML programmatically generated Layout

Is there any possible method to dump programmatically generated Layout?
For example I create Layout
LinearLayout mainLayout = new LinearLayout(this.mContext);
//...some code here
mainLayout.addView(picker);
mainLayout.addView(mOldColor);
mainLayout.addView(separator);
mainLayout.addView(mNewColor);
Now how to dump to Lod.d for example and get XML representation of Layout with attributes?
Afraid not. Views themselves know nothing about the xml they came from, so there's no way to force them to serialize back to xml.
Now if you wanted to do it on your own you can walk a view hierarchy fairly easily. But since views don't turn in to xml, you'd have to query each property individually and build the xml by hand. And it wouldn't work at all for custom views that could have custom attributes. And you'd have to use reflection to get the class name for the view type in the xml. Basically a lot of work for something fairly fragile.

Android Dynamic Content Design

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);

android repeater-like functionality

is there any type of 'repeater' type functionality in android? I have a relative layout (inside a row in a listview) and inside that I'd like to have a series of TextViews be displayed one after the other (as if they are child rows in the listview row). The issue is that the number of those "child rows" will vary. is there any way to do this, or should i just create the TextView objects in code, and programatically add them to a linear or table layout?
The closest thing (besides ListView/ListAdapter, naturally) that I can think of offhand is ViewSwitcher and ViewSwitcher.ViewFactory, but there's really nothing magical there: it's an interface that you can call to get a view.
Since it's only one line to get a view and add it to your current hierarchy anyways, though (View.inflate(context, R.layout.somelayout, myContainerViewGroup)) it feels silly to go with something heavier, but if you feel better wrapping that up in a Factory of some sort, check the AOSP source for ViewSwitcher.
One option is TextViews support Multi-line text. So you could create the text with a StringBuilder using "\n" for new lines, and not have to worry about multiple text views.

Categories

Resources