Why the background image of Linearlayout not showing in Android? - android

I have some layout question for Android , the following code is my 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:orientation="vertical"
tools:context="com.wen.test.MainActivity">
<LinearLayout
android:id="#+id/dashboard_layout"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/dashboard_item1_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#drawable/no_signal">
</RelativeLayout>
<RelativeLayout
android:id="#+id/dashboard_item2_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp">
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_weight="0.7"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
It seems like the following picture:
Question:
Why the background of dashboard_item1_layout and dashboard_item2_layout did not show via following code?
android:background="#drawable/no_signal"
But I can set the background image to the dashboard_layout.
Did I missing something? Thanks in advance.

I do nothing except adding background for dashboard_item1_layout and dashboard_item2_layout as you expected.
<?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">
<LinearLayout
android:id="#+id/dashboard_layout"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/dashboard_item1_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#drawable/like_outline">
</RelativeLayout>
<RelativeLayout
android:id="#+id/dashboard_item2_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:background="#drawable/like_star_outline"
android:layout_height="match_parent"
android:layout_margin="5dp">
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_weight="0.7"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>
The result:

set background in your code is ok but the problem seems like in no_signal.xml file. Can you show code this file?

Check this out on your phone. I just added background as expected in your code. Also, replace background image with the native one. If it is run then issue with your asset only.
<?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"
>
<LinearLayout
android:id="#+id/dashboard_layout"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="0dp"
android:background="#color/color_red_fab"
android:orientation="horizontal">
<RelativeLayout
android:id="#+id/dashboard_item1_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:background="#android:drawable/ic_menu_close_clear_cancel">
</RelativeLayout>
<RelativeLayout
android:id="#+id/dashboard_item2_layout"
android:layout_weight="0.5"
android:layout_width="0dp"
android:layout_height="match_parent"
android:background="#android:drawable/ic_menu_close_clear_cancel">
android:layout_margin="5dp">
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:layout_weight="0.7"
android:layout_width="match_parent"
android:layout_height="0dp">
<ImageView
android:src="#android:drawable/ic_menu_close_clear_cancel"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>
</LinearLayout>

Related

How can I solve this problem while creating recyclerview?

i'm trying to create Recyclerview+ Cardview with GridLayout
but Android Studio show me an error,
i need to know what is the wrong here
A picture showing the error,
the code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="190dp"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_margin="5dp"
cardview:cardcornerRadius="4dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/book_img_id"
android:layout_width="match_parent"
android:layout_height="160dp"
android:scaleType="centerCrop"
android:background="#2d2d2d"/>
<TextView
android:id="#+id/book_title_id"
android:textColor="#2d2d2d"
android:textSize="13sp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="سكر"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
Use app:cardCornerRadius="4dp" instead of cardview:cardCornerRadius="4dp"
cardCornerRadius attribute is under app
Try code like:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="120dp"
android:layout_height="190dp"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:cardCornerRadius="4dp">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="160dp"
android:src="#mipmap/ic_launcher_round"/>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:text="Android"/>
</LinearLayout>
</androidx.cardview.widget.CardView>
I hope it helps you

Scrollview not working for Relative Layout

I am trying to populate a Relative layout with 4 card views in it. Each cardview consists one image and a text view. I wrapped all the card views in a relative layout and the relative layout in a scroll view. It's not working. If I remove scrollview, its working fine. Tried by putting scrollview inside the relative layout. But didnt work
Code:
<?xml version="1.0" encoding="utf-8"?>
<Scrollview
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#659D32"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:paddingTop="16dp"
tools:context="com.example.MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:id="#+id/cardview_1"
app:cardElevation="10dp"
app:cardMaxElevation="15dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="#drawable/img_1"
android:id="#+id/cardImage_bang"
android:scaleType="centerCrop"/>
<TextView
android:layout_below="#+id/cardImage_1"
android:id="#+id/cardTitle_1"
android:gravity="center_horizontal"
android:text="Text1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:layout_below="#+id/cardview_1"
android:id="#+id/cardview_2"
app:cardElevation="10dp"
android:layout_marginTop="10dp"
app:cardMaxElevation="15dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="#drawable/img_2"
android:id="#+id/cardImage_2"
android:scaleType="centerCrop"/>
<TextView
android:layout_below="#+id/cardImage_2"
android:id="#+id/cardTitle_2"
android:gravity="center_horizontal"
android:text="Text2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
Hope this will help you.. (this snippet worked in my case)
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true"
android:paddingLeft="5dp"
android:background="#color/bg"
android:paddingRight="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:padding="10dp"
android:weightSum="1">
<!-- put entire thing here -->
</RelativeLayout>
</ScrollView>
you have written the scroll view tag wrong. That's the reason its not working. Change it from <Scrollview> to <ScrollView> and add the Scroll view end tag like </ScrollView>and re run the project, it will work fine.
Hope this will help you !
1) You've misspelled the ScrollView tag it must be <ScrollView> not <Scrollview>
2) You didn't close the <ScrollView> tag at the end of the code
-->Here's the right code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#659D32"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:orientation="vertical"
android:paddingTop="16dp"
tools:context="com.example.MainActivity">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:id="#+id/cardview_1"
app:cardElevation="10dp"
app:cardMaxElevation="15dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="#drawable/img_1"
android:id="#+id/cardImage_bang"
android:scaleType="centerCrop"/>
<TextView
android:layout_below="#+id/cardImage_1"
android:id="#+id/cardTitle_1"
android:gravity="center_horizontal"
android:text="Text1"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.CardView>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="200dp"
app:cardCornerRadius="5dp"
android:layout_below="#+id/cardview_1"
android:id="#+id/cardview_2"
app:cardElevation="10dp"
android:layout_marginTop="10dp"
app:cardMaxElevation="15dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="170dp"
android:src="#drawable/img_2"
android:id="#+id/cardImage_2"
android:scaleType="centerCrop"/>
<TextView
android:layout_below="#+id/cardImage_2"
android:id="#+id/cardTitle_2"
android:gravity="center_horizontal"
android:text="Text2"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</RelativeLayout>
</ScrollView>

Android XML Linear Layout Display

Good morning guys,
I am having a bit of trouble resolving an issue and could use some help. I created an XML resource file called comment__layout. For some reason I cannot get the layout to display full screen. Please can you review the XML code and help. As you will see in the screenshot the Comment layout is loaded over the parent layout which is exactly what I want however, I would like the Comment layout to be full screen.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/comment__layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_grey">
<LinearLayout
android:id="#+id/comment_heading__layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="3"
android:text="Comments"
android:textSize="20sp"
android:textColor="#color/gps_orange"
android:textStyle="bold"/>
<Button
android:id="#+id/comment_close__button"
android:layout_width="40dp"
android:layout_height="40dp"
android:text="X"/>
</LinearLayout>
<LinearLayout
android:id="#+id/comments_add_comment__layout"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText
android:id="#+id/comment_add__edit_text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"/>
<Button
android:id="#+id/comment_add__button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+Add"/>
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:id="#+id/comments_text_views__scroll_layout"
android:layout_width="match_parent"
android:layout_height="fill_parent">
<LinearLayout
android:id="#+id/comments_text_views__layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</ScrollView>
</LinearLayout>
</LinearLayout>
PARENT XML:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/component_wizard_layout_parent__relative"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/component_wizard_layout_parent"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/frame_component_wizard_manager"/>
<LinearLayout
android:id="#+id/component_wizard_component_popup__layout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</RelativeLayout>

Linear Layout does not fill the whole screen

Why the linear layout does not fill the whole screen?
As you can see I am using match parent in the linear layout.
If I delete the Scrollbar nothing changes. The button is not at the bottom of the screen.
main_activity.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/black"
tools:context="com.sarahp.demo.MainActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout ----------> HERE
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:layout_marginTop="32dp"
android:text="#string/demo"
android:textSize="42dp"/>
<RelativeLayout
android:id="#+id/fragment_container_a"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/fragment_container_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"/>
</LinearLayout>
</ScrollView>
</RelativeLayout>
F A is the fragment part.
Give android:fillViewport="true" to your ScrollView.
Your xml will looks like below. Main_Activity.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="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Test Demo"
android:textStyle="bold"
android:gravity="center"/>
<RelativeLayout
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#drawable/border_image">
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="Test text Fragement1" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/fragment2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
>
<TextView
android:layout_width="match_parent"
android:layout_height="100dp"
android:gravity="center"
android:text="Test text Fragement2" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
Remove the relative layout
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textview_demo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="32dp"
android:layout_marginTop="32dp"
android:text="#string/demo"
android:textSize="42dp"/>
<RelativeLayout
android:id="#+id/fragment_container_a"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<RelativeLayout
android:id="#+id/fragment_container_b"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:background="#color/transparent"/>
</LinearLayout>
</ScrollView>

XML Layout: how can we make an xml layout like this Duolingo app

I am a beginner in Android. So, I want to know how can an xml layout be achieved like this in the picture.
here is the code of what you want..
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#color/colorAccent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.3"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#CDDC39"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:orientation="vertical"
android:layout_weight="0.2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#F8BBD0"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:background="#727272"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
<LinearLayout
android:orientation="horizontal"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:background="#212121"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
<LinearLayout
android:background="#E91E63"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:background="#795548"
android:orientation="vertical"
android:layout_weight="1"
android:layout_width="match_parent"
android:layout_height="match_parent">
</LinearLayout>
</LinearLayout>
just copy whole code in your xml file and you will know.. and change by your requirement. now you can add textview, imageview, button and else inside particular layout. this is all about layout-weight attribute.. try to understand my code. and if you dont know about anything than try to write each layout line by line and you will know how its work..

Categories

Resources