3

I'm a beginner in android developer. In this moment need remove margin top represented in the image below. fragment margin top

The project have 3 files to present layout.

fragment_dashboard.xml

<?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"
    android:background="#153091">

    <TextView
        android:id="@+id/text_dashboard"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:textAlignment="center"
        android:textSize="20sp"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

DashboardFragment.kt

package com.luismakeapp.myapplication.ui.dashboard

import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.core.view.marginTop
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import androidx.lifecycle.ViewModelProviders
import com.luismakeapp.myapplication.R

class DashboardFragment : Fragment() {

    private lateinit var dashboardViewModel: DashboardViewModel

    override fun onCreateView(
        inflater: LayoutInflater,
        container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View? {
        dashboardViewModel =
            ViewModelProviders.of(this).get(DashboardViewModel::class.java)
        val root = inflater.inflate(R.layout.fragment_dashboard, container, false)
        val textView: TextView = root.findViewById(R.id.text_dashboard)
        dashboardViewModel.text.observe(this, Observer {
            textView.text = it
        })

        return root
    }
}

DashboardViewModel.kt

package com.luismakeapp.myapplication.ui.dashboard

import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.ViewModel

class DashboardViewModel : ViewModel() {

    private val _text = MutableLiveData<String>().apply {
        value = "This is dashboard Fragment"
    }
    val text: LiveData<String> = _text
}

Which edit file to change layout and remove margin represented in the picture above?

Thanks.

2 Answers2

3

Try removing android:paddingTop="?attr/actionBarSize" from the parent container layout of the activity that holds the fragment.

DareDeviL
  • 327
  • 3
  • 16
0

Your question is a little bit confusing. Try taking a look at How do I ask a good question?

Anyway, have you checked your Activity? I assume you're using a Fragment inside an Activity so I suggest you check your Activity layout.

If you're not doing this yet, then I highly reccoment doing it. Try adding for example a FrameLayout inside your MainActivity's layout and name it fragment_container. Make sure you don't add margins and your set all of your ConstraintLayout's constraints to Top, Bottom, Start and End with match_parent.

After that, you can use your menu options to switch between fragments like so:

FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.fragment_container, mFeedFragment);
fragmentTransaction.addToBackStack(null);
fragmentTransaction.commit();

You can find more info on FragmentTransaction here.

João Martins
  • 706
  • 1
  • 8
  • 20