Pass a layout from one activity to another - android

How do I pass an exactly looking RelativeLayout from one activity to another. The result shoul look something like this: Example
To explain:
I have a GridView which is populated from database using SimpleCursorAdapter and a partial layout.
Partial layout looks something like this:
<RelativeLayout>
<TextView/>
<TextView/>
<Button/>
<Button/>
</RelativeLayout>
When user clicks on a details button of an item, I woud like to pass that item's RelativeLayout to the DetailsActivity.
As I understand it cloning is not posible and inflating is not an option.
I coud pass every id, tag and text value and recreate a layout but I'm hopinbg there's an easier way.

You can't pass a layout.
What you can do is
use the same layout resource (defined in xml) in both the places.
pass the data to be used in layout from one screen to other.

Related

Querying root and indirect sibling views in Android ListView

I have a ListView to show a list of articles. Each ListView element is a LinearLayout. On each article, there is a TextView button to edit the title (another TextView). However, the button and the title are not under a direct parent (and actually not at the same level).
A sample structure is shown below:
<LinearLayout>
<LinearLayout
android:id="#+id/title_zone">
<TextView
android:id="#+id/title">
<ImageView
android:id="#+id/icon_popularity">
</LinearLayout><!--end of title_zone-->
<LinearLayout
android:id="#+id/content_zone">
...
</LinearLayout><!--end of content_zone-->
<LinearLayout
android:id="#+id/button_zone">
<LinearLayout
android:id="#+id/author_buttons">
<TextView
android:id="#+id/edit_title_button"
android:onClick="editTitle">
...
</LinearLayout><!--end of author_buttons-->
</LinearLayout><!--end of button_zone-->
</LinearLayout>
I write a SimpleAdapter to apply data to views, so the root LinearLayout will have a tag of the article ID. When editTitle() is called, it needs to find its parent's parent's parent to the root. And after new title is entered, a message will send to server containing new title and the article ID. Also, the title text will be changed visually, which means I need to find the title TextView based on the root.
The problem is that this querying root process is tightly coupled to the UI structure. If I changed the structure in XML, I need to pay attention to change the querying code in Java. (The querying title view is relatively easy, if the root is obtained.)
Is there a more maintainable way to implement my purpose?
OK. A solution that works for my situation is as follows:
In the customized simple adapter for this ListView, set the article ID as the edit_title_button's tag. And set the article ID composed string as the root LinearLayout's tag, e.g., "a_123" if the article ID is 123.
So, when editTitle(View v) is called, we can get the story ID by v.getTag(). Of course, we can also get the ListView by its ID (e.g., ListView list=findViewById(R.id.my_list_view);). Then, we can simply get the root LinearLayout by list.findViewWithTag("a_123").
Since the root LinearLayout is obtained without knowing the UI structure, following codes are easy to maintain.

One "list" below another inside a ScrollView

I would like to reproduce this layout :
The blue lists are not scrollable, but the red panel (probably LinearLayout) is.
I already tried two ListView but I don't think it is the good way to do this, it doesn't work.
I read an article that advised to add multiple items to a LinearLayout. But in doing so, how to handle events on a single item, or use a BaseAdapter ?
I know I'm a little vague, but I'm having a little trouble explaining what I really want, I started Android development a few days ago.
Thanks.
I don't think it uses any ListView.
You can probably create something like this by having the red panel be a LinearLayout or RelativeLayout that's inside a ScrollView, so that it scrolls.
Then the blue "list" is probably not a ListView if it doesn't scroll. I guess it's just a bunch of regular views. So, something like this (pseudo code)
<ScrollView>
<LinearLayout>
<whatever layout for the green cell>
<include layout="blue_cell>
<include layout="blue_cell>
...
</LinearLayout>
</ScrollView>
And then you just create a layout for your blue cell, see info here: http://developer.android.com/training/improving-layouts/reusing-layouts.html
If you have a variable number of blue cells, you can just have the ScrollView and LinearLayout in XML, and inflate the blue cells layout programatically and add them to the LinearLayout

building flexible GUI

I have a Layout that I want to populate with items consisting of 2 textviews and one button. I do not know before hand how many items that will populate my Layout.
Since I don't know when writing the layout.xml how many items I want to add, thats means that I have to add the items in the java instead of the xml. But I do not like to build GUI in java because it looks ugly.
Does anyone know if I can create an xml file for my item and then add new items to my layout during execution?
I have written some pseudo code to try to demonstrate what I want to accomplish:
MainLayout.xml
//My empty Layout
<Layout myMainLayout >
</RelativeLayout>
Fragment_post.xml
//one post
<TextView/>
<TextView/>
<Button/>
In the code somewhere
setContentView(R.layout.MainLayout);
MyMainLayout.addFragment(R.layout.Fragment_post);
You can add your fragment_post.xml wherever you want:
LayoutInflater inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout view=(LinearLayout)inflater.inflate(R.layout.yourfragment, null);
yourLayout.addView(view);
Please don't confuse a Fragment with a piece of the GUI. See here for details: http://developer.android.com/guide/components/fragments.html
Sure you can do this. Just set an initial empty layout to your activity.
onCreate()
{
setContentView(R.layout.initial_layout);
}
Then get and keep a reference to main layout.
LayoutInflater inflater =(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout mainLayout=(RelativeLayout)inflater.inflate(R.layout.initial_layout, null);
Next, add new views to your layout as and when you need them.
LinearLayout view=(LinearLayout)inflater.inflate(R.layout.fragment_post, null);
mainLayout.addView(view);
But note that what you refer to as fragments here are not what android refers to as fragments. Learn about actual android fragments here:
http://developer.android.com/guide/components/fragments.html

Speeding up complex Android View Inflation

I have a relatively big XML layout (38 kB, 600 lines) with hierarchy like:
<ScrollView>
<LinearLayout>
<TabHost>
<LinearLayout>
<FrameLayout> //tab widget
<LinearLayout> //tab content
<LinearLayout> //section
<TextView> //section name
<LinearLayout orientation="horizontal"> //item 1 box
<TextView> //item 1 title
<Spinner> //item 1 picker
</LinearLayout>
<LinearLayout> //item 2 box
<TextView> //item 2 title
<Spinner> //item 2 picker
</LinearLayout>
... //18 other items
</LinearLayout>
... //4 other sections with 15 items each
</>
It is a data entry form that has to have that many items and the best I can do now is to do to wrap setContentView and loading of data to the spinners in an AsyncTask with a "Loading..." dialog.
Does extensive using of themes also slow down the view inflation? The view inflater wouldn't need to look in loaded theme.xml, but if I were to inline the theme into the layout xml, it would also increase the size of the XML considerably, thus slow down the parser.
Is there something I could do to simplify the layout that would make it load at least twice as fast?
I'm thinking that I might try to get rid of the horizontal LinearLayouts, and build the "section" with TableLayout.
First, three general comments:
You really want to avoid layouts that deeply nested.
Consider using RelativeLayout to replace nested LinearLayouts.
Use the layout_opt tool to get some suggestions on layout optimizations.
Now, more on #1 … have you considered writing your own layout? It sounds complex at first, but it can sometimes drastically improve layout performance. For example, if RelativeLayout doesn't serve the purpose, you may be able to combine your Linear-Frame-Linear-Linear chain into a single custom layout.
Lastly, is there a reason your tab indicators are scrollable? That seems like a navigation/UX problem.
The only thing I can think to do would be to break your views into a couple different parts and inflate each view whenever the user gets to that part. Not sure if that helps at all but good luck.
Also might help to look at that portion of the code in traceview

Android XML Common Elements between pages? Frames?

I am skinning an app and I was wondering if there was a way to keep common elements the same between XML layouts without copying the same code around all the time.
For instance, I have a header and footer and background that will be the same between most pages. Can I make a "common" xml containing the header and footer and just load the individual page like a frame inside of it?
I'm not sure how I would reference that individual page within the XML. Maybe I could change an ID from the java side in an onCreate function, although it seems like more work now, if I changed something on the header and footer in the future I wouldn't have to change every Activity of the app.
Thanks for any insight!
I think it's the inclue tag you're looking for. Example:
<include layout="#layout/bar_header_top" />
Make a Java class that extends a View that holds all header elements and one that holds all footer elements and implement them in every view like this
<LinearLayout>
<com.[package].[classname of header] />
-- content here --
<com.[package].[clasname of footer] />
</LinearLayout>

Categories

Resources