Animate a linear layout in android - android

I am new to android and trying to create an application in native android.
I have a top bar which is 10% height of the whole screen and full width. Initially I want to show the bar when application launch. When user drag the application downwards, need to animate the "top bar" to -10%, so that user can't view. Also when user drag screen upwards then need to animate the "top bar" to 0, ie beginning of the screen ( normal position ).
So I have my layout like this
<relativelayout>
<linearLayout>
// Top bar content here
<linearLayout>
<linearLayout>
// With list items, etc
</linearLayout>
</relativelayout>
I am stuck on that animation part. How to get the events and do the animation.
Please give me a hint.
Thanks in advance

You could create an onTouch() method where the View is the whole screen. Specify what you want the top bar to do depending on the touch motion. Here's some code to start:
public boolean onTouch(View v, MotionEvent evt) {
switch(evt.getAction()){
case MotionEvent.ACTION_DOWN:
// your code that makes it disappear
return true;
case MotionEvent.ACTION_UP:
// your code that makes it reappear
return true;
}
return false;
}
In your xml, add ids to your layout.
<LinearLayout
android:id="my_layout"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<TextView
android:id="topbar"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="1">
<ListView
android:id="list"
android:layout_height="fill_parent"
android:layout_width="fill_parent"
android:layout_weight="9">
</LinearLayout>
The ids will be what you call upon in your onTouch() method to change the layout weight, or percentage of the top bar, from 1 to 0 and vice versa at your discretion.
Look into the first answer for an algorithm for figuring out whether the touch event scrolled up or down.

Related

ImageView larger than the screen as a background

Hello I have a ImageView(background) which's height should correspond to the screen height, the width on the other hand should be left intact.
Before this was done like this
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/songDetails_songImage"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:contentDescription="#string/songDetails_cd_songCover"
app:srcCompat="#drawable/default_song_cover" />
</HorizontalScrollView>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
...
This worked fine - the image view occupies whole background and the user can scroll right or left to see the whole image.
My goal is to animate the scroll, to do that I need to get rid of the horizontal view(to prevent user from scrolling) but whenever I change the HorizontalScrollView to Frame/Relative layouts(with the width set to wrap_content) the image view is scaled down to fit the screen.
Is there a way to keep the imageView width intact(with height matching that of the screen) so that it would remain bigger than the device screen?(without cropping)
I could then use custom animation to scroll right.
I have looked at similar questions but I wasn't able to find anything that would work in this case.
What you could do is to try to override the onTouchListener for the HorizontalScrollView.
For example, something like this:
scrollView.setOnTouchListener(new OnTouch());
private class OnTouch implements OnTouchListener
{
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
}
This would disable the user from scrolling the HorizontalScrollView while you could probably still scroll it programmatically.

Android ListView scrolls the entire screen

I have created a layout containing many linear layouts, and I have, in one of the linear layouts, 3 ListViews set side by side. I want to make each one of them scrollable, but singularily. For example, I want to be able to scroll one of them without anything else being affected.
Right now, if I set "android:nestedScrollingEnabled="true"" for any one of the lists, the entire screen scrolls. I want the scrolling of the list to be confined to the layout the list is in (so I can scroll the list and the rest of the screen stays in place).
How can I do that? So far, it's either no scrolling at all, or scrolling the entire screen. I just want the list to scroll.
Thanks!
PS: Here's how the relevant part of the XML looks like (nothing special about it):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_marginBottom="#dimen/list_vertical_margin">
<ListView
android:id="#+id/realtime_vehicle_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="#+id/realtime_errors_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<ListView
android:id="#+id/realtime_depot_list"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
One of your LinearLayouts is in a ScrollView, I guess. So remove the ScrollView.
Try to add the next following code in the activity that contains those Listviews:
ListView mylistview = (ListView) findViewById(R.id.mylistview);
mylistview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
v.getParent().requestDisallowInterceptTouchEvent(true);
return false;
}
I've figured it out. Apparently, the layout "continues" beyond the screen. I only put a few more elements to test if it scrolls, but in reality, the reason why only the 3rd list was scrolling and the other two weren't is because the 3rd list had more elements, and it went beyond its limits. That's why it was scrolling.
I was equating the "going out of the screen" with the idea that the list should scroll, but it's just a design problem - the operating system didn't think that the list was going off the screen.
As soon as I put more elements into the other two lists, they are now all scrolling.

scroll two list view in the same LinearLayout

I have two listViews in the same LinearLayout. Each listView looks like this:
<ListView
android:layout_width="fill_parent"
android:layout_height="0dp"
android:id="#+id/listViewReceived"
android:layout_weight="1"
>
</ListView>
<ListView ><!--same as above-->
The issue is that the two listViews have a scrollbar but I want show all the items without a scrollbar.
if I make scrolling in anyone the listviews the movement is generated internally. but I want show scrollbar in entire the screen. how the green bar. https://drive.google.com/file/d/0B_Z2NZi-dbUMc3FCOTQ1UDNWRnM/view
 issue is that the two listView have your scrollbar but I want show all the items without scrollbar.
Add this to all of the listview
android:scrollbars="none"
android:scrollbars=none will define which scrollbars to display or show any at all. This won’t disable scrolling as such (you’ll be able to scroll in the appropriate direction) but just hide the scrollbars from the user interface. Try Following Code
listView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true; // Indicates that this has been handled by you and will not be forwarded further.
}
return false;
}
});

Disabling all touches when loading indicator is shown

I have a simple main layout, and then I add a fragment layout that only shows an indicator loading on top of that main layout. Problem is that I can still press the buttons behind the loading indicator.
Is there anyway to disable touches so it doesn't pass through to the back? I don't want to have to disable each button on the main screen one by one.
You could set the "clickable" attribute to "true" in the layout that contains your ProgressBar:
<FrameLayout
android:id="#+id/progressBarContainer"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:clickable="true" >
<ProgressBar
android:id="#+id/progressBar"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="center" />
</FrameLayout>
Then while the ProgressBar is visible, its container (which fills the entire screen although it's invisible) will intercept any click events so they won't fall through to the underlying layout while your ProgressBar is showing.
To use this, do this when you want to show the ProgressBar:
findViewById(R.id.progressBarContainer).setVisibility(View.VISIBLE);
and then do this when you're done with it:
findViewById(R.id.progressBarContainer).setVisibility(View.INVISIBLE);
For instance, if you're using this in an AsyncTask, you could make it visible in onPreExecute(), then make it invisible in onPostExecute().
Return true while you wanna block user touches. Override this method in your Activity.
#Override
public boolean onTouchEvent( MotionEvent event ) {
return true;
}

Determine which view items are visible and most centered in a horizontal scrollview in android?

I have a custom horizontal scrollview defined as in xml:
<com.myapp.views.MyHorizontalScrollView
android:id="#+id/myScrollView"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:id="#+id/myLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
</LinearLayout>
</com.myapp.views.MyHorizontalScrollView>
I am dynamically inflating and adding child view to the linear layout (as above). This works nicely so far.
Also, I have extended the Horizontal scrollview. This is to add an onscroll listener which gives me onscroll event, and seems to be working
Question
When the user scrolls across on the scrollview, I need to determine if any of the views are now visible to the user i.e. shown on screen.
Also, I would like to determine the most centered view in scrollview (again that is visible to the user)
is this possible?
to determine if a View is visible you can do this like Bill Mote
Rect scrollBounds = new Rect();
scrollView.getHitRect(scrollBounds);
if (imageView.getLocalVisibleRect(scrollBounds)) {
// Any portion of the imageView, even a single pixel, is within the visible window
} else {
// NONE of the imageView is within the visible window
}
is stated as answer to this question Android: how to check if a View inside of ScrollView is visible?
there you can play to see which one is more centered with a little math and the order in which you added your views

Categories

Resources