having problem creating ScrollView with TextView inside ImageView - android

I want to create a scrollview including several imageviews that have some textviews inside them. like in the picture.
I tried to do this with constraintlayout and using sdp, but when the size of the screen even slightly changes, it gets messy.
what is the best way to do this?

Use RelativeLayout like this:
<?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"
android:layout_width="200dp"
android:layout_gravity="center"
android:layout_height="200dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/sdf"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginStart="5dp"
android:layout_marginEnd="5dp"
android:layout_marginBottom="5dp"
android:weightSum="3"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="#android:color/white"
android:text="Text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="#android:color/white"
android:text="Text1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textAlignment="center"
android:textColor="#android:color/white"
android:text="Text1"/>
</LinearLayout>
</RelativeLayout>
If you want now to add more of these into your ScrollView do it like this:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_gravity="center"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
//ADD VIEWS HERE
</LinearLayout>
</ScrollView>
You can add them manually or programmatically. The result is this:

Related

/FIXED\ Android - TextView don't show below another TextView

I've got some TextView in a CardView, but when i try to set my TextView "text_lieu" below the TextView "text_nom", nothing happened they are confused. Can you help me please ? <3
Here my xml :
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">
<TextView
android:id="#+id/text_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="start"
android:padding="20dp" />
<TextView
android:id="#+id/text_heure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:padding="20dp" />
<TextView
android:id="#+id/text_lieu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_below="#+id/text_nom"/>
</android.support.v7.widget.CardView>
A CardView extends a FrameLayout basically. That means that views are placed on top of each other, not in a linear fashion like in a LinearLayout. To do this you want to put your TextViews in a LinearLayout inside the CardView:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/text_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:layout_gravity="start"
android:padding="20dp" />
<TextView
android:id="#+id/text_heure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:padding="20dp" />
<TextView
android:id="#+id/text_lieu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"
android:layout_below="#+id/text_nom"/>
</LinearLayout>
</android.support.v7.widget.CardView>
Note the orientation vertical in the linear layout.
If you want to keep the FrameLayout style, an item in it has the property android:layout_gravity which you can set to bottom to place an item in the bottom.
android:layout_below is only effective in a RelativeLayout.
You can put your views inside a RelativeLayout like this:
<?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:layout_margin="10dp"
card_view:cardCornerRadius="10dp"
card_view:cardElevation="10dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/text_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:padding="20dp"
android:textStyle="bold" />
<TextView
android:id="#+id/text_heure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:padding="20dp" />
<TextView
android:id="#+id/text_lieu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/text_nom"
android:padding="20dp" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Edit:
if (myTextView3.getText().toString().trim().isEmpty())
myTextView3.setVisibility(View.GONE);
else
myTextView3.setVisibility(View.VISIBLE);
Wrap the textviews in a LinearLayout. You can adjust them they are are, you dont need to do the layout_below. LinearLayout will take care of that for you.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
card_view:cardCornerRadius="10dp"
android:orientation="vertical"
card_view:cardElevation="10dp"
android:layout_margin="10dp">
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_nom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:padding="20dp" />
<TextView
android:id="#+id/text_lieu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp"/>
<TextView
android:id="#+id/text_heure"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="20dp" />
</LinearLayout>
</android.support.v7.widget.CardView>

Horizontal LinearLayout not showing any elements after nested Vertical LinearLayout

Following the Udacity Android for Beginners course, I want to add an audio icon button to a list_item view. Now the list_item.xml has a parent Horizontal Linear Layout and a nested Vertical Linear Layout. The course instructor added the audio icon by changing to a Relative Layout but i want to see how I could do this with a Linear Layout.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/tan_background"
android:minHeight="#dimen/list_item_height"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="#dimen/list_item_height"
android:layout_height="#dimen/list_item_height"/>
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold"
tools:text=""/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textColor="#android:color/white"
tools:text=""/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/baseline_play_arrow_white_24"/>
</LinearLayout>
My problem now is that when i add any element after the nested Vertical Linear Layout, nothing will show. I'm trying different things but I just can't understand. All i can think of is that this is the cause:
android:layout_width:"match_parent"
Please let me know how i can add the audio icon to the right and in the center while keeping this a LinearLayout.
This is the output i am trying to achieve
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:background="#color/apptool"
android:orientation="horizontal">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:src="#android:drawable/btn_star"
android:tint="#color/black"
android:background="#ebed54"
android:layout_weight=".1"/>
<LinearLayout
android:id="#+id/text_container"
android:layout_width="0dp"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#color/colorPrimary"
android:layout_weight=".3"
android:paddingLeft="16dp">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold"
tools:text="sdfdsfdsf"/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textColor="#android:color/white"
tools:text="sdfdsfdsf"/>
</LinearLayout>
<ImageView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".1"
android:background="#color/colorPrimary"
android:src="#android:drawable/ic_media_play"/>
</LinearLayout>
</LinearLayout>
output
You make width of linear layout match_parent so it consumes all remain place so you need to first change match_parent to wrap_content like below :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/tan_background"
android:minHeight="#dimen/list_item_height"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="#dimen/list_item_height"
android:layout_height="#dimen/list_item_height"/>
<LinearLayout
android:id="#+id/text_container"
android:layout_width="wrap_conten"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="16dp">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="bottom"
android:textAppearance="?android:textAppearanceMedium"
android:textColor="#android:color/white"
android:textStyle="bold"
tools:text=""/>
<TextView
android:id="#+id/textView2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="top"
android:textColor="#android:color/white"
tools:text=""/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/baseline_play_arrow_white_24"/>
</LinearLayout>

How to create correctly the following design

So the design that i want is similar to this
enter image description here
What i have done so far is 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="horizontal"
android:paddingTop="20dp"
android:paddingStart="20dp"
android:paddingEnd="20dp">
<ImageView
android:layout_width="120dp"
android:layout_height="180dp" />
<LinearLayout
android:layout_width="200dp"
android:layout_height="180dp"
android:orientation="horizontal"
android:background="#FFF"
android:layout_marginStart="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Article Title"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:layout_marginStart="10dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:layout_gravity="end"
android:gravity="end"
android:text="10/11/2017"
android:paddingTop="10dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TEXT"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
I don't undesrtand how can i make the same ui usingLinearLayout.
I'm using LinearLayout so that i can use it with recyclerView later on, this design will be a list, can some one tell me what am i doing wrong?
Anyone have a clue about this ?
You don't have to use LinearLayout for RecyclerView items. You can use any layout for that.

place a linearLayout in center of a wrap_content relativeLayout

I have a listView, and view of each item of the list should be like this (a background image and group name) :
but when i set layout_centerInParent="true" to linearLayout, it placed in center of screen(not in center of image).like this :
and there is my xml :
<?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="wrap_content"
android:background="#444">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/test"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/linear_category_name"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:paddingRight="16dp"
android:paddingLeft="16dp">
<ir.dbasoft.test.myobject.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="18sp"
android:text="Gold"/>
</LinearLayout>
</RelativeLayout>
How can i place linearLayout in center of relaytiveLayout ??
For your scenario, I think it's a lot easier to use FrameLayout instead.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/gold_image"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#drawable/white_border"
android:orientation="vertical"
android:padding="8dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gold"
android:textColor="#android:color/white"
android:textSize="18sp"/>
</LinearLayout>
</FrameLayout>
I've tested this and it looked exactly like what you're trying to achieve.
Try 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="wrap_content"
android:background="#444">
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/test/>
<ir.dbasoft.test.myobject.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="18sp"
android:text="Gold"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
Try this
<ImageView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:layout_centerInParent="true"
android:src="#drawable/test"/>
<ir.dbasoft.test.myobject.MyTextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="18sp"
android:layout_centerInParent="true"
android:text="Gold"/>

How to correctly update my layout (based weight attribute) with cardview?

I am working to update my app with CardCiew widget, I already update some simple layout.
The problem that when I use a LinearLayout set with layout_weight attribute I get a different result I want.
layout.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"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/list_selector"
android:orientation="horizontal"
android:id="#+id/technologie_ll_item">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_weight="10"
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/iv_logo_technologie"
android:layout_width="75dp"
android:layout_height="75dp"
app:border_width="2dp"
app:border_color="#99069d78"
android:src="#drawable/after"
android:paddingLeft="0dp" />
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="5"
android:layout_height="fill_parent"
android:gravity="center_vertical"
android:orientation="vertical">
<TextView
android:id="#+id/tv_nom_technologie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textAllCaps="true"
android:paddingLeft="10dp"
android:gravity="center_vertical"
android:text="Orthodontics"/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/indic" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
Result
Maybe this can help you.
Approach 1
(Note i have changed your CircleImageView with a Imageview so i could make it works without errors ;))
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/technologie_ll_item">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:weightSum="10"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2">
<ImageView
android:id="#+id/iv_logo_technologie"
android:layout_width="75dp"
android:layout_height="75dp"
android:paddingLeft="0dp" />
</LinearLayout>
<TextView
android:id="#+id/tv_nom_technologie"
android:layout_width="0dp"
android:layout_weight="7"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textAllCaps="true"
android:paddingLeft="10dp"
android:layout_gravity="center_vertical"
android:text="Orthodontics" />
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="fill_parent"
android:gravity="right"
android:orientation="horizontal">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</android.support.v7.widget.CardView>
As you can see you will have 1 LinearLayout which android:weightSum is 10 (just to make the proportion better) that holds the 3 components. The first LinearLayout container that holds the CircleImageView (aka ImageView in my code) will have a android:layout_weight of 2, the text will have a android:layout_weight of 7, and the arrow container will have a android:layout_weight of 1.
Maybe you can get rid of the LinearLayout that holds the arrow, and set the weight directly in the ImageView, i think that if you apply the arrow as android:src it will be resized respecting the scale of the image given, but I am not sure about this now.
Approach 2:
Another solution will be the RelativeLayout, as the code below:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/technologie_ll_item">
<android.support.v7.widget.CardView
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<!-- CircleImageView here -->
<ImageView
android:id="#+id/iv_logo_technologie"
android:layout_width="75dp"
android:layout_height="75dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:paddingLeft="0dp" />
<!-- Arrow drawable here -->
<ImageView
android:id="#+id/arrow"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<!-- Text here, between circle and arrow -->
<TextView
android:id="#+id/tv_nom_technologie"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_toRightOf="#id/iv_logo_technologie"
android:layout_toLeftOf="#id/arrow"
android:textAllCaps="true"
android:paddingLeft="10dp"
android:layout_centerVertical="true"
android:text="Orthodontics" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
With this you remove the LinearLayout and you will have only 3 components, maybe better for perfomance.
Hope some of this will help you mate.
Regards!

Categories

Resources