Action bar moving down while replacing with fragment

Viewed 1666

I am adding a fragment to activity, and then i am replacing that fragment with the second fragment.After replacing the second fragment action bar is slightly moving down like this enter image description here

My Activity code

public class MyActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.meetinglist_activity);
    Fragment first_fragment = new FirstFragment();
    FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
    ft.add(R.id.content_frame, first_fragment).commit();
     }

Replacing second fragment in the first fragment's button click

public class Firstfragment extends Fragment  {


FloatingActionButton new_fab;

@Nullable
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {

    View list_view=inflater.inflate(R.layout.meetingslist_fragment,container,false);
    new_fab=(FloatingActionButton)list_view.findViewById(R.id.meeting_new);
    new_fab.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {

            Fragment second_fragment = new SecondFragment();
            FragmentTransaction ft =getActivity().getSupportFragmentManager().beginTransaction();
            ft.replace(R.id.content_frame, second_fragment).commit();
  }

Activity layout

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
 android:orientation="vertical" android:layout_width="match_parent"
 android:layout_height="match_parent">
 <FrameLayout
    android:id="@+id/content_frame"
    android:layout_width="match_parent"
    android:layout_height="match_parent"></FrameLayout>
   </LinearLayout>

Fragment's Xml

<!--First Layout-->
<?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">

<android.support.design.widget.FloatingActionButton
    android:id="@+id/meeting_new"
    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_dialog_email" />

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


 <!--Second Layout-->

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:textAppearance="?android:attr/textAppearanceLarge"
    android:text="Large Text"
    android:gravity="center"
    android:id="@+id/textView"
    android:layout_gravity="center_horizontal" />
    </LinearLayout>

I am using android.support.v4.app.Fragment;

I am using Android Studio version2.0 and latest API levels.

Can anybody help me? Thanks in advance.

3 Answers
Related