0

I'm trying to show a button over a fullscreen image. I tried to use RelativeLayout as shown in this question but I cannot see the Button.

Can someone tell me why this code isn't working?

<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/pitchImageView"
        android:src="@drawable/gaa_pitch"
        android:scaleType="centerCrop"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:id="@+id/stopwatchButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>
</RelativeLayout>

Apologies if this qualifies as a duplicate question.

EDIT: Screenshots of the activity.

Android design tab

Emulator at runtime

4 Answers4

0
<RelativeLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent">

    <ImageView
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:id="@+id/pitchImageView"
        android:src="@drawable/gaa_pitch"
        android:scaleType="centerCrop"/>

    <Button
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@string/start"
        android:id="@+id/stopwatchButton"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"/>

</RelativeLayout>
Ismael Di Vita
  • 1,806
  • 1
  • 20
  • 35
0

My friend you can use FrameLayout alternatively ,and checkout this site : Android Frame Layout

<FrameLayout
  android:layout_width="match_parent"
  android:layout_height="match_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pitchImageView"
    android:src="@drawable/desert_road"
    android:scaleType="centerCrop"/>

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Start"
    android:src="@drawable/ic_clear_white_24dp"
    android:id="@+id/stopwatchButton"
    android:layout_gravity="center" />
</FrameLayout>

The Result :

enter image description here

Yasin Kaçmaz
  • 6,573
  • 5
  • 40
  • 58
0

This is a common "problem". You can try a couple of things:

  1. Invalidate caches and restart Android Studio, or just restart without invalidating

  2. Try the Run -> Clean and rerun option

  3. Or the Build -> Clean project or Build -> Rebuild Project options

When you are done, run the project again.

Ojonugwa Jude Ochalifu
  • 26,627
  • 26
  • 120
  • 132
0

Well I got it working, I can't exactly explain what fixed it but these are the steps I took:

  1. Utilised a content and activity split. (to get an example of this create a basic activity in android studio).
  2. I deleted other versions of the layout files in the version compatible folders (such as "layout-v24").
  3. I also used "Clean" and "Rebuild" project options whenever I made a change to my code.

activity xml file:

<android.support.design.widget.CoordinatorLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:fitsSystemWindows="true">

    <include layout="@layout/content_record_game"/>
</android.support.design.widget.CoordinatorLayout>

This is my content.xml file

<RelativeLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent">

<ImageView
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:id="@+id/pitchImageView"
    android:contentDescription="@string/image_of_a_gaa_pitch"
    android:src="@drawable/gaa_pitch"
    android:scaleType="centerCrop"
    android:cropToPadding="false"
    />
<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/start"
    android:id="@+id/stopwatchButton"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />
</RelativeLayout>