2 activities appear next to each other depending on phone orientation - android

I am trying to make a stopwatch app that has Buttons that allow the user to start/pause the timer, reset the timer, and record a lap time. I would also have a Button that when clicked would bring up a new view that contains the lap times in a ScrollView.
That screen would have a Button which would bring the user back to the timer as well.
This alone would be relatively simple to do, it would just be two separate Activities. However, when the phone shifts to landscape mode I would like both screens (the timer and the lap times) to be displayed next to each other.
Is there an easy way to do this?
I was thinking about just having one Activity with two LinearLayouts inside of it (one for the stopwatch screen and one for the lap times) and having the Button that would normally switch between Activities just change the transparency of one of the view so only one was visible?
I imagine there is an easier way to do this, and I'm not sure if this solution would allow me to display each screen side by side anyways.
Thanks in advance!

I don't know of any way to display two activities at once, but I can tell you how I would solve this problem:
I would start by creating the stopwatch layout and the lap times layout in their own dedicated layout files - so in this example, we have /res/layout/layout_stop_watch:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- stopwatch views here -->
</RelativeLayout>
...and /res/layout/layout_lap_times:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- lap times views here -->
</RelativeLayout>
Then in /res/layout/activity_main.xml, we have the following for portrait mode:
<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper
android:id="#+id/root_viewFlipper"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- stopwatch views included in activity_lap_times layout -->
<include layout="#layout/layout_stop_watch" />
<!-- lap time views included in activity_lap_times layout -->
<include layout="#layout/layout_lap_times" />
</ViewFlipper>
The ViewFlipper is there so we can call ViewFlipper.setDisplayedChild(1) to show the lap times layout when the user clicks the button to view the lap times. Simply call ViewFlipper.setDisplayedChild(0) to go back to the stop watch layout.
For landscape mode, we have a separate layout - /res/layout-land/activity_main.xml. This layout will automatically display in landscape mode, so we don't need to handle that programmatically. We can set it up like so:
<?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">
<RelativeLayout
android:id="#+id/layout_stopwatch"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<!-- stopwatch views included in activity_lap_times layout -->
<include layout="#layout/layout_stop_watch" />
</RelativeLayout>
<FrameLayout
android:id="#+id/layout_lap_times"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1">
<!-- lap time views included in activity_lap_times layout -->
<include layout="#layout/layout_lap_times" />
</FrameLayout>
</LinearLayout>
And that's it.
One thing to keep in mind is the ViewFlipper won't be used in landscape mode, so you'll want to check if it's null before setting the displayed child. Also keep in mind that switching from portrait to landscape will re-create your activity. If you want to save any of the view states, you can use savedInstanceState.
The advantage of doing it this way is that we don't have to write the code twice for the stopwatch and lap times layout, for portrait and landscape. Also, since we're using one activity for all of it, all the associated java code can reside in MainActivity.java.

Related

Create a full screen view over the other

I'm creating an Android app.
When I click on a menu item, I need to open an activity/view/fragment (I don't know what is the best). This activity/fragment/view need to be placed as an "overlay" of my app (it will have a transparent background and we could see the "regular" activity behind).
I'm using a navigation drawer and a couple of fragment inside my app.
First I tried this :
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
tools:context=".AlertActivity"
android:id="#+id/alertLayoutRoot"
android:background="#android:color/transparent"
>
<TextView
android:layout_width="match_parent"
android:id="#+id/editText1"
android:text="#string/mes_favoris"
android:layout_height="match_parent"
>
</TextView>
</FrameLayout>
This is indeed showing my fragment, but this fragment isn't on top of all the others.
What could be the best way to do this (in working with Kotlin)?
EDIT : forgot to say, in this view, there will be a small slider.
Basicaly I want this : Overlay
You can use dialog fragment as it supports custom layouts like fragments and shows on top of another activity or fragment.
Here is a useful tutorial on dialog fragment.

Overlay view like YouTube app

I am creating an app which does some tracking on a map. The tracking part works great inside my activity.
However, I want to have some kind of small overlay in the bottom right corner (like the minimalised video playback in the YouTube app) that stays there, even when I switch activities.
I have looked at this, but it's not really what I need. I don't need it to be moveable, and I think this is impossible to keep when switching activities.
Is there some kind of class that I can implement (Widget, Fragment, ...) that would fit my needs?
Thank you,
DebboR
This is a bit late and probably not relevant to you anymore, but maybe somebody else will find this helpful.
I don't think it's possible to switch activities and keep a certain view on screen. How I think this should be done is have a main activity with fragments that swap and in the activity's layout have a view overlay on top of the fragments container.
For example:
<?xml version="1.0" encoding="utf-8"?>
<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"
tools:context=".MainActivity">
<!-- Container for the fragments -->
<FrameLayout
android:id="#+id/fragment_container"
android:layout_below="#id/toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<!-- This view will overlay the fragment in the bottom right corner -->
<View
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>

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.

Slidedown a new layout from top

I have a main layout being hosted by an activity (setContentView method).
I have a button in the layout. I want to be able to click this button and a new layout will slide down from the left/top until the middle of the screen. So, the screen now will have two layouts where one is on top and another one is just beneath it.
This is something like the UI in Android Jelly Bean where you can pull the settings layout down using a touch gesture.
What are the possible implementations?
A method I have tried:
add the slide down layout into main.xml but set its visibility to gone
when button is clicked, run some code that will set the layout visibility to
VISIBLE and add some animations.
the result of this implementation is that this layout will push the rest of the
views down in order to have a "squeeze" space for itself which is not
what I intended to do (see above)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingTop="5dp" >
<LinearLayout
android:id="#+id/PR_slidedown" <---this is the intended slidedown layout
android:layout_width="8dp"
android:layout_height="40dp"
android:visibility="gone"
android:orientation="vertical" >
</LinearLayout>
....
Your implementation is partially correct, the other part is to host your two layouts in a FrameLayout so they can overlap with each other. Search for FrameLayout in here, there are a lot of example on how to use it.
Or try the SlidingDrawer

How does Android determine whether to move the layout up when showing the softkeyboard?

How does Android determine whether to move the layout up when showing the softkeyboard?
Note: I am aware that the activity property android:windowSoftInputMode="adjustResize|adjustResize|adjustUnspecified"
exists, as described here http://developer.android.com/guide/topics/manifest/activity-element.html#wsoft
, but in my case it doesn't seem to have any effect. This is my problem:
I have two activities, pretty much the same layout, but the first one is using a ListView that holds a list of buttons. The second activity holds a scrollview with buttons.
The rest is the same, same number of buttons, same height of elements, etc.
Now, when I press the search button to open the search input bar, in my first activity, the entire layouts gets moved up.
While on the second activity, the layout is not being moved up but the softkeyboard just displays on top of it. This is actually how I want it to behave. How can I achieve the same with my activity that's using the ListView?
In my manifest, initially I didn't specify any android:windowSoftInputMode attribute, but even if I do, it doesn't make any difference; I tried all three values (adjustPan, adjustResize, adjustUndefined, without any difference).
This is my layout:
1) http://pastebin.com/5zzVxjbK
2) http://pastebin.com/KFtPuHvP
Interestingly though: when I set my ListView visibility in my layout 1 (left) to View.INVISIBLE, then the layout doesn not get moved up!
I can achieve this with a RelativeLayout and setting android:windowSoftInputMode="adjustPan" like so:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent" >
<include android:id="#+id/Header" layout="#layout/header"
android:layout_width="fill_parent" android:layout_height="wrap_content" />
<ListView android:id="#+id/android:list" android:layout_width="fill_parent"
android:layout_height="wrap_content" android:layout_below="#id/Header"
android:layout_above="#+id/Footer" />
<include android:id="#id/Footer" layout="#layout/footer"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
You may be able to achive this by setting the footer gravity (or layout_gravity) to "bottom", if you want to keep the LinearLayout, but I am not sure.

Categories

Resources