Change toolbar according to Fragment - java

I want to change the top toolbar according to clicked fragment but it seems not working for me.
What I want is when I click the fragment, the top toolbar should change. For example, add a Add button and change the title of the toolbar for Create Fragment.
Fragment.java
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setHasOptionsMenu(true);
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
Toolbar toolbar123 = (Toolbar) v.findViewById(R.id.toolbar123);
((AppCompatActivity)getActivity()).setSupportActionBar(toolbar123);
//((MainActivity) getActivity()).getSupportActionBar().hide();
return v;
}
Fragment 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"
tools:context="com.example.rick.check_in.ui.EventCreatedFragment">
<android.support.v7.widget.Toolbar xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/toolbar123"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#131313"
android:minHeight="?attr/actionBarSize">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="right"
android:background="#00aaaaaa">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000"
android:text="Delete"
android:textColor="#fff"
android:textSize="16dp" />
</LinearLayout>
</android.support.v7.widget.Toolbar>
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>

You can do it like this way. onAttach(Activity) called once the fragment is associated with its activity.
public class YourFragment extends Fragment {
private MainActivity mainActivity;
private Toolbar toolbar123;
public YourFragment() {
// Required empty public constructor
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
mainActivity = (MainActivity)activity;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_event_created, container, false);
toolbar123 = (Toolbar)v.findViewById(R.id.toolbar123);
setupToolbar();
return v;
}
private void setupToolbar(){
toolbar123.setTitle(getString("Your Fragment Title"));
mainActivity.setSupportActionBar(toolbar123);
}
}

Related

How to make a navbar in a fragment android studio

I was running into some trouble making a navbar in a fragment that was loaded by a different navbar.
Basically trying to solve the problem of having a menu in a fragment that was accessed by a navbar.
The easiest thing turned out to be creating a tablayout in the fragment. Here's the tutorial I followed: https://www.youtube.com/watch?v=pIKdHeOjYNw
Here's the basic code:
Main Fragment
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
TabLayout tabLayout = root.findViewById(R.id.tabLayout);
ViewPager2 viewPager2 = root.findViewById(R.id.viewPagerHomeScreen);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(viewPagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(#NonNull Fragment fragment) {
super(fragment);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position){
case 0 :
return new HomeScreenMembership();
case 1 :
return new HomeScreenCreatedClubs();
default :
return new HomeScreenMembership();
}
}
#Override
public int getItemCount() {
return 2;
}
}
Main Fragment XMl
<?xml version="1.0" encoding="utf-8"?>
<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=".ui.home.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/membership_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/created_clubs_tab" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPagerHomeScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
One of the Fragments For One Of the Tabs
(the rest were almost identical)
package com.freedommuskrats.clubbub.ui.home;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.freedommuskrats.clubbub.R;
public class HomeScreenCreatedClubs extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_screen_created_clubs, container, false);
}
}
XML for that fragment
<?xml version="1.0" encoding="utf-8"?>
<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=".ui.home.HomeScreenCreatedClubs">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Created" />
</FrameLayout>
This worked well and I'm even thinking of ditching the navbar in the future because it was so much more complex.

Fragment wont show in activity

I am very new to android development. I went through several tutorials on fragments but for some reason i cant seem to add a fragment from my main view. I'm not trying to do anything painfully complicated. All I want is to create my fragments and add them to my main activity. That's it. But every time I run my application all I get is a blank screen. I debug it and it is going to the fragment. This leads me to believe that the issue may be in the xml laout file (maybe).
Here is my MainActivity.java
package com.android.myCompany.nameOfApp;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, new FooFragment());
ft.commit();
}
}
Here is my Fragment.cs
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
/**
* A simple {#link Fragment} subclass.
*/
public class FooFragment extends Fragment {
public FooFragment () {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
TextView textView = new TextView(getActivity());
textView.setText(R.string.hello_blank_fragment);
return textView;
}
}
Here is my layout activity_main.xml
<?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"
tools:context=".MainActivity"
android:background="#drawable/background_portrait">
<FrameLayout
android:id="#+id/copyright_frame_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
</RelativeLayout>
Here is my fragment_foo.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/copyright_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".FooFragment">
<!-- TODO: Update blank fragment layout -->
<ImageView
android:id="#+id/imageView5"
android:layout_width="wrap_content"
android:layout_height="102dp"
android:layout_marginTop="16dp"
android:layout_marginBottom="8dp"
android:adjustViewBounds="false"
android:cropToPadding="false"
app:layout_constraintBottom_toTopOf="#+id/imageView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.504"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/logo" />
<ImageView
</RelativeLayout>
Can anyone tell me what is wrong with my set up? Many thanks in advance.
Change with this in your fragment class :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_foo, container, false); // This line will inflate your fragment_foo layout and return it's rootview for fragment inflation
}
and then find your fragment views in onViewCreated method of fragment.
Try adding this in your fragment class...
public static FooFragment newInstance()
{
return new FooFragment();
}
#Override
public View onCreateView(#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_layout, container, false);
// work with your ui code here.
return view;
}
And call this method in your activity...
// Begin the transaction
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.foo_frame_layout, FooFragment.newInstance());
ft.commit();

How to update fragment data using RecyclerView?

I have a ViewPager with 12 fragments. I need to get data from API and update the fragments when scrolling. How do I update the contents of fragment, with only one fragment updating for 12 pages?
My fragment layout fragment_item:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/surface">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:layout_marginStart="20dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/khaire"
android:gravity="center"
android:text="Overal Working Hour"
android:textColor="#color/colorPrimary"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:gravity="center"
android:padding="15dp"
android:text="January"
android:textColor="#color/white"
android:textSize="20sp"
android:textStyle="bold" />
My main activity code:
public class MainActivity extends AppCompatActivity {
private static final int num_pages = 12;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mPager = findViewById(R.id.pager);
mPagerAdapter = new ScreenAdapter(getSupportFragmentManager());
mPager.setAdapter(mPagerAdapter);
private class ScreenAdapter extends FragmentStatePagerAdapter {
public ScreenAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int i) {
return new SliderFragment();
}
#Override
public int getCount() {
return num_pages;
}
}
My SlideFragment class:
public class SliderFragment extends Fragment {
Toolbar toolbar;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup rootView = (ViewGroup) inflater.inflate(R.layout.fragment_item,container,false);
return rootView;
}
Now I want to have same fragment while scrolling the page but with diff texts how could I do that... And if possible use RecyclerView also.
You could create a new Class (UpdatingFragment) with public abstract updateView(); that extends Fragment.
Create your SliderFragment by extending UpdatingFragment instead of Fragment
In the SliderFragment in onCreateView() at the end call updateView().
Write an overwrite method in your SliderFragment for what you need to update (anything you want to show on the UI)
Adjust your screenAdapter to return and take UpdatingFragment’s instead of Fragments.
Now whenever your fragments onCreateView gets called all their UI data gets updated, and if you create a list of SliderFragments and pass that into your adapter instead of creating new ones, you can just run a for loop over that list and call updateView() for each fragment.

Access textfield of nested fragment

I have several fragments. Some fragments have the same buttons at the top and the bottom of the fragment, but the content is different.
Therefore I'm using nested fragments for the top and the buttom part. This is my java class: LogFragment.java. I insert two fragments for the top navigation and the footer at the bottom.
public class LogFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
return rootview;
}
}
This is the layout file: fragment_log.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout android:id="#+id/NavigationFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Start"/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:layout_below="#+id/button1"/>
</RelativeLayout>
<FrameLayout android:id="#+id/FooterFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The nested NavigationFragment and FooterFragment contains some buttons, images or textfields. For example: fragment_footer.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/logInfo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="LOG" />
</RelativeLayout>
My question:
How can I access/manipulate/fill in some data of the elements in my nested fragments by the LogFragment.java class?
For example:
I want to access the textview with the id logInfo of the nested fragment_footer.xmlby the LogFragment.java.
Just use [child fragment].getView().findViewById(R.id.[elemnet id]) to get the elements in nested fragments. For example :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootview = inflater.inflate(R.layout.fragment_log, container, false);
Fragment navigationFrag = new NavigationFrag();
FragmentTransaction trans1 = getChildFragmentManager().beginTransaction();
trans1.add(R.id.navigationFrag, navigationFrag).commit();
final Fragment footerFragment = new FooterFragment();
FragmentTransaction trans2 = getChildFragmentManager().beginTransaction();
trans2.add(R.id.footerFragment, footerFragment).commit();
Button btn1 = (Button)rootview.findViewById(R.id.button1);
btn1.setOnClickListener( new OnClickListener(){
#Override
public void onClick(View v) {
TextView txv = (TextView)footerFragment.getView().findViewById(R.id.logInfo);
txv.setText("Updated log of Footer");
}
});
return rootview;
}
By the way,
the child fragment is available to access only after onCreateView(),
so you need to declare the child fragment as final or member data of parent.

Fragment Dialog without Title issue

i'm trying to create a fragment dialog and need the full space to display information. so i want to disable the titlebar. But after i disabled it the isn't maximized in width anymore.
public class ArticleSearchFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(STYLE_NO_TITLE, getTheme());
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.article_search, container, false);
...
}
And the 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:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/searchField"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Ean / Name / Produkt Nummer"
android:completionThreshold="1" />
<Button
android:id="#+id/cancel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/cancel" />
</LinearLayout>
put this code in your onCreateView() of dialog fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_view_image, null);
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return view;
}
It will remove title bar from Dialog Fragment

Categories

Resources