How to inflate a activity in my case? - android

I would like to achieve this layout :
my main activity layout (main.xml);
(Please mouse right click the following image and view image)
I have made another ContentActivity (with content set to content_one.xml) which is supposed to be used as part (the right part) of the above layout:
I know I can inflate a layout by:
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
LayoutInflater inflater = (LayoutInflater)Home2.this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View inflatedView = (View) inflater.inflate(R.layout.content_one, null);
mainLayout.addView(inflatedView);
I am wondering, besides inflate the content_one layout into main layout, is it possible to inflate a activity class instead of inflating a layout in android? If possible, how to do it?

You cant inflate an Activity as such. Consider using Fragments.
http://developer.android.com/guide/topics/fundamentals/fragments.html

Related

How to handle dynamically inflated layout content (Same Layout inflated multiple times)

I have a Linear layout and inside which there are few dropdown and text views,now i want to inflate the layout each time on click of "add more" button, below is the code for inflating the layout. the problem i am facing is to assign the id to each inflated layout content .Inorder to handle the content the inlfated layout i need the unique id for each content for each time i am inflating the layout.
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row_view;
row_view = (View)inflater.inflate(R.layout.inflatefloor,null);
inflatefloordetails.addView(row_view);
here inflatefloor is the layout to inflate and inflatefloordetails is the main root layout inside which layout is to be inflated and i am inflating the inflatefloor layout each time on button click.
New to android pardon if anyone finds the question silly.
Note:Check out link below if anyone is facing similar problem, I found this link useful.
How to set Id of dynamic created layout?
I think it will be helpful.
you can put any object when set the tag to be unique:
yourView.setTag(Object object);
and to retrive it:
object.indexOf(yourView.getTag())
Otherwise you have to use custom view.

Unable to inflate a layout on button click

Hi I am trying to show a layout when a button is pressed but the layout is never shown. No exception is thrown for some reason. The button is located inside an activity. The button is called and declared and it works. The calling of the layout is just additional action to the button.
Here is my code to inflate the layout:
LayoutInflater layoutInflater = (LayoutInflater)getApplicationContext().getSystemService(LAYOUT_INFLATER_SERVICE);
View error = layoutInflater.inflate(R.layout.error_search_no_results,null);
Use this :
rv.addView(error);
Where rv is your parent layout.

Android - FrameLayout with a custom view requiring a constructor

What I am attempting to do is take a custom view (which is a constructor & onDraw method) and a set of controls in a RelativeLayout and display them together with-in a FrameLayout. My code currently gets to adding the overlay and crashes with a Null Pointer Execption but if I directly add the overlay and cut out the FrameLayout & custom view it works.
FrameLayout layout = new FrameLayout(this);
SampleView sv = new SampleView(this, objectA, objectB, ObjectC), RESULTS);
RelativeLayout overlay = (RelativeLayout) findViewById(R.layout.analysis_overlay);
layout.addView(sv);
layout.addView(overlay);
setContentView(layout);
I assume there is a better way to do this and I've seen examples of a FrameLayout existing in xml but I didn't see anyway of setting the constructor via that method... So is there a better way of doing this that still allows custom view to have a constructor or did I mess somthing up with my current code?
You are attempting to find an id of a layout resource. Instead, inflate your relative layout (the layout resource) and then add it.
FrameLayout layout = new FrameLayout(this);
SampleView sv = new SampleView(this, objectA, objectB, ObjectC), RESULTS);
RelativeLayout overlay = (RelativeLayout) LayoutInflater.from(this).inflate(R.layout.analysis_overlay, layout, false);
layout.addView(sv);
layout.addView(overlay);
setContentView(layout);
RelativeLayout overlay = (RelativeLayout) findViewById(R.layout.analysis_overlay);
You can never find anything with id from R.layout. Use R.id.
You can never find anything before setContentView() when using Activity findViewById().
Call findViewById() on a view hierarchy that actually contains the overlay you want to find.
If you just want to inflate an XML layout, use LayoutInflater.

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 add layout from xml to Extended FrameLayout Android?

I have a custom class called NoteView that extends FrameLayout. Previously, I had just been using the stock LinearLayout and have an XML layout built already that I want to reuse by adding that entire hierarchy (with some other overlay views which is why I needed framelayout) to the NoteView.
The problem is I can't figure out how to inflate the XML and add it to the NoteView from within the NoteView Class. I can add it after initialization is complete, but I want to be able to be able to inflate that hierarchy and add it automatically when my NoteView is either instantiated or inflated from XML.
How can I populate my extended FrameLayout with a layout from XML by adding it from within the NoteView class itself?
Try LayoutInflater
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout myView = (LinearLayout) inflater.inflate(R.layout.my_layout, null);
frameLayout.addChild(myView);
Have a look at the documentation for LayoutInflater here: http://d.android.com/reference/android/view/LayoutInflater.html

Categories

Resources