I have a fragment containing a LinearLayout containing a webview, when a change the html string in my webview, sometimes the background of my LinearLayout doesn't appear.
My fragment:
`
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/bg_item_single"
android:orientation="vertical"
android:padding="#dimen/list_margin" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
</LinearLayout>
</LinearLayout>
</ScrollView>
`
If I have a linearLayout in a other linearLayout is just to have a space on the bottom of my scrollView, isn't the problem.
My code:
`
private void populate() {
textView.setText(grant.getName());
// Configure w`enter code here`ebview
webview.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webview.getSettings().setJavaScriptEnabled(false);
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
webview.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return (event.getAction() == MotionEvent.ACTION_MOVE);
}
});
webview.loadDataWithBaseURL("file:///android_asset/", createFullHtmlPage("styles.css", grant.getGrantDescription(), 0), "text/html", "UTF-8", null);
}
`
I don't see were is the problem.
Try setting android:layout_height="wrap_content", and remove the transparent background of this view.
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/transparent" />
I fixed my problem. I used a shape as background of my LinearLayout, but if the webview is too big, for unknown reason, the shape don't work and don't appear. Therefore now I use a color background and 2 imageview for creat a bottom corner and top corner.
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarStyle="outsideOverlay"
tools:context=".NewsFragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingBottom="50dp" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_top" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/bg_item_default"
android:orientation="vertical" >
<TextView
android:id="#+id/text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textColor="#000"
android:textSize="20sp" />
<WebView
android:id="#+id/webview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/list_margin" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/bg_item_bottom" />
</LinearLayout>
</LinearLayout>
</ScrollView>
How I undestud problem is in this line
webview.setBackgroundColor(Color.argb(1, 0, 0, 0));
try this
webview.setBackgroundResource(color.blue);
Related
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
I am confused because in my RecyclerView, some items has their height = 0 (even when they are correctly displayed and visible) and some have their normal height.
How is that possible ?
If I monitor the height with addOnGlobalLayoutListener, 90% of item has their correct height calculated, and 10% still have 0.
Any idea of how android works for this ?
My code is in Kotlin but it doesn’t matter.
init {
var heightItemView = 0
var currentY = itemView.y
var oldY = currentY
itemView.viewTreeObserver.addOnGlobalLayoutListener(object : ViewTreeObserver.OnGlobalLayoutListener {
override fun onGlobalLayout() {
if (itemView.height > 0) {
heightItemView = itemView.height
itemView.viewTreeObserver.removeOnGlobalLayoutListener(this)
}
I also tried with measuredHeight().
My onCreateViewHolder() from the adapter:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): PairMarketAdapter.ViewHolder {
val v = android.view.LayoutInflater.from(parent.context)
.inflate(R.layout.item_pair_market, parent, false)
val holder = PairMarketAdapter.ViewHolder(v)
holder.itemView.setOnClickListener {
listener.onItemAction(holder.adapterPosition)
}
return holder
}
XML of the fragment:
<androidx.coordinatorlayout.widget.CoordinatorLayout
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:id="#+id/lCoordinatorRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rvPairMarket"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="#dimen/spacing_normal"
tools:listitem="#layout/item_pair_market"
/>
</LinearLayout>
And the item XML:
<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:background="?selectableItemBackground"
android:paddingBottom="#dimen/spacing_normal"
android:paddingTop="#dimen/spacing_small"
android:paddingEnd="#dimen/spacing_normal"
android:paddingStart="#dimen/spacing_normal"
android:orientation="horizontal"
>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_small"
android:layout_weight="1.5"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<TextView
android:id="#+id/tvSymbol"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="USD"
/>
<TextView
android:id="#+id/tvSymbolBase"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/spacing_tiny"
android:maxLines="1"
android:textAllCaps="true"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_large"
android:textStyle="bold"
tools:text="BTC"
/>
<View
android:id="#+id/vIsSelected"
android:layout_width="10dp"
android:layout_height="10dp"
android:layout_gravity="center"
android:layout_marginStart="#dimen/spacing_small"
android:background="#drawable/circle_green_dot"
android:visibility="gone"
/>
</LinearLayout>
<TextView
android:id="#+id/tvExchange"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/spacing_tiny"
android:maxLines="1"
android:textColor="?android:attr/textColorSecondary"
android:textSize="#dimen/font_normal"
tools:text="1. Bitfinex"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:orientation="vertical"
>
<TextView
android:id="#+id/tvPercent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:textColor="?android:attr/textColorPrimary"
android:textSize="#dimen/font_large"
tools:text="55.5%"
/>
</LinearLayout>
</LinearLayout>
itemView.measuredHeight android will try to measure your view before display try using itemView.height inside addOnGlobalLayoutListener
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
this is my project:
when I touch one picture in each row I want something like this:
This is what I have in my 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"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="android.goparty.rayoruiz.gopartyandroid.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="#string/choises_title"
android:id="#+id/choises_title"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<ScrollView
android:layout_width="500px"
android:layout_height="600px"
android:id="#+id/scrollView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_below="#+id/choises_title">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="109dp"
android:weightSum="1"
android:background="#mipmap/bottomlist"
android:paddingTop="10dp"
android:nestedScrollingEnabled="false"
android:paddingBottom="10dp"
android:layout_marginTop="10px">
<ImageButton
android:layout_width="0dip"
android:layout_height="109dp"
android:id="#+id/houseParty"
android:layout_weight="0.40"
android:background="#mipmap/houseicon"
android:layout_gravity="left"
android:layout_marginRight="#android:dimen/app_icon_size"
android:layout_marginLeft="#android:dimen/app_icon_size" />
<ImageButton
android:layout_width="0dip"
android:layout_height="109dp"
android:layout_weight="0.40"
android:id="#+id/clubParty"
android:layout_gravity="right"
android:background="#mipmap/nightclubsicon" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent"
android:minHeight="109dp"
android:weightSum="1"
android:background="#mipmap/bottomlist"
android:paddingTop="10dp"
android:nestedScrollingEnabled="false"
android:paddingBottom="10dp"
android:layout_marginTop="10px">
<ImageButton
android:layout_width="0dip"
android:layout_height="109dp"
android:id="#+id/drink"
android:layout_weight="0.40"
android:background="#mipmap/drink"
android:layout_gravity="left"
android:layout_marginRight="#android:dimen/app_icon_size"
android:layout_marginLeft="#android:dimen/app_icon_size" />
<ImageButton
android:layout_width="0dip"
android:layout_height="109dp"
android:layout_weight="0.40"
android:id="#+id/nodrink"
android:layout_gravity="right"
android:background="#mipmap/nodrink" />
</TableRow>
</TableLayout>
</ScrollView>
ok, I have the opacity setting alpha attribute.
public void onClick(View v){
int[] array = new int[2];
houseParty.getLocationOnScreen(array);
**houseParty.setAlpha(new Float(0.50));**
}
How do I get the picture of the button to change on click?
You could swap the image of the ImageButtons when you detect a click using setOnClickListener. Here is a possible implementation:
ImageButton houseParty = (ImageButton) findViewById(R.id.houseParty);
houseParty.setTag(0); // We associate 0 with not pressed
houseParty.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Change image.
if(houseParty.getTag()==0){
houseParty.setImageDrawable(getResources().getDrawable(
R.drawable.house_party_checked));
houseParty.setTag(1); // Now button is pressed
} else {
houseParty.setImageDrawable(getResources().getDrawable(
R.drawable.house_party));
houseParty.setTag(0); // Now button is not pressed
}
}
});
By default, in my current layout, there are two TextViews at the top and one ImageView at the bottom. I would like to change this by having the ImageView to be at the top and the TextViews be at the bottom using Java. Please help.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/text_view"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_weight="1"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:background="#drawable/text_view"
android:textSize="13dp"
android:textStyle="bold"
/>
</LinearLayout>
</FrameLayout>
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/text_view"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
</LinearLayout>
Here is what i have done :i have added 2 more frame layout in xml and
when u click the first text view it will change the view i mean the
above frame layout and will show next two.Hope this helps u
public class MainActivity extends Activity
{
LinearLayout ll;
TextView tv;
FrameLayout ff2, ff1, ff3, ff4;
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ff1 = (FrameLayout) findViewById(R.id.lay1);
ff2 = (FrameLayout) findViewById(R.id.lay2);
ff3 = (FrameLayout) findViewById(R.id.lay3);
ff4 = (FrameLayout) findViewById(R.id.lay4);
ff3.setVisibility(View.GONE);
ff4.setVisibility(View.GONE);
tv = (TextView) findViewById(R.id.tvText);
tv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
ff1.setVisibility(View.GONE);
ff2.setVisibility(View.GONE);
ff3.setVisibility(View.VISIBLE);
ff4.setVisibility(View.VISIBLE);
Toast.makeText(getApplicationContext(), "layout change",
Toast.LENGTH_LONG).show();
}
});
}
}
Here is the layout file :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="2" >
<FrameLayout
android:id="#+id/lay1"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay2"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay3"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/ivImage"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#drawable/ic_launcher"
android:scaleType="fitXY" />
</LinearLayout>
</FrameLayout>
<FrameLayout
android:id="#+id/lay4"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:weightSum="2" >
<TextView
android:id="#+id/tvText"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
<TextView
android:id="#+id/tvText2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/weed2"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textSize="13dp"
android:textStyle="bold" />
</LinearLayout>
</FrameLayout>
</LinearLayout>