Change Order Of FrameLayout With Java Code - java

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>

Related

CardView onClickListener to go to another intent

I'm trying to write code code such that when clicked on any cardview, it moves to another activity screen. Here is my code for the xml and java part. When the app is running, the cardview can be displayed but, the new activity is not opening.
This the code for XML.
<?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"
android:background="#drawable/homepage"
android:gravity="center"
android:id="#+id/LinearLayout1">
<!-- A CardView that contains a TextView -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="200dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/mypet"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/petlogo100"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="My Pet"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Information about your Pet"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/health"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/pethealth"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="Track Health"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow the health of your Pet"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:clipToPadding="false"
android:gravity="center"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:id="#+id/vetVisit"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/vetvisit2"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:text="Visit Vet"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Details about the medical schedule"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:id="#+id/overall"
android:foreground="?android:attr/selectableItemBackground"
android:clickable="true"
android:layout_width="160dp"
android:layout_height="190dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="64dp"
android:layout_height="64dp"
android:src="#drawable/overall_health"
android:padding="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_marginTop="10dp"
android:text="Track Health"/>
<View
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#color/lightgray"
android:layout_margin="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Follow the health of your Pet"
android:padding="5dp"
android:textColor="#android:color/darker_gray"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
This the code for JAVA.
package com.example.healthypet;
import androidx.appcompat.app.AppCompatActivity;
import androidx.cardview.widget.CardView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
private CardView petCard, healthCard,vetCard,overallCard;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
petCard = (CardView) findViewById(R.id.mypet);
healthCard = (CardView) findViewById(R.id.health);
vetCard = (CardView) findViewById(R.id.vetVisit);
overallCard = (CardView) findViewById(R.id.overall);
}
#Override
public void onClick(View v) {
Intent i;
switch (v.getId()){
case R.id.mypet:
i= new Intent(this,MyPetPage.class);
startActivity(i);
break;
case R.id.health:
i= new Intent(this,PetHealth.class);
startActivity(i);
break;
case R.id.vetVisit:
i= new Intent(this,VetPage.class);
startActivity(i);
break;
case R.id.overall:
i= new Intent(this,overall.class);
startActivity(i);
break;
default:break;
}
}
}

Why my pages doesn't exist when i click the button? [duplicate]

This question already has an answer here:
Cardview Click is not opening Activty
(1 answer)
Closed 3 years ago.
My problems :
My apps doesn't have error.
When I click at the button, my page isn't display.
It can exist when I click at the certain place not at the button.
I already do refer by youtube, but myself cannot resolve this problem.
This is my java file :
dashboard.java
package com.example.whatisthat;
import androidx.cardview.widget.CardView;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
public class Dashboard extends Activity implements View.OnClickListener
{
private CardView LoginBtn, CaptureBtn, AboutBtn, FeedbackBtn, IgBtn, HelpBtn;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_dashboard);
//DEFINE CARDS
//ADD CLICK LISTENER TO THE CARDS
LoginBtn = findViewById(R.id.Login);
LoginBtn.setOnClickListener(this);
CaptureBtn = findViewById(R.id.Capture);
CaptureBtn.setOnClickListener(this);
AboutBtn = findViewById(R.id.About);
AboutBtn.setOnClickListener(this);
FeedbackBtn = findViewById(R.id.Feedback);
FeedbackBtn.setOnClickListener(this);
IgBtn = findViewById(R.id.Ig);
IgBtn.setOnClickListener(this);
HelpBtn = findViewById(R.id.Help);
HelpBtn.setOnClickListener(this);
}
#Override
public void onClick(View view)
{
Intent i;
switch (view.getId())
{
case R.id.Login : i = new Intent(this,Login1.class);
startActivity(i);
break;
case R.id.Capture : i = new Intent(this, Capture.class);
startActivity(i);
break;
case R.id.About : i = new Intent(this, About.class);
startActivity(i);
break;
case R.id.Feedback : i = new Intent(this, Feedback.class);
startActivity(i);
break;
case R.id.Ig : i = new Intent(this, Instagram.class);
startActivity(i);
break;
case R.id.Help : i = new Intent(this, Help.class);
startActivity(i);
break;
default:break;
}
}
}
This is my xml file :
activity_dashboard.xml
<?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"
tools:context=".Dashboard">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="230dp"
android:orientation="vertical"
android:gravity="center"
android:background="#6A287E">
<ImageView
android:layout_width="90dp"
android:layout_height="90dp"
android:src="#drawable/iconfyp"
android:layout_gravity="center"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="DASHBOARD"
android:textColor="#color/colorAccent"
android:layout_gravity="center"
android:textStyle="bold"
android:textSize="18sp"
android:layout_marginTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="215dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/Login"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView1"
android:layout_height="50dp"
android:src="#drawable/people"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOGIN"
android:textSize="16sp"
android:layout_below="#+id/imageView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/Capture"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView2"
android:layout_height="50dp"
android:src="#drawable/cameraa"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CAPTURE"
android:textSize="16sp"
android:layout_below="#+id/imageView2"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/About"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView3"
android:layout_height="50dp"
android:src="#drawable/aboutt"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ABOUT"
android:textSize="16sp"
android:layout_below="#+id/imageView3"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/Feedback"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView4"
android:layout_height="50dp"
android:src="#drawable/feedback"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FEEDBACK "
android:textSize="16sp"
android:layout_below="#+id/imageView4"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="135dp"
android:orientation="horizontal"
android:layout_marginBottom="10dp">
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/Ig"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginRight="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView5"
android:layout_height="50dp"
android:src="#drawable/instagram"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="INSTAGRAM"
android:textSize="16sp"
android:layout_below="#+id/imageView5"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true"
android:clickable="true”
android:id="#+id/Help"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="50dp"
android:id="#+id/imageView6"
android:layout_height="50dp"
android:src="#drawable/help"
android:layout_centerInParent="true"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="HELP"
android:textSize="16sp"
android:layout_below="#+id/imageView6"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"/>
<Button
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/transparent"/>
</RelativeLayout>
</androidx.cardview.widget.CardView>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
add android:clickable="true" , android:focusable="true" and android:foreground="?android:attr/selectableItemBackground"
<androidx.cardview.widget.CardView
android:foreground="?android:attr/selectableItemBackground"
android:focusable="true
android:clickable="true"
android:id="#+id/Feedback"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:gravity="center"
android:layout_marginLeft="5dp"
android:orientation="vertical">
// your code
</androidx.cardview.widget.CardView>

How to push recyclerview below (behind) AppbarLayout on scroll collapsed

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

How to align ImageView and TextView in Custom ListView

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>

layout weight not working CardView

I want my screen to show a total of 3 cards distributed evenly taking up the whole space but nothing I try seems to be working. The cards are being loaded with the use of a RecyclerView/viewHolder.
I've tried two solutions.
Solution 1:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="3"
>
<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:id="#+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
and Solution 2:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/card_view_disciplines"
card_view:cardCornerRadius="4dp"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:weightSum="3"
>
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/disciplineName"
android:textSize="25sp"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
can someone please help?
Thanks in advance!
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:weight_sum="3"
android:padding="16dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 1"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:padding="16dp"
android:background="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 2"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
app:cardBackgroundColor="#000">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="40sp"
android:text="Card 3"
android:textColor="#FFF"
android:layout_gravity="center"/>
</android.support.v7.widget.CardView>
</LinearLayout>
I hope this will work as you expected:
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#888888"
android:orientation="vertical"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cardView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#000088"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#008800"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:id="#+id/cardView3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_margin="2dp"
android:layout_weight="1"
card_view:cardBackgroundColor="#880000"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="5dp">
</android.support.v7.widget.CardView>
</LinearLayout>
MainActivity.java
CardView cardView1 = (CardView) findViewById(R.id.cardView1);
CardView cardView2 = (CardView) findViewById(R.id.cardView2);
CardView cardView3 = (CardView) findViewById(R.id.cardView3);
cardView1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView1", Toast.LENGTH_SHORT).show();
}
});
cardView2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView2", Toast.LENGTH_SHORT).show();
}
});
cardView3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(MainActivity.this, "CardView3", Toast.LENGTH_SHORT).show();
}
});

Categories