Suppose if I am setting a horizontal orientation linear layout then what should be the total pixel of match parent so that I could adjust my buttons and text view according to that size.
I tried to place all margins and calculate...
the expected output would depend on screen size.
total pixels for match_parent of a view is depend on your parentView type, dimensions and composition of views within the layout, for example if your parentView is LinearLayout of size 100px * 100px then your match_parent for a view within that LinearLayout is 100px * 100px (iff you have only one view within that LinearLayout otherwise it depends on your composition) you can get your view's height and width (in pixels) programmatically to by using below code to any view or Layout
Java
view.post(new Runnable(){
#Override
public void run() {
int height = view.getMeasuredHeight();
int width = view.getMeasuredWidth();
}
});
kotlin
view.post {
val width = view.measuredWidth
val height = view.measuredHeight
}
after getting height and width of layout you can manage you margin or size according to your logic
The total pixels of match parent is the total pixels of the screen size, and if you to implement an auto resize for your views you should either use constraint layoutas a parent layout for your views, either use this great library that uses a new size unit called sdp https://github.com/intuit/sdp
Don't user pixels, it makes your screen not responsive to all screen sizes. use ConstraintLayout or Relative Layout if you want your screen to be responsive to all screen sizes.
Here is an example of a view that his width is qeual to all of the screen size using ConstraintLayout :
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Related
In ConstraintLayout version < beta5, I had layouts like this example below:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/square_image"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintDimensionRatio="H, 1:1"/>
</android.support.constraint.ConstraintLayout>
However, versions starting beta5 have removed the MATCH_PARENT constraint for child views.
The documentation gives examples of using app:layout_constraintDimensionRatio:
You can also use ratio if both dimensions are set to MATCH_CONSTRAINT
(0dp). In this case the system sets the largest dimensions the
satisfies all constraints and maintains the aspect ratio specified. To
constrain one specific side based on the dimensions of another. You
can pre append W," or H, to constrain the width or height
respectively. For example, If one dimension is constrained by two
targets (e.g. width is 0dp and centered on parent) you can indicate
which side should be constrained, by adding the letter W (for
constraining the width) or H (for constraining the height) in front of
the ratio, separated by a comma:
<Button android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
https://developer.android.com/reference/android/support/constraint/ConstraintLayout.html#DimensionConstraints
This example works when the parent layout has a fixed height or match_parent, but not when the parent is set to wrap_content.
Using my code example above, if I set the ImageView width to 0dp, the parent view collapses as if it has no content.
This was an incredibly useful feature, I feel like I'm just missing something in this new version. Any help appreciated.
ConstraintLayout v1.0.2 fixes this issue.
I want to get my layout or view (not screen size) width an height in some android devices, so how can i do this? For example:
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:paddingRight="8dp"
android:paddingLeft="8dp"
android:id="#+id/layout_scrol_left"/>
I have this layout and i want to get layout width and height size (in dip) in runtime, how can i do that?
First of all you need to take your layout.
LinearLayout ll_left = (LinearLayout)findViewById(R.id.layout_scrol_left);
Now if ll_left is not yet drawn both ll_left.getHeight() and ll_left.getWidth() will return 0.
So you have to get them after your view is drawn:
ll_left.post(new Runnable(){
public void run(){
int height = ll_left.getHeight();
int weight = ll_left.getWidth();
}
});
i think you can use something like
LinearLayout myView =(LinearLayout)findViewById(R.id.layout_scrol_left);
and you can use the methodes below after the UI thread has been sized and laid
myview.getHeight();
myview.getWidth();
hope this helps .
if you problems using the method above take a look at getWidth() and getHeight() of View returns 0
I want to set the width of my listView to 40% of the mobile screen width. Since 40% will differ in terms of pixels in different mobiles I won't be able to set the width in the xml layout file. Is there any way to set the width programmatically for a listView
Actually you can usually do this in XML using layout_weight. For a description see the Linear Layout guide. A couple of things to keep in mind:
Make sure you set the size, (layout_width for a horizontal layout, layout_height for a vertical one) to 0dp so it doesn't interfere with the dynamic layout.
You would usually have weights on other views in the layout so they take up the remaining 60%. If not, though, you can define a weight sum.
Display display = getWindowManager().getDefaultDisplay();
int width = (display.getWidth()*40)/100;
You have to get screen width size through above code after getting width u have to set into your code through setparam.
You have an error of concept in terms of views and its params. The different kinds of widths you can set programatically you can set it too in your xml layout. The advantage of setting layout params to views programatically is just the possibility of changing the views layout params during runtime.
The solution to your problem is easy, you just have to put your listView inside a LinearLayout and set its weight to .4
You can get screen width programmatically and then get 40% of screen width and set to to ListView.And if you wanna do this using xml then use LinearLayout and set android:weightSum for this and then use weight.Using this way you can do this easily.
To get 40% of screen weight
Display display= ((WindowManager)getSystemService(Context.WINDOW_SERVICE)).getDefaultDisplay();
int width = (display.getWidth()*40)/100;
set weight for views in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff"
android:orientation="vertical"
android:weightSum="100" >
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:text="button1"/>
<Button android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:text="button2"/>
</LinearLayout>
Let's assume a device of 800x1280 pixels in portrait mode with the following layout:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/MyScrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:scrollbars="none"
android:fadingEdgeLength="0dip" >
<LinearLayout
android:id="#+id/MyLinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
</ScrollView>
and with android:configChanges="orientation" in the Activity manifest.
Dynamically I add two childs views to MyLinearLayout with Layout of width = MATCH_PARENT and height = getWindowManager().getDefaultDisplay().getHeight(). So, I can scroll vertically between the two childs, each occuping a entire screen.
Now, when the device gets in landscape, the child's width get the dimension of 1280, stretching from the original 800, because of the MATCH_PARENT attribute.
Vertically, they are not stretched, since they were created with a fixed height of 1280 originally, they keep with 1280 of height.
What's the proper way to stretch this views vertically?
If I try to child.setLayoutParams inside onConfigurationChanged to width = MATCH_PARENT and height = 2048 (which is 1280 * 1280/800), I get an exception.
I'm working with dynamically instantiated CanvasView (from the S-Pen SDK) childs. I was able to do what I wanted with the help of this answer.
I have inherented com.samsung.sdraw.CanvasView and overrided onMeasure to preserve aspect ratio the way I wanted, and I needed to call setEnableZoom(false) for each child to correct content stretching, it was stretching the contents in double in landscape mode, going out of the screen boundaries.
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());
}