I am inflating interface from XML using
View add_phone = getLayoutInflater().inflate(R.layout.phone_info, null);
Now how can i access RelativeLayout from add_phone view? is there any methos like getChildCount() ?
yes , getChildCount(), works on a ViewGroup like LinearLayout, RelativeLayout etc..
ViewGroup add_phone = (ViewGroup) getLayoutInflater().inflate(R.layout.phone_info, null);
int childCount = add_phone.getChildCount();
you must make sure the inflated layout has viewGroup as parent view, otherwise you will get class cast exception. viewGroup can be anything like LinearLayout, RelativeLayout etc..
You can find child views of a view through
View.findViewById(int id)
In your case, that translates to
RelativeLayout child = (RelativeLayout)add_phone.findViewById(R.layout.phone_info)
As long as you have unique id's for the child elements in add_phone, this should return the correct element.
Related
I am trying to set the visibility of some views as gone after layout inflation but it doesn't work. If I try to access the tag of the view , I can clearly see that I am accessing the right view. This code doesn't result to any errors, so I am trying to understand why it's not working.
I am passing in as parameters the resource ids for the views (hideView) and the layout(layout):
public void hideViews(String title, ArrayList<Integer> hideView, int layout){
final LayoutInflater factory = getLayoutInflater();
final View originalView = factory.inflate(layout, null);
for (int i = 0; i < hideView.size(); i++) {
View view = originalView.findViewById(hideView.get(i));
if (title.equals("Admin") || title.equals("Manager")){
view.setVisibility(View.VISIBLE);
}else{
view.setVisibility(View.GONE);
}
}
}
originalView is not attached to your layout.
Either provide the parent view where you want to inflate this layout
Or add originalView as a child of other attached view after inflating.
Let's assume that this is your parent view
ViewGroup parentView = (ViewGroup)findViewById(R.id.parentView);
Solution 1
Replace this:
final View originalView = factory.inflate(layout, null);
With:
final View originalView = factory.inflate(layout, parentView);
Solution 2
Add this:
parentView.add(originalView)
I have this requirement where I have a fragment Instance and I want to dynamically add a view on the top of the fragment irrespective of the root viewGroup of the Fragment.
Something like this
:
So is it possible to get the root view of the fragment from its instance? I know for activity I can do this: activity.findViewById(android.R.id.content);
Also since a fragment can have any type of view group as its rootview like LinearLayout, RecyclerView, RelativeLayout, FrameLayout, Is there a generic way to add an overlay view on the top of the fragment?
If it is not possible then should I just add a dummy view on top of the fragment in xml layout and then use that view to add the overlay?
The getView() method returns a View that contains the Fragment's root layout. From there you could either cast it to ViewGroup and leave it at that to add a simple view, use an if-else block with instanceof to determine its type:
First:
ViewGroup viewGroup = (ViewGroup) fragment.getView();
Second:
View view = fragment.getView();
if (view instanceof FrameLayout) {
FrameLayout root = (FrameLayout) view;
} else if (view instanceof RelativeLayout) {
}
//etc...
Third:
Edit: Removed this option. It actually wont work.
You could use a Child Fragment but if you are only interested in the Fragments root view, just retain the View you are returning in the fragments onCreateView() method
At some point in my app in need to get views (actually, it should be one) from a layout.
What I know about the layout is it's int reference, in the form of R.layout.my_layout.
How can I get the views inside?
I tried getting the root view with
View rootView = (View) getResources().getLayout(R.layout.my_layout)
but obviously it didn't work. getLayout() returns a XMLResourceParser object, which I don't know how to manage.
I can't work with IDs.
Use layout inflater
View rootView = View.inflate(context, R.layout.my_layout, null);
To load the view:
LayoutInflater inflater = LayoutInflater.from(getContext());
View v = inflater.inflate(R.layout.your_layout, null);
And to get sub views from it:
View sub = v.findViewById(R.id.your_sub_view_id);
Use this:
ViewGroup rootView = (ViewGroup) getLayoutInflater().inflate(R.layout.my_layout,null);
and get access to the childviews with
rootView.getChildAt(x);
or total amount of childs:
rootView.getChildCount();
If you have the layout resource ID, you need to inflate the layout and get the Views from that:
View rootView = getLayoutInflater().inflate(layoutId, null);
Once you get the view, you just need to use -
view.findViewById(R.id.childID)
where,
childID is the child id of the parent view's child.
I am adding a layout using addContentView().
How can i remove this layout on a Button click ?
Assuming contentView is the view that was added via window.addContentView()
((ViewGroup) contentView.getParent()).removeView(contentView);
try it
View youAddedView;
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
for (int i = 0; i < rootView.getChildCount(); i++) {
if(rootView.getChildAt(i) == yourAddedView) {
// do anything here
}
}
If you already have the reference to the view you can simply just do :
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
rootView.removeView(viewToRemove);
Instead of looping through the ViewGroup.
Unfortunately there's no way of removing a content view that was added with addContentView(). The best you can you do is to call setVisibility(View.GONE) on it, to hide it.
That is why the activity's onContentChanged() only gets called when the content view is set or added to an activity.
you can do two things over here you can set the visibility to gone on the click event of the button.
OR
you can set the layout parameter to layout width and height to 0dp
It will hide your layout display
I want to inflate a childView of ExpandableChildView component.
Code:
public View getChildView(int groupPosition, int childPosition, boolean isLastChild, View convertView, ViewGroup parent) {
View v = convertView;
LinearLayout linearOpt = themesOptions.get(childPosition);
if(v == null) {
v = mInflater.inflate(R.layout.itemrow, null);
}
LinearLayout ll = (LinearLayout) v.findViewById(R.id.root);
ll.addView(linearOpt);
return v;
}
Where linearOpt is a vector that contains a lot of LinearLayout objects that I have instantiated.
private void prepareThemes(){
View v = mInflater.inflate(R.layout.widget_configure, null);
LinearLayout theme = (LinearLayout) v.findViewById(R.id.themeLayout);
LinearLayout color = (LinearLayout) v.findViewById(R.id.colorLayout);
LinearLayout trans = (LinearLayout) v.findViewById(R.id.transpLayout);
themesOptions.add(theme);
themesOptions.add(color);
themesOptions.add(trans);
}
This is R.layout.itemrow xml:
But I received this error:
07-18 10:48:49.740:
ERROR/AndroidRuntime(2738):
java.lang.IllegalStateException: The
specified child already has a parent.
You must call removeView() on the
child's parent first.
How I can resolve this issue?
I think the problem is caused by the manner you prepare the layouts in prepareThemes().
You didn't mention, but I assume your 'layout/widget_configure.xml' defines a structure like this:?
<LinearLayout android:id="#+id/root">
<LinearLayout android:id="#+id/themeLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="#+id/colorLayout> <!-- ... --> </LinearLayout>
<LinearLayout android:id="#+id/transpLayout> <!-- ... --> </LinearLayout>
</LinearLayout>
Then you inflate this in prepareThemes() to get the 3 sub layouts. But at this moment they are already registered as children of the surrounding layout (which I called "root"). As the error implies a view instance can be added to just one parent.
You could store each LinearLayout in its own xml file and then inflate 3 times. Those stored layouts then could be add exactly once to a child.
I'm not sure what you want to do, but I would guess, it would be better - instead of preparing - to just have 3 different layouts that include the part from layout.itemrow and then just make a switch case in getChildView() and inflate the needed layout on the fly. Because even when you store a not bind layout in prepareThemes: As soon as you have more then one group you would get the same error when adding a prestored layout to the child of the snd group.
I hope this helps.