Why my android emulator isn't showing buttons? - android-studio

I made this simple activity in my Android Studio. But when I run this on my Android emulator it doesn't shows the buttons that I added to my activity
<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=".MainActivity">
<TextView
android:layout_width="101dp"
android:layout_height="101dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="151dp"
android:layout_marginTop="340dp"
android:layout_marginEnd="159dp"
android:layout_marginBottom="355dp"
android:text="This is app"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/btn1"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_alignParentBottom="true"
android:layout_marginStart="153dp"
android:layout_marginTop="392dp"
android:layout_marginEnd="152dp"
android:layout_marginBottom="291dp"
android:text="````

This code should work:
<?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=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<TextView
android:layout_width="101dp"
android:layout_height="101dp"
android:text="This is app"
android:layout_centerHorizontal="true"
android:layout_marginBottom="32dp"/>
<Button
android:id="#+id/btn1"
android:layout_width="105dp"
android:layout_height="wrap_content"
android:text="Click Me"/>
</LinearLayout>
</RelativeLayout>

Related

Linear Layout and children not visible

Hi sorry I am having difficulty in getting my layout to been seen when I run the application
I don't see anything when I run my application
here is my XML code
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/edtName"
android:layout_width="match_parent"
android:layout_height="44dp"
android:text="Name" />
<ScrollView
android:id="#+id/scrlText"
android:layout_width="match_parent"
android:fillViewport="true"
android:isScrollContainer="true"
android:layout_height="638dp">
<LinearLayout
android:id="#+id/lltext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<LinearLayout
android:id="#+id/hllsend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/edtInput"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnSend"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
How do I make my items visible when running the application?
In your simple layout code, there are many problems. For example:
1.
android:layout_width="409dp"
android:layout_height="729dp"
such android screen size is hard to get. And in Android layout principal, you'd better use relative layout rather than such hard code size. Here you can replace it with match_parent or wrap_content.
In the same way.
android:layout_height="638dp"
totally over screen height ,make no space for hllsend to show.
I suggest you learn some android layout principal before write android app.
It's not a good practice to add LinearLayout in ConstraintLayout and I think it's the issue with your code you just need to make you LinearLayout as a parent and remove your ConstraintLayout.
NOTE: please do necessary adjustment.
Please check quick example for your code I didn't run this.
<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"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="409dp"
android:layout_height="729dp"
android:layout_marginStart="1dp"
android:layout_marginLeft="1dp"
android:layout_marginTop="1dp"
android:layout_marginEnd="1dp"
android:layout_marginRight="1dp"
android:layout_marginBottom="1dp"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/edtName"
android:layout_width="match_parent"
android:layout_height="44dp"
android:text="Name" />
<ScrollView
android:id="#+id/scrlText"
android:layout_width="match_parent"
android:fillViewport="true"
android:isScrollContainer="true"
android:layout_height="638dp">
<LinearLayout
android:id="#+id/lltext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<LinearLayout
android:id="#+id/hllsend"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<EditText
android:id="#+id/edtInput"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/btnSend"
android:layout_width="10dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Button" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
put all your xml codes in LinearLayout and remove ConstraintLayout . thats may helps.

4 images in one view using linear layout android

I wish to make 2 by 2 image using LinearLayout with you equal weight. Can someone please assist me thanks. Your help will be appreciated thanks
I am trying to get my xml for my Android app working but I have some problems. I am trying to display to equal sized 2 images side by side in a linear layout. There has to be a margin between them, on both sides right and left, and on top.
MY code can only give me 3 images but I need 4 images.
I'd appreciate any help solving this problem
This is my code:
<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="horiztontal"
android:weightSum="2">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="2">
<ImageView
android:id="#+id/imageView2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageView3"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
use following layout
<?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"
android:weightSum="2">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:layout_margin="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<ImageView
android:layout_margin="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageView
android:layout_margin="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
<ImageView
android:layout_margin="10dp"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Note:further you can optimize this layout using Relative or ConstrainLayout.
Use the Code split 4 images equally
<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:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear1"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:id="#+id/imageview"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:id="#+id/imageview1"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/linear2"
android:orientation="horizontal"
android:layout_weight="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:id="#+id/imageview3"/>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:id="#+id/imageview14"/>
</LinearLayout>
Each Image View Set the Scaletype property to set as FitXY

toolbar overlaps my fragments (multi pane)

I am trying to have two fragments shown at the same time on a horizontal orientation of my activity alongwith a toolbar. But the toolbar overlaps the fragments. I'm not sure where to place it. Btw, the toolbar comes with a navigation view, so it's in a drawerlayout.
Here's my xml.
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<LinearLayout
android:id="#+id/main_content_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<android.support.v7.widget.Toolbar
android:id="#+id/main_app_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<fragment
android:id="#+id/activity_fragment_main_event_list"
class="com.torneyo.torneyoadmin.fragments.EventListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/activity_fragment_main_event_detail"
class="com.torneyo.torneyoadmin.fragments.EventDetailFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:itemIconTint="#color/colorAccent"
app:itemTextColor="#color/colorTextSecondary"
app:menu="#menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>
Thoughts? I need to have the fragments show up. I'm not sure I'm using the layouts correctly.
Thanks!
Hello I see your layout file. I think the problem in it. It should inside the relative layout. kindly see the below modified layout version. Apply it let me know if you have any question in it.
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.Toolbar
android:id="#+id/main_app_toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:background="#color/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
<LinearLayout
android:id="#+id/main_content_fragments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/main_app_toolbar"
android:orientation="horizontal">
<fragment
android:id="#+id/activity_fragment_main_event_list"
class="com.torneyo.torneyoadmin.fragments.EventListFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/activity_fragment_main_event_detail"
class="com.torneyo.torneyoadmin.fragments.EventDetailFragment"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="3"
android:background="?android:attr/detailsElementBackground" />
</LinearLayout>
</RelativeLayout>
<android.support.design.widget.NavigationView
android:id="#+id/main_drawer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/drawer_header"
app:itemIconTint="#color/colorAccent"
app:itemTextColor="#color/colorTextSecondary"
app:menu="#menu/menu_drawer" />
</android.support.v4.widget.DrawerLayout>

list view got messy when i tried to move up and down

<?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" android:paddingLeft="8dp"
android:paddingRight="8dp" android:weightSum="1">
<TextView android:id="#+id/textView1" android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:layout_weight="0.12" android:layout_width="match_parent"
android:text="#string/songsbyyear" android:gravity="center"
android:textColor="#color/white" android:textSize="22dp"
android:background="#color/steelblue" android:textStyle="bold"></TextView>
<ListView android:layout_width="wrap_content"
android:drawSelectorOnTop="false" android:id="#android:id/list"
android:background="#color/darkslateblue" android:layout_height="456dp"> </ListView>
</LinearLayout>
I have the listview with background colors set. When I tried to move up/down, it got messy. Any suggestions to fix this issue?

Pop up view different orientation problem in Android

I have got horizontal oriented view and i want to show vertical oriented pop up view on it. Even i set vertical orientation on my pop up layer view it looks horizontal.
My pop Up view xml is :
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="wrap_content" android:layout_weight="0.35" android:layout_width="321dp">
<Button android:text="Button" android:id="#+id/button1" android:layout_height="wrap_content" android:layout_width="fill_parent"></Button>
</LinearLayout>
</LinearLayout>
And on my manifest file xml:
<activity android:name="PopUpLayer" android:label="#string/app_name"
android:theme="#android:style/Theme.Dialog" >
Thanks for any help.
I don't think that you can have landscape view and portrait popup at the same time if that's what you mean.
I changed your xml as follows.Now check.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:weightSum="1" android:orientation="vertical"
android:layout_width="wrap_content" android:layout_height="fill_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_height="wrap_content" android:layout_weight="0.35" android:layout_width="321dp" android:orientation="vertical">
<Button android:text="Button" android:id="#+id/button1" android:layout_height="wrap_content" android:layout_width="fill_parent"></Button>
</LinearLayout>
</LinearLayout>
If not work then see this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="wrap_content" android:orientation="vertical" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="#+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>
Okay.Now see the output of the following xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout android:layout_width="wrap_content" android:orientation="horizontal" android:layout_height="match_parent" android:id="#+id/linearLayout1">
<Button android:text="Button" android:id="#+id/button3" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Button" android:id="#+id/button4" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
</LinearLayout>

Resources