How to create 3 generic card views? - android

I'm trying to create 3 cardviews, each have the same height, weight etc..
I managed to create one:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/card_view"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_margin="8dp"
android:padding="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_alignParentTop="true"
android:scaleType="centerInside"
android:src="#drawable/moon20"
android:background="#android:color/white"
android:padding="8dp"/>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:maxLines="3"
android:padding="8dp"
android:text="20 min Power Nap"
android:textColor="#color/colorSecondaryText"
android:textStyle="bold"
android:textSize="20dp"
android:textAlignment="center"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
I need two more of this cardview, but I'm getting
"multiple root tag" error
Do I have to create a base layout like Relative layout for all cardviews?

You need a root view to put inside your CardViews. Right now you are putting one CardView inside the other (Because your root is a CardView). Try to put them inside a LinearLayout.
<LinearLayout>
<CardView>
<CardView>
<CardView>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android"
>
<CardView>
<CardView>
<CardView>
</LinearLayout>

Try to make your cardView element inside a LinearLayout or some other Layout views.
example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android" .......

Related

Cannot see textview

I am trying to make a layout with this code :
<?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:card_view="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="12dp"
android:layout_marginRight="12dp"
>
<android.support.v7.widget.CardView
app:cardCornerRadius="#dimen/cardview_corner_radius"
app:cardElevation="2dp"
card_view:cardUseCompatPadding="true"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:cardBackgroundColor="#f27181"
android:id="#+id/charity_overview_card"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<com.sharesmile.share.views.LBTextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#color/white"
android:padding="9dp"
android:text="Women Empowerment"
android:id="#+id/iv_charity_category_title"
/>
<ImageView
android:background="#color/colorPrimaryDark"
android:layout_width="128dp"
android:layout_height="126dp"
android:id="#+id/iv_charity_overview"
/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<com.sharesmile.share.views.LBTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_charity_amount"
android:textColor="#color/black"
android:textSize="15sp"
android:text="Test"
/>
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
but the last textview is not visible, even in the preview it is not showing, it shows like this :
As you can see the textview is not visible, i haven't given any weight, still facing this issue.
Also when i increase the size of the textview the value is visible as you can see here :
LinearLayout inside other parent LinearLayout with dynamic size will produce this type of issues. You should avoid nesting layout inside layout as much as possibile.
This issue can be simply fixed by removing the parent layout orientation.
For improvement
<CardView>
<LinearLayout> vertical
//Add 'N' child items here
</LinearLayout>
</CardView>
Change the following properties of your main LinearLayout, Make height and width to match_parent
android:layout_width="match_parent"
android:layout_height="match_parent"

Why TextView intersects with ScrollView?

Why my TextView intersects with ScrollView at the right side of Activity and how to fix it?
This is screenshot of my Activity.
This is code XML of my Activity.
<?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:layout_marginBottom="16dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:orientation="vertical">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/rules"
android:textSize="#dimen/text_font_tall"
android:textStyle="bold|italic" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="start"
android:text="#string/rules_text"
android:textSize="#dimen/text_font_medium" />
</LinearLayout>
</ScrollView>
</LinearLayout>
Use margin for the second LinearLayout the same way as you used for the first LinearLayout.
android:layout_margin="16dp"
BTW: as long as the parameters are the same for all of the margins (layout_marginTop, layout_marginBottom, layout_marginLeft and layout_marginRight) you can simply use only one attribute: layout_margin
BTW2: Do you really need the parent LinearLayout? you can try it like that:
<ScrollView>
<LinearLayout>
<TextView/>
<TextView/>
</LinearLayout>
</ScrollView>

assingning scrollbars to a layout which is has two child layout

The XML file for my activity has ScrollView as rootview . it has two relative layout as child . this is giving me error .
I want to keep two relative layout as child view in ScrollView of this XML.
Here is my XML file:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg_about">
<TextView
android:id="#+id/textAboutUs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="clip_vertical"
android:text="#string/about_us"
android:textColor="#ffffff" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativeLayout"
android:layout_weight="0.96"
android:background="#E8E9EA" />
</RelativeLayout>
</ScrollView>
I want it to be scroll-able either the whole layout or just the second layout.
Wrap one of your layouts in a ScrollView (or HorizontalScrollView, if you want it that way) or if you want the ScrollView at the root, place it at the start of your code. Either way, you have to ensure that there is only ONE child element for the ScrollView (this child element can have many child elements) - below is an example of the whole thing being scrollable:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1">
<RelativeLayout
android:id="#+id/relativeLayout"
android:layout_width="match_parent"
android:layout_height="150dp"
android:background="#drawable/bg_about">
<TextView
android:id="#+id/textAboutUs"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:gravity="clip_vertical"
android:text="#string/about_us"
android:textColor="#ffffff" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>

Can we use cardView in Relative layout in Android Studio?

I'm using a simple card View in Relative Layout.
Can i use it in Relative Layout ? If yes then How ?
If you use CardView inside RelativeLayout like the following :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/black"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
....>
<....>//Imagine a TextView here
<....>
</android.support.v7.widget.CardView>
</RelativeLayout>
then you will not be able to arrrage the elements inside the cardview in a right way.
In short imagine CardView as a Holder, you will have to use a layout inside CardView to make it work in a correct way
for example:
<android.support.v7.widget.CardView
....>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#android:color/black"
android:layout_height="match_parent">
<....>//Imagine a TextView here
<....>
</RelativeLayout>
</android.support.v7.widget.CardView>
Yes we can Use please follow the code:
<!-- About Panel -->
<android.support.v7.widget.CardView
android:id="#+id/cv_About"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cv_Help"
android:layout_gravity="center"
android:elevation="3dp"
app:cardCornerRadius="0dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="#dimen/pad_20dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:text="#string/about"
android:textColor="#color/dimblack"
android:id="#+id/tvAbout"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
Cheers!!

Using ScrollView with LinearLayout, Android

In my xml file , i have one linearLayout and a button. The linearLayout contains a lot of textViews and checkboxes. So i wanted these textViews and checkboxes to scroll but the button should remain at its place i.e it should not scroll with the textViews. For this purpose, i cover my linearLayout with scrollView like this
<?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:background="#drawable/homeo11" >
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/nexttodetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
</LinearLayout>
</ScrollView>>
<Button android:id="#+id/Next4"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Next" />
</LinearLayout>
But doing this , hid my button. means the textviews and checkboxes are now scrolling properly but i can't see my next button. I try replacing the scrollView layout:height from fill_parent to wrap_content but this didn't work as well. Any help?
Use a RelativeLayout as your root View and then set your component with align_above, align_parent_top and align_parent_bottom like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/homeo11" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_above="#+id/Next4" >
<LinearLayout
android:id="#+id/nexttodetail"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="10dp">
(...)
</LinearLayout>
</ScrollView>>
<Button
android:id="#+id/Next4"
android:layout_gravity="right"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_marginTop="10dp"
android:text="Next" />
</RelativeLayout>

Categories

Resources