Creating an overlay activity - java

I am trying to create a overlay activity which is sitting in a corner of the screen taking a very small portion of the screen, while the rest of the screen is interactive that is I tap on anything that is displaying on rest of the screen. So far I am unsuccessful in obtaining my goal. I have added these properties to my window but they don't seem to work.
hello.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="50dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/tepp" >
</RelativeLayout>
What can I do to solve this problem?

From what I can remember, only one Activity can be active at any given time. So while that small Activity in the corner is running, the other activitys will be paused and you wont be able to interact with them. You should try using a Fragment instead. You can have multiple fragments visible at the same time. Although I believe that only one Fragment can be active at any given time like an Activity, when you click on different fragments, the one you click on becomes active and you can interact with it. This might help you achieve what your looking for.

i don't think that there is such a thing "an overlay activity" .
however, instead of using an activity, you could make the view on top, like i've shown here . when the user touches it, you would need to decide what would happen, and if you need to expand its size to be larger to show something else to the user...
btw, if all the activity does is to create an on top view, you can call finish() right at the end of the onCreate() method. also you can avoid using setContentView in case you inflate the view anyway...

Related

For supporting different screen size why should i use fragment in android instead of activity

I am beginner to android..I am started new android project..for supporting
different screen size..in fragment documentation they given to use fragment..but
why cant i use activity in android..if i use activity or fragment..which i should i use in this both..please dont give link of activity or fragment..please anyone answer me..i dont know which to use?...i want about all documentation they given about activity and fragment but i dint understand which to use..below is the link i read about fragment..if i use activity i should do more codings?
https://developer.android.com/guide/components/fragments.html
In fact you can't use a Fragment alone, Fragments are inside the Activity.
One point of using the Fragments for supporting different screen sizes is the ability to implement some views like a "Master/Detail" view.
A Fragment, as its name says, is a part of a bigger controller "the Activity", its reference can be removed and it's cleaner than having a big massive Activity to handle all the states of a view.
So the use case is completely depends on your project and its User Interface. I'd be glad to help you if you give me more information about your project and its design.
I think you will need at least one activity. And then for better handling different device rotations and screen sizes you can use one or more fragments inside this activity.
I try to explain this with an example:
You want to create a nice music player app which should look nice in portrait and landscape mode.
You split your app up into three fragments:
Here you can see how the app looks in portrait mode. The activity shows two fragments: The first fragment only consists of a listview. There the song titles are listed. On the bottom you can see the second fragment, which displays the song title of the current playing song and got a button for pausing the music.
When your user uses the music player app on a tablet in landscape mode you have more space for displaying stuff. Then the activity shows the list fragment (which also gets displayed in portrait mode) and it shows a third fragment which shows detailed information about the current playing song (e.g. the album image) and a progress bar.
By using fragments you only need to write the code for the list once.
Sweet and Simple thing, What i recommend is always use Fragments,
But for Fragment you will require Activity.
Take it in this way , Activity is a Canvas on which you can put any number of Fragments.
Whatever your UI is always use Fragment present on a activity if you want to show one screen even then also, So that you will always have Flexibilty to use all those cool things which fragments provides,maybe in future or in current.
If you use activity it has limits,FOR EXAMPLE, LIKE in INSTAGRAM AT BOTTOM, It has FIVE OPTIONS, Suppose THOSE OPTIONS ARE ON A ACTIVITY AND BY CLICKING ON THEM YOU CAN SWITCH TO DIFFERENT FRAGMENTS.
For more info:
Here is the most accepted answer for this topic.

Animated Views move outside of my layout

I have a ScrollView which contains several RelativeLayouts and LinearLayouts like this:
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<RelativeLayout >
</RelativeLayout>
<LinearLayout >
</LinearLayout>
</ScrollView>
Now, when I click on one of those layouts, I want it to expand and reveal more information. I have managed to do this by scaling the layout I want vertically with a PropertyAnimator:
relativeLayout.animate().scaleY(100).setDuration(duration).start();
At the same time, I use another PropertyAnimator to move any Views below the one I expanded vertically so that there's enough space for the expanded layout. So far it is working.
Unfortunately, the Views that move somehow end up outside of the viewport of the ScrollView, so I'm unable to scroll down and see the information in those Views. Essentially, the vertical translation of those views renders their lower part unreachable, since the viewport does not expand too.
I have set android:fillViewPort="true" on the ScrollView. And I have also tried to do it programmatically with setFillViewPort() but neither has had any effect.
What's wrong? Why is it not working?
When you perform translation animations on Views then those Views don't really move inside the layout. Its just visual for the User, but when it comes to layouting and/or measuring than any translation values are ignored. It is always as if the Views are not translated at all.
What I am guessing you are doing right now is this:
You react to the click event and expand the View you want to expand.
You calculate how much the other Views need to move to accommodate the expanded View.
Then you perform translate animations on those Views by much they need to move.
And then as a result suddenly a few Views move off screen.
This approach can actually never work. You always need to remember that a Views position in the layout is determined just by the layout. All your translations are essentially just for show. So this is what's actually happening when you try to do the above:
You react the to the click event and expand the View.
This expansion causes Android to start a layouting and measuring process. The positions and sizes of all Views is calculated and they are positioned at their new location with their new size.
Since now the Views are already at the location they are going to be after the expansion you translation animation just moves the Views further down, beyond the point they are supposed to be.
As a side effect of this the Views seem to move off screen for no apparent reason.
So what can you do about this? Essentially you need to tackle this problem the other way around. As I mentioned above Android already calculates the new sizes and positions of all Views for you, and you can use that to your advantage.
There are two basic solutions for your problem. Either you let Android perform the animations for you with LayoutTransitions or you perform your animations manually. Both ways use something called the ViewTreeObserver. It can be used to listen for changes in the layout or new drawing processes.
But first and foremost: ScrollView is supposed to work with only one child. So to prevent any future bugs or problems put all your items in the ScrollView inside of another LinearLayout with vertical orientation.
1) Using LayoutTransition
This would only work from API Level 16 and above. Below API Level 16 visibility animations and translation animations would be handled automatically, but to get height changes animated you need to have API level 16.
One important thing I have to mention is that:
LayoutTransition animates changes for you. So you can remove all you custom animations if you use it. If you leave your own animations in you are just going to create conflicts with the animations performed by LayoutTransition.
If you don't like the animations performed by LayoutTransition you can customise them! I will explain how to do that further down below.
I usually use a helper method like this to setup a LayoutTransition.
public static void animateLayoutChanges(ViewGroup container) {
final LayoutTransition transition = new LayoutTransition();
transition.setDuration(300);
if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
transition.enableTransitionType(LayoutTransition.CHANGING);
transition.enableTransitionType(LayoutTransition.APPEARING);
transition.enableTransitionType(LayoutTransition.CHANGE_APPEARING);
transition.enableTransitionType(LayoutTransition.DISAPPEARING);
transition.enableTransitionType(LayoutTransition.CHANGE_DISAPPEARING);
}
container.setLayoutTransition(transition);
}
This will enable all possible automatic transitions on API Level 16 and above and just use the by default enabled transitions below that. Just use it like this:
AnimatorUtils.animateLayoutChanges(linearLayout);
If you call this method on the LinearLayout in your ScrollView then all changes to height/width/visibility of the LinearLayout and its direct children will be animated for you. Also item add/remove animations are taken care of for you.
To enable all kinds of transitions like resize animations you need to set the LayoutTransitions in code, but you can enable basic transitions like item add/remove animations by setting the property
android:animateLayoutChanges="true"
on a ViewGroup in your xml layout.
There exists only minimal documentation on LayoutTransitions, but the basics are covered here.
If you want you can customise the animations for each event like adding/removing a View or changing something about the View like this:
// APPEARING handles items being added to the ViewGroup
transition.setAnimator(LayoutTransition.APPEARING, someAnimator);
// CHANGING handles among other things height or width changes of items in the ViewGroup
transition.setAnimator(LayoutTransition.CHANGING, someOtherAnimator);
Here is a DevByte video which explains LayoutTransitions in greater detail:
LayoutTransitions enable easy fade/move/resize animations
Also note that container views can essentially cut off parts of the animations when the height of a parent changes. This won't happen in your case since your ScrollView has a fixed size and does not resize based on the children inside the ScrollView, but if you implement something like this in a ViewGroup with wrap_content then you need to set android:clipChildren="false" on all containers above the Views you are trying to animate. You can alternatively also use setClipChildren() in code.
2) Animating all items manually.
This is a much more difficult than using LayoutTransitions, mainly because you have to know a lot about the layouting and measuring process, otherwise you are going to cause problems. Nevertheless once you get the hang of it you can perform all kinds of custom animations.
The basic process is like this:
Record current View state.
Change layout to the state after the animations are finished
After Android is done layouting and measuring everything record the new values.
Now animate the Views from their old position to their new one
The core of this process is listening for changes in the view hierarchy. This is done using the ViewTreeObserver. There are multiple possible callbacks you can use, for example OnPreDrawListener or OnGlobalLayoutListener. Generally you would implement them like this:
final Animator animator = setupAnimator();
animator.setTarget(view);
// Record the current state
animator.setupStartValues();
modifyChildrenOfLinearLayout();
linearLayout.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
// Remove the callback immediately we only need to catch it this one time.
linearLayout.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// Record the new state
animator.setupEndValues();
// Start the animation
animator.start();
}
});
OnGlobalLayoutListener is better at catching layout changes since it is called after a layouting process has finished. OnPreDrawListener is called before the next frame is drawn, but their is no guarantee that the layouting process has already finished. But in practice this difference is negligible. Much more important is that on older slower devices there might be a short flash of the layout in its new state because they need some time to process each step. You can prevent that by using an OnPreDrawListener and returning false once. Since OnGlobalLayoutListener is also only completely available on newer API levels you should in most cases use OnPreDrawListener.
If LayoutTransitions does not provide you with an adequate solution to your problem and you have/want to implement the animations manually than learning how to perform animations efficiently is important. You can look at the source code of LayoutTransition here. The implementation of LayoutTransition essentially does exactly what I have been explaining here and it is a best practice implementation. I often find myself looking through the source code of the android.animation package to learn new things about how to animate efficiently and if you want to understand animations on Android I suggest you do the same!
You can also watch a few Android DevBytes videos about animations like this one:
ListView Expanding Cells Animation
In this video he explains how to animate an expanding cell in ListView by using an OnPreDrawListener.
Just always remember, the Layouting Engine is your friend. Don't try to reinvent the wheel and do stuff manually a layouting process would already do for you. And never call requestLayout() while performing animations!
From Android Developer site :
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
I can see that you have added multiple layouts as child in Scroll View, please add one linear layout and add rest of layout in that LinearLayout.
Hope it will solve your problem
Try this.
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scroll"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<RelativeLayout >
</RelativeLayout>
<LinearLayout >
</LinearLayout>
</LinearLayout>
</ScrollView>
Scrollview must have only one child to it. So I created only one LinearLayout child and add your rest code in it.
just add android:clipChildren="false"to your parent animated view and animation works outside of view.

Layout And Views Visibility on app

I was testing my android app on device by enabling device show layout boundaries in developer option on device.
I check my listview with inflated view with textviews , rating bar and other views clearly seen as shown below .
later I tried twitter app but surprise to see only single view ???
anyone know how to get twitter like single view on listview ??
anyone know how to get twitter like single view on listview ??
Each list item is a single custom View object, not a ViewGroup or layout. Essentially, all the content is drawn directly onto the Canvas in onDraw() rather than relying on child ImageView and TextView elements. Images can be drawn easily enough by calling Drawable.draw() or Canvas.drawBitmap() and text is typically drawing using a Layout.
Additionally, this means all touch events are handled directly inside onTouchEvent() to handle taps on the lower icons and/or the avatar image, so there are no click listeners.
Edit: Here's a quick 30 second example that should be enough to get you started: https://gist.github.com/devunwired/8704007
They are probably using the include tag to include another layout file
you can do the same thing if you make your list_row_item layout something like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width=”match_parent”
android:layout_height=”match_parent”
>
<include layout="#layout/another_layout"/>
</LinearLayout>
The benefit to doing this is that you can swap layouts on the fly, perhaps even using a server side changes, rather than having to issue a software update

Scroll View in Android shows up when I scroll it

I have a Scroll-view layout, which is, say my main menu, when user clicks on an item I want to make my menu disappear, and show the contents that user wants!
everything works fine, I use this code:
mainMenulay.setVisibility(View.Gone);
but, when I touch the screen, and swipe the screen (like scrolling when it was actually visible), where the main menu was located, the scroll-View show up in an unwanted buggy way !
its not functional, but it shows up !
I have fixed this issue with Z Order and defined a background and have brought it in front of the Main Menu and it wont show up, but I'm curious to know what's wrong, how to avoid it ?!
If you have questions about How I defined the Scroll-View, well. it looks like this:
<ScrollView
android:id="#+id/main_menu_lay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="61dp"
android:layout_marginTop="65dp"
android:visibility="visible" >
Thanks in Advance :-)
I've seen other people with the same problem, but only when an animation i used to hide the view.
android View with View.GONE still receives onTouch and onClick
and
Unable to make view completely GONE after using TranslateAnimation
Probably, you can take a look at
View.setClickable();
and
ViewParent.requestDisallowInterceptTouchEvent()

how to show a "window/subactivity/dialog" on top of an Activity but keeping the focus in the Activity

My activity A is a game and it does some background operations. When I press a button in the contextual menu, I want to pop up a "small window/dialog/subactivity" (lets call it B) that appears on top of activity A and displays some data about those background operations. But I need to keep the focus on the activity A in order to continue interacting with it (playing the game).
In essence, I want to be able to see the data display by B while playing the game.
I'm not really sure how to implement this. After reading the documentation I have the next conclusions:
I know that I can't use Dialogs because the have the focus. Is it possible to avoid this?
Using a subactivity with a Dialog theme it's another option that looks tempting...but I believe that the subactivity has the focus. Ditto.
My last option is to try to add a LinearLayout with my data to the main Layout, "sharing/splitting" the screen. It's not pretty, but at least I know that this is possible. What I don't like about this approach is that I use the width and height of the screen.
Any suggestions? Solutions?
PS: I found some this thread here that are very related to my question:
Android ==> Sub Activity?
Create an Activity with style Theme.Dialog. This is a normal activity which looks like a dialog, while being modeless and accepting events.
Additional catch is in setting WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL and resetting WindowManager.LayoutParams.FLAG_DIM_BEHIND.
See this answer for complete example: timed modeless dialog
Why not use a FrameLayout that is apart of your Activity? Just ensure that this View has a higher z index (make sure you declare it last in your XML layout or create it at runtime). That way you never leave your Activity.

Categories

Resources