Access Fragment Components - android

I have created tabbed activity application in android. I want to change textview's text when application is start or the button is pressed. (Button is in Tab1Fragment, TextView is in Tab2Fragment.) I created a interface and implemented this interface in MainActivity. But when i click button the application is closed. Here is my codes
MainActivity (respond is Communicator's method)
#Override
public void respond(String s) {
FragmentManager fragmentManager = getSupportFragmentManager();
Tab2 testFragment = (Tab2)fragmentManager.findFragmentById(R.id.tab2Fragment);
testFragment.update(s);
}
Tab1
Communicator comm;
private static int counter;
public Tab1() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
comm = (Communicator)getActivity();
Button button = (Button)getActivity().findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
comm.respond("" + counter);
}
});
}
Tab2
public class Tab2 extends Fragment {
TextView textView;
public Tab2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_tab2, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
textView = (TextView)getActivity().findViewById(R.id.textView);
}
public void update(String s){
textView.setText(s);
}
}
activity_main
<?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:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.oguz.faircontrol_v11.MainActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
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:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
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"
tools:context="com.oguz.faircontrol_v11.Tab1">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/button"
android:text="Tikla"
android:gravity="center"/>
</FrameLayout>
fragment_tab2
<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="com.oguz.faircontrol_v11.Tab2"
android:id="#+id/tab2Fragment">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Test"
android:id="#+id/textView"
android:textSize="30dp"
android:textStyle="bold"
android:gravity="center"/>
</FrameLayout>

clear this code ;
Button button = (Button)getActivity().findViewById(R.id.button);
like this;
Button button = (Button)view.findViewById(R.id.button);
It will resolve your problem.

Related

Can't see Bottom Sheet inside fragment

I have a viewpager which have several fragments inside. I want to add a bottom sheet to one of the fragments. Lets call it report_fragment.
The layouts for the fragment and the bottom sheet are:
<androidx.coordinatorlayout.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layoutDirection="rtl">
<include layout="#layout/report_page_content" />
<!-- include the bottom sheet -->
<iclude layout="#layout/report_bottom_sheet_table" />
</androidx.coordinatorlayout.widget.CoordinatorLayout>
report_page_content layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:app="http://schemas.android.com/apk/res-auto"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/report_page_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
bottom sheet layout:
<?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"
android:id="#+id/report_bottom_sheet"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:behavior_hideable="true"
app:behavior_peekHeight="120dp"
android:background="#color/report_bottom_sheet_bck_color"
app:layout_behavior="com.google.android.material.bottomsheet.BottomSheetBehavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:gravity="center"
android:text="#string/daily_bs_tests"
/>
</LinearLayout>
The java code for fragment is:
public class FragmentReportPage extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.report_page_content, container, false);
// Init other views ....
// LinearLayout bottomSheetLayout = v.findViewById(R.id.report_bottom_sheet);
// If i uncomment below line, an exception will arise
// BottomSheetBehavior bottomSheetBehavior = BottomSheetBehavior.from(bottomSheetLayout);
// bottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
return v;
}
Unfortunately, no bottom sheet is shown in the fragment. What i have done wrong?
in your gradle
implementation 'com.google.android.material:material:1.1.0-alpha09'
style
<style name="AppTheme" parent="Theme.MaterialComponents.Light.DarkActionBar">
<!-- Customize your theme here. -->
<item name="colorPrimary">#color/colorPrimary</item>
<item name="colorPrimaryDark">#color/colorPrimaryDark</item>
<item name="colorAccent">#color/colorAccent</item>
</style>
Mainactivity.java
public class Main2Activity extends AppCompatActivity {
private ViewPager mPager;
private PagerAdapter pagerAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = (ViewPager) findViewById(R.id.vpPager);
pagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager());
mPager.setAdapter(pagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
public ScreenSlidePagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new ItemFragment();
default:
return new ItemFragment();
}
}
#Override
public int getCount() {
return 1;
}
}
}
activity_main.xml
<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"
android:fitsSystemWindows="true"
tools:context=".Main2Activity">
<androidx.viewpager.widget.ViewPager
android:id="#+id/vpPager"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
</androidx.constraintlayout.widget.ConstraintLayout>
ItemFragment.java
public class ItemFragment extends Fragment {
View view;
BottomSheetBehavior behavior;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_item_list, container, false);
LinearLayout bottomSheet = view.findViewById(R.id.botttomsheet);
behavior = BottomSheetBehavior.from(bottomSheet);
behavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View bottomSheet, int newState) {
}
#Override
public void onSlide(#NonNull View bottomSheet, float slideOffset) {
}
});
RecyclerView recyclerView = view.findViewById(R.id.list);
recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
recyclerView.setAdapter(new MyItemRecyclerViewAdapter(DummyContent.ITEMS));
return view;
}
}
fragment_item_list.xml
<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"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/list"
android:name="com.andy.faceread.fragment.ItemFragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
app:layoutManager="LinearLayoutManager"
tools:context=".fragment.ItemFragment"
tools:listitem="#layout/fragment_item" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
android:id="#+id/botttomsheet"
app:behavior_hideable="false"
app:behavior_peekHeight="80dp"
android:orientation="vertical"
android:clickable="true"
android:focusable="true"
android:background="#color/colorAccent"
app:layout_behavior="#string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="20dp"
android:textSize="20dp"
android:textColor="#android:color/white"
android:text="bottomsheet"/>
</LinearLayout>
</androidx.coordinatorlayout.widget.CoordinatorLayout>

fragment is not shown

i set on click on the card view when i click on the card view the fragment is shown on the activity i assume that but the fragment is not shown the onclick of cardview is working but fragment is not shown i try alot but no solution i do not know why this happen. There is no error also but my fragment is not shown even no error Advance thanlks
here's code
Activity
public class SubmitAddActivity extends AppCompatActivity implements CallbackFromFragment {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.submitadd_layout);
findViewById(R.id.choose_category_layout).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.d("bullhead", "onClick: hellow rodl ");
fragmentTransaction=getSupportFragmentManager().beginTransaction();
fragmentTransaction.replace(R.id.container,fragmentUsage);
fragmentTransaction.commit();
fragmentUsage.setTYPE(1);
}
});
submitbutton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
settingToolbar();
}
#Override
public void setValues(int type, String value) {
if(type==1)
{
fragmentTransaction.remove(fragmentUsage);
category=value;
}
else if(type==2)
{
fragmentTransaction.remove(fragmentUsage);
location=value;
}
}
}
Activity layout
<ScrollView
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">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>
fragment
public class SelectFragment extends Fragment implements com.asad.taleembazar.adpaters.callback {
private SelectCategorySubmitAddAdapter adapter;
private RecyclerView.LayoutManager layoutManager;
private ArrayList<String> arrayList=new ArrayList<>();
CallbackFromFragment communication;
int type;
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
public SelectFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_select_category_submit,container,false);
arrayList.add("Cars");
arrayList.add("Mobiles");
arrayList.add("Bags");
RecyclerView recyclerViewforsubmitadd = (RecyclerView)view.findViewById(R.id.recyclerview_for_categoriessubmit);
layoutManager = new LinearLayoutManager(getActivity());
recyclerViewforsubmitadd.setLayoutManager(layoutManager);
recyclerViewforsubmitadd.setHasFixedSize(true);
adapter = new SelectCategorySubmitAddAdapter(arrayList);
adapter.setOnClick(this);
recyclerViewforsubmitadd.setAdapter(adapter);
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
communication = (CallbackFromFragment) context;
}
#Override
public void onClick(int adapterPosition) {
if(type==1)
communication.setValues(1,arrayList.get(adapterPosition));
else if(type==2)
communication.setValues(1,arrayList.get(adapterPosition));
}
public void setTYPE(int i)
{
type=i;
}
}
match_parent of your container Framelayout won't work inside your ScrollView. You can either set android:fillViewport="true" in your ScrollView or You can set a fixed height for your container.
Try below code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:fillViewport="true">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<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="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/containerfragments"
android:fitsSystemWindows="true"
android:orientation="vertical"
tools:context="com.asad.taleembazar.activities.HomeActivity">
<include
layout="#layout/cardview_choose_category_submitadds"
android:layout_height="wrap_content"
android:layout_width="match_parent"
/>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
/>
</FrameLayout>
</ScrollView>

Fragments above ViewPager not scrolling

I have a CollapsingToolbarLayout and below that I have a LinearLayout with 2 fragments and a ViewPager. However, when I am scrolling, both fragments stay sticky and only the viewpager containing a RecyclerView is scrolling properly. How can I make both fragments scrollable as well?
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<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"
android:fitsSystemWindows="true"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginBottom="90dp"
app:expandedTitleMarginEnd="64dp">
<ImageView
android:id="#+id/hero"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax"/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_gravity="top"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
<android.support.design.widget.TabLayout
android:id="#+id/tab_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
style="#style/MyCustomTabLayout"
android:layout_gravity="bottom"
app:layout_collapseMode="pin"
app:tabMode="scrollable"/>
<LinearLayout android:id="#+id/ButtonBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center|start"
app:layout_collapseMode="pin"
android:fitsSystemWindows="true"
android:layout_marginTop="40dp"
android:layout_marginLeft="24dp"
android:padding="#dimen/activity_horizontal_margin_half">
<TextView
android:id="#+id/SignUpButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sign Up"
android:textAllCaps="true"
android:background="#drawable/rounded_border_white_inverted_selector"
android:textColor="#drawable/rounded_border_white_text_inverted_selector"
android:padding="#dimen/activity_horizontal_margin_half"/>
<TextView
android:id="#+id/LoginButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login"
android:textAllCaps="true"
android:layout_marginLeft="#dimen/activity_horizontal_margin_half"
android:background="#drawable/rounded_border_white_selector"
android:textColor="#drawable/rounded_border_white_text_selector"
android:padding="#dimen/activity_horizontal_margin_half"/>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<fragment
android:id="#+id/fragmentSponsored"
android:name="SponsoredFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_sponsored" />
<fragment
android:id="#+id/fragmentAnnouncement"
android:name="AnnouncementFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:layout="#layout/fragment_announcement" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/mainactivity"
android:orientation="vertical">
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</LinearLayout>
MainActivity
public class MainActivity extends AppCompatActivity {
MyPagerAdapter TabAdapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final ViewPager pager = (ViewPager) findViewById(R.id.viewPager);
pager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
pager.setOffscreenPageLimit(2);
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch (pos) {
case 0:
return SHGCreationFragmentFirst.newInstance("SHGCreationFragmentFirst, Instance 1");
case 1:
return MemberListFragment.newInstance("MemberListFragment, Instance 2");
}
return null;
}
#Override
public int getItemPosition(Object object) {
return POSITION_NONE;
}
#Override
public int getCount() {
return 2;
}
}
}
FragmentFirst
public class FragmentFirst extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_first, container, false);
return v;
}
public static FragmentFirst newInstance(String text) {
FragmentFirst f = new FragmentFirst();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
FragmentSecond
public class FragmentSecond extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_second, container, false);
return v;
}
public static FragmentSecond newInstance(String text) {
FragmentSecond f = new FragmentSecond();
Bundle b = new Bundle();
b.putString("msg", text);
f.setArguments(b);
return f;
}
}
fragment_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000000"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>
fragment_Second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fcfcfc"
android:orientation="vertical"
android:padding="10dp">
</LinearLayout>

Android toolbar in fragment

I have a activity which shows two buttons sign in and signup and has no toolbar
I am trying to show the toolbar directly in the fragments how can I do this ?
public class SignUpFragment extends Fragment implements LoaderManager.LoaderCallbacks<Cursor> {
private AutoCompleteTextView mEmailView, mPhoneNoView;
private EditText mFirstNameView, mLastNameView, mPasswordView, mConfirmPasswordView;
private LoginActivity mActivity;
private Toolbar toolbar;
/* private SignInActivity mSignInActivity;
private SignInButton mPlusSignInButton;*/
private Button mFBSignInButton,mPlusSignInButton;
private UserRegisterTask mUserRegisterTask;
public SignUpFragment() {
// Required empty public constructor
}
public static SignUpFragment newInstance() {
SignUpFragment fragment = new SignUpFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_sign_up, container, false);
mFirstNameView = (EditText) rootView.findViewById(R.id.fragment_signup_et_firstname);
mLastNameView = (EditText) rootView.findViewById(R.id.fragment_signup_et_lastname);
Toolbar toolbar = (Toolbar) rootView.findViewById(R.id.toolbar_signup);
Toolbar toolbar1 = ((Toolbar)getActivity()).getSupportActionBar();
toolbar1.setTitle("First Fragment");
toolbar1.setDisplayHomeAsUpEnabled(true);
toolbar1.setHomeButtonEnabled(true);
This is its Xml
<android.support.v4.widget.NestedScrollView android:id="#+id/activity_event_details_scrollview"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"
xmlns:app="http://schemas.android.com/apk/res-auto">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar_signup"
app:title="Register"
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v7.widget.Toolbar>
<View
android:layout_width="wrap_content"
android:layout_height="3dp"
android:layout_gravity="center_vertical"
android:layout_weight="1"
android:background="#color/grey200"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="16dp"
>
<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">
<TextView
android:id="#+id/toolbar_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:drawableLeft="#drawable/ic_backnarrow_white"
android:drawablePadding="14dp"
android:gravity="center"
android:text="REGISTER"
android:textColor="#color/background_light"
android:textSize="16dp"
app:textStyle="#integer/RALEWAY_MEDIUM"/>
</android.support.v7.widget.Toolbar>

Snackbar displaying on top of Fragment Fab button

I have activity with Textview and below viewpager. In one of the fragments of view pager, I have a fab button. When I am calling the Snackbar on Menu click of Activity, the Snackbar is coming over the fab button. Every thing works fine (fab button is pushed over snack bar) , if the fab button is in activity, but i need it fragment. Please let me know where I am doing mistake
Fragment Layout
<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:id="#+id/coordLayoutComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/mdtp_white"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/rvComments"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fabComments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_marginBottom="#dimen/btn_fab_margins"
android:layout_marginRight="#dimen/btn_fab_margins"
android:elevation="6dp"
android:src="#drawable/ic_comment_24_5"
app:borderWidth="0dp"
app:fabSize="mini"
app:pressedTranslationZ="12dp" />
</android.support.design.widget.CoordinatorLayout>
Activity Layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:card_view="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:orientation="vertical"> <android.support.design.widget.CoordinatorLayout
android:id="#+id/coordLayout"
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="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include
android:id="#+id/newtoolbar"
layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="?attr/actionBarSize"
android:orientation="vertical">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="120dp"
card_view:cardCornerRadius="5dp"
card_view:cardElevation="2dp"
card_view:contentPadding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/tvTitle"
fontPath="fonts/SourceSansPro-Regular.otf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:ellipsize="end"
android:textSize="28sp" />
<TextView
android:id="#+id/tvDesc"
fontPath="fonts/Roboto-Italic.ttf"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="5dp"
android:gravity="center_horizontal"
android:textSize="15sp"
android:textStyle="normal|italic"
tools:ignore="MissingPrefix" />
</LinearLayout>
</android.support.v7.widget.CardView>
<android.support.design.widget.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/newtoolbar"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:scrollbars="horizontal" />
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
</RelativeLayout>
In you xml layout, there are two CoordinatorLayouts. One in Activity Layout and the other one in Fragment Layout. Now Snackbar.make() function's first argument is the parent Coordinator Layout in which this Snackbar will be placed, and all the elements of this Coordinator Layout will be affected by the motion/view changes of the Sanckbar. Try and use Fragment Layout's Coordinator Layout as the parent of the Snackbar.
Activity Code
public class HomeActivity extends AppCompatActivity
implements FragmentListener {
private LoadingDialog loadingDialog;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
HomeActivity.this.showSnackbar(view, "Replace with your own action");
}
});
}
#Override
public void showSnackBar(View view, String message) {
Snackbar.make(view,message,Snackbar.LENGTH_LONG).show();
}
}
FragmentListener Code
public interface FragmentListener {
void showSnackBar(View view,String message);
}
Fragment Code
public class MyFragment extends Fragment {
private static final String TAG = MyFragment.class.getSimpleName();
private FragmentListener fragmentListener;
private FloatingActionButton floatingActionButton;
public MyFragment() {
// Required empty public constructor
}
public static MyFragment getInstance(String uniqueId) {
MyFragment MyFragment = new MyFragment();
return MyFragment;
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_surveys, container, false);
floatingActionButton = view.findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
fragmentListner.showSnackBar(floatingActionButton,"hello");
}
});
return view;
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
try {
fragmentListener = (FragmentListener) context;
} catch (Exception e) {
throw new ClassCastException(" must implement OnListItemSelectedListener");
}
}
}
For displaying the Snackbar in Activity and Fragment with FAB you can use this simple approach.
Activity and Fragment parent View should be CoordinatorLayout.
Create FragmentListener and implement in Activity

Categories

Resources