drawing multiple layouts inside a view - android

I have a view/layout that I need to render multiple times on a canvas. Call it stats. So I create a stats.xml file and lay out my views in it. Then in the .java file, I inflate the layout and edit the appropriate child views of stats. At this point how do I add the view to my canvas at a specific location? Actually I already tried but my view is NOT showing. Here is the code:
LayoutInflater mInflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
RelativeLayout parentView = (RelativeLayout) mInflater.inflate(R.layout.stats, null, false);
addDateToTextView(date, (TextView) parentView.findViewById(R.id.timestamp));
parentView.setX(50);
parentView.setY(yOffset);
parentView.draw(canvas);
I am using the canvas from onDraw

So I find a crappy solution. I have to call layout and .draw(canvas) on every child view. I am convinced there must be a more sensible solution.

Related

Add XML content to a View

im developing an application and I need to add elements dynamically. I wonder if I can append elements (stored in a XML file) in my current Activity, like innerHTML in JavaScript.
I tried LayoutInflater but that replaces all the content and I need to append.
Thanks!
The easiest way to do this is to use the LayoutInflater as you said. I'm not sure how you were doing it (hence why I asked to see your inflating code), but the simplest way to understand is to do the following:
LayoutInflater inflater = getLayoutInflater();
View viewToAppend = inflater.inflate(R.layout.some_layout, null);
// Optional, create LayoutParams and apply to view with
// viewToAppend.setLayoutParams(params);
mainView.addView(viewToAppend);

Dynamic layout design according to data received from web-service call

Hi ,in my app i need to build layout dynamically.All the dimensions of views in layout i will get from web-service.In the web-service we may have different tile sizes,According on tile size we have to place the tile in appropriate position.
Can any one help me to build layout dynamically.
You can inflate different layout programmatically.
So you should have something like this:
LayoutInflater inflater = getLayoutInflater();
ViewGroup layoutLoaded = (ViewGroup)inflater.inflate(R.layout.yourlayout, null);
mainLayout.addView( layoudLoaded );
In this way you can load all layout you want when you need.

How to inflate another view from another view in Android

I know in my onCreate() I can inflate a view from XML by something like:
loadingScreen = (RelativeLayout) findViewById(R.id.loadingScreen);
But how could I do this from another view? Im trying to call up a loading screen by setting its visibility from GONE to VISIBLE but cant seem to figure out how to do this from my glSurfaceView
If you want to inflate a layout the code looks like this:
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myRoot = new LinearLayout(context);
View itemView = inflater.inflate(R.layout.layout_details, myRoot);
Here you first create a new LinearLayout an then inflate the layout with id R.layout.layout_details into it. The inflate method then returns the myRoot view.
Here is a tutorial about the LayoutInflater:
Layout resources in Android
Thats actually not inflating. Inflating is the process that parses a XML layout file and creates a structure of View and ViewGroup class instances out of it (setContentView() does this for you in the background for example).
What you do is getting a reference to a view in code that you have defined in your XML layout file. To change the visibility of your GLSurfaceView you have to reference it like you did above. But remember that the View (GLSurfaceView in this case) has to be defined in your layout file.
After referencing you have to call GLSurfaceView.setVisibility() to change it's visibility.
Here's an example:
GLSurfaceView glsurface = (GLSurfaceView) findViewById(R.id.myglsurfaceid);
glsurface.setVisibility(View.VISIBLE);
Of course you can use View.INVISIBLE or View.GONE either, depending on what you want to do.
If you reference a layout (such as a RelativeLayout), you may find children of this layout with the findViewById() of your RelativeLayout instance:
RelativeLayour rl = (RelativeLayout) findViewById(R.id.mylayout);
(Button) mybutton = (Button) rl.findViewById(R.id.mybutton);
But thats usually not neccessary (at least when you just started with Android) because the activities findViewById() finds all Views that are displayed, even in sublayouts. You only have to use it if you have duplicate ids in your ui structure (tbh I never had that case yet) and want to specifiy where to look for your particular View.
You can't get a reference to a View that's doesn't exists in your current Layout, or your current View, (your current Activity content) , but you can create a new View from another XML layout, using LayoutInflater from current Activity.
you can add to you current Activity content, a new View, that's what you mentioned as " loading screen ", even by showing it as a Dialog or by creating View and then add it to root layout in your Activity
I hope I helped you
If I correctly understood what you wanna do:
Supposing you have a glSurfaceView object and you wanna grab a view that's inside that one.
You'll do just the same thing you did for you normal view. Let's say a button:
Button button = (Button) glSurfaceView.findViewById(R.id.buttonid);
If you meant something different let me know in the comments.
EDIT: And then you can just set the button's visibility:
button.setVisibility(Button.GONE)

how to fill in scroll view from file or database or any data source?

I want to bind data from an xml file? how can I do that where i'm using a layout xml file to define a scrollview ??
If i've understood your question right, you want to read a layout file and insert it into a ScrollView.
You would want to take a look LayoutInflater for this task.
Example for doing this from an activity.
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
ScrollView sv = findViewById(R.id.id_of_scrollview);
inflater.inflate(R.layout.id_of_layoutfile_to_include, sv);
EDIT:
After having read your comment i realise that i misunderstood your question.
ScrollView is not the view you want for binding your data, scrollView is a specialized frameLayout, and therefore only allows one child.
Your most likely looking for a view like ListView (Which adds scrolling automatically)
Another solution is to use a Layout in a scrollview and dynamically (From code) add the views.
LinearLayout ll = findViewById(R.id.id_of_linearlayout);
//Loop data and build a view for each data entry and add it with
ll.addView(yourView);

Inflator in Android Application Development

Can anybody please tell What Inflator is and how it is being used in an Android application?
I don't know the exact use of it and Why it is being used.
My preferred way to handle inflation:
//First get our inflater ready, you'll need the application/activity context for this
LayoutInflater mInflater;
mInflater = LayoutInflater.from(mContext);
//Inflate the view from xml
View newView = mInflater.inflate(R.layout.my_new_layout, null);
//Then you'll want to add it to an existing layout object
mMainLayout.add(newView);
//Or perhaps just set it as the main view (though this method can also
// inflate the XML for you if you give it the resource id directly)
setContentView(newView);
Basically, you use it to inflate existing xml layouts at runtime. Usually you go ahead and insert those new views into previously defined ViewGroups or List objects.
Not quite sure what you mean, but if its related with inflating views, its used to load layout xml files into your application. e.g by
View myWelcome = View.inflate(this, R.layout.welcome, null);
Its easier and consider best practice to have you view definition inside layout xml files, instead of creating your views fully by code.
layout inflator is used to return a java object of your complete layout
suppose you have a layout xml file in which the root element is relative layout and it contains a imageview and textview then using layout inflator you can return a view object that refers to entire layout.
this basically is used in list view and grid view to plug into them a layout object of single row or element which is to be repeated.
you were asking for use of Inflator..
basically when you want to use two xml files in one java class ,inflator is used and its code is simple which is given below..
TextView text;
View layout;
LayoutInflater inflator=getLayoutInflater();
layout =inflator.inflate(R.layout.new_xml_that you want to use in that java class,null);
text=(TextView)layout.findViewById(R.id.text);
text.setText("progressing");
here i use textview,this is present in next xml with id=text
thats it..
if you find this worthy then please like this..
thanks

Categories

Resources