Android: Changing the height of root view of a layout programmatically - android

I'm new to Android. Can anyone tell me how to change the height of RootView of a layout programmatically in Android? The code I've tried is given below
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.sample, null);
view.getRootView().getLayoutParams().height = value;
After executing this I'm getting a NullPointerException on the above line.
Can anyone help me? Thanks in advance.

The View returned by the inflate call is the root View, therefore this should work:
View view = inflater.inflate(R.layout.sample, null);
view.setLayoutParams(new ListView.LayoutParams(LayoutParams.MATCH_PARENT,<height>));
If you were to provide a ViewGroup to inflate into like this:
View view = inflater.inflate(R.layout.sample, containerForTheInflatedView);
Then view would be the ViewGroup, not the root View that you want.

Related

How to get child views programmatically from a layout [from a R.layout resource]

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.

How to add dynamic layout in ViewGroup in android?

I created FrameLayout dynamically in my activity. Like this
FrameLayout path_zoom;
path_zoom = new FrameLayout(this);
path_zoom.setLayoutParams(new FrameLayout.LayoutParams(1500,1119));
Now i want to add this dynamic layout in ViewGroup for layout zooming. I need to add my layout like this.
View v = ((LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.ground_zoom, null, false);
Instead of null i want to pass ViewGroup object. How can i do this? Can any body tell me? Thanks in advance.
Try this
ViewGroup parentView = (ViewGroup)SelectorView.your_context.getParent();
LayoutInflater layoutInflater = (LayoutInflater)activityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View layout = layoutInflater.inflate(R.layout.ground_zoom , parentView);

Layout params of inflated view are ignored

I inflated a view and added it to my layout :
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View vi = inflater.inflate(R.layout.to_add,null,false);
DyL.addView(vi);
and I want to change the view parameter dynamically (height) but getLayoutParams doesn't work, I always have a null result .So how can I do this? I tried all the solutions I found on the net with no success.
Thank you.
try to set the parent view,
View vi = inflater.inflate(R.layout.to_add, DyL, false);
where DyL is you parent view
Use your parent view in argument when doesn't require parent.add(vi)
View vi = inflater.inflate(R.layout.to_add, parent , false);

How to inflate View on my layout?

How to inflate View on layout, where all my ui elements which are on layout should be visible.
Am trying this lines but it is not working am not able to see my background image, button and other images.
MyView view1=new MyView(this);
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view1= (MyView) inflater.inflate(R.layout.main, null);
Please help.. Thanks in advance.
For what you are doing, use
setContentView(R.layout.main);
The reason for not getting your view after inflating is that you need to add that View in the current View somewhere. Currently your inflated view is orphaned.
this usually works...
LayoutInflater inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view1= inflater.inflate(R.layout.main, null);
or
LayoutInflater inflater = LayoutInflater.from(getApplicationContext);
View view1= inflater.inflate(R.layout.main, null);
and then add this view to the contentView.
Updated:
Assuming you have need to draw a line in your main.xml, let me give you this suggestion. instead of drawing line at runtime by extending View class, create a View in xml like this
<View android:layout_width = "fill_parent"
android:layout_height = "2dp" //assuming you want line of 2dp thickness
android:background= "#android:color/black"/> // define your desired color here
hope it helps..
This worked finally.
getWindow().addContentView(view1,
new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));

Cast XML layout as view in android before setContentView

I want to cast multiple XML layouts as views. Because of a change request I need to use a ViewSwitcher. So I have to cast the XML layout to Views before setContentView.
I always get a NullPointerException. I tried:
view = (View) findViewById(R.id.viewgroup_id);
view = (RelativeLayout) findViewById(R.id.viewgroup_id);
view = (View) findViewById(R.layout.xml_layout);
view = (View) getResources().getLayout(R.layout.xml_layout);
Try using LayoutInflater. Use this code inside your activity code-
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.your_layout, null);
You need to inflate layouts for that
Check this link

Categories

Resources