in Android how to make a view have same height as its screen size, is it possible to achieve this with only the xml? or if it must use script, tell me how
Thanks.
Sorry for being not clear, and thanks for your reply
but i think, match_parent and fill_parent attribute is not reliable, because when i put the view inside one container or change the view container hierarchy, it won't work.
Here my complete xml layout.
The element i want to make the height sam with device screen is the last list view inside relative layout
No you cannot achieve this in XML only.
As Android supports multiple screen sizes, at runtime you need to check for each device size. The height for each device can be calculated like this:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int height = size.y;
With the above code, you will get the height of the screen and you need to set this height in dp to your view at runtime.
Do this in your activity:
// get view you want to resize
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.main);
// get layout parameters for that view
ViewGroup.LayoutParams params = mainLayout.getLayoutParams();
// change height of the params e.g. 480dp
params.height = 480;
// initialize new parameters for my element
mainLayout.setLayoutParams(new LinearLayout.LayoutParams(params));
This can be possible from xml layout. To do this make the parent layout height and width fill_parent or match_parent and then set each child view width fill_parent or match_parent. Don't set any padding or margin to parent layout. Hope it will work. Here I am giving you an example:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/btn_swap"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Left or Right" />
<SurfaceView
android:id="#+id/my_surface"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
Attention: If you use ScrollView, you have to set fillViewport="true" otherwise it will not work. A Google engineer said about it before. Check it from here
Display screenDisplay = getActivity().getWindowManager().getDefaultDisplay();
LayoutHeight = screenDisplay.getHeight();
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
LayoutHeight, LinearLayout.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(listLayoutParams);
Related
I have a text view with a height which wraps content and a width that matches parent. So that the textView width == the screen Width.
But at one point I want the text to rotate 90 degrees.
Now I want to be able to change the views width so that it is the devices height instead of width.
This would cause that the width would expand.
(Basically like when one does orientation changes, but I can´t just have an orientation change for only one textview so I have to rotate it.)
My problem: I can´t set the textViews width larger than the device width.
Even when I am already done with the animation.
So is it possible to make the textView width larger than the device width?
and if not can anyone please suggest how I could solve my problem because I really need to change the orientation of the textView...
EDIT----EDIT
Is there a way to create a landscape text view and put it in a portait activity? That would completly solve my problems of the last week...
EDIT
My fragment:
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/f"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/t"
/>
</RelativeLayout>
I tried different things like
fragment.setRotation(90);
LayoutParams params = fragment.getLayoutParams();
params.height = text.getHeight();
params.width = deviceHeight;
fragment.setLayoutParams(params);
LayoutParams params1 = text.getLayoutParams();
params1.height = text.getHeight();
params1.width = deviceHeight;
text.setLayoutParams(params1);
EDIT________EDIT
Or has anyone ever written something like a verticalTExtView Class?
You can set the attributes of TextView as android:singleLine="true" and android:layout_width="wrap_content" and android:layout_height="wrap_content" // incase if you do not want the height to be increashed so singleLine is making the text to go out of screen width then you will need ScrollView for such behavior.
Hope this helps.
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"
I have a list of photos, each having some text in it. I would like each photo to horizontally fill the screen and would like to height to be the same as of the width
Trying to accomplish something like
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="self.width"
...
How could I accomplish this ?
Since I will be using a recycleview I can edit the height at runtime.
XML is not dynamic. Screen measurements have to be done in Java during onCreate.
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point(); display.getSize(size);
int width=size.x; int height=size.y;
LinearLayout.LayoutParams params= new LinearLayout.LayoutParams(width,width);
yourView.setLayoutParams(params);
find width of your layout programmatically, and set that value as height of your layout.
//get Width
int width=layout.getWidth();
//set height
layout.getLayoutParams().height=width;
should be like this,if you want height for full screen use match_parent also
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
I'm using a SlidingPaneLayout in my activity:
<android.support.v4.widget.SlidingPaneLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/myslidingpanelayout"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<!-- menu left -->
<LinearLayout
android:id="#+id/menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#8d305f"
android:orientation="vertical" >
...
</LineareLayout>
<!-- main page right-->
<LinearLayout
android:id="#+id/right_main"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical" >
...
</LineareLayout>
</android.support.v4.widget.SlidingPaneLayout>
I want the menu to cover 3/4 of the page I want it to work on all the phones so I can't put for example
android:layout_width="300dp"
I want to calculate the screen width and set it to the left pane
Thank for your help
Thanks for you all I found this answer and it works with me:
int width;
int height;
if (android.os.Build.VERSION.SDK_INT >= 13){
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
width = size.x;
height = size.y;
}else {
Display display = getWindowManager().getDefaultDisplay();
width = display.getWidth(); // deprecated
height = display.getHeight(); // deprecated
}
if(width>0&&height>0){
LinearLayout layout = (LinearLayout)findViewById(R.id.menu);
// Gets the layout params that will allow you to resize the layout
LayoutParams params = layout.getLayoutParams();
// Changes the height and width to the specified *pixels*
params.height = height;
params.width = width*3/4;
}
Just looking up the doc for sliding pane, looks like it functions like a linear layout, and can use the
layout_weight
parameter to set a percentage based width since the parent viewgroup is match_parent
In the case of 3/4 = 75% you can
android:layout_weight="0.75"
From the android docs http://developer.android.com/reference/android/support/v4/widget/SlidingPaneLayout.html:
Like LinearLayout, SlidingPaneLayout supports the use of the layout parameter layout_weight on child views to determine how to divide leftover space after measurement is complete. It is only relevant for width. When views do not overlap weight behaves as it does in a LinearLayout.
When views do overlap, weight on a slideable pane indicates that the pane should be sized to fill all available space in the closed state. Weight on a pane that becomes covered indicates that the pane should be sized to fill all available space except a small minimum strip that the user may use to grab the slideable view and pull it back over into a closed state.
And from the LinearLayout docs http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
Note: You will end up setting the layout_width parameter to 0dp since the view group will actually use the weight to lay the children out
Apart from Selecsosi's answer, which is correct, there is also this view I wrote to always display the second item as a pane (ignoring the default show-side-by-side-if-the-fit behaviour). It can, as the name shows, wrap around the sliding view.
You can implement the behaviour you're after by either using a lot of #dimen resources and switching them based on swXXXdp-(port|land) or just setting the sliding view's width at runtime (something I'm reasonably certain you can do with the default layout as well).
Is it possible to create a view that is bigger than the screen?
I need a view that has a bigger width then the screen of the device. I use this view in a rotation animation. During the rotation the parts that were not on the screen before animating the view will become visible.
Is there a way to achieve this effect with the android framework?
Update
I tried to set my parent layout much bigger then the screen and it is working. This will make somethings a little bit uncomfortable but it could work. The next problem now is that my layout still starts at the left side of the screen. I can't think of a method to make the layout to expand itself to the left and the right of the screen.
Ok I got an answer. It is not very nice because it uses a deprecated View class but it works at least on my current testing screen resolution other resolutions are tested tomorrow.
I wrapped the view that I wanted to expand beyond the screen in an absolute layout like this:
<AbsoluteLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true">
<ImageView
android:id="#+id/content"
android:layout_width="600dip"
android:layout_height="420dip"
android:scaleType="centerCrop"
android:layout_x="-200dip"
android:layout_y="60dip"
android:src="#color/testcolor" />
</AbsoluteLayout>
The -200 x coordinate makes the view stick 200dip out of the left side of the screen. If I'm animating the view those parts that are outside the screen will gradually become visible.
E.g. setting negative bottom margin together with setting extra large layout_height (large enough for you) solved the similar issue as for me.
Works fine at least using API 11+ animations/rotations.
Could look like:
android:layout_marginBottom="-1000dp"
android:layout_height="1000dp"
In case anyone still comes up on this page. The key is your root layout, it will only work with a FrameLayout (or the deprecated absolutelayout). Then you have two options to make your child view bigger.
through xml, this is quick and easy but you don't know the actual screen width & height in advance so your off with setting a ridiculously high value for layout_width & layout_height to cover all screens.
Calculate the screen size programatically and make the view's width/height proportional bigger to this..
Also be aware that your bigger view still starts in the top left corner of the screen so to account this you will have to give a negative top & left margin that's half of what you are adding to the view's width/height
FrameLayout.LayoutParams params = (FrameLayout.LayoutParams) viewToMakeBigger.getLayoutParams();
int marginLeft = (int) (viewToMakeBigger.getWidth()*0.1);
int marginTop = (int) (viewToMakeBigger.getHeight()*0.1);
params.width = (int) (viewToMakeBigger.getWidth()*1.2);
params.height = (int) (viewToMakeBigger.getHeight()*1.2);
params.leftMargin = -marginLeft;
params.topMargin = -marginTop;
viewToMakeBigger.setLayoutParams(params);
HorizontalScrollView:
http://developer.android.com/reference/android/widget/HorizontalScrollView.html
Layout container for a view hierarchy that can be scrolled by the user, allowing it to be larger than the physical display.
The simple axml below creates an ImageView that is 400dp wider than the screen (even though the layout_width is set to equal the parent's width) using a negative left and right margin of 200dp.
The ImageView is situated 250dp above the top of the screen using a negative top margin, with 450dp of 700dp vertical pixels visible on the screen.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:background="#FFFF0000"
android:layout_height="700dp"
android:layout_marginLeft="-200dp"
android:layout_marginRight="-200dp"
android:layout_marginTop="-250dp"
android:layout_width="match_parent" />
</RelativeLayout>
You can override the views in the onMeasure method. This will set your View dimensions to 1000x1000 px.
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
setMeasuredDimension(1000, 1000);
}
Is it possible to create a view that is bigger than the screen?
Why not, you can define the layout_width and layout_height in px(or dip) as you want:
android:layout_width="10000px"
android:layout_height="20000px"
You need to change the size of the window, by getWindow().setLayout. This will increase the size for your window. Since the root layout can be as big as its parent you can then increase the size of the view you want to be bigger than the screen size. It works for me let me know
You can use ViewSwitcher to handle that. Used with Animation and a OnGestureListener looks pretty good.
You can do it programmatically:
FrameLayout.LayoutParams rootViewParams = (FrameLayout.LayoutParams) rootView.getLayoutParams();
rootViewParams.height=displayMetrics.heightPixels+(int)dpToPixels(60);
rootViewParams.width=displayMetrics.widthPixels+(int)dpToPixels(60);
rootView.setLayoutParams(rootViewParams);
rootView.setX(rootView.getX() - dpToPixels(30));
rootView.setY(rootView.getY() - dpToPixels(30));
MUST BE ONLY IN
"public void onWindowFocusChanged(boolean hasFocus)" method.
and
rootView = (RelativeLayout) findViewById(R.id.rootLayout);
Inside "protected void onCreate(Bundle savedInstanceState)" method.
Where yout .xml file is like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/rootLayout"
tools:context="com.example.Activity">
<RelativeLayout
android:layout_width="match_parent"
android:layout_centerHorizontal="true"
android:layout_margin="30dp"
android:layout_centerVertical="true"
android:layout_height="match_parent">
// Bla bla bla
</RelativeLayout>
and:
public float dpToPixels(float dp) {
return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dp, context.getResources().getDisplayMetrics());
}