Home > other >  Android UI Elements Not Appearing, Despite Showing in XML Design View
Android UI Elements Not Appearing, Despite Showing in XML Design View

Time:05-12

I have a firebase application with a login page and a dashboard. After logging in, the user is redirected to the dashboard where i only have one sign out button and nothing else. The button appears in the xml design view but when the application is installed it doesn't appear on my phone

This is my XML file

<?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=".Dashboard">

    <Button
        android:id="@ id/signoutButton"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="145dp"
        android:layout_marginTop="306dp"
        android:layout_marginEnd="172dp"
        android:layout_marginBottom="377dp"
        android:text="@string/Sign_out"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


</androidx.constraintlayout.widget.ConstraintLayout>

And this is the .kt file

package com.example.contract

import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.widget.Button
import com.google.firebase.auth.ktx.auth
import com.google.firebase.ktx.Firebase

class Dashboard : AppCompatActivity() {
    private lateinit var signoutBtn : Button
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_dashboard)

        signoutBtn = findViewById<Button>(R.id.signoutButton)
        signoutBtn.setOnClickListener {
            Firebase.auth.signOut()
            val intent = Intent(this, Login::class.java)
            intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP)
            startActivity(intent)
        }
    }
}

CodePudding user response:

Change parent layout to

<LinearLayout

And remove all this lines

  android:layout_marginStart="145dp"
    android:layout_marginTop="306dp"
    android:layout_marginEnd="172dp"
    android:layout_marginBottom="377dp"
    app:layout_constraintBottom_toBottomOf="parent"
    app:layout_constraintEnd_toEndOf="parent"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"

Then add these lines

android:text="@string/Sign_out" 
android:layout_gravity="center"

CodePudding user response:

Change the button code like this, this will put it left top corner, and by this you can see the button regardless of phone size and type. this will give you a clue.

    <Button
    android:id="@ id/signoutButton"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="@string/Sign_out"
    app:layout_constraintStart_toStartOf="parent"
    app:layout_constraintTop_toTopOf="parent"/>                                           
  • Related