Replace Fragment: IllegalArgumentException: No view found for id - android

All of my activities extend MainActivity and share activity_main layout. In 1 of my activities, I'm using the slidinguppanel library, so the layout is different. When I try to attach the fragment after adding the layout view, I get error:
10-16 15:25:14.155 32590-32590/com.myproject.myapp E/AndroidRuntime: java.lang.RuntimeException: Unable to start activity ComponentInfo{com.myproject.myapp/com.myproject.myapp.search.SearchActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f0d00ea (com.myproject.myapp/searchContainer) for fragment SearchFragment{2e02f51a #0 id=0x7f0d00ea SearchFragment}
What am I doing wrong?
MainActivity.java oncreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
if (getSupportActionBar() != null) {
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_hamburger_icon);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
mMainContainer = (FrameLayout) findViewById(R.id.mainContainer);
mCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.coordinatorLayout);
mAppBarLayout = (AppBarLayout) findViewById(R.id.appBarLayout);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
mUserImageView = (RoundedImageView) navigationView.findViewById(R.id.userImageView);
mRealNameView = (TextView) navigationView.findViewById(R.id.realNameText);
mUserNameView = (TextView) navigationView.findViewById(R.id.userNameText);
showUserInfo(mUser != null);
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/drawer_items" />
</android.support.v4.widget.DrawerLayout>
SearchActivity.java onCreate (I marked the line having the problem):
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
EventBus.getDefault().register(this);
if (getSupportActionBar() != null) {
getSupportActionBar().hide();
}
mDrawerLayout.removeView(mCoordinatorLayout);
LayoutInflater inflater = (LayoutInflater) getSystemService(LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.search_appbar_layout, mDrawerLayout, false);
mSlidingPanel = (SlidingUpPanelLayout) view.findViewById(R.id.sliding_layout);
Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar);
mDrawerLayout.addView(view);
toolbar.setTitleTextColor(Color.WHITE);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toggleDrawer();
}
});
SearchFragment searchFragment = (SearchFragment) getSupportFragmentManager().findFragmentByTag("SearchFragment");
if (searchFragment == null) {
FrameLayout searchContainer = (FrameLayout) view.findViewById(R.id.searchContainer);
searchContainer.setId(R.id.searchContainer);
getSupportFragmentManager()
.beginTransaction()
.add(R.id.searchContainer, SearchFragment.newInstance(), "SearchFragment") // here's the problem
.commit();
}
viewPager = (ViewPager) view.findViewById(R.id.vp_searchResultViewPager);
SearchResultPagerAdapter pagerAdapter = new SearchResultPagerAdapter(getSupportFragmentManager());
viewPager.setAdapter(pagerAdapter);
TabLayout tabStrip = (TabLayout) view.findViewById(R.id.tl_searchResultTabs);
tabStrip.setupWithViewPager(viewPager);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
}
#Override
public void onPageScrollStateChanged(int state) {
if (state == ViewPager.SCROLL_STATE_SETTLING) {
switch (viewPager.getCurrentItem()) {
case 1:
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchMapEvent(foursquareResultsObject));
}
break;
default: //case 0
if (foursquareResultsObject != null) {
EventBus.getDefault().post(new PopulateSearchListEvent(foursquareResultsObject));
}
break;
}
}
}
});
}
search_appbar_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<com.sothree.slidinguppanel.SlidingUpPanelLayout xmlns:sothree="http://schemas.android.com/apk/res-auto"
android:id="#+id/sliding_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="bottom"
sothree:umanoDragView="#+id/toolbar"
sothree:umanoInitialState="expanded"
sothree:umanoOverlay="true"
sothree:umanoPanelHeight="?attr/actionBarSize"
sothree:umanoParallaxOffset="0dp"
sothree:umanoShadowHeight="4dp">
<!-- MAIN CONTENT -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:orientation="vertical"
android:paddingBottom="?attr/actionBarSize">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
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.support.design.widget.TabLayout
android:id="#+id/tl_searchResultTabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/vp_searchResultViewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
</LinearLayout>
<!-- SLIDING LAYOUT -->
<LinearLayout
android:id="#+id/ll_searchDragView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar xmlns:bar="http://schemas.android.com/apk/res-auto"
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:gravity="end|center_vertical"
android:paddingEnd="#dimen/activity_horizontal_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingStart="#dimen/activity_horizontal_margin"
bar:navigationIcon="#drawable/ic_action_hamburger_icon"
bar:theme="#style/ThemeOverlay.AppCompat.ActionBar"
bar:title="#string/search_venues"
bar:titleTextAppearance="#style/TextAppearance.AppCompat.Widget.ActionBar.Title">
</android.support.v7.widget.Toolbar>
<FrameLayout
android:id="#+id/searchContainer"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</com.sothree.slidinguppanel.SlidingUpPanelLayout>

In your SearchActivity you are using the activity_main layout instead of the search_app_bar_layout where you have the fragment container.
Change from this
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
to this:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.search_appbar_layout);

Related

android fragment not showing up beside the drawer

why my WelcomeFragment is not showing up inside my main_container, and all i get is a green screen like show in the images below:
my code:
MainActivity
public class MainActivity extends AppCompatActivity implements WelcomeFragment.ArrowBtnListener
, SetupAccountFragment.BtnOkListener {
FragmentManager fm;
private DrawerLayout drawer;
// for account setup fragment
#Override
public void btnOkMethod() {
FoodListFragment foodListFragment = new FoodListFragment();
fm.beginTransaction()
.replace(R.id.main_container, foodListFragment)
.commit();
}
// for account setup fragment
// for welcome fragment
#Override
public void lockDrawer() {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
#Override
public void unlockDrawer() {
drawer.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
}
#Override
public void onArrowClick() {
SetupAccountFragment setupAccountFragment = new SetupAccountFragment();
fm.beginTransaction()
.replace(R.id.main_container, setupAccountFragment)
.addToBackStack(null)
.commit();
}
// for welcome fragment
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar,
R.string.drawer_open, R.string.drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
fm = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.drawer_fragment_container, new DrawerListFragment());
fragmentTransaction.commit();
Fragment fragment = fm.findFragmentById(R.id.main_container);
if (fragment == null) {
// adding fragment to main container
fragment = new WelcomeFragment();
fm.beginTransaction()
.replace(R.id.main_container, fragment)
.addToBackStack(null)
.commit();
}
}
// make back button close navigation drawer
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
}
activity_main
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:id="#+id/drawer_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
WelcomeFragment
public class WelcomeFragment extends Fragment {
private static final String TAG = "WelcomeFragment";
private Button btnArrow;
interface ArrowBtnListener {
void onArrowClick();
void lockDrawer();
void unlockDrawer();
}
private ArrowBtnListener arrowBtnListener;
#Override
public void onAttach(#NonNull Context context) {
super.onAttach(context);
try {
arrowBtnListener = (ArrowBtnListener) context;
} catch (ClassCastException e) {
Log.d(TAG, "onAttach: " + e.getMessage());
}
}
public WelcomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
//arrowBtnListener.lockDrawer();
return inflater.inflate(R.layout.fragment_welcome, container, false);
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
btnArrow = view.findViewById(R.id.welcome_frag_btn_arrow);
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
btnArrow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
arrowBtnListener.onArrowClick();
}
});
}
}
fragment_welcome
<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=".WelcomeFragment">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:background="#drawable/image_welcome"
android:scaleType="fitXY"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="180dp"
android:layout_height="131dp"
android:layout_marginTop="180dp"
android:background="#mipmap/ic_launcher"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#+id/imageView" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="restaurant name"
android:textAlignment="center"
android:textColor="#860707"
android:textSize="36sp"
android:textStyle="bold"
app:layout_constraintTop_toBottomOf="#+id/imageView2"
tools:layout_editor_absoluteX="16dp" />
<Button
android:id="#+id/welcome_frag_btn_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:background="#drawable/welcome_arrow"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
Change your activity_main.xml like below:
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context=".MainActivity"
tools:openDrawer="start">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<androidx.appcompat.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary"
android:elevation="4dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/main_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
<FrameLayout
android:id="#+id/drawer_fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true" />
</androidx.drawerlayout.widget.DrawerLayout>

Why this tab layout doesn't stay in the bottom?

I've created my application with Navigation Drawer. In my Activity I put this code:
public class MenuActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener
{
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_menu);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close)
{
public void onDrawerOpened(View view)
{
super.onDrawerOpened(view);
}
public void onDrawerClosed(View v)
{
super.onDrawerClosed(v);
}
};
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
onNavigationItemSelected(navigationView.getMenu().getItem(0));
navigationView.setNavigationItemSelectedListener(this);
}
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu, menu);
return true;
}
public boolean onOptionsItemSelected(MenuItem item)
{
int id = item.getItemId();
if (id == R.id.action_settings)
{
return true;
}
return super.onOptionsItemSelected(item);
}
public boolean onNavigationItemSelected(MenuItem item) {
Fragment fragment = null;
String title = null;
int id = item.getItemId();
Class fragmentClass=null;
if (id == R.id.Home)
{
fragmentClass = HomeFragment.class;
title="Home";
}
else if (id == R.id.Profile)
{
fragmentClass= ProfileFragment.class;
title="Profile";
}
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (InstantiationException e) {
e.printStackTrace();
} catch (IllegalAccessException e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
if (getSupportActionBar() != null)
{
getSupportActionBar().setTitle(title);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
now in the HomeFragment I want to put a tab in the bottom of the screen. So i put this code:
public class HomeFragment extends Fragment {
public static TabLayout tabLayout;
public static ViewPager viewPager;
public static int int_items = 3 ;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View inflatedView = inflater.inflate(R.layout.fragment_home, null);
tabLayout = (TabLayout) inflatedView.findViewById(R.id.tabs);
viewPager = (ViewPager) inflatedView.findViewById(R.id.viewpager);
viewPager.setAdapter(new MyAdapter(getChildFragmentManager()));
tabLayout.post(new Runnable() {
#Override
public void run() {
tabLayout.setupWithViewPager(viewPager);
}
});
return inflatedView;
}
class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fm) {
super(fm);
}
/**
* Return fragment with respect to Position .
*/
#Override
public Fragment getItem(int position)
{
switch (position){
case 0 : return new SearchFragment();
case 1 : return new CardFragment();
case 2 : return new MapFragment();
}
return null;
}
public int getCount() {
return int_items;
}
This is fragment_home.xml:
<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=".HomeFragment">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/black_overlay"
app:tabSelectedTextColor="#000"
app:tabTextColor="#FFFFFF"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>
</FrameLayout>
and this the activity xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_menu"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<FrameLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/containerView">
</FrameLayout>
<android.support.design.widget.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_menu"
app:menu="#menu/activity_menu_drawer" />
</android.support.v4.widget.DrawerLayout>
But finally I get this result:
How could I put this tabLayout in the toolbar?
You don't need to take frame layout for fragment_home as root layout. Simplify your xml file as:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
app:tabGravity="fill"
app:tabMode="fixed"
android:background="#color/material_blue_grey_800"
app:tabIndicatorColor="#color/black_overlay"
app:tabSelectedTextColor="#000"
app:tabTextColor="#FFFFFF"
android:layout_gravity="bottom"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true">
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/tabs">
</android.support.v4.view.ViewPager>
</RelativeLayout>

Add Spinner as Tab item using design support library

I am using Design support library to create Tabs. I added Toolbar and Tab view layout.
I want to add Spinner widget as one of the TAB with design support library.
Is it possible to add Spinner as one of the TAB item? Can you give example?
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<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/rootLayout"
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.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
<FrameLayout
android:id="#+id/flContent"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#android:color/white"
app:headerLayout="#layout/layout_drawer_header"
app:itemTextColor="#000000"
app:menu="#menu/global"></android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
public class MainActivity extends AppCompatActivity {
private ActionBarDrawerToggle drawerToggle;
private DrawerLayout drawerLayout;
CoordinatorLayout rootLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_main);
initToolbar();
//getSupportFragmentManager().beginTransaction().replace(R.id.flContent, new LettersFragment()).commit();
rootLayout = (CoordinatorLayout) findViewById(R.id.rootLayout);
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.opendrawer,
R.string.closedrawer);
drawerLayout.setDrawerListener(drawerToggle);
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
if (navigationView != null) {
setupDrawerContent(navigationView);
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
setupViewPager(viewPager);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tab_layout);
tabLayout.setupWithViewPager(viewPager);
tabLayout.setOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
switch (tab.getPosition()) {
case 0:
showToast("One");
break;
case 1:
break;
case 2:
showToast("Three");
break;
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
}
void showToast(String msg) {
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
private void setupViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.accent_material_light)), "ENGLISH");
adapter.addFrag(new DummyFragment(getResources().getColor(R.color.ripple_material_light)), "TELUGU");
viewPager.setAdapter(adapter);
}
}
TabLayout tabLayout = findViewById(R.id.tab_layout);
TabLayout.Tab tab = tabLayout.newTab();
Spinner spinner = new Spinner(MainActivity.this);
// spinner.setAdapter(new SimpleAdapter());
tab.setCustomView(spinner);
tabLayout.addTab(tab);

Android - Unable to change text in TextView

I just started to learning Android and I have a problem. With new Android Studio i have two XML files for each activity, so not to get NullPointerException I had to change my findViewById a little bit. But now I am unable to change text in TextView.
Code:
public class MainActivity extends AppCompatActivity {
LinearLayout layoutMain;
TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) View.inflate(this, R.layout.content_main, null);
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
XML files:
<?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="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_main" />
Change
tvText = (TextView) layoutMain.findViewById(R.id.tvText);
to
tvText = (TextView) findViewById(R.id.tvText);
You were getting an incorrect reference to the TextView.
public class MainActivity extends AppCompatActivity {
View includedView;
TextView tvText;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
includedView = (View) findViewById(R.id.includedView);
tvText = (TextView) includedView.findViewById(R.id.tvText);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
switch (id) {
case R.id.action_settings1:
tvText.setText("one");
Log.d("USER","one");
break;
case R.id.action_settings2:
tvText.setText("two");
Log.d("USER", "two");
break;
}
return super.onOptionsItemSelected(item);
}
}
Also little bit change in your xml files
<?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="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.example.myapp6.MainActivity"
tools:showIn="#layout/activity_main">
<TextView
android:id="#+id/tvText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
</LinearLayout>
and second xml
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
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_main"
android:id="#+id/includedView"/>
Copy and Paste your content_main layout inside activity_main xml and delete the content_main xml file.
Then change the code in onCreate() as:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
layoutMain = (LinearLayout) findViewById(R.id.content_main);
//content_main should be the id of the layout you copied!
tvText = (TextView) findViewById(R.id.tvText);
}

Android kitkat - AppCompat: Doesn't load fragments in my FrameLayout container.(Using a Navigation drawer)

I have implemented a DrawerLayout with Material Design, using AppCompat and it's working ok. The problem is when I try to load a fragment in the container never appear.. and I don't know why.
Below is my activity class
public class MyActivity extends ActionBarActivity {
private Toolbar toolbar;
private DrawerLayout drawerLayout;
private ActionBarDrawerToggle drawerToggle;
private ListView leftDrawerList;
private ArrayAdapter<String> navigationDrawerAdapter;
private String[] leftSliderData = {"Estaciones", "Favorito", "Contacta"};
private int mCurrentSelectedPosition = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
nitView();
if (toolbar != null) {
setSupportActionBar(toolbar);
}
initDrawer();
}
private void nitView() {
leftDrawerList = (ListView) findViewById(R.id.left_drawer);
toolbar = (Toolbar) findViewById(R.id.toolbar);
drawerLayout = (DrawerLayout) findViewById(R.id.drawerLayout);
navigationDrawerAdapter=new ArrayAdapter<String>( MyActivity.this, android.R.layout.simple_list_item_1, leftSliderData);
leftDrawerList.setAdapter(navigationDrawerAdapter);
leftDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Log.e("TEST", position + "");
FragmentManager fragmentManager = getFragmentManager();
Fragment fragment = new Fragment();
switch(position) {
case 0:
fragment = new Estacion();
break;
case 1:
fragment = new Favorito();
break;
case 2:
fragment = new Contacta();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
});
}
private void initDrawer() {
drawerToggle = new ActionBarDrawerToggle(this, drawerLayout, toolbar, R.string.drawer_open, R.string.drawer_close) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
}
};
drawerLayout.setDrawerListener(drawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
drawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
drawerToggle.onConfigurationChanged(newConfig);
}
}
my xml layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include layout="#layout/toolbar" />
<android.support.v4.widget.DrawerLayout
android:layout_width="match_parent"
android:id="#+id/drawerLayout"
android:layout_height="match_parent">
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#android:color/white"
android:text="Activity Content"
android:layout_height="wrap_content" />
</RelativeLayout>
<!-- navigation drawer -->
<RelativeLayout
android:layout_gravity="left|start"
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<ListView
android:id="#+id/left_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:divider="#eee"
android:background="#fff"
android:dividerHeight="1dp" />
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
Thanks!
your main content view of drawer must be one ViewGroup so you must put all your views as children of one viewgroup so put
<!-- The main content view -->
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
as a child of
<!-- activity view -->
<RelativeLayout
android:layout_width="match_parent"
android:background="#fff"
android:layout_height="match_parent">
<TextView
android:layout_centerInParent="true"
android:layout_width="wrap_content"
android:textColor="#android:color/white"
android:text="Activity Content"
android:layout_height="wrap_content" />
</RelativeLayout>

Categories

Resources