i need a vertical ScrollView which contains 4 Buttons.
Each Button should be of the Size of the Phone.
Something like this:
<ScrollView
android:id="#+id/scroll_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="First Content."
android:textSize="50sp"/>
<Button
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:text="Second Content."
android:textSize="50dip"/>
</LinearLayout>
</ScrollView>
I tried a lot of options but dont get how to manage this.
All you have to do a dynamic layout
keep a layout in your xml
<LinearLayout
android:id="#+id/lnr_layout_container"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
and then in your activity/fragment java file
LinearLayout lnLayoutContainer=(LinearLayout)findViewById(R.id.lnr_layout_container);
Calculate height of your device
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
int height = displayMetrics.heightPixels;
Now Inflate and add Buttons to your layout container
for(int i=0;i<4;i++)
{
Button btnView= new Button(context);
<set your button properties and listener here>
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, height);
layoutParams.weight = 1;
btnView.setLayoutParams(layoutParams);
lnLayoutContainer.addView(txtViewChar);
}
Related
I have a simple linear layout and want to divide the screen into two equally sized parts.
In the upper one, I want to put a SurfaceView that has exactly the same size as its parent, i.e. half of the screen. Unfortunately, it always either takes all the screen or nothing (depending on the configuration).
Why is that and how can I change it?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="2"
android:orientation="vertical">
<LinearLayout android:id="#+id/layoutUpperDaw"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#192832">
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0pt"
android:layout_weight="1"
OR
android:layout_height="match_parent"
>
</com.example.MySurvfaceView>
</LinearLayout>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#193222">
...
</LinearLayout>
</LinearLayout>
When you provide weight you have to set height or weight to 0dp as below.
<LinearLayout android:id="#+id/layoutUpperDaw"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#192832">
and
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
Update: Answer above is correct about weight usage, make sure either hight or width is 0dp at all places where you used weight.
You don't really need nested LinearLayout. Following should work.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.example.MySurvfaceView
android:id="#+id/id_mySurfaceView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
>
</com.example.MySurvfaceView>
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#193222">
</LinearLayout>
</LinearLayout>
Programmatically:
Find the parent layout.
GridLayout gridLayout =(GridLayout)findViewById(R.id.gridLayout);`
Get measured height and Width of parent.
int height = gridLayout.measuredHeight();
int width = gridLayout.measuredWidth();
3.Calculate child view height and width.For example two child each taking 50% of height in the parent but taking full width of parent:
int childHeight = height/2;
int childWidth = width;
4.Get instanceof LayoutParams depending on parent type.
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
5. Set height and width on layout params.
params.width = childWidth;
params.height = childHeight;
6.Create child view and set Layout params.
ImageView imageview = new ImageView();
imageview.setLayoutParams(params);
7.Add child View to parent.
gridLayout.addView(ImageView);
dynamically increase and decrease the relative layout , i want to shrink the size of relative layout when recycler view popup from bottom of screen and after using it agian the screen will return into normal size(in recycler view im sung icons to put on relative view)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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">
android:layout_weight="1">
////////tool bar
<include layout="#layout/toolbar" />
<RelativeLayout
android:id="#+id/rel1"
android:layout_width="wrap_content"
android:layout_height="400dp"
android:layout_below="#+id/toolbar"
android:layout_weight="1">
<com.cmlibrary.CircleMenu
android:id="#+id/circle_menu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="180dp"
android:layout_marginTop="180dp"
/>
</RelativeLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="300dp"
android:layout_below="#+id/rel1"
android:padding="5dp" />
</RelativeLayout>
Better way is to show and hide the visibility of views as required. But if you want to increase and decrease the size then below code will help you.
private void layoutParams() {
RelativeLayout.LayoutParams layoutParams = (RelativeLayout.LayoutParams) relativeLayout.getLayoutParams();
ViewGroup.LayoutParams params = recyclerView.getLayoutParams();
/* When recyclerView shows*/
layoutParams.height = layoutParams.height - params.height;
relativeLayout.setLayoutParams(layoutParams);
/* When recyclerView hidden*/
layoutParams.height = RelativeLayout.LayoutParams.MATCH_PARENT;
relativeLayout.setLayoutParams(layoutParams);
}
Hope this will help.
Is it possible to have two views that are the same height of the activity screen inside of a linear_layout inside of a scrollview? I tried achieving this like so:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
<Button android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
</ScrollView>
The only thing that appears one button with the height of the screen. The other button seems to have disappeared.
I suppose it is paradoxical to have two views to with the attribute match_parent.
However, I was hoping to get two views the same size of the available screen using xml, and having the scrollview make both views accessible. I am aware that it can be done via java, but I want an answer for xml.
In Linear Layout you have to define two things...
1 .android:orientation="vertical"
2 .android:weightSum="2"
weightSum is sum of all element in it's parent layout.It Enable you to define the proportion of width/height a element should take in a layout..
For further details go to weightSum
<ScrollView 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:layout_below="#+id/textView1"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<Button
android:id="#+id/but1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
<Button
android:id="#+id/but2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:text="but 1" />
</LinearLayout>
</ScrollView>
output
You cannot achieve this in XML only.
As android support multiple screen sizes, At runtime you need to check for each device , 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 above code you will get the height of screen and you need to set this height in dp to your view at runtime.
Do this in your activity for both the buttons:
// get view you want to resize
Button but1= (Button) findViewById(R.id.but1);
LinearLayout.LayoutParams listLayoutParams = new LinearLayout.LayoutParams(
height , LinearLayout.LayoutParams.MATCH_PARENT);
but1.setLayoutParams(listLayoutParams);
I want to have 2 layouts inside ScrollView. First layout should be on the full screen and second layout below first one. When activity starts it is not possible see second layout. Second layout is possible see after scroll screen. Please take a look on my image for better understand.
Layout code: `
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="15dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/layout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<LinearLayout
android:id="#+id/layout2"
android:layout_width="match_parent"
android:layout_height="200dp"
android:orientation="vertical" >
<!-- this layout can be any height you want -->
</LinearLayout>
</LinearLayout>
</ScrollView>`
Get the device screen height:
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
int screenHeight = size.y;
Find the LinearLayout:
LinearLayout layout = (LinearLayout)findViewById(R.id.layout1);
Set the height of the layout based on the screen height you got earlier:
LayoutParams params = layout.getLayoutParams();
params.height = screenHeight - getStatusBarHeight();
Done. Be sure to call all of those methods in your onCreate method.
Hope this helps!
I'm trying to make android app which has a scrollable layout that scrolls different other layouts vertically and horizontally, I want each layout of theses different layouts to fits the screen width and nearly fits the Height, look at the following figure that illustrates what I'am trying to do:
I just tried to set all width and height of these layouts to match_parent and put all of them in a main_layout assigned with match_parent parameters also, then I create a Layout and put the Layouts(4,1,5) in it and put this Layout in a ScrollView , then i put the main_layout layout in a HorizontalScrollView that contains three layouts(Layout3, Layout(4,1,5), Layout2), but that gave me an interfered view of the Layouts.
<HorizontalScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.9" >
<!-- just to give it 0.9 of the screen in order to give a space to the top layout (it takes 0.1 and the weight sum is 1) -->
<LinearLayout
android:id="#+id/main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="horizontal" >
<LinearLayout
android:id="#+id/Layout3"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="none" >
<LinearLayout
android:id="#+id/Layouts(4,1,5)"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/Layout4"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/Layout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
<LinearLayout
android:id="#+id/Layout5"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</LinearLayout>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/Layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
</HorizontalScrollView>
i'd try that:
RelativeLayout instead of ScrollView
all LinearLayouts with match_parent, transformed programmatically:
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int sw = size.x; // screen width
int sh = size.y; // screen height
LinearLayout lay2 = (LinearLayout) findViewById(R.id.Layout2);
lay2.setTranslationX = sw;
LinearLayout lay3 = (LinearLayout) findViewById(R.id.Layout3);
lay2.setTranslationX = -sw;
LinearLayout lay4 = (LinearLayout) findViewById(R.id.Layout4);
lay2.setTranslationY = -sh;
LinearLayout lay5 = (LinearLayout) findViewById(R.id.Layout5);
lay2.setTranslationY = sh;
and then touch listener on RelativeLayout that would control swipes, changing it's position.
I found all i needed in that link , it provides a sample project source code that achieves the approach i needed by depending on Android fragmentation in a way easy and efficient way (but this way can provide horizontal navigation only).
And so, after a searching i found this library that provides the process in both directions(vertically & horizontally)