David Faizulaev
David Faizulaev

Reputation: 5721

Android - FloatingActionButton is not displayed & Spinner inexplicably does

I've integrated tab to my MainActivity, using Fragment, TabLayout, ViewPager & my custom PageAdapter.

The layout should contain the FloatingActionButton but for reason it does not, I tried placing it explicitly in the tab1.xml file but it did not help either. In addition, a Spinner object (which was used previously) still appears while its no defined in any of the layouts.

activity_main.xml

    <?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
    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:fitsSystemWindows="true"
    tools:context=".Main2Activity">

<android.support.design.widget.AppBarLayout android:layout_height="wrap_content"


    android:layout_width="match_parent" android:theme="@style/AppTheme.AppBarOverlay">

    <android.support.v7.widget.Toolbar android:id="@+id/toolbar"


        android:layout_width="match_parent" android:layout_height="?attr/actionBarSize"

        android:background="?attr/colorPrimary" app:popupTheme="@style/AppTheme.PopupOverlay" />

</android.support.design.widget.AppBarLayout>

<include layout="@layout/content_main2" />

<android.support.design.widget.FloatingActionButton
    android:id="@+id/fab"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="bottom|end" android:layout_margin="@dimen/fab_margin"
    android:src="@android:drawable/ic_input_add"
    android:onClick="newTaskButtonClick"/>

</android.support.design.widget.CoordinatorLayout>

Content_MainActivity.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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
app:layout_behavior="@string/appbar_scrolling_view_behavior"
tools:context="il.ac.shenkar.david.todolistex2.Main2Activity"
tools:showIn="@layout/activity_main2">

<android.support.design.widget.TabLayout
    android:id="@+id/tab_layout"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_below="@+id/toolbar"
    android:background="?attr/colorPrimary"
    android:elevation="6dp"
    android:minHeight="?attr/actionBarSize"/>

<android.support.v4.view.ViewPager
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="fill_parent"
    android:layout_below="@id/tab_layout"/>

Here are the Fragments I use for tabs: Tab1:

<?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"
android:orientation="vertical">

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="8dp"
    android:id="@+id/Main2ActivitylinearLayout3">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="right"
        android:paddingRight="10dp"
        android:text=""
        android:id="@+id/allt_tab_totalTask"
        android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="25dp"
    android:id="@+id/Main2ActivitylinearLayout">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="match_parent"
        android:gravity="center_vertical"
        android:paddingLeft="5dp"
        android:paddingRight="8dp"
        android:text="Sort:"
        android:textAppearance="?android:attr/textAppearanceMedium" />

    <Spinner android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:entries="@array/sort_array"
        android:id="@+id/sortSpinner"
        android:gravity="center"
        android:textAlignment="center"/>

</LinearLayout>

<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:layout_marginTop="50dp"
    android:id="@+id/Main2ActivitylinearLayout2">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:textAppearance="?android:attr/textAppearanceLarge"
        android:text="No Tasks to Display"
        android:layout_marginTop="60dp"
        android:layout_marginLeft="60dp"
        android:id="@+id/alltab_emptylist"
        android:gravity="center"
        android:layout_centerHorizontal="true"
        android:textStyle="bold" />

    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@+id/alltasks_listView"
        android:layout_centerHorizontal="true" />
</LinearLayout>

Tab2

<?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"
android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="8dp"
        android:id="@+id/Main2ActivitylinearLayout1">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="match_parent"
            android:gravity="right"
            android:paddingRight="10dp"
            android:text=""
            android:id="@+id/totalTask"
            android:textAppearance="?android:attr/textAppearanceMedium" />
    </LinearLayout>

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginTop="50dp"
        android:id="@+id/Main2ActivitylinearLayout2">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textAppearance="?android:attr/textAppearanceLarge"
            android:text="No Tasks to Display"
            android:gravity="center"
            android:layout_marginTop="60dp"
            android:layout_marginLeft="60dp"
            android:id="@+id/emptylist"
            android:layout_centerHorizontal="true"
            android:textStyle="bold" />

        <ListView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:id="@+id/waitingtasks_listView"
            android:layout_centerHorizontal="true" />
    </LinearLayout>

</RelativeLayout>

As you can see, the FloatingActionButton is declared in the activity_main.xml, but for some reason is not displayed. And not Spinner object is defined in any of the XML files, but does appear.

The onCreate method

    @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main2);
    Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
    setSupportActionBar(toolbar);
itemListAllTasks = dbM.getAllTasks();
        TaskHashList.addTaskList(itemListAllTasks);
    itemListWaitingTasks = dbM.getSortedTasks(Sorting.fromInteger(Sorting.WAITING.ordinal()));
    TaskHashList.addTaskList(itemListWaitingTasks);

    tabLayout = (TabLayout) findViewById(R.id.tab_layout);
    tabLayout.addTab(tabLayout.newTab().setText("All Tasks"));
    tabLayout.addTab(tabLayout.newTab().setText("Waiting"));
    tabLayout.setTabGravity(TabLayout.GRAVITY_FILL);

    final ViewPager viewPager = (ViewPager) findViewById(R.id.pager);
    final PageAdapter adapter = new PageAdapter
            (getSupportFragmentManager(), tabLayout.getTabCount());
    viewPager.setAdapter(adapter);
    viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
    tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
        @Override
        public void onTabSelected(TabLayout.Tab tab) {
            Log.w("changed tab","");
            viewPager.setCurrentItem(tab.getPosition());
        }

        @Override
        public void onTabUnselected(TabLayout.Tab tab) {

        }

        @Override
        public void onTabReselected(TabLayout.Tab tab) {

        }
    });
}

Upvotes: 0

Views: 274

Answers (2)

David Faizulaev
David Faizulaev

Reputation: 5721

This was resolved after I rearranged 'Content_MainActivity.xml'.

Upvotes: 1

ishwor kafley
ishwor kafley

Reputation: 938

the FAB should be the last child of coordinator layout. Your xml looks bit of a confusing, format the xml properly and try running again.

Upvotes: 1

Related Questions