I am a beginner in android , today i have created a BottomNavigationView activity , i want to show 3 different tabs with navigation buttons, so i created 3 fragments, the problem is after adding the fragments , the BottomNavigationView is showing in the top side, what should i do , if i want BottomNavigationView in the bottom as like it was before adding the fragments
here is my main activity code
package com.hackerinside.jaisonjoseph.polysocial;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.design.widget.TabLayout;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
private TextView mTextMessage;
public BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.navigation_home:
tab1 radio = new tab1();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();
case R.id.navigation_dashboard:
tab2 radio1 = new tab2();
android.support.v4.app.FragmentManager manager1 = getSupportFragmentManager();
manager1.beginTransaction().replace(R.id.container, radio1, radio1.getTag()).commit();
case R.id.navigation_notifications:
tab3 radio2 = new tab3();
android.support.v4.app.FragmentManager manager2 = getSupportFragmentManager();
manager2.beginTransaction().replace(R.id.container, radio2, radio2.getTag()).commit();
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tab1 radio = new tab1();
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction().replace(R.id.container, radio, radio.getTag()).commit();
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
}
}
this is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/title_home" />
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
app:menu="#menu/navigation" />
this is my first fragment tab1
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.hackerinside.jaisonjoseph.polysocial.tab1">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
The BottomNavigationView does not appear automatically on the bottom of the view. You have to place them manually.
You can use a RelativeLayout for that.
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.hackerinside.jaisonjoseph.polysocial.MainActivity">
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#android:color/holo_blue_dark">
<TextView
android:id="#+id/message"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:text="#string/title_home" />
</FrameLayout>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/navigation" />
</RelativeLayout>
If changed your root LinearLayout to a RelativeLayout and added the parameter android:layout_alignParentBottom="true" to your BottomNavigationView. Hope that will help.
If you need more help, probably this link can help: https://medium.com/#hitherejoe/exploring-the-android-design-support-library-bottom-navigation-drawer-548de699e8e0
By the way you can also look at https://github.com/roughike/BottomBar
It's a custom view like new Material Design Bottom Navigation pattern (https://material.io/guidelines/components/bottom-navigation.html#bottom-navigation-behavior).
Related
I have a BottomNavigationView and both my fragments have a RecyclerView which content is partially hidden under the BottomNavigationView
Lots of people seem to have had this problem yet I tried all of the answers (make rootview LinearLayout and set height of FrameLayout to 0 etc...) and none of them work.
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.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"
tools:context=".MainActivity"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.viewpager.widget.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="0dp" />
<FrameLayout
android:id="#+id/main_fragment_container"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
/>
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="?android:attr/windowBackground"
android:foreground="?attr/selectableItemBackground"
app:itemTextColor="#color/color12"
app:itemIconTint="#color/color12"
app:menu="#menu/menu_sample_store">
</com.google.android.material.bottomnavigation.BottomNavigationView>
</androidx.coordinatorlayout.widget.CoordinatorLayout>
this is how I initialize it (MainActivity.java)
private void initHandlers()
{
this.adapter = new ViewPagerAdapter(getSupportFragmentManager(),this);
viewPager.setAdapter(adapter);
bottomNavigationView.setOnNavigationItemSelectedListener(new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id)
{
case R.id.menu_option_local:
fragment = new AlphaListFragment(getApplicationContext());
break;
case R.id.menu_option_store:
fragment = new BetaListFragment(getApplicationContext());
break;
}
setActiveFragment(fragment);
return true;
}
});
}
private void setActiveFragment(Fragment fragment)
{
getSupportFragmentManager().beginTransaction().replace(R.id.main_fragment_container, fragment).commit();
}
And if it might help, this is the xml file of a fragment
<?xml version="1.0" encoding="utf-8"?>
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerViewBeta"
android:layout_width="match_parent"
android:layout_height="match_parent">
</androidx.recyclerview.widget.RecyclerView>
<ProgressBar
android:id="#+id/loadingBar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_centerHorizontal="true" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
EDIT: SOLUTION FOUND
Make the rootview of activity_main a LinearLayout
Set its orientation to vertical
Set the layout_weigth of the FrameLayout to 1
Make sure that the FrameLayout has a height of 0dp
I have bottom navigation bar activity with a fragment on top of it.
1 out of 3 menus that I have a fragment that uses Coordinator layout as its parent with App bar layout and collapsing toolbar. another menu works fine with Relative layout parent, but the fragment that has Coordinator layout doesn't work pretty well and make the bottom navigation bar expand its height by itself. Here I'm providing my Code and some screenshots. Thankyou
Here is the code of my fragment 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"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:background="#FFF"
app:layout_collapseMode="parallax">
<TextView
android:id="#+id/nama_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:layout_marginStart="10dp"
android:text="#string/nama_sample"
android:textSize="20dp"
android:textStyle="bold"/>
<TextView
android:id="#+id/lokasi_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lokasi_sample"
android:layout_alignStart="#id/nama_user"
android:layout_below="#id/nama_user"
android:layout_alignEnd="#id/foodie_user"
android:layout_marginTop="5dp"
android:drawableStart="#drawable/ic_location_on_black_24dp"
android:gravity="center"
android:textStyle=""/>
<TextView
android:id="#+id/foodie_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/foodie_sample"
android:layout_alignStart="#id/nama_user"
android:layout_below="#id/lokasi_user"
android:layout_marginTop="5dp"
android:drawableStart="#drawable/ic_foodie"
android:drawablePadding="5dp"
android:layout_marginBottom="20dp"
android:gravity="center"
android:textColor="#color/color_yellow"/>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_alignTop="#id/lokasi_user"
android:layout_alignBottom="#id/foodie_user"
android:layout_alignEnd="#id/nama_user"
android:gravity="center_horizontal">
<TextView
android:id="#+id/review_count_user"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="46"
android:textSize="25dp"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Reviews"/>
</LinearLayout>
<ImageView
android:id="#+id/image_user"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="10dp"
android:src="#drawable/ic_person_black_24dp"
android:tint="#color/color_grey"
/>
</RelativeLayout>
</android.support.design.widget.CollapsingToolbarLayout>
<View
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="#color/color_red" />
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
Here is the code of my Activity with a bottom navigation bar
<?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:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".HomeActivity">
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/nav_view"
/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#6DCCD1D8"
android:layout_above="#id/nav_view"/>
<android.support.design.widget.BottomNavigationView
android:id="#+id/nav_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/windowBackground"
android:layout_alignParentBottom="true"
app:menu="#menu/bottom_nav_menu" />
</RelativeLayout>
and this is its activity java
public class HomeActivity extends AppCompatActivity implements
DiscoverFragment.OnFragmentInteractionListener,
SearchFragment.OnFragmentInteractionListener,
ProfileFragment.OnFragmentInteractionListener{
FrameLayout fragment;
final Fragment fragment1 = new DiscoverFragment();
final Fragment fragment2 = new SearchFragment();
final Fragment fragment3 = new ProfileFragment();
final FragmentManager fm = getSupportFragmentManager();
Fragment active = fragment1;
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.discover_home:
fm.beginTransaction().hide(active).show(fragment1).commit();
active = fragment1;
return true;
case R.id.search_home:
fm.beginTransaction().hide(active).show(fragment2).commit();
active = fragment2;
return true;
case R.id.profile_home:
fm.beginTransaction().hide(active).show(fragment3).commit();
active = fragment3;
return true;
}
return false;
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
BottomNavigationView navView = findViewById(R.id.nav_view);
fragment = findViewById(R.id.fragment_holder);
navView.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
fm.beginTransaction().add(R.id.fragment_holder, fragment3, "3").hide(fragment3).commit();
fm.beginTransaction().add(R.id.fragment_holder, fragment2, "2").hide(fragment2).commit();
fm.beginTransaction().add(R.id.fragment_holder,fragment1, "1").commit();
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
This is how my error looks like
Here is a screenshot of my error when I select profile tab
it pooped out when I choose profile bar
Thank you :)
please try removing this line from your XML
`android:fitsSystemWindows="true"
because it sets the padding of the View to ensure the contents don’t overlay the system windows.
I have tried everything but still, I am not able to change Fragments from CalculatorP1 to Calculator_P2. When I click the button only one Fragment is shown but when I click the button again the second Fragment does not show itself in the APP. I tried everything and I also tried to use TWO BUTTONS bt only one Fragment Shows itself. The second Fragment doesn't show itself. If I switch the fragments only the first Fragment shows bt the 2nd fragment doesn't show itself.
Please help.
My App... See how the button text changes but the Fragment remains the same
My XML codes are given Below:
<?xml version="1.0" encoding="utf-8"?>
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">
<LinearLayout
android:layout_width="98dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:orientation="vertical">
<Button
android:id="#+id/bn_f1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 1" />
<Button
android:id="#+id/bn_f2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Fragment 2" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragment_container"
android:layout_width="276dp"
android:layout_height="350sp"
android:layout_alignParentBottom="true"
android:orientation="vertical"></LinearLayout>
My Activity.java is given below:
package com.example.sudeepsarker.sudeepcalculatorvv;
import android.content.Intent;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
private Button bn_fragment;
private boolean status= false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
bn_fragment = findViewById(R.id.bn_f1);
bn_fragment.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.app.FragmentManager fragmentManager = getFragmentManager();
android.app.FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
if(!status){
CalculatorP1 p1 = new CalculatorP1();
fragmentTransaction.add(R.id.fragment_container,p1);
fragmentTransaction.commit();
bn_fragment.setText("Fragment 2");
status=true;
}
else{
Calculator_P2 p2= new Calculator_P2();
fragmentTransaction.add(R.id.fragment_container,p2);
fragmentTransaction.commit();
bn_fragment.setText("Fragment 1");
status=false;
}
}
});
}
}
My Fragment one xml file:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#8F3A1E"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My first Part of calculator"
android:textColor="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
My Fragment Two XML File:
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="350sp"
tools:context=".CalculatorP1"
android:background="#077715"
>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="My Second Part of calculator"
android:textColor="#ffffff"
android:layout_gravity="center"
android:gravity="center"
android:textAppearance="?android:textAppearanceLarge"
/>
I have this problem:
I'm trying to create an App with 2 buttons on a Layout and this buttons open 2 different Fragments, but the problem is that when I open the app it crashes.
Here's my code:
MainActivity:
package com.example.akroma.feina_final;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.support.v4.app.Fragment;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button boton1,boton2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
boton1 = (Button) findViewById(R.id.button2);
boton1.setOnClickListener(this);
boton2 = (Button) findViewById(R.id.button);
boton2.setOnClickListener(this);
}
#Override
public void onClick(View v) {
Fragment fragment;
android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
android.support.v4.app.FragmentTransaction ft = fm.beginTransaction();
switch (v.getId()) {
case R.id.button:
fragment = new FragmentOne();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
case R.id.button2:
fragment = new FragmentTwo();
ft.replace(R.id.fragment_container, fragment);
ft.commit();
break;
}
}
}
I created the fragments standard (Blank).
Name of fragments:
FragmentOne
FragmentTwo
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:orientation="vertical"
tools:context="com.example.akroma.feina_final.MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Mapa" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Negocis" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</LinearLayout>
Name of fragments on the tree:
FragmentOne
FragmentTwo
Edit: error: Error inflating class fragment
Solution to the inflating:
Thanks to: #svkaka
Change the fragment for:
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
New error:
must implement OnFragmentInteractionListener
Any ideas? I don't know what I'm doing bad.
Thanks in advance
Replace this
<fragment
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
with this
<View
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
U are mixing dynamic and static fragments
I'm having overlay in my application, I did a search and I found some solutions that apparently were simple, but did not work for me. Probably I have a little confusion because I don't understand why it's not working.
So, in my activity_main.xml file I have a FrameLayout element 'container_body'. In my MainActivity I'm set the content of this xml. In this activity I have a drawer-list with some fragments.
When I click in any fragment I'm seeing overlay.
In MainActivity I'm calling the displayView that is responsible to make the fragments transitions, I'm replacing the container_body element for the content of fragment, but this doesn't working.
Any suggestions, please?
[Images] The first screen
The fragment screen
MainActivity
#Override
protected void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
private void displayView(int position) {
Fragment fragment = null;
String title = getString(R.string.app_name);
switch (position) {
case 0:
fragment = new PostView();
title = getString(R.string.title_section1);
break;
case 1:
fragment = new RegionView();
title = getString(R.string.title_section2);
break;
default:
break;
}
if (fragment != null) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.container_body, fragment);
fragmentTransaction.commit();
// set the toolbar title
getSupportActionBar().setTitle(title);
}
activity_main.xml
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_height="?attr/actionBarSize"
android:layout_width="match_parent"
android:minHeight="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:contentInsetStart="72dp" />
<FrameLayout
android:id="#+id/container_body"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="1.25"
android:layout_gravity="top|left|bottom|right">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/tile_bg"
android:orientation="vertical" >
<ListView
android:id="#+id/list_view_messages"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#null"
android:divider="#null"
android:transcriptMode="alwaysScroll"
android:stackFromBottom="true">
</ListView>
</LinearLayout>
</ScrollView>
<LinearLayout
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="3" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="2"
android:background="#color/bg_msg_input"
android:textColor="#color/text_msg_input"
android:paddingLeft="6dp"
android:paddingRight="6dp"/>
<Button
android:id="#+id/btnSend"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/bg_btn_join"
android:textColor="#color/white"
android:text="#string/btn_send" />
</LinearLayout>
</FrameLayout>
<com.heinrichreimersoftware.materialdrawer.DrawerView
android:id="#+id/drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
Fragment
public class PostView extends Fragment implements CommonPresenter {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_post, null);
return (view);
}