How to split the screen dynamically in android without xml - android

I have tried to split the screen using xml..here is the code
Please try this xml..I need the same layout programatically.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="318dp"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/holo_purple">
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="318dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal" android:background="#android:color/holo_purple">
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="318dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/holo_blue_dark">
</LinearLayout>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="318dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="horizontal"
android:background="#android:color/darker_gray">
</LinearLayout>
</LinearLayout>
</LinearLayout>
But, I need to do programatically to split the screen. Can anyone provide me the solution?

By splitting the screen you mean dividing layouts width then you can try as below..
// gets the screen width
float screenWidth = getWindowManager()
.getDefaultDisplay().getWidth();
You will get screen width from above code..
and for screen height
// gets the screen height
float screenHeight = getWindowManager()
.getDefaultDisplay().getHeight();
then all you have to do is set the layout width using LayoutParams.
LinearLayout view = (LinearLayout) findViewById(R.id.ur_layout_id);
view.setLayoutParams(new LinearLayout.LayoutParams((int) screenWidth / 3,
(int) screenHeight , 1f));
you can set layoutwidth to divide screen in half by screenWidth / 2, in 3 parts by screenWidth / 3 and so on..
Hope this helps..

Related

MaterialCalendarView not occupying full width android

I am placing a Material calendar view and i want the calendar height to occupy half of the screen and it was made by using layout weight but the calendar not occupying the full width.
one more problem is that I want to increase the text size of the date please refer me any idea
The XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
tools:context="symphony.acube.com.symphony.activity.patient.acitivity.mySchedule.MyScheduleActivity">
<include layout="#layout/toolbar_layout"></include>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/llCalendarContainer"
android:orientation="vertical"
android:layout_weight="1">
<com.prolificinteractive.materialcalendarview.MaterialCalendarView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/calendarView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:mcv_showOtherDates="all"
app:mcv_selectionColor="#color/toolbar_red"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
</LinearLayout>
</LinearLayout>
</LinearLayout>
The following is the actual window i am getting currently
In the above calendar should occupy full width and also the text size of the date must be some big
Thankyou in advance
To stretch the width of the calendar should:
The measureTileSize reduce by a factor of (in the method onMeasure class MaterialCalendarView)
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec ((int) (p.height * (measureTileSize / 1.5)), MeasureSpec.EXACTLY);
The same is done in the MonthView
int childHeightMeasureSpec = MeasureSpec.makeMeasureSpec ((int) (measureTileSize / 1.5), MeasureSpec.EXACTLY);
Something come up with the background (stretched into an oval)
For more follow this issue

Getting padding in fullscreen camera view (FrameLayout)

I have this layout:
`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:orientation="vertical"
android:padding="0dp"
android:background="#android:color/transparent"
android:id="#+id/parent_view"
>
<FrameLayout
android:id="#+id/camera_preview"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/button_capture"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="50dp"
android:text="Capture" />
</FrameLayout>
</LinearLayout>
`
I am getting black padding in camera view like this, on the right side, notice the vertical black space/strip on right:
Don't mind the capture button in middle of screen, I'll position it later. But the question I have is, how to remove the unwanted padding and make FramLayout full screen?
CameraSourcePreview doesn't fill whole screen height that bug fixed by
Comment or remove below lines from CameraSourcePreview.java
if (childHeight > layoutHeight) {
childHeight = layoutHeight;
childWidth = (int)(((float) layoutHeight / (float) height) * width);
}

two views with match_parent in linear_layout in scrollview

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);

full screen layout in Scroll View

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!

How can I set an Android text box to only take up 60% of the overall width in XML?

I want to set up a text box, but I want it on multiple devices and to only take up 70% of the room on the screen. Can I do this?
hi try this way in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:weightSum="10"
android:id="#+id/commonlayout" >
<TextView android:id="#+id/tvlogin"
android:layout_height="fill_parent"
android:text="My Text View in 60%"
android:layout_width="0dp"
android:layout_weight="6">
</TextView>
</LinearLayout>
use android:padding = "yourvaluespx";
PX is the value at yourvalues insert any number
Add a empty LinearLayout beside it, set both Layout Width to 0dp. Then set the weight of TextView (or EditText) and the LinearLayout to 0.7 and 0.3 respectively
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout android:layout_height="match_parent" android:id="#+id/linearLayout1" android:layout_weight="0.3" android:layout_width="0dp"></LinearLayout>
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_height="wrap_content" android:layout_weight="0.7" android:layout_width="0dp"></TextView>
</LinearLayout>
please try this::
Display display = getWindowManager().getDefaultDisplay();
int width = display.getWidth();
int height = display.getHeight();
int int_var = (width * 60)/100;
and put int_var as width

Categories

Resources