ScrollView is not scrolling? - android

Ok I am trying to create a screen with a lot of buttons on it, obviously I ran out of space so I set my layout to a ScrollView with a relative layout underneath it and then placed all of my buttons inside that.
heres the code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
android:layout_height="1000dp" android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:fillViewport="true"
tools:context="com.cecode.www.gjcode.MainTypes"
android:animateLayoutChanges="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Options Menu(placeholder)"
android:id="#+id/optionsBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="Top Text"
android:id="#+id/button1"
android:layout_marginTop="110dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button2"
android:layout_marginTop="20dp"
android:layout_below="#+id/button1"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button3"
android:layout_marginTop="20dp"
android:layout_below="#+id/button2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button4"
android:layout_marginTop="20dp"
android:layout_below="#+id/button3"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="mid text"
android:id="#+id/textView3"
android:layout_below="#+id/button4"
android:layout_alignRight="#+id/optionsBtn"
android:layout_alignEnd="#+id/optionsBtn"
android:layout_marginTop="37dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="bottom text"
android:id="#+id/textView4"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/textView3"
android:layout_alignStart="#+id/textView3" />
</RelativeLayout>
</ScrollView>
heres a screenshot of the screen-
My problem is that when I test this on a device it will not scroll past the mid text point, so I cannot get to the bottom text bit. Thanks!

replace
android:layout_height="1000dp"
with
android:layout_height="match_parent"
and let me know if it works
ok as you specified as per your requirement you can use LinearLayout with verticla orientation and between mid and bottom text put another linear layout with wrap_content height to add your buttons if you want some space before adding buttons to it you can specify minheight to layout take look at below code
<ScrollView 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
android:fillViewport="true"
tools:context="com.cecode.www.gjcode.MainTypes"
android:animateLayoutChanges="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:gravity="center"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Options Menu(placeholder)"
android:id="#+id/optionsBtn"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="Top Text"
android:id="#+id/button1"
android:layout_marginTop="110dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button2"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button3"
android:layout_marginTop="20dp"
/>
<Button
android:layout_width="400dp"
android:layout_height="80dp"
android:text="placeholder text"
android:id="#+id/button4"
android:layout_marginTop="20dp"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="mid text"
android:id="#+id/textView3"
/>
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:minHeight="50dp"
android:orientation="vertical">
<!-- add your button here -->
</LinearLayout>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="bottom text"
android:id="#+id/textView4"
/>
</LinearLayout>
</ScrollView>

it is really hard to catch your requirements. Let me explain how it works at the moment. ScrollView doesn't give exact height so when you set match_parent for relative layout I guess it sets max height for this view - Integer.MAX_VALUE. That is why Android Studio warns you to change match_parent to wrap_content.
If you need this view in the bottom - set the margin of your bottom view in runtime.

Related

Unwanted space appearing above linear layout

This is my LayoutActivity, and above the LinearLayout where are the "No, Name, Price and Change 24H." appear a big gap on my phone (Samsung S7), look here. On my Android Studio virtual machine, there isn't one, look here. What is causing this gap and how to fix it?
I include the LayoutActivity code:
<?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=".HomeActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#android:color/white"
android:elevation="10dp"
android:weightSum="3">
<Button
android:id="#+id/home_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="#5078909c "
android:text="Overview"
android:textStyle="bold" />
<Button
android:id="#+id/portfolio_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Portfolio" />
<Button
android:id="#+id/settings_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentTop="true"
android:layout_weight="1"
android:background="#android:color/white"
android:text="Settings" />
</LinearLayout>
<Button
android:id="#+id/refresh_button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginStart="0dp"
android:text="Refresh" />
<ListView
android:id="#+id/CoinList"
android:layout_width="match_parent"
android:layout_height="385dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="48dp" >
</ListView>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="30dp"
android:layout_above="#+id/CoinList"
android:background="#android:color/white"
android:layout_alignParentEnd="true"
android:elevation="10dp"
android:orientation="horizontal">
<TextView
android:layout_width="60dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="No"
android:textColor="#FF000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView2"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_alignParentStart="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="NAME"
android:textColor="#ff000000"
android:textStyle="bold" />
<TextView
android:id="#+id/textView4"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_below="#+id/Background_for_buttons"
android:layout_centerHorizontal="true"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="PRICE"
android:textColor="#ff000000"
android:textStyle="bold" />
<TextView
android:id="#+id/text_view_test"
android:layout_width="128dp"
android:layout_height="30dp"
android:layout_alignParentEnd="true"
android:layout_below="#+id/Background_for_buttons"
android:layout_weight="1"
android:background="#803f51b5"
android:gravity="center"
android:text="CHANGE 24H."
android:textColor="#ff000000"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
Your ListView has a fixed height and is aligned with parent's bottom. Your LinearLayout with headers is put on top of the ListView. So based on vertical resolution of the screen there may or may not be a gap between the LinearLayout and the top of the screen. More specifically, if the screen is taller than 30+385 dp you'll see a gap.
What you probably want to do is align the headers layout with the top of the parent, align the list to the bottom of the headers and make lists's height "match_parent"
Edit: didn't notice the refresh button. You may also need to put
android:layout_above="#+id/refresh_button"
on the ListView

Push Up Fragment content when keyboard is activated

I've searched it everywhere, and I've come up wrapping all my content inside a ScrollView, and setting the keyboard this way:
getActivity().getWindow().setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_STATE_VISIBLE|WindowManager.LayoutParams.SOFT_INPUT_ADJUST_RESIZE);
But when I enable the keyboard by tapping the EditText, all the content gets pushed up except for a TextView in the Top part, why is that?
XML Layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.FET.leonardo.scurcola.NameSelection"
android:background="#e0ab18">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/whoMaster"
android:textColor="#color/white"
android:textSize="50sp"
android:layout_marginTop="50dp"
android:textAlignment="center"
android:id="#+id/whoMaster"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPersonName"
android:hint="#string/master"
android:ems="10"
android:textColorHint="#color/white"
android:textColor="#color/white"
android:textColorHighlight="#color/white"
android:id="#+id/names"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/next"
android:layout_marginBottom="50dp"
android:onClick="onClick"
android:text="#string/next"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/names"
android:textColor="#e0ab18" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/back"
android:id="#+id/back"
android:enabled="false"
android:onClick="onClick"
android:textColor="#e0ab18"
android:layout_marginBottom="50dp"
android:layout_alignParentBottom="true"
android:layout_alignStart="#+id/names" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/finish"
android:visibility="gone"
android:text="#string/finish"
android:layout_alignParentBottom="true"
android:layout_alignEnd="#+id/names"
android:layout_marginBottom="50dp"
android:onClick="onClick"
android:textColor="#e0ab18"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/zero"
android:textSize="50sp"
android:textColor="#color/white"
android:id="#+id/playersLeft"
android:layout_below="#+id/names"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp" />
</RelativeLayout>
Instead of RelativeLayout try to use Linear Layout after scrollView.Set Orientation to Vertical.

ScrollView inside a RelativeLayout

I need to do a scroll view inside a relative layout.
When I added the ScrollView this was the result http://postimg.org/image/6abxth299/ where the components are are only taking up half of the screen and when this screen is opened the keyboard comes up by default
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:scrollbars="vertical" >
<RelativeLayout 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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:layout_centerHorizontal="true"
android:id="#+id/data_entry_layout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post Title"
android:id="#+id/post_title"
android:layout_alignBaseline="#+id/post_title_input"
android:layout_alignBottom="#+id/post_title_input"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="26px"/>
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/post_title_input"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_toRightOf="#+id/post_title"
android:layout_toEndOf="#+id/post_title"
android:inputType="textCapSentences"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Post Description"
android:id="#+id/post_description"
android:layout_below="#+id/post_title_input"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="39dp"
android:textSize="26px"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine|textCapSentences"
android:ems="10"
android:id="#+id/editText"
android:layout_marginTop="16dp"
android:layout_below="#+id/post_description"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ImageButton
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/imageButton1"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:baselineAlignBottom="false"
android:contextClickable="true"
android:src="#drawable/image_button_icon"
android:onClick="galleryIntent"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/cancel"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#id/imageButton1"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/submit"
android:layout_alignTop="#+id/cancel"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_below="#id/imageButton1"/>
</RelativeLayout>
</ScrollView>
The height of first dirsct child of scrollview, be it relative or linear layout should be wrap content and not match parent. Also the inner childs should have specific weight or height and not match parent like you have done for imageButton.
Try adding below to your AndroidManifest in your this activity to remove keyboard comes up by default
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"
and Use android:layout_height="wrap_content" for scrollview , sugesting to use LinearLayout with vertical orientation layout for this kind of use.
I think ScrollView doesn't work inside Relativelayout. It's better to use LinearLayout.
Try this one...
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<RelativeLayout
android:id="#+id/data_entry_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="true"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<RelativeLayout
android:id="#+id/Rel12345"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/post_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/post_title_input"
android:layout_alignBottom="#+id/post_title_input"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Post Title"
android:textSize="26px" />
<EditText
android:id="#+id/post_title_input"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_toEndOf="#+id/post_title"
android:layout_toRightOf="#+id/post_title"
android:inputType="textCapSentences" />
<TextView
android:id="#+id/post_description"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/post_title_input"
android:layout_marginTop="39dp"
android:text="Post Description"
android:textSize="26px" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/post_description"
android:layout_marginTop="16dp"
android:ems="10"
android:inputType="textMultiLine|textCapSentences" />
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/editText"
android:baselineAlignBottom="false"
android:contextClickable="true"
android:onClick="galleryIntent"
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/Rel1234"
android:layout_below="#id/Rel12345"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<Button
android:id="#+id/cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:text="Cancel"/>
<Button
android:id="#+id/submit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/cancel"
android:text="Submit" />
</RelativeLayout>
</RelativeLayout>
</ScrollView>
and Try adding below to your AndroidManifes to remove keyboard coming by default
android:windowSoftInputMode="stateAlwaysHidden|adjustResize"

How to auto align the buttons to fit in center of the screen

Newbie to android, Stuck with aligning the button to center of the screen.
here the code
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.kissmat.gbrf_ebook.SingupIntro"
android:background="#drawable/lib_gradient">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Author"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:layout_marginLeft="70dp"
android:layout_marginStart="70dp"
android:layout_marginTop="82dp"
android:id="#+id/authorBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:elegantTextHeight="true"
android:drawableTop="#drawable/author" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/publisher"
android:text="PUBLISHER"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:id="#+id/pubBtn"
android:layout_alignTop="#+id/authorBtn"
android:layout_toRightOf="#+id/authorBtn"
android:layout_toEndOf="#+id/authorBtn"
android:layout_marginLeft="48dp"
android:layout_marginStart="58dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/student"
android:text="STUDENT"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:layout_marginTop="57dp"
android:id="#+id/stbBtn"
android:layout_below="#+id/authorBtn"
android:layout_alignLeft="#+id/authorBtn"
android:layout_alignStart="#+id/authorBtn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/user"
android:text="READER"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:id="#+id/readerBtn"
android:layout_alignTop="#+id/stbBtn"
android:layout_alignLeft="#+id/pubBtn"
android:layout_alignStart="#+id/pubBtn" />
</RelativeLayout>
How it looks when i run on the device.
I just removed the margins (Left, Start and Top) from your first button and added the property android:gravity="center" to your layout. The last one positions the contents in the center. There's no need to do that with fixed margin values. This is very helpful as your layout will be centered in all the different screen sizes and pixel densities.
<RelativeLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.kissmat.gbrf_ebook.SingupIntro"
android:background="#drawable/lib_gradient"
android:gravity="center">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:text="Author"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:id="#+id/authorBtn"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:elegantTextHeight="true"
android:drawableTop="#drawable/author" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/publisher"
android:text="PUBLISHER"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:id="#+id/pubBtn"
android:layout_alignTop="#+id/authorBtn"
android:layout_toRightOf="#+id/authorBtn"
android:layout_toEndOf="#+id/authorBtn"
android:layout_marginLeft="48dp"
android:layout_marginStart="58dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/student"
android:text="STUDENT"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:layout_marginTop="57dp"
android:id="#+id/stbBtn"
android:layout_below="#+id/authorBtn"
android:layout_alignLeft="#+id/authorBtn"
android:layout_alignStart="#+id/authorBtn" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="onClick"
android:drawableTop="#drawable/user"
android:text="READER"
android:textColor="#ffffffff"
android:background="#00ffffff"
android:id="#+id/readerBtn"
android:layout_alignTop="#+id/stbBtn"
android:layout_alignLeft="#+id/pubBtn"
android:layout_alignStart="#+id/pubBtn" />
</RelativeLayout>

Setting TextView on top of ImageView - Android Layouts

I'm building an android app and I am trying to set my layout without any success.
I searched everywhere and I can't seem to find any solution.
I need to place 2 text views on top of an image.
the first text view need to be about 10dp above the vertical center of the image.
and the second text view should be below text view 1.
here's my code so far, though it doesn't work.
can anyone here tell me what I'm missing or what am I doing wrong?
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.boromedia.parve.MainActivity" >
<RelativeLayout
android:id="#+id/blueOvalLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginBottom="20dp"
android:layout_marginTop="75sp" >
<ImageView
android:id="#+id/blueOval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/blueoval" />
<ImageView
android:id="#+id/greenOval"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scaleType="centerInside"
android:src="#drawable/greenoval_small"
android:visibility="gone" />
<TextView
android:id="#+id/greenOvalText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="65dp"
android:gravity="center|center_vertical"
android:text="#string/counter_activity_oval_done"
android:textColor="#fff"
android:textSize="28sp"
android:visibility="gone" />
<TextView
android:id="#+id/blueOvalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/counter_activity_oval_text1"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:id="#+id/blueOvalTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/blueOvalText"
android:layout_centerHorizontal="true"
android:text="#string/counter_default"
android:textColor="#fff"
android:textSize="25sp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
<TextView
android:id="#+id/head_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="15dp"
android:gravity="center_vertical|center"
android:text="#string/main_title"
android:textSize="30sp" />
<Button
android:id="#+id/stopButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:text="#string/counter_activity_stop_button"
android:textSize="20sp" />
Try this workaround: Change your textView for:
<RelativeLayout
android:id="#+id/wrapper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp" >
<TextView
android:id="#+id/blueOvalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/counter_activity_oval_text1"
android:textColor="#fff"
android:textSize="18sp" />
</RelativeLayout>
This is a wrapper for setting the margin of 10dp.
Don't forget that your second textview should be below the wrapper, not below the textview!
I hope this would help;)
You can put your 2 TextView in a LinearLayout which will be centered in the parent :
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true" >
<TextView
android:id="#+id/blueOvalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:text="#string/counter_activity_oval_text1"
android:textColor="#fff"
android:textSize="18sp" />
<TextView
android:id="#+id/blueOvalTimer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/blueOvalText"
android:layout_centerHorizontal="true"
android:text="#string/counter_default"
android:textColor="#fff"
android:textSize="25sp" />
</LinearLayout>

Categories

Resources