Getting root ViewGroup in PreferenceActivity - android

I'm trying to implement a custom dialog in a PreferenceActivity that still has the Android-generic positive/neutral/negative buttons in the dialog, i.e. a custom AlertDialog. The way to go seems to be described in the bottom example of this article: Dialogs on Android developer pages .
However, I can't find a way to get the root for a PreferenceActivity for doing this:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.??????));
Does anyone know how to do this?

Try this to get rootview of any view
View layout = inflater.inflate(R.layout.custom_dialog,null);
View view = layout.getRootView();

Related

How to get root ViewGroup from a layout that was set using setContentView?

I cannot seem to find a way to get this ViewGroup. Not sure if there are any standard ways to get ViewGroups.
What I want to accomplish is to dynamically add a full screen overlay view to any Layout at run time, and in order to do this, I always need to get the root ViewGroup. Is this the right way to do this?
Try using
ViewGroup view = (ViewGroup) findViewById(android.R.id.content);
Every layout has this ViewGroup as parent.
getWindow().getDecorView().getRootView();
set this view directly
Works for me
getWindow().getDecorView().findViewById(android.R.id.content);

ActionBar custom layout like ICS edit contact

I just started playing with the new support library with ActionBar support. I'm trying to implement a bar that looks basically identical to the Ice Cream Sandwich layout on the edit contact screen. I understand I probably need to implement a custom view something similar to this - How to display custom view in ActionBar?. What I don't understand is exactly what that view is, and the best way to implement it.
Here's the screenshot of what I want in my actionbar:
Is that just a view with an image and some text, or a styled button, or something totally different? It has some state pressed properties.
Thanks for the help.
You can try using a custom view, by adding the following code when you want the button to appear :
// Inflate the view from XML file
LayoutInflater inflator = (LayoutInflater) this .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflator.inflate(R.layout.search, null);
// Tell the view to occupy the whole actionBar view (MATCH_PARENT)
LayoutParams layout = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
getActionBar().setCustomView(v, layout);

Creating Custom AlertDialog ? What is the root view?

what i am trying to do:
Create a custom Alert Dialog. Buttons just like any Alert Dialog but above are two TextEdit input boxes. I don't want to create a custom Dialog but a customized Alert Dialog
Here is what I am trying #3:
http://developer.android.com/guide/topics/ui/dialogs.html
It says:
AlertDialog.Builder builder;
AlertDialog alertDialog;
Context mContext = getApplicationContext();
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(LAYOUT_INFLATER_SERVICE);
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
TextView text = (TextView) layout.findViewById(R.id.text);
text.setText("Hello, this is a custom dialog!");
builder = new AlertDialog.Builder(mContext);
builder.setView(layout);
alertDialog = builder.create();
Documentation says:
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
where the first parameter is the layout resource ID and the second is the ID of the root View.
Problem is I don't know what the layout root is? this is a dialog I am going to kick of in an Activity. Should I use the layout id if the activity? Is layout_root pulled out of a hat?
Also tried:
View layout = inflater.inflate(R.layout.my_custom_layout,
(ViewGroup) findViewById(android.R.id.content).getRootView());
result null pointer.
Even though an older question, this article might be useful for others who search for this answer:
Layout Inflation as Intended:
If you’ve ever written something like the following code using
LayoutInflater in your Android application:
inflater.inflate(R.layout.my_layout, null);
PLEASE read on, because you’re doing it wrong and I want to explain to
you why.
...BUT...
Every Rule Has An Exception
There are of course instances where you can truly justify a null
parent during inflation, but they are few. One such instance occurs
when you are inflating a custom layout to be attached to an
AlertDialog. Consider the following example where we want to use our
same XML layout but set it as the dialog view:
AlertDialog.Builder builder = new AlertDialog.Builder(context);
View content = LayoutInflater.from(context).inflate(R.layout.item_row, null);
builder.setTitle("My Dialog");
builder.setView(content);
builder.setPositiveButton("OK", null);
builder.show();
The issue here is that AlertDialog.Builder supports a custom view, but
does not provide an implementation of setView() that takes a layout
resource; so you must inflate the XML manually. However, because the
result will go into the dialog, which does not expose its root view
(in fact, it doesn’t exist yet), we do not have access to the eventual
parent of the layout, so we cannot use it for inflation. It turns out,
this is irrelevant, because AlertDialog will erase any LayoutParams on
the layout anyway and replace them with match_parent.
The article has an explanation on why you should supply a parent ViewGroup in most other cases than Dialog building.
Ok. The root view in the documentation refers to the element in the custom layout. So the custom layout will have an outermost view called the root view. You need to give this an Id and than you can pass it in as shown. So first argument is the custom view id, the second argument is id of the root layout element within the custom view.
View layout = inflater.inflate(R.layout.custom_dialog,
(ViewGroup) findViewById(R.id.layout_root));
So in this example given in the documentation above, R.id.layout_root refers to the id you give to say for example the outermost LinearLayout within the custom_dialog layout.
Have you tried this?
View layout = inflater.inflate(R.layout.custom_dialog,null);
builder.setView(layout);
layout.getRootView();
Should give LinearLayout.

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)

add layout as a view in Android

I have a pretty complex layout defined in xml file , now I want to add this layout as a view using addView or something else.
As layout is pretty much complex , its pretty cumbersome process to write the code for layout and then add it as a view. So is there anyway to load layout resource and add it as a view.
I want to add the layout into WebView
Use
LayoutInflater factory = LayoutInflater.from(this);
View myView = factory.inflate(R.layout.my_layout_id, null);
then use addView(myView)
You can also reduce this to one line of code;
View view = View.inflate(getActivity, R.layout.my_layout,null);
then add to your view.
If your view is a ViewGroup (layout)
You can use InflaterService.inflate(id, ViewGroup) and set the ViewGroup, it will set the current child(s) with the content of your xml.

Categories

Resources