Framelayout different heights - android

I have a Activity conainting a header with some buttons(See picture).
You can switch the framelayout by clicking on the buttons. I have the framelayout set on wrap_content in the height for making the whole thing scrollable in a scrollview.
But I want to show only a map in the framelayout if you push on button2. And the height of the map need to be FILL_PARENT INSTEAD of WRAP_CONTENT. So I need to change the heigth of the FRAMELAYOUT.
Any ideas how I can do that?
This is what I tried:
ViewGroup.LayoutParams layoutParams = frameLayout.getLayoutParams();
layoutParams.height = ViewGroup.LayoutParams.MATCH_PARENT;//MATCH_PARENT because FILL_PARENT is deprectated
frameLayout.setLayoutParams(layoutParams);

Related

First child of LinearLayout inside ScrollView with 1/3 space of device height

I have a ScrollView with a LinearLayout with 3 elements inside. I would like that the first element has a height of 1/3 the height of the device height and the other 2 with wrap_content, is this possible to do in xml or how would you do this? Using weight alone it does not seem possible because the LinearLayout inside the ScrollView could be longer than the device's height.
According to Android docs, android:layout_weight:
Indicates how much of the extra space in the LinearLayout is allocated to the view associated with these LayoutParams. Specify 0 if the view should not be stretched. Otherwise the extra pixels will be pro-rated among all views whose weight is greater than 0.
This implies that the weight is not dependent on the screen height, but rather only on its parent view, so you can't achieve that from xml. To achieve this I would compute the height of the screen and then resize the view:
Display screen = getWindowManager().getDefaultDisplay();
Point size = new Point();
screen.getSize(size);
int screenHeight = size.y;
myView.setLayoutParams(new LayoutParams(myView.getWidth(), screenHeight / 3));
You can give the first linear layout weight=0.333 with height=0 and the other two layout wrap_content

How to change width and height of existing layout params

Please consider ImageView inside RelativeLayout it has some specific layout params for RelativeLayout. I need to change the width and height of my ImageView programatically according to the screen size.
How can I save all layout parameters but only change width and height
Following code creates empty layout params but I need to save my old params and change only height and width.
imageViewArticleImage.setLayoutParams(new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, mScreenSize.y / 3));
How can I achieve this ?
Use getLayoutParams to get the current layout params. Change the width and height and go from there. IF the view is already visible, call requestLayout to force a new layout pass.

HorizontalScrollView with four RelativeLayout, each of which occupies the entire screen

I am having a problem creating a HorizontalScrollView on Android that will serve as a tutorial.
This ScrollView contains a LinearLayout with horizontal orientation, and inside there are 4 RelativeLayout, each of which must fill the screen.
But if I set layout_width = "match_parent" on each RelativeLayout, this does not work at all, but it's like it was set to "wrap_content"
The layout_width of ScrollView is set as "wrap_content" and on LinearLayout is set as "0dp", but changing this I did not see any changes.
How can I solve the problem? Thanks
It seems your RelativeLayout width is set to match_parent of the parent LinearLayout which is0dp.
Try giving your LinearLayout some width
By the way why do you have to use HorizontalScrollView, Use ViewPager instead.More about viewpager here
Example
in your parent_layout.xml
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
You can find more about the code at GitHub
int size = horizontalScrollView.getChildCount();
int screenW =getResources().getDisplayMetrics().widthPixels;
for(int i = 0 ;i <size ;i++){
View v = horizontalScrollView.getChildAt(i);
ViewGroup.LayoutParams lp = v.getLayoutParams();
lp.height = ViewGroup.LayoutParams.MATCH_PARENT;
lp.width = screenW;
v.setLayoutParams(lp);
}
when you use the scroll view you should use the width of Relative layout in fix dp like 300dp or 200 dp otherwise scroll view take its width as it require like wrap_content
so use like this layout_width = "200dp"

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width

In framelayout, if I want to put a view in the position that starts from 1/2 of the screen width. How can I do it? Thanks.
You can accomplish that in multiple ways. It only depends if you really need to stick with the FrameLayout.
Two easiest would be those:
FrameLayout - you would need to know whats the width of the layout (so if your layout is wrap_content it might get tricky). Set your view's gravity to left, and set its margin_left to half of the layout's width.
LinearLayout - either put it inside your FrameLayout (not very good practice), or (if you can) swap it with your FrameLayout. Set it's orientation to horizontal, add dummy view with width = 0dp, weight = 1. Then add your view with the same values for width and weight.

Layout will not center

I have one linearLayout inside another Linearlayout that fills most of the screen. When I set the gravity of the child layout to center, it only centers vertically, but does not center horizontally.
Midlayout is the child layout inside of top layout.
LayoutParams params=new LayoutParams(height,height);
params.gravity=Gravity.CENTER;
LayoutParams params2=new LayoutParams(MainActivity.screenWidth,MainActivity.screenHeight-height);
params2.gravity=Gravity.CENTER;
((LinearLayout)findViewById(R.id.arrange_midlayout)).setLayoutParams(params);
((LinearLayout)findViewById(R.id.arrange_toplayout)).setLayoutParams(params2);
LinearLayout only respects gravity on the opposite axis to it's orientation, e.g. if it's orientation is horizontal then it will only set the vertical gravity. You should probably use FrameLayout instead for the outer container.

Categories

Resources