I'm working with navigation component and made one fragment with authorization which should be shown at the beginning, but if I press "Run app" there is nothing on the screen, but after I click "Apply changes and Restart Activity" I can see all views etc.
Is this a normal thing or how can I fix this?
mainActivity.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"
tools:context=".MainActivity">
<androidx.fragment.app.FragmentContainerView
android:id="#+id/main_fragments_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:name="androidx.navigation.fragment.NavHostFragment"
app:defaultNavHost ="true"
app:navGraph ="#navigation/nav_graph_common"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Related
I have followed a guide on creating android navigation with the androidx navigation package. However, I am having troubles displaying anything. When the app starts it shows and empty activity and not the content of my fragment.
Do you have to do anything extra in order to let it show the start screen of the navigation graph?
Launcher xml
<?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_height="match_parent"
android:layout_width="match_parent">
<fragment
android:id="#+id/launcherNavHostFragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:navGraph="#navigation/navigation_launcher" />
</FrameLayout>
Navigation graph (navigation_launcher)
<?xml version="1.0" encoding="utf-8"?>
<navigation 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:id="#+id/navigation_launcher"
app:startDestination="#id/launcherFragment">
<fragment
android:id="#+id/launcherFragment"
android:name="com.myapp.Views.LauncherFragment"
android:label="fragment_launcher"
tools:layout="#layout/fragment_launcher" >
<action
android:id="#+id/action_launcherFragment_to_mainActivity"
app:destination="#id/mainActivity" />
</fragment>
<activity
android:id="#+id/mainActivity"
android:name="com.myapp.Views.MainActivity"
android:label="activity_main"
tools:layout="#layout/activity_main" />
</navigation>
The fragments are just basic fragments with a text view inside. Nothing special here.
Any ideas on what is missing?
As far as I see from the code you posted there are some errors in your configuration:
The nav host fragment should be contained in activity_main.xml (or your main activity layout file, the main activity should be defined in your manifest file and it's the one responsible to load the first fragment displayed by the app
The launcher fragment layout should not contain a NavHostFragment itself, but it should have some views inside the frame layout (a textview displaying some text for example)
- activity_main.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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#+id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/navigation_launcher" />
</androidx.constraintlayout.widget.ConstraintLayout>
- fragment_launcher.xml
<?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_height="match_parent"
android:layout_width="match_parent">
<TextView android:text="I'm the launcher fragment"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</FrameLayout>
Android dev beginner here. I've followed a tutorial for fragment-based navigation. I'm running into troubles when changing device orientation. It works fine on a real device, but in the emulator it is broker.
I've read some things about the view being re-rendered on device orientation change, but in my case the entire layout gets messed up, including the top bar as well as the bottom bar (back, etc.). A screenshot and code included below.
Note: layout looks completely fine when seeing it in design tab of XML layout file.
This is what it looks like in a vertical orientation:
Code for main activity and main fragment is below:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
</androidx.constraintlayout.widget.ConstraintLayout>
and my main fragment is this one:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
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"
tools:context=".MainFragment"
android:id="#+id/parentLayout">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="30dp"
android:gravity="center">
...some stuff here...
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
=> this looks incorrect:
android:layout_width="0dp"
android:layout_height="0dp"
Try to follow this example :
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment android:name="com.example.news.ArticleListFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0dp"
android:layout_height="match_parent" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
Using the "Activity Bottom Navigation" template in Android Studio, I am then adding a GoogleMap fragment inside one of the default fragments.
The main fragment, unchanged from the template:
<?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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.0"
app:navGraph="#navigation/mobile_navigation" />
The fragment that contains my mapfragment. Only slightly modified from the template's fragment:
<?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=".ui.map.MapFragment">
<fragment
android:id="#+id/gmap"
android:name="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
I am not including any code in the fragment, because it doesn't matter if there IS code or not - the margin/padding appears at top.
When run, I get the following:
Notice the almost-action-bar madding before the map starts. In the Android Studio designer screen, it IS shows that the fragment is taking up
Am I missing some kind of flag or setting or layout quirk to allow the map area to fill up the entire parent (its own) fragment?
The root ConstraintLayout on your main fragment is causing the empty space:
android:paddingTop="?attr/actionBarSize"
Delete this attribute and the space will disappear.
I'm very new to Android Studio and thre's this whitespace that I haven't defined at the top of my app when I launch it, but not in the preview screen in Android Studio:
I have no idea where it's coming from, as the top of my definition XML doesn't define this whitespace:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/scrollContainer"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/containerLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
Below this is just two more LinearLayouts inside the one shown above. I've tried just adding text and the whitespace still appears. I also haven't added anything in code for this to appear.
Any help / ideas would be appreciated!
Edit: Below is the activity_main 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"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="?attr/actionBarSize">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/bottom_nav_menu" />
<fragment
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:layout_constraintBottom_toTopOf="#id/nav_view"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:navGraph="#navigation/mobile_navigation" />
</androidx.constraintlayout.widget.ConstraintLayout>
In activity_main.xml, try removing the line:
android:paddingTop="?attr/actionBarSize"
This adds padding around the ConstraintLayout content equal to the height of the action bar, which is not needed in this case.
I'm using mapbox in my application, normal fragment page full mapview showing,but when using viewpager in that fragment, mapview is not showing full in bottom its showing black color in version 8.0.0, below versions is showing properly.
Here is an image, showing how it looks.
tabs.xml
<?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">
<RelativeLayout
android:id="#+id/rlay"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:id="#+id/appBarLayout">
<android.support.design.widget.TabLayout
android:id="#+id/gs_tabLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/lightgray"
app:tabTextColor="#color/onegray"
app:tabSelectedTextColor="#color/onegray"
android:minHeight="?attr/actionBarSize"
app:tabTextAppearance="#style/MyTabStyle"
android:scrollbars="vertical"/>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:layout_below="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/gs_viewPager">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</RelativeLayout>
fragmenta.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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"
xmlns:mapbox="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.mapbox.mapboxsdk.maps.MapView
android:id="#+id/mapView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintHorizontal_bias="1.0"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="1.0"
mapbox:mapbox_cameraTargetLat="12.973520"
mapbox:mapbox_cameraTargetLng="77.633006"
mapbox:mapbox_cameraZoom="5"
mapbox:mapbox_styleUrl="mapbox://styles/mapbox/streets-v9"
tools:layout_constraintBottom_creator="1"
tools:layout_constraintLeft_creator="1"
tools:layout_constraintRight_creator="1"
tools:layout_constraintTop_creator="1">
</com.mapbox.mapboxsdk.maps.MapView>
</android.support.constraint.ConstraintLayout>
Note: I don't know whether viewpager is problem or not, without tabs means its showing full map view. Please someone help me.
I know this can be a little late but when I faced the same problem, I did a little bit of reading and research to find out that the map when displayed through bootstrap Modal, angular, etc. shows default 400px width and will not take the previous container's width as it is not aware of it and hence its a good practice to use map.resize() method inside the map.on('load',function(){}) in the end of it.
While using bootstrap modal
map.on('load', function() {
// some code before the resize option
$('#myModal').on('shown.bs.modal', function() {
map.resize();
});
}
where my #myModal is the id of the Div that contains my bootstrap modal.
Hope this helps someone who is struggling with the same issue.