I want to achieve something not usual for a AppBarLayout + Recyclerview scrollingviewbehavior effect.
I want to make the recyclerview move with AppBarLayout and go behind AppBarLayout when user scrolls. I need it to be really behind AppBarlayout because the background of AppbarLayout will be half transparent so recyclerview will be visible.
I don't know how to do this.
I did try multiple variants of ScrollViewBehavior.
I did try also setting negative values to SwipeRefreshlayout topMaring and change this value during scroll. Values where provided from AppBarLayout.addOnOffsetChangedListener from given i "vertical scroll offset". The result was that the swipeRefreshLayout flickered during scroll and during going behind AppBarLayout.
Please help me with this. I have no more options left.
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:id="#+id/clRootMain"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="false"
android:background="#color/backgroundGray">
<androidx.swiperefreshlayout.widget.SwipeRefreshLayout
android:id="#+id/srlSwipeRefresh"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"
android:isScrollContainer="true"
app:layout_behavior=".Util.View.ScrollingViewOverlapBehavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:divider="#android:color/transparent"
android:dividerHeight="#dimen/dp5"
android:drawSelectorOnTop="true"
android:fadeScrollbars="true"
android:fadingEdge="none"
android:fadingEdgeLength="#dimen/dp0"
android:gravity="center"
android:isScrollContainer="true"
android:nestedScrollingEnabled="true"
android:overScrollMode="never"
android:paddingLeft="#dimen/list_margin"
android:paddingRight="#dimen/list_margin"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarSize="#dimen/dp4"
android:scrollbarStyle="outsideOverlay"
android:scrollbars="vertical"
android:smoothScrollbar="false"
app:fastScrollEnabled="true"
app:fastScrollHorizontalThumbDrawable="#drawable/scrollbar_draggable"
app:fastScrollHorizontalTrackDrawable="#drawable/scrollbar_track_line"
app:fastScrollVerticalThumbDrawable="#drawable/scrollbar_draggable"
app:fastScrollVerticalTrackDrawable="#drawable/scrollbar_track_line" />
<TextView
android:id="#+id/tvEmptyListText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/dp20"
android:layout_marginTop="#dimen/dp10"
android:layout_marginRight="#dimen/dp20"
android:alpha="0.5"
android:gravity="center"
android:text="#string/category_product_list_empty"
android:textColor="#color/textGreyDark"
android:visibility="gone" />
</LinearLayout>
</androidx.swiperefreshlayout.widget.SwipeRefreshLayout>
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/ablMain"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#80ffffff"
android:fitsSystemWindows="true"
android:orientation="vertical"
app:liftOnScroll="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:orientation="vertical"
app:layout_scrollFlags="scroll|enterAlwaysCollapsed|exitUntilCollapsed">
<LinearLayout
android:id="#+id/llOpenOrderListContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/tvOpenOrdersTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/dp10"
android:layout_marginTop="#dimen/dp15"
android:alpha="0.6"
android:clickable="false"
android:gravity="center|left"
android:singleLine="true"
android:text="#string/current_orders_title"
android:textAllCaps="true"
android:textAppearance="#style/TextAppearance.AppCompat.Body2"
android:textColor="#color/textColor"
android:textSize="#dimen/sp10"
android:textStyle="normal" />
<androidx.viewpager.widget.ViewPager
android:id="#+id/vpOpenOrderList"
android:layout_width="match_parent"
android:layout_height="#dimen/dp75"
android:background="#android:color/transparent"
android:visibility="visible" />
</LinearLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/rlTopContainer"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#android:color/white"
app:layout_constraintDimensionRatio="H,16:9"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/vpBannerPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent" />
<RelativeLayout
android:id="#+id/rlControlContainer"
android:layout_width="#dimen/dp100"
android:layout_height="#dimen/dp30"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="#dimen/dp15"
android:background="#android:color/transparent">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tlBannerDots"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
app:tabBackground="#drawable/tab_selector_images"
app:tabGravity="center"
app:tabIndicatorHeight="0dp"
app:tabMinWidth="#dimen/dp3"
app:tabMode="fixed" />
</RelativeLayout>
<ImageView
android:id="#+id/ivHalfCircleBackgroundWithStroke"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_marginTop="#dimen/dp50"
android:scaleType="fitXY"
app:srcCompat="#drawable/ic_bottom_arc"
/>
</RelativeLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/rlCategoryContainer"
android:layout_width="match_parent"
android:layout_height="#dimen/dp75"
android:background="#android:color/transparent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvCategoryList"
android:layout_width="wrap_content"
android:layout_height="#dimen/dp70"
android:layout_above="#+id/llSeparatorLine"
android:layout_centerHorizontal="true"
android:clipChildren="false"
android:divider="#android:color/white"
android:dividerHeight="#dimen/dp10"
android:drawSelectorOnTop="true"
android:fadeScrollbars="true"
android:fastScrollEnabled="false"
android:gravity="center"
android:isScrollContainer="true"
android:nestedScrollingEnabled="true"
android:overScrollMode="never"
android:scrollIndicators="none"
android:scrollbars="none" />
<LinearLayout
android:id="#+id/llSeparatorLine"
android:layout_width="match_parent"
android:layout_height="#dimen/dp1"
android:layout_alignParentBottom="true"
android:background="#drawable/grey_line_with_fading_edges_light"
android:orientation="horizontal" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlBottomDownArcContainer"
android:layout_width="match_parent"
android:layout_height="#dimen/dp40"
android:background="#android:color/transparent">
<TextView
android:id="#+id/tvSectionName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#android:color/transparent"
android:clickable="false"
android:gravity="center_horizontal|top"
android:singleLine="true"
android:text="KWIATY"
android:textAllCaps="true"
android:textAppearance="#style/TextAppearance.AppCompat.Body1"
android:textColor="#color/textGrey"
android:textSize="#dimen/sp12"
android:textStyle="bold" />
</RelativeLayout>
</com.google.android.material.appbar.AppBarLayout>
<com.google.android.material.floatingactionbutton.FloatingActionButton
android:id="#+id/fabFilter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginLeft="#dimen/dp5"
android:layout_marginTop="#dimen/dp5"
android:layout_marginRight="#dimen/dp10"
android:layout_marginBottom="#dimen/dp10"
android:elevation="#dimen/dp4"
android:stateListAnimator="#animator/lift_on_touch"
android:visibility="visible"
app:backgroundTint="#android:color/white"
app:borderWidth="0dp"
app:elevation="4dp"
app:fabSize="normal"
app:rippleColor="#color/colorAccent"
app:srcCompat="#drawable/ic_icon_filtr_black" />
<!--</RelativeLayout>-->
</androidx.coordinatorlayout.widget.CoordinatorLayout>
public class ScrollingViewOverlapBehavior extends AppBarLayout.ScrollingViewBehavior {
public ScrollingViewOverlapBehavior() {
super();
}
public ScrollingViewOverlapBehavior(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onDependentViewChanged(CoordinatorLayout parent, View child,
View dependency) {
super.onDependentViewChanged(parent, child, dependency);
child.setTranslationY(dependency.getY());
return false;
}
}
This is what I have achieved so far but it leaves space at the bottom. I am using setTranslationY on my custom ScrollingViewBehavior (ScrollingViewOverlapBehavior).
https://drive.google.com/open?id=12Ri87BGKmAmwNq_rC-wpmCcYZyansmGl
Related
Although I used the "replace" method on the piece, the second part of the piece runs on the first part It should be noted that my code is only in (main activity) and there is no code in the second fragment section (I mean the "about" fragment here) (there is a fragment class by default) so I sent it I did not
I also put a background in the xml file, but the following part works although it is not clear
Please guide me on this
Thanks
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
.
.
.
LinearLayout lnrRecyclerNoConnection, li_home, li_share, li_about, li_logout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
replace(new HomeFragment());
...
}
.
.
.
#Override
public void onClick(View view) {
int id = view.getId();
if (id == R.id.ivMenu) {
mDrawer.openMenu(true);
} else if (id == R.id.li_home) {
replace(new HomeFragment(), "Home");
mDrawer.closeMenu(true);
} else if (id == R.id.li_about) {
replace(new AboutFragment(), "About");
mDrawer.closeMenu(true);
} else if (id == R.id.li_share) {
Toast.makeText(this, "share", Toast.LENGTH_SHORT).show();
mDrawer.closeMenu(true);
} else if (id == R.id.li_logout) {
Toast.makeText(this, "exited", Toast.LENGTH_SHORT).show();
finish();
System.exit(0);
}
}
private void replace(Fragment fragment, String string) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_main, fragment);
fragmentTransaction.addToBackStack(string);
fragmentTransaction.commit();
}
private void replace(Fragment fragment) {
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.fl_main, fragment);
fragmentTransaction.commit();
}
}
This is also the XML code section
I put "Replace" in the main activity section
<?xml version="1.0" encoding="utf-8"?>
<com.mxn.soul.flowingdrawer_core.FlowingDrawer xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawerlayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
app:edPosition="2"
app:edMenuSize="260dp"
app:edMenuBackground="#F1F1F1">
<androidx.coordinatorlayout.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_marginEnd="0dp"
android:animateLayoutChanges="true"
android:background="#color/colorBackground">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/mainAppBar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true">
<com.google.android.material.appbar.CollapsingToolbarLayout
android:id="#+id/mainCollapsingToolbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleGravity="start|top"
app:expandedTitleMarginTop="56dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:id="#+id/mainHeader"
android:layout_width="match_parent"
android:layout_height="200dp">
<ImageView
android:id="#+id/imgChannelCover"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="fitXY"
android:src="#drawable/cover_placeholder" />
<TextView
android:id="#+id/txtChannelFollower_cnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:layout_marginBottom="32dp"
android:fontFamily="#font/yekan"
android:gravity="center_horizontal"
android:textColor="#color/colorPrimary"
android:textSize="16sp"
tools:ignore="NotSibling" />
<TextView
android:id="#+id/txtVideo_cnt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignStart="#+id/txtChannelFollower_cnt"
android:layout_alignEnd="#+id/txtChannelFollower_cnt"
android:layout_alignParentBottom="true"
android:layout_marginBottom="16dp"
android:fontFamily="#font/yekan"
android:gravity="center_horizontal"
android:textColor="#color/colorPrimary"
android:textSize="16sp" />
</RelativeLayout>
<androidx.appcompat.widget.Toolbar
android:id="#+id/mainToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:paddingStart="16dp"
android:paddingEnd="16dp"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<ImageView
android:id="#+id/imgShowChannelDescription"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="start"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="16dp"
android:src="#drawable/person_outline_24" />
<ImageView
android:id="#+id/ivMenu"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_gravity="end"
android:layout_marginStart="8dp"
android:layout_marginTop="24dp"
android:layout_marginEnd="8dp"
android:src="#drawable/menu_open_24" />
</androidx.appcompat.widget.Toolbar>
</com.google.android.material.appbar.CollapsingToolbarLayout>
</com.google.android.material.appbar.AppBarLayout>
<LinearLayout
android:id="#+id/lnrRecyclerNoConnection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:orientation="vertical"
android:visibility="gone">
<pl.droidsonroids.gif.GifImageView
android:id="#+id/gifVideoLoading"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_gravity="center"
android:src="#drawable/no_connection"
android:tint="#color/colorPrimary" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fontFamily="#font/yekan"
android:gravity="center_horizontal"
android:text="#string/noConnection" />
<Button
android:id="#+id/btnRecyclerTryConnection"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="16dp"
android:background="#drawable/btn_selector"
android:fontFamily="#font/yekan"
android:text="refresh"
android:textColor="#color/colorWhiteTextColor"
android:textStyle="bold" />
</LinearLayout>
<com.facebook.shimmer.ShimmerFrameLayout
android:id="#+id/shimmerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include layout="#layout/shimmer_screen" />
<include layout="#layout/shimmer_screen" />
<include layout="#layout/shimmer_screen" />
<include layout="#layout/shimmer_screen" />
</LinearLayout>
</com.facebook.shimmer.ShimmerFrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/mainRecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="16dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/CircleImageChannelProfile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:src="#drawable/image_placeholder"
app:civ_border_color="#color/colorPrimary"
app:civ_border_width="1dp"
app:layout_anchor="#id/mainAppBar"
app:layout_anchorGravity="center_vertical|center_horizontal" />
<!--<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/CircleImageChannelProfile"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="32dp"
android:layout_marginEnd="32dp"
android:src="#drawable/image_placeholder"
app:civ_border_color="#color/colorPrimary"
app:civ_border_width="0.5dp"
app:layout_anchor="#id/mainAppBar"
app:layout_anchorGravity="bottom|end" />-->
<!--content-->
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
<!--menu-->
<com.mxn.soul.flowingdrawer_core.FlowingMenuLayout
android:id="#+id/menulayout"
android:layout_width="wrap_content"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/id_container_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="200dp"
android:id="#+id/header">
<ImageView
android:id="#+id/imgChannelCoverDrawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#drawable/bg_card_video_main"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/CircleImageChannelProfileDrawer"
android:layout_width="80dp"
android:layout_height="80dp"
android:layout_gravity="center_horizontal|center_vertical"
android:layout_marginTop="50dp"
android:src="#drawable/image_placeholder"
app:civ_border_color="#color/colorPrimary"
app:layout_anchor="#id/mainAppBar"
app:layout_anchorGravity="center_vertical|center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="title"
android:textSize="28sp"
android:fontFamily="#font/yekan"
android:textColor="#color/cardview_dark_background"
android:gravity="center_horizontal|center_vertical"/>
</LinearLayout>
</RelativeLayout>
<LinearLayout
android:id="#+id/li_home"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="48dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/home_24"
android:padding="4dp"
app:tint="#color/cardview_dark_background"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="home"
android:textColor="#color/cardview_dark_background"
android:fontFamily="#font/yekan"
android:textSize="16sp"
android:padding="4dp"/>
</LinearLayout>
<!--<LinearLayout
android:id="#+id/li_about"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/li_share"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/person_outline_24"
android:padding="4dp"
app:tint="#color/cardview_dark_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="about"
android:textColor="#color/cardview_dark_background"
android:fontFamily="#font/yekan"
android:textSize="16sp"
android:padding="4dp"/>
</LinearLayout>-->
<LinearLayout
android:id="#+id/li_share"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/li_home"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/share_24"
android:padding="4dp"
app:tint="#color/cardview_dark_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="اشتراک گذاری"
android:textColor="#color/cardview_dark_background"
android:fontFamily="#font/yekan"
android:textSize="16sp"
android:padding="4dp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/li_logout"
android:orientation="horizontal"
android:gravity="center_vertical"
android:layoutDirection="rtl"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/li_share"
android:layout_marginHorizontal="24dp"
android:layout_marginTop="24dp" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_baseline_power_settings_new_24"
android:padding="4dp"
app:tint="#color/cardview_dark_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="exit"
android:textColor="#color/cardview_dark_background"
android:fontFamily="#font/yekan"
android:textSize="16sp"
android:padding="4dp"/>
</LinearLayout>
</RelativeLayout>
</com.mxn.soul.flowingdrawer_core.FlowingMenuLayout>
</com.mxn.soul.flowingdrawer_core.FlowingDrawer>
I'm using a view pager on my application with multiple fragment inside, I also disable the my viewpager swipe using these code, but in my layout 'fragment_hr_link.xml' I can swipe it but it got stuck between 2 fragments
viewPager.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
It works fine in some of my fragment but, I'm having problem with this layout, the swipe is working but it got stuck
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.hr.HrLinkFragment">
<ScrollView
android:fillViewport="true"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:padding="15dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<GridLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:rowCount="3"
android:columnCount="2"
android:alignmentMode="alignMargins"
android:columnOrderPreserved="false">
<androidx.cardview.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/card_view_hr_daily_schedule"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="8dp"
android:layout_margin="12dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher_foreground"
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Daily Schedule"
android:textSize="16sp"
android:textColor="#3253F8"
android:layout_marginTop="12dp"
android:textAlignment="center" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/card_view_hr_daily_time_record"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="8dp"
android:layout_margin="12dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher_foreground"
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Daily Time Record"
android:textSize="16sp"
android:textColor="#3253F8"
android:layout_marginTop="12dp"
android:textAlignment="center" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/card_view_hr_evaluation"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="8dp"
android:layout_margin="12dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher_foreground"
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Evalution"
android:textSize="16sp"
android:textColor="#3253F8"
android:layout_marginTop="12dp"
android:textAlignment="center" />
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:clickable="true"
android:focusable="true"
android:foreground="?android:attr/selectableItemBackground"
android:id="#+id/card_view_hr_leave_balance"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_columnWeight="1"
android:layout_rowWeight="1"
app:cardElevation="6dp"
app:cardCornerRadius="8dp"
android:layout_margin="12dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:padding="16dp"
android:orientation="vertical">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#drawable/ic_launcher_foreground"
android:layout_gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Leave Balance"
android:textSize="16sp"
android:textColor="#3253F8"
android:layout_marginTop="12dp"
android:textAlignment="center" />
</LinearLayout>
</androidx.cardview.widget.CardView>
</GridLayout>
</LinearLayout>
</ScrollView>
You have a viewpager which should not be swipable?
Try making your custom viewpager like this:
public class SwipeDisabledViewPager extends ViewPager {
public SwipeDisabledViewPager(Context context) {
super(context);
}
public SwipeDisabledViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
// returning false will not propagate the swipe event
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return false;
}
}
you could also have a look at the new viewPager2 from android architecture components:
https://developer.android.com/reference/androidx/viewpager2/widget/ViewPager2
https://developer.android.com/training/animation/vp2-migration
hope this helps ;-)
I'm trying to make a visualization of some data in Android using a vertical bar chart, but when I increase or decrease the height of a View programatically by clicking a button, it changes from top to bottom, as in the image below (default value is 300).
How do I change the height from bottom to top, ie keeping the base of the view fixed?
Expected result:
My code in Java:
public final class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(R.layout.activity_main);
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
LinearLayout layoutXAxis = (LinearLayout)this.findViewById(R.id.layoutX);
LinearLayout layoutYAxis = (LinearLayout)this.findViewById(R.id.layoutY);
LinearLayout layoutZAxis = (LinearLayout)this.findViewById(R.id.layoutZ);
TextView txtXAxis = (TextView)layoutXAxis.findViewById(R.id.txtXAxis);
TextView txtYAxis = (TextView)layoutYAxis.findViewById(R.id.txtYAxis);
TextView txtZAxis = (TextView)layoutZAxis.findViewById(R.id.txtZAxis);
View graphXAxis = (View)layoutXAxis.findViewById(R.id.XAxis);
View graphYAxis = (View)layoutYAxis.findViewById(R.id.YAxis);
View graphZAxis = (View)layoutZAxis.findViewById(R.id.ZAxis);
LinearLayout layoutBtnAdd = (LinearLayout)this.findViewById(R.id.layAdd);
Button btnAddX = (Button)layoutBtnAdd.findViewById(R.id.btnXAdd);
Button btnAddY = (Button)layoutBtnAdd.findViewById(R.id.btnYAdd);
Button btnAddZ = (Button)layoutBtnAdd.findViewById(R.id.btnZAdd);
LinearLayout layoutBtnSub = (LinearLayout)this.findViewById(R.id.laySub);
Button btnSubX = (Button)layoutBtnSub.findViewById(R.id.btnXSub);
Button btnSubY = (Button)layoutBtnSub.findViewById(R.id.btnYSub);
Button btnSubZ = (Button)layoutBtnSub.findViewById(R.id.btnZSub);
txtXAxis.setText(String.valueOf(graphXAxis.getLayoutParams().height));
txtYAxis.setText(String.valueOf(graphYAxis.getLayoutParams().height));
txtZAxis.setText(String.valueOf(graphZAxis.getLayoutParams().height));
btnAddX.setOnClickListener((OnClickListener)(new OnClickListener() {
public final void onClick(View it) {
View graph = graphXAxis;
LayoutParams params = graph.getLayoutParams();
params.height += 10;
graph = graphXAxis;
graph.setLayoutParams(params);
TextView text = txtXAxis;
View graph = graphXAxis;
text.setText(String.valueOf(graph.getLayoutParams().height));
}
}));
... // onClickListener of the other buttons
}
My code in XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:id="#+id/layoutY">
<TextView
android:id="#+id/txtYAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Y AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_blue_light"/>
...
<View android:id="#+id/YAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_blue_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutX"
android:layout_toLeftOf="#id/layoutY"
android:layout_toStartOf="#id/layoutY"
android:layout_marginRight="50dp"
android:layout_marginEnd="50dp">
<TextView
android:id="#+id/txtXAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="X AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_red_light"/>
...
<View android:id="#+id/XAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/layoutZ"
android:layout_toRightOf="#id/layoutY"
android:layout_toEndOf="#id/layoutY"
android:layout_marginLeft="50dp"
android:layout_marginStart="50dp">
<TextView
android:id="#+id/txtZAxis"
android:layout_gravity="center"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:textAlignment="center"
android:text="Z AXIS"
android:textStyle="bold"
android:textSize="18sp"
android:textColor="#android:color/holo_green_light"/>
...
<View android:id="#+id/ZAxis"
android:layout_width="75dp"
android:layout_height="200dp"
android:background="#android:color/holo_green_dark"/>
</LinearLayout>
...
</RelativeLayout>
You might consider constructing your layout like the following.
<?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="wrap_content"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/button_layout"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutY"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtYAxis"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Y AXIS"
android:textColor="#android:color/holo_blue_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/YAxis"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_blue_dark"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutX"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtXAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="X AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_red_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/XAxis"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_red_dark" />
</LinearLayout>
<LinearLayout
android:id="#+id/layoutZ"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|bottom"
android:layout_weight="1"
android:orientation="vertical">
<TextView
android:id="#+id/txtZAxis"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Z AXIS"
android:textAlignment="center"
android:textColor="#android:color/holo_green_light"
android:textSize="18sp"
android:textStyle="bold" />
<View
android:id="#+id/ZAxis"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="30dp"
android:layout_marginRight="30dp"
android:background="#android:color/holo_green_dark" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:id="#+id/button_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Here is the output.
Hope that helps!
I have created a Custom Adapter to show ImageView and TextView together in ListView. But the ImageView is showing on the top of TextView in every row of Listview. I have attached a screenshot of app below:
Custom Adapter Class:
public class CustomeAdapter extends BaseAdapter {
private Context context;
private ListView listView;
private ArrayList<PlayersModel> playersModelArrayList;
public CustomeAdapter(Context context, ArrayList<PlayersModel> playersModelArrayList) {
this.context = context;
this.playersModelArrayList = playersModelArrayList;
}
#Override
public int getViewTypeCount() {
return getCount();
}
#Override
public int getItemViewType(int position) {
return position;
}
#Override
public int getCount() {
return playersModelArrayList.size();
}
#Override
public Object getItem(int position) {
return playersModelArrayList.get(position);
}
#Override
public long getItemId(int position) {
return 0;
}
#Override
public View getView(int position,View convertView,ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.lv_item, null, true);
holder.icon = (TextView) convertView.findViewById(R.id.TextID);
holder.tvname = (TextView) convertView.findViewById(R.id.name);
holder.tvdescr = (TextView) convertView.findViewById(R.id.descr);
holder.tvlink = (TextView) convertView.findViewById(R.id.link);
holder.ImageIcon = (ImageView) convertView.findViewById(R.id.logoImageView);
convertView.setTag(holder);
}else {
holder = (ViewHolder)convertView.getTag();
}
holder.ImageIcon.setImageResource(playersModelArrayList.get(position).getImageId());
holder.icon.setText(playersModelArrayList.get(position).getICON());
holder.tvname.setText(playersModelArrayList.get(position).getName());
holder.tvdescr.setText(playersModelArrayList.get(position).getDescr());
holder.tvlink.setText(playersModelArrayList.get(position).getLink());
Glide.with(context)
.load("http://csalabs.in/wp-content/uploads/2017/10/CSA-Final-PNG-300x350.png")
.override(200,200)
.into(holder.ImageIcon);
Log.d("Icon URL",holder.icon.toString());
return convertView;
}
private class ViewHolder {
protected TextView icon, tvname, tvdescr, tvlink;
protected ImageView ImageIcon;
}
}
Layout File
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="buttonClickNew">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
tools:srcCompat="#tools:sample/avatars[0]" />
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Name" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Country" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="#string/city"
android:visibility="gone"/>
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"/>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent"/>
</LinearLayout>
How to align ImageView on the left side of TextView? Thanks in advance.
BTW I am newbie in android and java.
Try this
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitStart"
tools:srcCompat="#tools:sample/avatars[0]" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="buttonClickNew"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Country"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="city"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:visibility="visible" />
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent" />
</LinearLayout>
Use like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:padding="10dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:onClick="buttonClickNew">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitStart"
android:adjustViewBounds="true"
tools:srcCompat="#tools:sample/avatars[0]" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:onClick="buttonClickNew">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:layout_marginTop="10dp"
android:textSize="15sp"
android:textStyle="bold"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Name" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="Country" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#383838"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"
android:text="#string/city"
android:visibility="gone"/>
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#313131"
android:textSize="12sp"
android:gravity="center_vertical"
android:textAppearance="?android:attr/textAppearanceMedium"
android:paddingLeft="10dp"/>`enter code here`
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginTop="10dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent"/>
</LinearLayout>
Try to wrap Your text to separated LinearLayout with vertical orientation, and than put this LinearLayout with text on same level as ImageView inside another LinearLayout with orientation="horizontal"
But also, you should remember about render and measure performance.
You can read about this in https://android.jlelse.eu/constraint-layout-performance-870e5f238100
<LinearLayout
android:orientation="horizontal">
<ImageView
android:layout_width="0dp"
android:layout_weight="1" // try to read about this parameter more
/>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="4"
android:orientation="vertical">
<TextView ... />
<TextView ... />
<TextView ... />
</LinearLayout>
</LinearLayout>
Try to create two linear layouts with orientation vertical under parent linear layout which has the horizontal orientation.
Set the second linear layout's width to match_parent and the image layout should be a fixed width value.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="buttonClickNew"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent">
<ImageView
android:id="#+id/logoImageView"
android:layout_width="50dp"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:scaleType="fitXY"
tools:srcCompat="#tools:sample/avatars[0]" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Name"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:textSize="15sp"
android:textStyle="bold" />
<TextView
android:id="#+id/descr"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="Country"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
<TextView
android:id="#+id/link"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:text="City"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#383838"
android:visibility="gone" />
<TextView
android:id="#+id/TextID"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:paddingLeft="10dp"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#313131"
android:textSize="12sp" />
</LinearLayout>
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="10dp"
android:layout_marginTop="10dp"
android:layout_marginRight="10dp"
android:background="#color/colorAccent" />
</LinearLayout>
In order to simplify your xml, you should take a look to ConstraintLayout
To use it, make the import in your app gradle by adding implementation 'com.android.support.constraint:constraint-layout:1.1.3'
Then, refactor your xml as below:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/colorAccent" android:id="#+id/view"
tools:layout_editor_absoluteX="0dp" app:layout_constraintTop_toBottomOf="#+id/logoImageView"
android:layout_marginTop="8dp"/>
<ImageView
android:layout_width="64dp"
android:layout_height="64dp" tools:srcCompat="#tools:sample/avatars"
android:id="#+id/logoImageView"
app:layout_constraintStart_toStartOf="parent" android:layout_marginStart="8dp"
android:layout_marginTop="8dp" app:layout_constraintTop_toTopOf="parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:id="#+id/name"
tools:text="Name"
android:textAppearance="#style/TextAppearance.AppCompat" android:textStyle="bold"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toTopOf="#+id/descr" app:layout_constraintStart_toEndOf="#+id/logoImageView"
android:layout_marginStart="8dp"/>
<TextView
tools:text="Description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/descr" android:textColor="#313131"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="#+id/logoImageView"
app:layout_constraintStart_toEndOf="#+id/logoImageView" android:layout_marginStart="8dp"/>
</android.support.constraint.ConstraintLayout>
This question already has answers here:
Dynamically changing the linearlayout width or height on Android
(4 answers)
Closed 4 years ago.
In my application I want set dynamically weight to LinearLayout and for this I write below codes, but when running application show me Force close error!
Force close message in logCat :
java.lang.ClassCastException: android.widget.LinearLayout$LayoutParams cannot be cast to android.widget.RelativeLayout$LayoutParams
My XML codes :
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:background="#color/white"
tools:context=".activity.UpdateActivity">
<!--Header-->
<RelativeLayout
android:id="#+id/dialogEndCount_headerLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size120"
android:background="#color/tabHomeColor">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="#dimen/size40"
android:text="#string/updateDialogTitle"
android:textColor="#color/white"
android:textSize="#dimen/font16" />
<com.airbnb.lottie.LottieAnimationView
android:id="#+id/dialogUpdate_animate"
android:layout_width="#dimen/size150"
android:layout_height="#dimen/size150"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:layout_marginLeft="#dimen/size20"
app:lottie_autoPlay="true"
app:lottie_colorFilter="#color/white"
app:lottie_fileName="update.json"
app:lottie_loop="true" />
</RelativeLayout>
<!--Content-->
<RelativeLayout
android:id="#+id/dialogEndCount_contentLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogEndCount_buttonsLay"
android:layout_below="#+id/dialogEndCount_headerLay"
android:layout_marginBottom="#dimen/size10"
android:layout_marginTop="#dimen/size10"
android:background="#drawable/bg_dialog_vip_white">
<!--Type-->
<TextView
android:id="#+id/dialogUpdate_infoTypeTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_marginLeft="#dimen/size15"
android:layout_marginRight="#dimen/size15"
android:layout_marginTop="#dimen/size10"
android:gravity="center_vertical"
android:text="نوع بروز رسانی : اجباری"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<!--Content-->
<ScrollView
android:id="#+id/dialogUpdate_infoContentScrollLay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/dialogUpdate_downloadProgress"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_below="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size20">
<RelativeLayout
android:id="#+id/dialogUpdate_infoContentLay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/dialogUpdate_infoContentTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:gravity="center_vertical"
android:text="#string/updateContent"
android:textColor="#color/black"
android:textSize="#dimen/font14" />
<TextView
android:id="#+id/dialogUpdate_infoContentTxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/dialogUpdate_infoContentTitle"
android:layout_marginTop="#dimen/size5"
android:gravity="center_vertical"
android:textColor="#color/favColorON"
android:textSize="#dimen/font14" />
</RelativeLayout>
</ScrollView>
<!--Download layout-->
<com.tellfa.colony.view.DownloadProgressView
android:id="#+id/dialogUpdate_downloadProgress"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignRight="#+id/dialogUpdate_infoTypeTxt"
android:layout_marginLeft="#dimen/size15"
android:layout_marginTop="#dimen/size10" />
</RelativeLayout>
<!--Buttons-->
<LinearLayout
android:id="#+id/dialogEndCount_buttonsLay"
android:layout_width="match_parent"
android:layout_height="#dimen/size60"
android:layout_alignParentBottom="true"
android:orientation="horizontal"
android:weightSum="3">
<!--Cancel-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_cancelBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/red"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size3">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_cancelBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/ic_close"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_cancelBtnImg"
android:ellipsize="end"
android:gravity="center"
android:singleLine="true"
android:text="#string/updateCancel"
android:textColor="#color/white"
android:textSize="#dimen/font14" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<!--Direct-->
<android.support.v7.widget.CardView
android:id="#+id/dialogUpdate_directBtn"
android:layout_width="#dimen/margin_0dp"
android:layout_height="#dimen/size50"
android:layout_marginBottom="#dimen/size5"
android:layout_marginLeft="#dimen/size5"
android:layout_marginRight="#dimen/size5"
android:layout_marginTop="#dimen/size5"
android:layout_weight="1"
app:cardBackgroundColor="#color/green"
app:cardCornerRadius="#dimen/size3"
app:cardElevation="#dimen/size2">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/dialogUpdate_directBtnImg"
android:layout_width="#dimen/size25"
android:layout_height="#dimen/size25"
android:layout_centerVertical="true"
android:layout_marginLeft="#dimen/size5"
android:src="#drawable/appro_ic_download_circle"
android:tint="#color/white" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_toRightOf="#+id/dialogUpdate_directBtnImg"
android:ellipsize="end"
android:gravity="center"
android:layout_marginRight="#dimen/size1"
android:singleLine="true"
android:text="#string/updateOK"
android:textColor="#color/white"
android:textSize="#dimen/font12" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</RelativeLayout>
My JAVA codes:
public class UpdateActivity extends BaseActivity {
#BindView(R.id.dialogEndCount_buttonsLay)
LinearLayout dialogEndCount_buttonsLay;
private Window window;
private Activity activity;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_update);
//Initialize
ButterKnife.bind(this);
activity = this;
window = activity.getWindow();
//Set color to statusBar
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
window.setStatusBarColor(getResources().getColor(R.color.tabHomeColor));
}
//Set dynamically weight
dialogEndCount_buttonsLay.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
How can I fix this problem?
If you want set dynamically weightSum just use this code :
dialogEndCount_buttonsLay.setWeightSum(3f);
i hope help you
You're mixing LinearLayout and RelativeLayout. Change your layout to use a LinearLayout then weight is an available property on your LayoutParams.
val params = LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT)
params.weight = 1f