EditText get hide when keyboard open - android

/*This is my mainActivity.class*/
public class MainActivity extends ActionBarActivity{
attachFragments();
}
private void attachFragments() {
CustomFragment chatFragment=new CustomFragment();
FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.first, chatFragment);
transaction.commit();
}
}
/*mainActivity.xml*/
<?xml version="1.0" encoding="utf-8"?>
< com.prospus.poms.layout.CustomSlidingLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainLayout"
android:layout_height="match_parent"
android:layout_width="match_parent">
<RelativeLayout
android:id="#+id/first"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/second"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
<RelativeLayout
android:id="#+id/third"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
</RelativeLayout>
</com.prospus.poms.layout.CustomSlidingLayout>
CustomSlidingLayout.java
public class CustomSlidingLayout extends LinearLayout {
private static final int SLIDING_DURATION = 500;
private static final int QUERY_INTERVAL = 16;
int mainLayoutWidth;
private View menuRight;
private View content;
private enum MenuState {
HIDING, HIDDEN, SHOWING, SHOWN,
};
private int contentXOffset;
private int menuWidth;
private int rightMenuOffset;
private MenuState currentMenuState = MenuState.HIDDEN;
private Scroller menuScroller = new Scroller(this.getContext(),
new EaseInInterpolator());
private Runnable rightMenuRunnable = new MenuRightRunnable();
private Handler menuHandler = new Handler();
public CustomSlidingLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
public CustomSlidingLayout(Context context) {
super(context);
}
#Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
mainLayoutWidth = MeasureSpec.getSize(widthMeasureSpec);
menuWidth=mainLayoutWidth;
}
#Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
content = this.getChildAt(0);
menuRight = getChildAt(1);
menuRight.setVisibility(View.GONE);
}
#Override
protected void onLayout(boolean changed, int left, int top, int right,
int bottom) {
if (changed) {
LayoutParams contentLayoutParams = (LayoutParams) content
.getLayoutParams();
contentLayoutParams.height = this.getHeight();
contentLayoutParams.width = this.getWidth();
LayoutParams menuLayoutParams = (LayoutParams) menuRight.getLayoutParams();
menuLayoutParams.height = this.getHeight();
menuLayoutParams.width = this.getWidth();
}
menuRight.layout(right - rightMenuOffset, top, right - rightMenuOffset
+ menuWidth, bottom);
content.layout(left, top, right, bottom);
}
#SuppressLint("NewApi")
public void toggleRightMenu() {
if (currentMenuState == MenuState.HIDING
|| currentMenuState == MenuState.SHOWING)
return;
switch (currentMenuState) {
case HIDDEN:
currentMenuState = MenuState.SHOWING;
menuRight.setVisibility(View.VISIBLE);
menuScroller.startScroll(0, 0,
-menuRight.getLayoutParams().width, 0, SLIDING_DURATION);
contentXOffset = 0;
rightMenuOffset = 0;
invalidate();
break;
case SHOWN:
currentMenuState = MenuState.HIDING;
menuScroller.startScroll(contentXOffset, 0, -contentXOffset, 0,
SLIDING_DURATION);
break;
default:
break;
}
menuHandler.postDelayed(rightMenuRunnable, QUERY_INTERVAL);
this.invalidate();
}
protected class MenuRightRunnable implements Runnable {
#Override
public void run() {
boolean isScrolling = menuScroller.computeScrollOffset();
adjustRightContentPosition(isScrolling);
}
}
#SuppressLint("NewApi")
private void adjustRightContentPosition(boolean isScrolling) {
int scrollerXOffset = menuScroller.getCurrX();
menuRight.offsetLeftAndRight(scrollerXOffset - contentXOffset);
rightMenuOffset += (contentXOffset-scrollerXOffset );
contentXOffset = scrollerXOffset;
this.invalidate();
if (isScrolling)
menuHandler.postDelayed(rightMenuRunnable, QUERY_INTERVAL);
else
this.onMenuSlidingComplete();
}
private void onMenuSlidingComplete() {
switch (currentMenuState) {
case SHOWING:
currentMenuState = MenuState.SHOWN;
break;
case HIDING:
currentMenuState = MenuState.HIDDEN;
break;
default:
return;
}
}
protected class EaseInInterpolator implements Interpolator {
#Override
public float getInterpolation(float t) {
return (float) Math.pow(t - 1, 5) + 1;
}
}
public boolean isMenuShown() {
return currentMenuState == MenuState.SHOWN;
}
}
/*I am using this customlayout in main activity and in each sub layout attching a fragment. */
/* Layout file of fragment: */
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:fillViewport="true" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/header_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomLayout"
android:layout_below="#id/title_layout"
android:orientation="horizontal" >
<ListView
android:id="#+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/black"
android:fadeScrollbars="false"
android:fadingEdge="none"
android:isScrollContainer="false"
android:listSelector="#android:color/transparent"
android:overScrollMode="never"
android:scrollbarStyle="insideInset"
android:scrollbars="vertical"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/title_background"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/upload"
android:layout_width="#dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/upload_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
<EditText
android:id="#+id/chattext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="#dimen/BASE_5_DP"
android:layout_marginTop="#dimen/BASE_5_DP"
android:layout_weight="1"
android:background="#android:color/white"
android:maxHeight="#dimen/EditTextHeight"
android:minHeight="#dimen/BASE_45_DP"
android:padding="#dimen/BASE_5_DP"
android:selectAllOnFocus="true"
android:singleLine="false" />
<RelativeLayout
android:layout_width="#dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>
kayboard hide the editText but when keyboard hides, editText comes in middle of screen. Exactly opposite the requirement. I know main problem is my custom layout. Please tell me what is wrong in this customlayout. Everything works fine if i replace the customLayout with relativeLayoot. but i have to use this layout. Please help me.

<?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="wrap_content"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/header_layout" />
</LinearLayout>
<LinearLayout
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/bottomLayout"
android:layout_below="#id/title_layout"
android:orientation="horizontal" >
<ListView
android:id="#+id/chatList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:cacheColorHint="#android:color/transparent"
android:divider="#android:color/black"
android:fadeScrollbars="false"
android:fadingEdge="none"
android:listSelector="#android:color/transparent"
android:overScrollMode="never"
android:scrollbarStyle="insideInset"
android:scrollbarThumbVertical="#drawable/scrollbar_thumb"
android:scrollbarTrackVertical="#drawable/scrollbar_vertical_track"
android:scrollbars="vertical"
android:stackFromBottom="true"
android:transcriptMode="alwaysScroll" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#id/bottomLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/title_background"
android:orientation="horizontal" >
<RelativeLayout
android:id="#+id/upload"
android:layout_width="#dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/upload_icon"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/upload_icon" />
</RelativeLayout>
<EditText
android:id="#+id/chattext"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_marginBottom="#dimen/BASE_5_DP"
android:layout_marginTop="#dimen/BASE_5_DP"
android:layout_weight="1"
android:background="#android:color/white"
android:maxHeight="#dimen/EditTextHeight"
android:minHeight="#dimen/BASE_45_DP"
android:padding="#dimen/BASE_5_DP"
android:selectAllOnFocus="true"
android:singleLine="false" />
<RelativeLayout
android:layout_width="#dimen/BASE_50_DP"
android:layout_height="match_parent"
android:layout_gravity="bottom" >
<ImageView
android:id="#+id/done"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="#drawable/input_done_icon" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
</ScrollView>

put all view is scrollview
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/title_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<include layout="#layout/header_layout" />
</LinearLayout>
.....
</RelativeLayout>
</ScrollView>

Just wrap the whole Relative layout inside a ScrollView.
also
NOTE that scrollview can only have 1 child view
and that will be your outermost reletive layout.
ans the above link to documentation for scrollview methods that can be used to set different properties in java file.

Related

Google logo is hide when add parallax effect between bottom sheet and mapView

I'm trying to add a parallel effect in between bottom sheet layout and map view.but google log in map view is hidden when expending the bottom sheet layout. [Google logo is hidden]
show google logo when bottom sheet collapse
I'm using below code:
layout
<android.support.design.widget.CoordinatorLayout
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Green"
tools:context=".ui.activities.ChooseLocationActivity">
<RelativeLayout
android:id="#+id/map_rlyt"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="com.koya.android.ui.widget.CollapseBehavior"
app:layout_anchor="#+id/bottom_sheet_layout">
<fragment
android:id="#+id/g_map"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/category_activity_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/category_search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/custom_edit_text_margin_top"
android:layout_marginLeft="#dimen/custom_edit_text_margin_left_right"
android:layout_marginRight="#dimen/custom_edit_text_margin_left_right"
android:background="#android:color/transparent">
<android.support.v7.widget.AppCompatImageView
android:id="#+id/ic_back_iv"
android:padding="#dimen/location_activity_back_button_padding"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:src="#mipmap/ic_back_black"/>
<EditText
android:id="#+id/editTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/custom_edit_text_padding"
android:textColor="#color/black"
android:drawablePadding="8dp"
android:imeOptions="actionDone"
android:inputType="text"
android:maxLines="1"
android:drawableLeft="#mipmap/ic_search_gray"
android:textCursorDrawable="#drawable/edit_text_cursor_drawable"
android:drawableRight="#mipmap/ic_close_gray"
android:textSize="#dimen/custom_edit_text_text_size"
android:background="#drawable/drawable_custom_edittext"
/>
</LinearLayout>
<ProgressBar
android:id="#+id/progressBar"
style="?android:attr/progressBarStyle"
android:layout_centerInParent="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:visibility="gone"/>
<Button
android:id="#+id/redo_search_in_map_area_button"
style="#style/CategoryChooseButtonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/category_search_box"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/fifteen_text_size"
android:animateLayoutChanges="true"
android:paddingEnd="#dimen/redo_search_button_padding"
android:paddingStart="#dimen/redo_search_button_padding"
android:text="#string/redo_search_in_map_area"
android:textAllCaps="false"
android:visibility="gone"
/>
</RelativeLayout>
<include
android:id="#+id/bottom_sheet_layout"
layout="#layout/bottom_sheet"/>
<Button
android:id="#+id/select_location_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:text="#string/use_selected_location"
android:visibility="gone"
tools:visibility="visible"
style="#style/CategoryChooseButtonStyle"/>
<include android:id="#+id/error_layout"
layout="#layout/error_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom"
/>
And Implement Coordinatorlayout.Behavior
public class CollapseBehavior<V extends ViewGroup> extends CoordinatorLayout.Behavior<V>{
public CollapseBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onDependentViewChanged(CoordinatorLayout parent, V child, View dependency) {
if (isBottomSheet(dependency)) {
BottomSheetBehavior behavior = ((BottomSheetBehavior) ((CoordinatorLayout.LayoutParams) dependency.getLayoutParams()).getBehavior());
int peekHeight = behavior.getPeekHeight();
// The default peek height is -1, which
// gets resolved to a 16:9 ratio with the parent
final int actualPeek = peekHeight >= 0 ? peekHeight : (int) (((parent.getHeight() * 1.0) / (16.0)) * 9.0);
if (dependency.getTop() >= actualPeek) {
// Only perform translations when the
// view is between "hidden" and "collapsed" states
final int dy = dependency.getTop() - parent.getHeight();
ViewCompat.setTranslationY(child, dy/2);
return true;
}
}
return false;
}
private static boolean isBottomSheet(#NonNull
View view) {
final ViewGroup.LayoutParams lp = view.getLayoutParams();
if (lp instanceof CoordinatorLayout.LayoutParams) {
return ((CoordinatorLayout.LayoutParams) lp)
.getBehavior() instanceof BottomSheetBehavior;
}
return false;
}
}
I need this type of scrolling in my application -->
https://streamable.com/g0tf9
How to scroll google map when bottom sheet expend with parallax effect? I would appreciate it a lot!
Thanks in Advance.

Animate a view from 0dp width to MATCH_PARENT

I'm trying to animate my RecyclerView item when its clicked by making a rectangle grow from zero width to 100% (MATCH_PARENT) and become the background of the item.
However I can't see the animation working. I mean, the initial background is white, but the rectangle is gray, so the clicked item would become gray. But this is not happening.
Here's the item xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView 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="wrap_content"
app:cardCornerRadius="0dp"
android:focusable="true"
android:clickable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="72dp"
android:orientation="horizontal">
<View
android:id="#+id/colored_bar"
android:layout_width="3dp"
android:layout_height="match_parent"
android:background="#drawable/colored_bar_bg1"></View>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<View
android:id="#+id/option_background_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#e0e0e0"></View>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingTop="16dp">
<ImageView
android:id="#+id/icon"
android:layout_width="48dp"
android:layout_height="48dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="13dp"
app:srcCompat="#drawable/ic_lock" />
<TextView
android:id="#+id/card_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingBottom="16dp"
android:paddingLeft="8dp"
android:textColor="?android:attr/textColorPrimary"
android:textSize="16sp"
tools:text="#string/option_title_label" />
<TextView
android:id="#+id/card_subtitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/card_title"
android:layout_toEndOf="#+id/icon"
android:layout_toRightOf="#+id/icon"
android:paddingLeft="8dp"
android:textSize="14sp"
tools:text="#string/option_description_label" />
</RelativeLayout>
</FrameLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
And the code to make the animation:
public class OptionListItemHolder extends RecyclerView.ViewHolder {
private TextView cardTitle;
private TextView cardSubtitle;
private ImageView icon;
private View coloredBar;
private View optionBackground;
public OptionListItemHolder(View v) {
super(v);
cardTitle = (TextView)v.findViewById(R.id.card_title);
cardSubtitle = (TextView)v.findViewById(R.id.card_subtitle);
icon = (ImageView)v.findViewById(R.id.icon);
coloredBar = v.findViewById(R.id.colored_bar);
optionBackground = v.findViewById(R.id.option_background_container);
v.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ObjectAnimator animation = ObjectAnimator.ofInt(optionBackground, "width", 0, view.getWidth());
animation.setDuration(600);
animation.setInterpolator(new DecelerateInterpolator());
animation.start();
}
});
}
}
Why it is not working?
I've found that using a ValueAnimator instead works
ValueAnimator widthAnimator = ValueAnimator.ofInt(view.getWidth(), newWidth);
widthAnimator.setDuration(500);
widthAnimator.setInterpolator(new DecelerateInterpolator());
widthAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
#Override
public void onAnimationUpdate(ValueAnimator animation) {
view.getLayoutParams().width = (int) animation.getAnimatedValue();
view.requestLayout();
}
});
widthAnimator.start();
If the view's width needs to match the parent, you can get the width of the parent by
int parentWidth = ((View)view.getParent()).getMeasuredWidth();

How to smothly dragview in and out of screen?

How to implament "Parallax Animations" http://imgur.com/ah4l5oj.gif
if my scollable view (listview and scrollview) are placed inside fragments in viewpager?
MainLayout
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/header_holder"
android:layout_width="match_parent"
android:layout_height="#dimen/video_holder_size"
android:background="#color/dark_gray">
<ImageView
android:id="#+id/im_photo"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop" />
<TextView
android:id="#+id/tv_artist_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="16dip"
android:layout_marginLeft="16dip"
android:fontFamily="sans-sarif"
android:text="#string/artist_bio"
android:textColor="#color/white"
android:textSize="25sp" />
</RelativeLayout>
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
xmlns:slider="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="#dimen/controll_size"
slider:pstsIndicatorHeight="0dip"
slider:pstsPaddingMiddle="true"
slider:pstsTextAllCaps="true"
slider:pstsTextColorSelected="#color/white" />
<android.support.v4.view.ViewPager
android:id="#+id/video_body_pager"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:animationCache="false" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#drawable/shadow_concierge_list_upper"
android:gravity="left|center_vertical"
android:orientation="horizontal">
<ImageButton
android:id="#+id/ibtn_back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dip"
android:background="#drawable/ibtn_transparent"
android:padding="8dip"
android:src="#drawable/ic_action_back" />
</LinearLayout>
I tried to change view size by setting LayoutParams according to scroll delta, by performance is very poor
relatedHeight -= dY;// dY is a scroll delta
Log.e(getClass().getSimpleName(),"height "+relatedHeight+", dY "+dY);
if(relatedHeight < 0){
relatedHeight = 0;
} else if(relatedHeight > originalHeight){
relatedHeight = originalHeight;
} else{
LinearLayout.LayoutParams params =
(LinearLayout.LayoutParams) headerHolder.getLayoutParams();
params.height = (int) relatedHeight;
headerHolder.setLayoutParams(params);
}
I solved it using observableListview , otto bus and ViewHelper.
In pager fragment, I attach list.setScrollViewCallbacks(this); to my list.
In callback I pass offset to Bus
#Override
public void onScrollChanged(int scrollY, boolean firstScroll, boolean dragging){
BusProvider.getInstance().post(new ScrollOffset(0, scrollY));
}
#Override
public void onDownMotionEvent(){
}
#Override
public void onUpOrCancelMotionEvent(ScrollState scrollState){
}
In main fragment, I use viewhelper to translate header and viewpager
#Subscribe
public void moveHeader(ScrollOffset event){
ViewHelper.setTranslationY(headerHolder, -event.getY() / 2);
ViewHelper.setTranslationY(pager,headerHolder.getHeigh() -event.getY()/2);
int baseColor = getResources().getColor(R.color.dark_gray);
float alpha = Math.min(1, (float) event.getY()*10 / (originalHeight ));
appBarHolder.setBackgroundColor(ScrollUtils.getColorWithAlpha(alpha, baseColor));
}

TranslateAnimation move tomake other views appear

I'm using a TranslateAnimation to make a fragment (GoogleMap) sliding down to give space to an EditText and a TextView to be visible.
so I used this:
text: TextView
edit: EditText
MapLayout: a LinearLayout that contains the Map
Animation animation = new TranslateAnimation(
MapLayout.getX(), MapLayout.getY(),MapLayout.getY(), text.getHeight()+edit.getHeight());
The problem is that I can't make the slide because text.getHeight()+edit.getHeight() returns 0 so there's no slide!
I tried using a number (100 for exemple), the slide is made, but it's different between the devices, I tested on a Galaxy S3 and the slide is not complete, there's still a part of the EditText which is not visible, as for the emulator it worked ok.
When I tried to make the number a bit bigger, so the slide will be longer (200 for exemple), well... the slide was good for the S3, but i was big for the emulator.
So what I want to know is that if there's any way to make the slide move to a point, without depending on the device, I mean without using pixels; so the slide will work perfectly in any device/
I hope that my problem is clear.
Thank you
Update: I don't if this will help, I added a Toast message, show the height of the EditText and the TextView, in the Emulator it says: 85 and in the S3 it says 181
So yeah, I need to make the map slide down in any device like I said
MainActivity:
protected Animation animation;
protected LinearLayout MapLayout;
protected EditText edit;
protected TextView text;
MapLayout = (LinearLayout)findViewById(R.id.MapLayout);
edit = (EditText)findViewById(R.id.Recherche);
text = (TextView)findViewById(R.id.CaptionRecherche);
Toast.makeText(context, "Height: "+(edit.getHeight()+text.getHeight()), 1000).show();
animation = new TranslateAnimation(MapLayout.getX(), MapLayout.getY(), MapLayout.getY(), text.getHeight()+edit.getHeight());
animation.setDuration(1000);
animation.setFillAfter(true);
MapLayout.startAnimation(animation);
Main XML:
------- I'm using a DrawerLayout...I have a slide menu tu show in the application...just for your information-------
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/DrawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<include
android:id="#+id/ContenuPrincipal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
layout="#layout/activity_main_relative"
/>
<!-- ListView... La liste des options du menu -->
<ListView
android:id="#+id/Menu"
android:layout_width="250dp"
android:layout_height="fill_parent"
android:choiceMode="singleChoice"
android:layout_gravity="start"
android:background="#333"
android:divider="#666"
android:dividerHeight="1dp"
android:paddingLeft="15dp"
android:paddingRight="15dp"
/>
</android.support.v4.widget.DrawerLayout>
Main2 XML (The one I included 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:background="#E8E8E8">
<!-- Champs de saisie pour effectuer la recherche: -->
<TextView
android:id="#+id/CaptionRecherche"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Entrer l'emplacement que vous cherchez: "
android:textSize="20sp"
android:layout_marginTop="7dp"
android:layout_marginLeft="20dp"
/>
<EditText
android:id="#+id/Recherche"
android:layout_width="250dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_marginTop="10dp"
android:hint="Salle, Deparetement..."
android:layout_marginLeft="20dp"
android:layout_marginBottom="20dp"
android:maxLength="100"
android:maxLines="1"
android:layout_below="#id/CaptionRecherche"/>
<!-- La map: -->
<LinearLayout
android:id="#+id/MapLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<fragment
android:id="#+id/map"
android:name="com.google.android.gms.maps.MapFragment"
android:layout_width="match_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
</RelativeLayout>
As a part of my application, I have a status bar which contains some text. This status bar is hidden until the user clicks a button at which point it slides down (hiding the topmost content of the layout below).
The code I use to get the correct height of the hidden status bar:
private int hiddenStatusHeight;
private int currentStatusBarHeight;
private void getStatusBarHeight() {
final ViewTreeObserver observer = hiddenStatus.getViewTreeObserver();
observer.addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#SuppressLint("NewApi") #SuppressWarnings("deprecation") #Override public void onGlobalLayout() {
hiddenStatus.measure(MeasureSpec.UNSPECIFIED,
MeasureSpec.UNSPECIFIED);
hiddenStatusHeight = hiddenStatus.getMeasuredHeight();
currentStatusBarHeight = statusBar.getHeight();
ViewTreeObserver obs = hiddenStatus.getViewTreeObserver();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
obs.removeOnGlobalLayoutListener(this);
} else {
obs.removeGlobalOnLayoutListener(this);
}
}
});
}
The code that is executed when the button is clicked:
private OnClickListener ExpandClickListener = new OnClickListener() {
#Override public void onClick(View v) {
boolean isExpanded = (Boolean) expandButton
.getTag(R.id.TAG_EXPANDED);
int originalHeight = (Integer) expandButton
.getTag(R.id.TAG_ORIGINAL_HEIGHT);
if (isExpanded) {
expandButton.setTag(R.id.TAG_EXPANDED, false);
expandButton.setImageResource(R.drawable.ic_action_down);
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, originalHeight));
Log.d(TAG, "Collapsing to " + originalHeight);
ValueAnimator va = ValueAnimator.ofInt(currentStatusBarHeight,
originalHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
} else {
expandButton.setTag(R.id.TAG_EXPANDED, true);
expandButton.setImageResource(R.drawable.ic_action_collapse);
currentStatusBarHeight = originalHeight + hiddenStatusHeight;
// statusBar.setLayoutParams(new FrameLayout.LayoutParams(
// LayoutParams.MATCH_PARENT, currentStatusBarHeight + 15));
Log.d(TAG, "Expanding to " + originalHeight + "+"
+ hiddenStatusHeight + "=" + currentStatusBarHeight);
ValueAnimator va = ValueAnimator.ofInt(originalHeight,
currentStatusBarHeight);
va.setDuration(500);
va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
public void onAnimationUpdate(ValueAnimator animation) {
Integer value = (Integer) animation.getAnimatedValue();
statusBar.getLayoutParams().height = value.intValue();
statusBar.requestLayout();
}
});
va.start();
}
}
};
And finally my layout XML (it has to be a FrameLayout):
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ScrollView
android:id="#+id/scrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="70dp" >
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animateLayoutChanges="true"
android:orientation="vertical"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="8dp"
android:showDividers="middle" >
</LinearLayout>
</ScrollView>
<RelativeLayout
android:id="#+id/displayStatusBar"
style="#style/DisplayStatusBar"
android:layout_width="match_parent"
android:layout_height="65dp" >
<RelativeLayout
android:id="#+id/status_always_visible"
style="#style/StatusBar"
android:layout_width="match_parent"
android:layout_height="20dp" >
<TextView
android:id="#+id/status_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="#string/received" />
<TextView
android:id="#+id/status_time_received"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/status_received" />
<TextView
android:id="#+id/status_time_delete_relative_text"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/status_time_delete_relative"
android:text="#string/is_deleted" />
<TextView
android:id="#+id/status_time_delete_relative"
style="#style/StatusBarText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="#string/minutes" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/status_hidden"
style="#style/StatusHidden"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/status_always_visible" >
<LinearLayout
android:id="#+id/status_hydrants_near_address_container"
style="#style/StatusHiddenText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:divider="#android:drawable/divider_horizontal_bright"
android:orientation="vertical"
android:paddingLeft="10dp"
android:showDividers="middle" >
<TextView
style="#style/StatusHiddenText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/no_information" />
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/optionsBar"
android:layout_width="match_parent"
android:layout_height="45dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="#999"
android:orientation="horizontal"
android:paddingTop="5dp" >
<ImageButton
android:id="#+id/button_hydrants"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:alpha="50"
android:contentDescription="#string/module_hydrants"
android:src="#drawable/ic_action_place" />
<ImageButton
android:id="#+id/button_route"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:contentDescription="#string/module_directions"
android:src="#drawable/ic_action_directions" />
<ImageButton
android:id="#+id/button_pdf"
style="#style/android:Widget.ImageButton"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:clickable="false"
android:contentDescription="#string/module_accessplan"
android:src="#drawable/ic_action_attachment" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<ImageButton
android:id="#+id/button_more"
style="#style/android:Widget.ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_action_down" />
</RelativeLayout>
</LinearLayout>
</RelativeLayout>
<!-- The "empty" view to show when there are no items in the "list" view defined above. -->
<TextView
android:id="#android:id/empty"
style="?android:textAppearanceSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="32dp"
android:text="#string/no_information"
android:textColor="?android:textColorSecondary" />
</FrameLayout>
Hope some of this may be helpful to you.
Just to mention it, I have asked a similar question where the visible layout is pushed downwards as the menu expands. So have a look at that if you rather want this behaviour: How to animate a slide in notification view that pushes the content view down
Happy coding

Random NullPointerException on nested inflated view

I've a layout with a menu that can be reused more times in more Activities...
The menu layout is called menu_up_row.xml and every button of the layout is called menu_up_cell.xml.
So I inflate programmatically the menu_up_row.xml for every Activity and, in the meanwhile, also menu_up_cell.xml many times as how many buttons the menu has.
This exception occurs moreabout once every 70 users...
Here is the call to the static method of a class called MenuUp that raise the exception:
public static void *etMenuUpButtonEnabled(Context ctx, LinearLayout menuUpLayout, int buttonID, boolean enabled) {
LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(getResourceId(buttonID));
//SOMETIMES NULL POINTER EXCEPTION HERE:
ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView); //<-- NULL POINTER EXCEPTION HERE
...
where :
private static int getResourceId(int buttonID) {
int retVal = 0;
switch (buttonID){
case BUTTON_MENU_UP_BTN1:
retVal = R.id.menuCell1;
break;
case BUTTON_MENU_UP_BTN2:
retVal = R.id.menuCell2;
break;
case BUTTON_MENU_UP_BTN3:
retVal = R.id.menuCell3;
}
return retVal;
}
The above method is called from the Main Activity:
...
LinearLayout menuUpLayout = (LinearLayout) findViewById(R.id.main_menu_up_row);
MenuUp.setMenuUpButtonEnabled(ctx, menuUpLayout, MenuUp.BUTTON_MENU_UP_BTN1,false);
...
In the xml of the Main Activity there is this for including the menu:
<LinearLayout android:id="#+id/main_menu_up_row"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50sp"
android:visibility = "gone"
>
</LinearLayout>
To inflate the menu, in the Main Activity I have this code:
MenuUp.inflateMenuUpCustom(ctx, menuUpLayout);
where:
public static void inflateMenuUpCustom(Context ctx, LinearLayout menuUpLayout) {
LayoutInflater layoutInflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
menuUpLayout.addView(layoutInflater.inflate(R.layout.menu_up_row, menuUpLayout, false) );
//Aggiungo i bottoni del menu
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell1, R.drawable.menu_grid_order_by, R.string.menu_grid_order_by_str);
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell2, R.drawable.menu_grid_add, R.string.menu_grid_add_str);
addMenuUpButton(ctx, menuUpLayout, R.id.menuCell3, R.drawable.menu_grid_filter_by, R.string.menu_grid_filter_by_str);
}
and:
private static void addMenuUpButton(final Context ctx
, LinearLayout menuUpLayout
, int cellResourceId
, int imageDrawableId
, Integer stringResourceId) {
LayoutInflater layoutInflater = (LayoutInflater)
ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout btnLayout = (LinearLayout) menuUpLayout.findViewById(cellResourceId);
btnLayout.addView(layoutInflater.inflate(R.layout.menu_up_cell, btnLayout, false) );
ImageView menuCellImageView =(ImageView) btnLayout.findViewById(R.id.menuCellImageView);
LinearLayout menuCellLinearLayout = (LinearLayout) btnLayout.findViewById(R.id.menuCellLinearLayout);
menuCellImageView.setImageDrawable(ctx.getResources().getDrawable(imageDrawableId));
...
Here is menu_up_row.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/menu_row"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
>
<LinearLayout android:id="#+id/radRowToBeSeen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:id="#+id/linearLayoutDets4"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#e7e7e7"
>
<LinearLayout android:id="#+id/menuCellCont1"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell1"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/menuCellCont2"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell2"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout android:id="#+id/menuCellCont3"
android:layout_weight="0.2"
android:layout_width="0dip"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<LinearLayout android:id="#+id/menuCell3"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
Here is menu_up_cell.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menuCellLinearLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="2dp"
>
<LinearLayout
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/menuCellImageView"
android:layout_gravity="center_vertical"
android:adjustViewBounds="true"
android:scaleType="centerInside"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:padding="2dp"
android:src="#drawable/menu_dummy"
android:visibility="gone"
>
</ImageView>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:orientation="horizontal"
>
<TextView
android:id="#+id/menuCellTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:maxLines="2"
android:text="ciao"
android:textColor="#ffffff"
android:textSize="13sp" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
What am I wrong?

Categories

Resources