Android: Programmatically changing XML layout to "Lefty Flip" - android

I'm trying to allow users of my app to toggle the UI to be either standard orientation or "lefty flip". I'm unsure how low-level of a solution I need to program, considering Android might provide an abstracted way to easily do this.
Visually, the standard orientation would be:
and the lefty flip orientation would be:
So, currently what I'm thinking of attempting is a manual rearrangment of the xml layout, component by component, within a menu button's onTouch() logic.
I get the feeling that there may be a simpler way than this. Any suggestions? Is a series of programmatic calls to rearrange the view the best way? Xml file below.
<?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="horizontal"
android:baselineAligned="false"
android:keepScreenOn="true">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.40"
android:orientation="vertical"
android:id="#+id/toolbarGestureOverlay" >
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_weight="3.60"
android:id="#+id/openglsurface">
</LinearLayout>
</LinearLayout>

I think you can achieve it by using rotate function (from API 11 only):
View view = findViewById(R.id.yourParentLayout);
view.setRotation(270);
// you must canculate your screen size to make your view fit your screen.
view.getLayoutParams().width = 800;
view.getLayoutParams().height = 480;
// For flip, you can rotate a long x or y axis:
view.setRotationY(180);

Related

How to resize View with pull the edge gesture?

I am looking for solution as on images bellow:
I need to have two resizable views in one layout.
User just needs to move separation line to the top (ScrolView B becames higher) or to the bottom (ScrolView A becames higher).
What is the best solution, which gives this behavior? I know that I can extends from ScrollView and override public boolean onTouchEvent(MotionEvent ev) and protected void onDraw(Canvas canvas), but may be there is more simple solution. I want to avoid calculation the math of moving. Thank you for any information.
If you want to solve this problem quickly, I suggest you use Split Pane Layout.
Usage :
<com.mobidevelop.spl.widget.SplitPaneLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:spl="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/splitPaneLayout"
android:layout_height="match_parent"
spl:splitterSize="12dp"
spl:orientation="vertical"
spl:splitterPosition="50%"
spl:splitterBackground="#781b23">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="" />
</ScrollView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:text=""/>
</ScrollView>
</com.mobidevelop.spl.widget.SplitPaneLayout>
I solved your problem by creating two xmls for portrait and landscape mode. For portrait mode, i set the panel's orientation as vertical by adding spl:orientation="vertical" and for lanscape mode, i set the panel's orientation as horizontal by adding spl:orientation="horizontal".
After doing all this, I got the look like below.
Made this into an answer.
You basically want the split screen view from Android N. You could base your code off the open source implementation in SystemUI:
http://androidxref.com/7.1.1_r6/xref/frameworks/base/packages/SystemUI/src/com/android/systemui/stackdivider/DividerView.java
Along with this for the handle:
http://androidxref.com/7.1.1_r6/xref/frameworks/base/packages/SystemUI/src/com/android/systemui/stackdivider/DividerHandleView.java
You can throw away all code that has to do with stacks (which is the row of screenshots off different activities in your history), buss events and anything that has to do with running another activity, such as the code for Vsyncing between apps (mSurfaceFlingerOffsetMs).
It should leave you with quite small and easy to use classes.

How to use two activites in a single screen in android?

I want to display two different activities in a single screen how can i do that in android?Please if anybody has idea share it.And I don't wanna use fragments.
I want to display a screen which contains some fields and below(at the bottom of the screen) I want another screen with some buttons.
Is this possible in android?
If so, How can i do this ?
You can't have two activities in one screen. You can have only one. So, ultimate solution is Fragments.
An activity is not directly a visual component, so I'm thinking that what you're really asking is how to have a single activity display different views.
There's nothing that says you can't rerun setContentView() with a different layout/view ID. But there's another non-fragments way of doing what your probably want.
You can define more than one full-size (match_parent) view in a layout. What you want to do is set the visibility for one of them to "visible" with android:visibility="visible" and all the others to "gone" with android:visibility="gone".
Then when you want to switch the displayed view, you'll run setVisibility(View.GONE) on the outgoing view and setVisibility(View.VISIBLE) on the incoming. It's important to use GONE and not INVISIBLE or the layouts won't render correctly.
Sample layout file:
<FrameLayout 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"
tools:context=".MainActivity" >
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageView
android:id="#+id/img"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible" />
<SurfaceView
android:id="#+id/video"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
<WebView
android:id="#+id/web"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />
</FrameLayout>
Sample Code to switch view:
video.setVisibility(View.VISIBLE);
img.setVisibility(View.GONE);
web.setVisibility(View.GONE);
That said, you probably want to learn how to use fragments since you can handle switching the view along with other state in a single unit of work (a transaction). But the above approach above does work for simple view changes.

How to divide Android game screen

I am working on a game application and in a horizontal orientation.
I use the left 1/3 of the screen for displaying scores and controls and the right 2/3 for displaying my game board.
My game works as is, but I currently use a single canvas to display everything. This makes my game logic more complicated because I must always add 1/3 of the screen width when displaying any graphics.
Seems like *I should be able to define 2 sections of the scree*n (left 1/3 and right 2/3), maybe using two canvases, such that each section provide me a unique coordinate system starting at 0,0. This would make my game coordinate math much easier.
Can I use multiple Canvases in one activity? Can I place them wherever I'd like?
Thanks
You can use fragments for dividing the screens.
Its better to use fragments or Fragment video tutorial, identify 3 fragments and in let in each fragment have a canvas
You can use weights in linear layout
which are used to divide the layout as desired ratio
http://developer.android.com/guide/topics/ui/layout/linear.html#Weight
Fragments
http://developer.android.com/reference/android/app/Fragment.html
use fragments for second layout
example
<?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="horizontal"
android:weightSum="1" >
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.33333"
android:background="#android:color/holo_blue_bright" >
</LinearLayout>
<LinearLayout
<--fragment here -->
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.6666"
android:background="#android:color/holo_green_dark" >
</LinearLayout>
</LinearLayout>

XML Drawable not expanded to View size, or View not inheriting its parent's height correctly, on Android API < 11

Consider the following layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg1" >
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/bg2" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<!-- some views here -->
</LinearLayout>
</FrameLayout>
I'm using it to have bg2 on top of bg1 while bg2 remains completely independent, such that I can apply e.g. a tween alpha animation to it without affecting anything else.
bg1 and bg2 are XML drawables. Obviously, they're supposed to be scaled to their respective View's dimensions. This is normally the case and it doesn't seem to make much sense to specify their dimensions explicitly.
Unfortunately, on Android versions prior to 3/API 11, it looks as if the size of bg2 will be zero. Maybe the two-phase layout measurement is buggy (note how bg2 is supposed to inherit its height from its parent, which in turn needs to adjust to the LinearLayout´s height and propagate that information to the View containing bg2). Or maybe the View won't accept its parent's height (although I tried ImageView which changed nothing).
The Layout you see is really used for items in a list.
The XML drawables are stateful and use gradients.
Can you think of an approach which would also work for Android API 8 to 10?
A bit of testing (subclassing View, overriding onMeasure() and onLayout()) reveals that FrameLayout is broken in this respect in older Android versions.
Since FrameLayout fails to pass its own height down the hierarchy in this scenario (the View will always see 0 both with onMeasure() and onLayout()) there is no obvious way to work around this problem by subclassing.
The question then was, is there another way to overlay the two views which Android 2.2 aka API 8 can handle correctly.
And alas, there is. The same can be achieved with a RelativeLayout, which involves much more overhead of course, though the actual increase in rendering effort should be limited.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg1" >
<View
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignTop="#+id/item"
android:layout_alignBottom="#+id/item"
android:background="#drawable/bg2" />
<LinearLayout
android:id="#+id/item"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
</LinearLayout>
</RelativeLayout>

Layout help with custom view

I'm playing around with 2D drawing within a custom view. So far I've got it drawing what I want(Point data from an array) but I want the drawing to scale and be able to extend it beyond the screen(such that 1 second of data per screen width). The method I'm using for drawing relies on getHeight and getWidth to set the bounds of the information in the screen. I'm using the screens width to extend the canvas in onDraw to several screen widths in size(depending on the time of the data).
I've been unable to find a way of drawing that will allow me to scroll horizontally, and support a zoom like function... is there an easy way of doing this? ScrollView doesn't do Horizontally, and when I go beyond the screen it just get's cut off. My layout so far is
<HorizontalScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<view class="com.box.sand.SandGraph$GraphView"
android:id="#+id/graph"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</HorizontalScrollView>
But when I put this in, the onDraw of my view never get's called.
This may solve your problem.
Although I doubt that it has a zoom function, you should be able to implement the rest of what you're asking for using this.
EDIT:
The hierarchy would go as follows:
<HorizontalScrollView ... >
<LinearLayout android:orientation="horizontal" >
<Content goes here />
</LinearLayout>
</HorizontalScrollView>

Categories

Resources