android studio XML layout isn't aligning - android

I have an XML script and in the android studio editor, the preview window shows the screen as it is supposed to look (see picture below). When running on the emulator it looks like all components shift position.
Here is the code for the layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
tools:context="com.example.ansel.companionapp.Main_menu">
<ImageView
android:id="#+id/logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_alignParentEnd="true"
android:layout_marginTop="40dp"
android:layout_marginEnd="113dp"
android:src="#drawable/companionapp" />
<TextView
android:id="#+id/infoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:paddingTop="20dp"
android:text="Large Text"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<Button
android:id="#+id/TPS_button"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_below="#+id/infoText"
android:layout_alignStart="#+id/infoText"
android:layout_marginStart="-25dp"
android:layout_marginTop="53dp"
android:background="#drawable/button_shape"
android:onClick="TPS"
android:text="#string/ThirdPersonShooter"
android:textColor="#color/colorWhite" />
<Button
android:id="#+id/TDS_Button"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_below="#+id/TPS_button"
android:layout_alignStart="#+id/infoText"
android:layout_marginStart="-23dp"
android:layout_marginTop="38dp"
android:background="#drawable/button_shape"
android:onClick="TwoDS"
android:text="#string/TwoDimensionalShooter"
android:textColor="#color/colorWhite" />
</RelativeLayout>
Here is my desired versus actual layout.

I removed all the negative margins and alignings and added for all the views:
android:layout_centerHorizontal="true"
So check this:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite"
tools:context="com.example.ansel.companionapp.Main_menu">
<ImageView
android:id="#+id/logo"
android:layout_width="150dp"
android:layout_height="150dp"
android:layout_centerHorizontal="true"
android:layout_marginTop="40dp"
android:src="#drawable/companionapp" />
<TextView
android:id="#+id/infoText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/logo"
android:layout_centerHorizontal="true"
android:layout_marginTop="24dp"
android:paddingTop="20dp"
android:text="Large Text"
android:textColor="#color/colorPrimary"
android:textSize="18dp" />
<Button
android:id="#+id/TPS_button"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_below="#+id/infoText"
android:layout_centerHorizontal="true"
android:layout_marginTop="53dp"
android:background="#drawable/button_shape"
android:onClick="TPS"
android:text="#string/ThirdPersonShooter"
android:textColor="#color/colorWhite" />
<Button
android:id="#+id/TDS_Button"
android:layout_width="146dp"
android:layout_height="wrap_content"
android:layout_below="#+id/TPS_button"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:background="#drawable/button_shape"
android:onClick="TwoDS"
android:text="#string/TwoDimensionalShooter"
android:textColor="#color/colorWhite" />
</RelativeLayout>

Related

Shadow in Android

enter image description here
As you can see there is a shadow around the ticket. I cannot make this because I am using the library (TicketView) and there is a problem with Scallops
HERE IS MY CODE
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#f3f6f9">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="18dp"
android:layout_marginTop="52.5dp"
android:layout_marginEnd="18dp"
android:orientation="vertical"
android:elevation="14dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="37.5dp"
android:background="#drawable/gradient_tickets">
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="10dp"
android:elevation="2dp"
android:text="Bus 79"
android:textColor="#fefefe"
android:textSize="12sp"
tools:layout_editor_absoluteX="37dp"
tools:layout_editor_absoluteY="69dp" />
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_margin="10dp"
android:scaleX="0.6"
android:scaleY="0.6"
android:src="#drawable/arrow_list" />
<TextView
android:id="#id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/text1"
android:layout_marginTop="40dp"
android:layout_marginBottom="10dp"
android:text="11/03/2017"
android:textColor="#a4158a"
android:textSize="12sp" />
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/img1"
android:layout_alignParentRight="false"
android:layout_alignParentBottom="false"
android:layout_marginTop="40dp"
android:text="$0.30"
android:textColor="#a4158a"
android:textSize="12sp" />
</RelativeLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<com.vipulasri.ticketview.TicketView
android:id="#+id/ticketView"
android:layout_width="wrap_content"
android:layout_height="142dp"
app:ticketBackgroundColor="#FFFFFF"
app:ticketDividerColor="#16191c"
app:ticketDividerDashGap="4dp"
app:ticketDividerDashLength="3dp"
app:ticketDividerPadding="1.5dp"
app:ticketDividerType="dash"
app:ticketDividerWidth="0.5dp"
app:ticketOrientation="horizontal"
app:ticketScallopPositionPercent="25.7"
app:ticketScallopRadius="7dp"
app:ticketShowBorder="false"
app:ticketShowDivider="true"
/>
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20.5dp"
android:layout_marginTop="16.5dp"
android:elevation="2dp"
android:text="12/03/2017"
android:textColor="#a4158a"
android:textSize="10sp" />
<TextView
android:id="#+id/txt2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/ticketView"
android:layout_alignRight="#+id/ticketView"
android:layout_marginTop="16.5dp"
android:layout_marginEnd="17dp"
android:layout_marginRight="17dp"
android:elevation="2dp"
android:text="$0.30"
android:textColor="#a4158a"
android:textSize="10sp" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="31dp"
android:layout_marginTop="47dp"
android:layout_marginRight="31dp"
android:layout_marginBottom="45dp"
android:elevation="3dp"
android:src="#drawable/barcode_img" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I need to have a shadow there but I cannot manage to do it. Also I used the TicketView which is for making the form of the ticket. Could u please say what I need to add there in order to have such a shadow?
You can use CardView. There is a parameter which app:cardElevation.
But if it's not possible, You can create a drawable and you can create shadow with using gradient.

RecyclerView Cutting Off in the last data in LinearLayout

I have a LinearLayout activity that contains two contents which are CardView 1 and CardView 2. Everything is doing well, also with NestedScrollView. But I have problem in the last RecycleView data, it is cutting off a little bit in very bottom.
I already tried many solution that I have searched for in Stack Overflow and this still doing bad. I'm new in this, please help
This is my 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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.yehezkiel.eclassapp.MateriActivity"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_nilai"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
My Custom RecyclerView
<?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="fill_parent"
android:layout_height="90dp"
android:background="#color/white"
android:padding="10dip">
<TextView
android:id="#+id/nama_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignWithParentIfMissing="true"
android:layout_toStartOf="#+id/view_2"
android:gravity="center_vertical"
android:text="Pemrogaman Web Tugas 1"
android:textColor="#5e1b2c"
android:textSize="16sp"
android:textStyle="bold" />
<View
android:id="#+id/view_2"
android:layout_width="0.5dp"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_marginEnd="14dp"
android:layout_toStartOf="#+id/imageView10"
android:background="#c0c0c0" />
<TextView
android:id="#+id/bobot_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_below="#+id/imageView8"
android:textAlignment="center"
android:layout_marginTop="0.5dp"
android:ellipsize="end"
android:gravity="center"
android:text="Bobot"
android:textColor="#555555"
android:textSize="8sp" />
<ImageView
android:id="#+id/imageView8"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_alignEnd="#+id/bobot_layout"
android:layout_below="#+id/nama_nilai"
android:layout_marginTop="8dp"
app:srcCompat="#drawable/percentage" />
<TextView
android:id="#+id/bobot_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView8"
android:layout_alignTop="#+id/percent"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/bobot_layout"
android:text="5.00" />
<TextView
android:id="#+id/percent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView8"
android:layout_toEndOf="#+id/bobot_nilai"
android:text="%"
android:layout_marginStart="2dp"/>
<ImageView
android:id="#+id/imageView9"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_alignBottom="#+id/percent"
android:layout_marginStart="9dp"
android:layout_toEndOf="#+id/percent"
app:srcCompat="#drawable/equality" />
<TextView
android:id="#+id/skala_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/imageView9"
android:layout_alignStart="#+id/imageView9"
android:layout_below="#+id/imageView9"
android:layout_marginTop="1dp"
android:ellipsize="end"
android:gravity="center"
android:text="Skala"
android:textAlignment="center"
android:textColor="#555555"
android:textSize="8sp" />
<TextView
android:id="#+id/skala_nilai"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/skala_layout"
android:layout_marginStart="8dp"
android:layout_toEndOf="#+id/imageView9"
android:text="100.00" />
<ImageView
android:id="#+id/imageView10"
android:layout_width="24dp"
android:layout_height="24dp"
android:layout_below="#+id/nama_nilai"
android:layout_toStartOf="#+id/nilai_nilai"
app:srcCompat="#drawable/score" />
<TextView
android:id="#+id/nilai_layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/imageView10"
android:layout_toStartOf="#+id/nilai_nilai"
android:ellipsize="end"
android:text="Nilai"
android:textAlignment="center"
android:textColor="#555555"
android:textSize="12sp" />
<TextView
android:id="#+id/nilai_nilai"
android:layout_width="90dp"
android:layout_height="50dp"
android:layout_alignBottom="#+id/nilai_layout"
android:layout_alignParentEnd="true"
android:layout_below="#+id/information"
android:text="89.70"
android:padding="15dp"
android:textAlignment="center"
android:textSize="18sp" />
<ImageView
android:id="#+id/information"
android:layout_width="15dp"
android:layout_height="15dp"
android:layout_alignEnd="#+id/nilai_nilai"
android:layout_alignParentTop="true"
app:srcCompat="#drawable/information" />
</RelativeLayout>
I don't think it's overwritten but I think the textview is hiding behind the card itself since its in a relative layout.
Can you try adding layout below attribute in your cardview and assign the ID of the textview "static1"
Let me know how it goes.

Error:(4) Error parsing XML: not well-formed (invalid token)

Whenever i run the app i get this error. I have tried all the solutions online but all of them failed.
Any help?
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dishname"
android:id="#+id/item_name"
android:textSize="24dp"
android:textColor="#396fd1"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/item_type" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="price"
android:id="#+id/item_price"
android:textSize="16dp"
android:layout_below="#+id/item_name"
android:layout_toEndOf="#+id/item_name"
android:layout_marginStart="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type"
android:id="#+id/item_type"
android:textSize="12dp"
android:layout_below="#+id/item_name"
android:layout_alignStart="#+id/item_nofingredients" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#"
android:id="#+id/item_nofingredients"
android:textSize="12dp"
android:layout_below="#+id/item_type"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingredients"
android:id="#+id/textView5"
android:textSize="12dp"
android:layout_below="#+id/item_type"
android:layout_toEndOf="#+id/item_type" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deleteButton"
android:layout_alignBottom="#+id/item_price"
android:layout_alignParentEnd="true"
android:layout_marginEnd="19dp"
android:background="#android:color/transparent"
android:src="#android:drawable/ic_menu_delete"/>
</RelativeLayout>
You have 11 invisible vertical tab characters in your XML, the first one at the end of row #4. Vertical tabs are not valid whitespace characters in XML.
Android Studio does not display the vertical tabs. You can use an editor like Sublime Text to see them.
I edited for you.You can copy it.
<?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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="dishname"
android:id="#+id/item_name"
android:textSize="24dp"
android:textColor="#396fd1"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/item_type" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="price"
android:id="#+id/item_price"
android:textSize="16dp"
android:layout_below="#+id/item_name"
android:layout_toEndOf="#+id/item_name"
android:layout_marginStart="32dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="type"
android:id="#+id/item_type"
android:textSize="12dp"
android:layout_below="#+id/item_name"
android:layout_alignStart="#+id/item_nofingredients" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#"
android:id="#+id/item_nofingredients"
android:textSize="12dp"
android:layout_below="#+id/item_type"
android:layout_alignParentStart="true"
android:layout_marginStart="15dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Ingredients"
android:id="#+id/textView5"
android:textSize="12dp"
android:layout_below="#+id/item_type"
android:layout_toEndOf="#+id/item_type" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/deleteButton"
android:layout_alignBottom="#+id/item_price"
android:layout_alignParentEnd="true"
android:layout_marginEnd="19dp"
android:background="#android:color/transparent"
android:src="#android:drawable/ic_menu_delete"/>
</RelativeLayout>

Setting background color of RelativeLayout in Xamarin.Android

I am trying to add a background color to the search form area of a RelativeLayout in my Android app in Xamarin. Basically I have an EditText and a button that should be contained within a colored background at the top of the screen. Using the code below, the background color is applied but my EditText and button disappear while the content below them move up and take their place. Here's my code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:screenOrientation="portrait"
android:background="#ffffff">
<RelativeLayout
android:id="#+id/TestLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#EAF4F9">
<com.neopixl.pixlui.components.textview.TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="My Heading"
pixlui:typeface="Lato-Bold.ttf"
android:layout_marginBottom="15dp"/>
<EditText
android:inputType="numberDecimal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:id="#+id/SearchField"
android:layout_marginRight="10dp"/>
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/textView1"
android:layout_toRightOf="#id/SearchField"
android:id="#+id/btnSearch"
android:layout_alignParentRight="true" />
<TextView
android:text="Use my current location"
android:layout_width="wrap_content"
android:layout_height="20dp"
android:layout_below="#id/SearchField"
android:id="#+id/txtCurrentLocation"
android:gravity="center"/>
</RelativeLayout>
<View
android:id="#+id/strut"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_centerHorizontal="true" />
<com.neopixl.pixlui.components.button.Button
android:id="#+id/btnOne"
android:text="Button One"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_below="#id/txtCurrentLocation"
pixlui:typeface="Lato-Light.ttf"
android:gravity="center" />
<com.neopixl.pixlui.components.button.Button
android:text="Button Two"
android:id="#+id/btnTwo"
android:layout_width="0dp"
android:layout_height="100dp"
android:layout_alignLeft="#id/strut"
android:layout_alignParentRight="true"
android:layout_below="#id/txtCurrentLocation"
android:gravity="center"
pixlui:typeface="Lato-Light.ttf" />
</RelativeLayout>
Should I be utilizing a LinearLayout here instead or is there a better way to construct this?
Try something like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:pixlui="http://schemas.android.com/apk/com.neopixl.pixlui"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffffff">
<View
android:background="#EAF4F9"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/header"
android:layout_alignBottom="#+id/btnOne" />
<TextView
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:layout_marginBottom="15dp"
android:layout_alignParentTop="true"
android:text="Hello" />
<EditText
android:inputType="numberDecimal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/header"
android:layout_alignParentLeft="true"
android:id="#+id/search"
android:layout_marginRight="10dp"
android:layout_marginLeft="10dp"
android:hint="Search for..." />
<Button
android:text="Search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnSearch"
android:layout_marginRight="10dp"
android:layout_alignParentRight="true"
android:layout_alignBaseline="#+id/search" />
<TextView
android:text="Use my current location"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/search"
android:layout_marginTop="10dp"
android:id="#+id/txtCurrentLocation"
android:gravity="center" />
<Button
android:id="#+id/btnOne"
android:text="Button One"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:layout_alignRight="#id/strut"
android:layout_alignParentLeft="true"
android:layout_below="#id/txtCurrentLocation"
android:gravity="center" />
</RelativeLayout>
Just replace the buttons and textviews with the pixlui versions where needed.

Add background image while remain RelativeLayout

This is my *activity_login.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/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/userid"
android:layout_centerHorizontal="true"
android:text="#string/userid"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/password"
android:layout_centerHorizontal="true"
android:text="#string/password"
android:textSize="20sp" />
<EditText
android:id="#+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignRight="#+id/password"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/login"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_above="#+id/newuser"
android:layout_alignRight="#+id/textView3"
android:layout_marginBottom="27dp"
android:gravity="center"
android:text="#string/button_login" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/register"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:gravity="center"
android:text="#string/button_register" />
<TextView
android:id="#+id/newuser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/register"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="#string/newuser" />
</RelativeLayout>
I want pos_malaysia.png to be set as my login page background on the top center on the screen.. I saw some of them say use <bitmap> .
The thing is when I replace RelativeLayout with bitmap all the android:layout_above="#+id/userid" and android:layout_centerHorizontal="true" cannot be used since its only applies for RelativeLayout. what should I do to maintain the layout with added background?
There are 2 possible solutions for it.
1st add android:background="#drawable/[PIC]" to your Relative Layout.
2nd add ImageView as 1st layer (on bottom) of Relative Layout.
Depends on what kind of background (logo, image) you should consider using 9patch, so it will nicely scale.
Try
<RelativeLayout>
<ImageView
android:layout_centerHorizontal="true"
android:layout_alignParentTop="true">
</ImageView>
</RelativeLayout>
Try this. You can same in linear layout as well
<?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/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText"
android:background="#drwable/your_bg_image"
>
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/userid"
android:layout_centerHorizontal="true"
android:text="#string/userid"
android:textSize="20sp" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/password"
android:layout_centerHorizontal="true"
android:text="#string/password"
android:textSize="20sp" />
<EditText
android:id="#+id/userid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/textView3"
android:layout_alignRight="#+id/password"
android:ems="10"
android:inputType="textPersonName" />
<Button
android:id="#+id/login"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_above="#+id/newuser"
android:layout_alignRight="#+id/textView3"
android:layout_marginBottom="27dp"
android:gravity="center"
android:text="#string/button_login" />
<EditText
android:id="#+id/password"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/login"
android:layout_centerHorizontal="true"
android:layout_marginBottom="25dp"
android:ems="10"
android:inputType="textPassword" />
<Button
android:id="#+id/register"
android:layout_width="100dp"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="42dp"
android:gravity="center"
android:text="#string/button_register" />
<TextView
android:id="#+id/newuser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/register"
android:layout_centerHorizontal="true"
android:layout_marginBottom="14dp"
android:text="#string/newuser" />
</RelativeLayout>
try this:
<?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/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:useDefaultMargins="true"
tools:ignore="ExtraText" >
<ImageView
android:id="#+id/imgv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/*****YOUR IMAGE NAME****"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
if you would like to add background use
android:background="#drawable/image_name"
(provided that the image you are planning to load is in the Project resources.)
in widget or RelativeLayout
if you would like to maintain its size create syle or just create imageView

Categories

Resources