fragment not showing up in MainActivity page - android

Good Day everyone, I using the left navigation bar and i use fragment for the activity in the navigation bar. But the problem is once I click on the activity, the fragment could not show up. I am not sure what is the problem.
Main Activity
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.nav_layout);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
initCollapsingToolbar();
recyclerView = (RecyclerView) findViewById(R.id.recycler_view);
informationList = new ArrayList<>();
adapter = new AlbumsAdapter(this, informationList);
RecyclerView.LayoutManager mLayoutManager = new GridLayoutManager(this,
2);
recyclerView.setLayoutManager(mLayoutManager);
recyclerView.addItemDecoration(new GridSpacingItemDecoration(2,
dpToPx(10), true));
recyclerView.setItemAnimator(new DefaultItemAnimator());
recyclerView.setAdapter(adapter);
prepareAlbums();
//Cover Pic
try {
Glide.with(this).load(R.drawable.project1_cover).into((ImageView)
findViewById(R.id.backdrop));
} catch (Exception e) {
e.printStackTrace();
}
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open,
R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView)
findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
setupDrawerContent(navigationView);
}
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(getApplicationContext(), "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(getApplicationContext(), "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
fragment = fragment_manageProfile.class;
break;
case R.id.logout:
break;
default:
break;
}
try{
myFragment = (Fragment)fragment.newInstance();
}catch(Exception e){
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.frameLayout,
myFragment).addToBackStack(null).commit();
menuItem.setChecked(true);
drawer.closeDrawers();
}
content_main xml File
<?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:background="#color/viewBg"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="info.androidhive.kopilim.MainActivity"
tools:showIn="#layout/activity_main">
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:scrollbars="vertical" />
</RelativeLayout>
app_bar_main xml file
<?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"
tools:context=".MainActivity">
<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.support.design.widget.CoordinatorLayout>
activity_main xml file
<?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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/detail_backdrop_height"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="64dp"
app:expandedTitleMarginStart="48dp"
app:expandedTitleTextAppearance="#android:color/transparent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
app:layout_collapseMode="parallax" />
</RelativeLayout>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<include layout="#layout/content_main" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/frameLayout"
android:background="#drawable/login_background">
</FrameLayout>
</android.support.design.widget.CoordinatorLayout>
PS fragmentLayout is currently in my activity_main.xml file there. Because I not sure where it has to be. For now, everything i mean the view of my content, my layout is fined. But just for the activity in the navigation bar, fragment there is not showing.
Where did I make my mistake?
Edited: My fragment Class
fragment layout
<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"
tools:context="info.androidhive.kopilim.fragment_manageProfile">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginBottom="35dp"
android:textSize="22dp"
android:textColor="#10019f"
android:textStyle="bold"
android:text="#string/manageProfile"/>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empEmail"
android:text="#string/email"
android:layout_margin="10dp"
android:textSize="18dp"
android:gravity="center_vertical"
android:textColor="#color/colorAccent"
/>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empID"
android:hint="#string/profileID"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empName"
android:hint="#string/profileName"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empIC"
android:hint="#string/profileIC"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<RadioGroup
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/radioGroup"
android:layout_margin="5dp"
android:orientation="horizontal"
android:weightSum="1"
>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/male"
android:id="#+id/maleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"
android:checked="true" />
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/female"
android:id="#+id/femaleRadio"
android:textColor="#color/colorAccent"
android:layout_weight="0.3"/>
</RadioGroup>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empPh"
android:hint="#string/profileHp"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<android.support.design.widget.TextInputLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColorHint="#color/colorAccent"
android:layout_margin="5dp">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/empAddress"
android:hint="#string/profileAddress"
android:textColor="#color/colorAccent"
/>
</android.support.design.widget.TextInputLayout>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="40dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:id="#+id/profileBtn"
android:text="#string/updateProfileBtn"
android:background="#color/buttonLogin"/>
</LinearLayout>
</ScrollView>
</FrameLayout>
fragment java class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_manage_profile, container,
false);
....
....
//my activity
return v;
}

FrameLayout is ok in your activity layout file because you want to display the fragment on the same screen. About the fragment displaying . You need to have a custom fragment class to show it's view and to support it's business logic.
public void selectedItemDrawer(MenuItem menuItem){
Fragment myFragment = null;
Class fragment = null;
switch(menuItem.getItemId()){
case R.id.checkIn:
Toast.makeText(MainActivity.this, "Check-In",
Toast.LENGTH_SHORT).show();
break;
case R.id.checkOut:
Toast.makeText(MainActivity.this, "Check-Out",
Toast.LENGTH_SHORT).show();
break;
case R.id.applyOff:
break;
case R.id.reportBug:
break;
case R.id.manageProfile:
showMyFragment();
break;
case R.id.logout:
break;
default:
break;
}
}
public void showMyFragment(){
Fragment menuFragment = new MyCustomFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction()
.replace(R.id.frameLayout, menuFragment).commit();
}
And here you have to declare the custom fragment class :
public class MyCustomFragment extends Fragment {
//this text view is got from the xml below
private TextView tvTest;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.my_fragment_layout, container, false);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
bindView(view);
//start your logic here
}
private void bindView(View view){
tvTest = view.findViewById(R.id.tv_fragment_test);
Toast.makeText(getContext(),tvTest.getText().toString(),Toast.LENGTH_SHORT).show();
}
}
my_fragment_layout.xml
// could be any kind of layout
// for example
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical">
<TextView
android:id="#+id/tv_fragment_test"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="I'm showing on a fragment"
android:src="#drawable/app_img_small" />
</LinearLayout>

Related

Remove Margin in Fragment

I am using a RecyclerView in a Fragment. I want the fragment to match the parent in width and height. But this is my output.
There is that space being left around the fragment. I want to eliminate that. I have tried everything, i.e, making the third parameter false, using parent container , changing the LayoutParams to match_parent but nothing has worked.
My code:-
Fragment.xml
<?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:id="#+id/RLGEVENT"
android:background="#color/white">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:clickable="true"
android:layout_marginLeft="16dp"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:src="#drawable/postbutton"
android:id="#+id/floatingButton" />
<android.support.v7.widget.RecyclerView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
/>
Fragment
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.activity_gevent_main, container, false);
RelativeLayout rl = (RelativeLayout) view.findViewById(R.id.RLGEVENT);
rl.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT));
rl.invalidate();
//code
return view;
}
Root.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_start"
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:background="#color/white"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:insetForeground="#color/white"
android:theme="#style/AppTheme.Lolwa"
app:headerLayout="#layout/nav_header_start"
app:itemTextColor="#color/black"
app:menu="#menu/activity_start_drawer" />
</android.support.v4.widget.DrawerLayout>
Row.xml
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:id="#+id/card_view"
android:layout_height="wrap_content">
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gEventRowRL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:background="#fff">
<ImageView
android:id="#+id/exampleProfilePic"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_marginLeft="10dp"
android:scaleType="fitXY"
android:src="#drawable/background_profile_pic" />
<ImageView
android:id="#+id/photoOf"
android:layout_width="match_parent"
android:layout_height="400dp"
android:layout_below="#+id/gEventsDetails"
android:scaleType="fitXY"
android:src="#drawable/black_total" />
<TextView
android:id="#+id/geventTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/exampleProfilePic"
android:text="Title"
android:textSize="18sp"
/>
<TextView
android:id="#+id/gEventsDetails"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/exampleProfilePic"
android:layout_margin="10dp"
android:text=""
android:textSize="16sp" />
<TextView
android:id="#+id/gEventsDescription"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/geventTitle"
android:layout_margin="10dp"
android:layout_toRightOf="#+id/photoOf"
android:text="" />
</RelativeLayout>
</RelativeLayout>
app_bar_start.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="com.example.anubhavr.firebase.StartActivity">
<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_start" />
</android.support.design.widget.CoordinatorLayout>
Activity
#Override
protected void onCreate(Bundle savedInstanceState) {
setTheme(R.style.AppTheme);
super.onCreate(savedInstanceState);
overridePendingTransition(R.anim.slide_in, R.anim.slide_out);
setContentView(R.layout.activity_start);
fragmentManager = getSupportFragmentManager();
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);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
com.example.anubhavr.firebase.Global_Events.MainActivity mainActivity = new com.example.anubhavr.firebase.Global_Events.MainActivity();
setListener(mainActivity);
fragmentManager.beginTransaction().replace(R.id.nav_home, mainActivity, mainActivity.getTag()).commit();
}
The padding should be in content_start

Collapsible Toolbar - Make Fragment Footer Always Visible in Android

I am making an app that has a ProfilePage with three fragments - About | Posts | Gallery, and I am using a collapsible toolbar with user's image. My second and third fragment will have a footer that should always be visible, but this is what I get:
When my image is expanded the footer disappears. What I want is for my two fragments to have these footers always visible and not depending on toolbar being collapsed or not. My gallery footer should be similar to post footer.
profile_layout.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"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
contentScrim="?attr/colorPrimary"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:contentDescription="#string/profile_photo"
app:layout_collapseMode="none"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<ImageView
android:id="#+id/toolbarEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:contentDescription="#string/block"
android:paddingEnd="20dp"
android:paddingStart="5dp"
app:srcCompat="#drawable/ic_icon_edit" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="#+id/myProfileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabContentStart="72dp"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/myProfileViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
posts_fragment.xml:
<LinearLayout 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"
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
android:id="#+id/llMsgCompose"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
android:weightSum="4" >
<EditText
android:id="#+id/inputMsg"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:layout_weight="3"
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/ppdColorOrange"
android:textColor="#color/white"
android:text="#string/send" />
</LinearLayout>
My PostsFragment (not yet implemented):
public class MyProfilePostsFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.navdrawer_my_profile_fragment_posts, container, false);
return view;
}
}
I had the same requirement for my one of apps. I just did workaround.
Add your required layout in tab layout:
<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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="256dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
contentScrim="?attr/colorPrimary"
android:layout_height="match_parent"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/profile_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:scaleType="centerCrop"
android:contentDescription="#string/profile_photo"
app:layout_collapseMode="none"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin">
<ImageView
android:id="#+id/toolbarEdit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:contentDescription="#string/block"
android:paddingEnd="20dp"
android:paddingStart="5dp"
app:srcCompat="#drawable/ic_icon_edit" />
</android.support.v7.widget.Toolbar>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.design.widget.TabLayout
android:id="#+id/myProfileTabs"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="bottom"
app:tabContentStart="72dp"
app:tabGravity="fill"
app:tabMode="scrollable" />
<android.support.v4.view.ViewPager
android:id="#+id/myProfileViewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
<-- Your required layout -->
<LinearLayout
android:id="#+id/tab123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:gravity="bottom"
android:orientation="horizontal"
android:visibility="gone"
android:weightSum="4">
<EditText
android:id="#+id/inputMsg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="3"
android:paddingLeft="6dp"
android:paddingRight="6dp" />
<Button
android:id="#+id/btnSend"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="send" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
Add following code in your Tab activity:
TabLayout tabLayout;
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);//add your viewpager to TabLayout
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
LinearLayout l=(LinearLayout) findViewById(R.id.tab123);
final EditText text=(EditText) findViewById(R.id.inputMsg);
Button send=(Button) findViewById(R.id.btnSend);
if (tab.getPosition() == 0) {
l.setVisibility(View.GONE);
System.out.println("About tab");
} else if (tab.getPosition() == 1) {
l.setVisibility(View.VISIBLE);
System.out.println("Posts tab");
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg = null;
msg = text.getText().toString(); //get input
// Perform your desired task.
}
});
} else if (tab.getPosition() == 2) {
l.setVisibility(View.VISIBLE);
System.out.println("Gallery tab");
send.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String msg = null;
msg = text.getText().toString(); //get input
//Perform your desired task.
}
});
}
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});

How using Coordinator Layout load from Detail activity

i have a problem with my Coordinator layout xml, it takes from detail activity class. I don't know if click the item of RecyclerView and then my app force close.. Please tell me what is wrong with my class and my xml.
here the code of detail class
public class DetailActivity extends AppCompatActivity {
private CoordinatorLayout coordinatorLayout;
private CollapsingToolbarLayout collapsingToolbarLayout;
private Toolbar toolbar;
TextView nameTxt,propTxt,descTxt;
ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_detail);
Intent i=this.getIntent();
String name=i.getExtras().getString("NAME_KEY");
String propellant=i.getExtras().getString("PROPELLANT_KEY");
String desc=i.getExtras().getString("DESCRIPTION_KEY");
String imageurl=i.getExtras().getString("IMAGEURL_KEY");
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
initToolbar();
coordinatorLayout =(CoordinatorLayout) findViewById(R.id.coordinator);
collapsingToolbarLayout = (CollapsingToolbarLayout) findViewById(R.id.maincollapsing);
collapsingToolbarLayout.setTitle("NAME_KEY");
nameTxt= (TextView) findViewById(R.id.nameTxtDetail);
propTxt= (TextView) findViewById(R.id.propellantTxtDetail);
descTxt= (TextView) findViewById(R.id.descDetailTxt);
img= (ImageView) findViewById(R.id.mainbackdrop);
nameTxt.setText(name);
propTxt.setText(propellant);
descTxt.setText(Html.fromHtml(desc));
PicassoClient.downloadImage(this,imageurl,img);
}
private void initToolbar(){
toolbar = (Toolbar) findViewById(R.id.maintoolbar);
setSupportActionBar(toolbar);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.sample_actions, menu);
return true;
}
}
and this my xml code :
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/coordinator"
android:background="#android:color/background_light"
tools:context="com.tutorials.hp.ditkeu.m_DetailActivity.DetailActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/mainappbar"
android:layout_width="match_parent"
android:layout_height="300dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/maincollapsing"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginEnd="48dp"
app:expandedTitleMarginStart="10dp"
android:fitsSystemWindows="true"
app:expandedTitleMarginBottom="16dp"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
app:expandedTitleTextAppearance="#style/CollapsedAppBarTopic">
<ImageView
android:id="#+id/mainbackdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/placeholder"
app:layout_collapseMode="parallax" />
<android.support.v7.widget.Toolbar
android:id="#+id/maintoolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingTop="24dp">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp">
<LinearLayout
style="#style/Widget.CardContent"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/nameTxtDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textStyle="bold"
android:layout_alignParentStart="true"
android:paddingLeft="5dp"
android:text="Judul Berita"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#color/input_register_bg" />
<TextView
android:id="#+id/propellantTxtDetail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="5dp"
android:text="Tanggal "
android:textAppearance="? android:attr/textAppearanceSmall"
android:textColor="#color/input_register_bg" />
<TextView
android:id="#+id/descDetailTxt"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Isinya... "
android:textColor="#color/input_login"
android:paddingLeft="5dp"/>
</LinearLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
one of the reasons you are calling
setSupportActionBar(toolbar);
before
initToolbar();
Actually u are calling setSupportActionBar(toolbar) twice in onCreate and in initToolbar() Do thisremove setSupportActionBar(toolbar) and getSupportActionBar().setDisplayHomeAsUpEnabled(true) from onCreate

creating a sidebar navigation drawer with a button

I'm trying to create a sidebar navigation drawer that slides when a user clicks the button. I'm trying to add this feature into my existing layout and I'm having trouble with it. As you can see in my layout, I have created a menu resource file and included into my main layout but it is not working. Any help would be appreciated.
Here's my main layout
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar"
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:background="?attr/colorPrimaryDark"
android:layout_width="match_parent"
android:layout_height="113dp"
android:fitsSystemWindows="true"
app:popupTheme="#style/Theme.AppCompat.DayNight.DarkActionBar"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
/>
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<EditText
android:id="#+id/search_box"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:paddingLeft="8dp"
android:paddingBottom="12dp"
android:paddingTop="12dp"
android:textSize="14sp"
android:hint="#string/search_hint"
android:background="#drawable/bottom_border"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<include
layout="#layout/item_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#+id/relativeAd"
android:layout_below="#+id/search_box"
/>
<LinearLayout
android:id="#+id/relativeAd"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#color/divider"
android:gravity="center"
android:orientation="vertical">
</LinearLayout>
</RelativeLayout>
</FrameLayout>
<android.support.design.widget.FloatingActionButton
android:layout_width="50dp"
android:layout_height="53dp"
android:clickable="true"
app:fabSize="mini"
app:srcCompat="#drawable/menu_1"
android:id="#+id/floatingActionButton9"
app:backgroundTint="?attr/actionModeSplitBackground"
app:rippleColor="#android:color/holo_red_dark"
android:layout_gravity="bottom|left"
app:layout_anchor="#+id/frameLayout"
app:layout_anchorGravity="center_vertical|right" />
Here is my screenshot of my layout
My existing layout
Try this out.
This a code for navigation drawer which opens and closes using button and swipe both.
so below is the layout code for setting up navigation drawer.
xml file code:
<?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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
android:theme="#style/NavigationTheme">
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.whiskey.servicedog.TrainingProgrammes">
<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.support.design.widget.CoordinatorLayout>
<android.support.design.widget.NavigationView
android:id="#+id/navigation_view"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header"
app:menu="#menu/menu_navigation"
android:background="#drawable/nav_bg"
android:theme="#style/NavigationViewStyle"
app:itemTextColor="#color/White"
app:itemIconTint="#color/White"
/>
</android.support.v4.widget.DrawerLayout>
now java code for setting up navigation drawer.
NavigationView navigationView = (NavigationView) findViewById(R.id.navigation_view);
navigationView.setNavigationItemSelectedListener(new NavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
switch (id) {
case R.id._aboutus:
Intent aboutusintent = new Intent(TrainingProgrammes.this, AboutUS.class);
aboutusintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(aboutusintent);
break;
case R.id._trainingprog:
Toast.makeText(getApplicationContext(), "Training Programmes", Toast.LENGTH_SHORT).show();
drawerLayout.closeDrawers();
break;
case R.id._taskhis:
Intent historyintent = new Intent(TrainingProgrammes.this, TaskHistory.class);
historyintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(historyintent);
break;
case R.id.taskrem:
Intent newintent = new Intent(TrainingProgrammes.this, TaskReminders.class);
newintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(newintent);
break;
case R.id.settingschange:
Intent settingIintent = new Intent(TrainingProgrammes.this, Settings.class);
settingIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(settingIintent);
break;
case R.id.help_btn:
Intent helpIintent = new Intent(TrainingProgrammes.this, Help.class);
helpIintent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(helpIintent);
break;
case R.id.logout:
Intent intent = new Intent(TrainingProgrammes.this, MainActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intent);
SharedPreferences preferences = getSharedPreferences("MyPREFERENCES", Context.MODE_PRIVATE);
if (preferences.contains("password")) {
SharedPreferences.Editor editor = preferences.edit();
editor.remove("password");
editor.commit();
}
finish(); // Call once you redirect to another activity.
}
return true;
}
});
Hope this helps you out.
Here is the screenshot of above working code.
Use below xml to add a Drawer Layout and include your previous layout
<include layout="#layout/content_home_page" /> by replacing this
name it 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/drawerLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:openDrawer="start">
<!--Main Content Goes Here-->
<include layout="#layout/content_home_page" />
<android.support.design.widget.NavigationView
android:id="#+id/navigationView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
app:headerLayout="#layout/nav_header_main"
app:itemIconTint="#color/colorPrimary"
app:itemTextColor="#color/colorPrimary"
app:menu="#menu/navigation_menu"
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/nav_header_height"
android:background="#drawable/side_menu_header_bg">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:orientation="vertical">
<ImageView
android:layout_width="70dp"
android:layout_height="70dp"
android:src="#drawable/user_placeholder" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedBold
android:id="#+id/memberFullName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Mr. John Doe"
android:textColor="#color/colorWhite"
android:textSize="18sp" />
<com.sslwireless.gulshanclub.Fonts.RobotoCondensedRegular
android:id="#+id/memberEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="john.doe#mail.com"
android:textColor="#color/colorWhite"
android:textSize="16sp" />
</LinearLayout>
</RelativeLayout>
</android.support.design.widget.NavigationView>
</android.support.v4.widget.DrawerLayout>
In your activity:
private DrawerLayout drawer;
private NavigationView navigationView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawerLayout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
navigationView = (NavigationView) findViewById(R.id.navigationView);
navigationView.setNavigationItemSelectedListener(this);
}
#Override
public void onBackPressed() {
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
inside your button click listener click event,
call drawer.openDrawer(GravityCompat.START);

Why when I want to access a TextView in the layout of the toolbar it turns null

I want to access an element in the layout of the toolbar, but it turns null. What do I need to do to access this TextView?
And is it the right way to organize the toolbar in every class?
This is my main_activity.xml
<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/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.myapp.activity.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="40dp"
android:background="?attr/colorBackgroundFloating"
android:theme="#style/ThemeOverlay.AppCompat.ActionBar"
app:contentInsetStart="0dp"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/txtToolbarTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:text="myapp"
android:textColor="#color/black"
android:textSize="20sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/imbToolbarAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp"
android:background="#color/white"
android:src="#drawable/plus_circle"
android:visibility="invisible" />
</RelativeLayout>
</android.support.v7.widget.Toolbar>
</android.support.design.widget.AppBarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
app:tabGravity="fill"
app:tabIndicatorHeight="0dp"
app:tabMode="fixed" />
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/tabLayout"
android:layout_below="#id/appbar" />
This is where I want to access to TextView;
public class MyFragment extends BaseFragment {
public MyFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_cldirtanlar, container, false);
setToolbar(view, "New Title", View.VISIBLE);
return view;
}
public void setToolbar(View view, String title, int addButton) {
TextView txtToolbarTitle = (TextView) view.findViewById(R.id.txtToolbarTitle);
txtToolbarTitle.setText("Kontlar");
ImageButton imbToolbarAdd = (ImageButton) view.findViewById(R.id.imbToolbarAdd);
imbToolbarAdd.setVisibility(addButton);
}
}
You are using view.findViewById(), which only returns views in your R.layout.fragment_cldirtanlar, not views set in your MainActivity.
You can instead use getActivity().findViewById() if you want to retrieve views created by your activity.

Categories

Resources