childfragment does not work when i click the button - java

I am trying to create a memory game, at the first page when I click on the play button it shows the categories and I used fragments. When I click on a button to go to another fragment it doesn't work.
Mainactivity.java
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageViewA = findViewById(R.id.play_title);
imageViewA.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
android.support.v4.app.FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
FragmentPlay fragA = new FragmentPlay();
transaction.add(R.id.fragmentContainer_main, fragA);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
}
FragmentPlay.java
public class FragmentPlay extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_play,container,false);
return rootView;
}
public void addFragB(){
FragmentManager childFragMan=getChildFragmentManager();
FragmentTransaction childFragTrans = childFragMan.beginTransaction();
FragmentAnimal fragmentAnimal = new FragmentAnimal();
childFragTrans.add(R.id.fragmentContainer_main2, fragmentAnimal);
childFragTrans.addToBackStack(null);
childFragTrans.commit();
}
}
FragmentAnimal.java
public class FragmentAnimal extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView=inflater.inflate(R.layout.fragment_animal,container,false);
return rootView;
}
}

Related

open dialog fragment in Fragment

I have DialogFragment within just some text
and I want open it in Fragment
It's Dialog Fragment first
public class subscribe_dialog_frag extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
and it's my fragment code
public class Main4Fragment extends Fragment {
private ImageButton btn_money;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_main4, container, false);
btn_money = (ImageButton)view.findViewById(R.id.btn_momey);
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Fragment subscribe_dialog_frag = new subscribe_dialog_frag();
FragmentTransaction transaction = getChildFragmentManager().beginTransaction();
transaction.replace(R.id.btn_momey, subscribe_dialog_frag).commit();
}
});
return view;
}
}
I just open the dialogFragment.
possible?? or i do another way?
first use naming convention for classes/variables... Rename your dialog to:
public class SubscribeDialog extends DialogFragment {
#Override
#Nullable
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState){
super.onCreateView(inflater, container, savedInstanceState);
return inflater.inflate(R.layout.subscribe_dialog_fragment, container, false);
}
}
in your Main4Fragment call something like this:
btn_money.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
SubscribeDialog subscribeDialog = SubscribeDialog();
subscribeDialog.show(getChildFragmentManager(),"SubscribeDialog");
}
});

Change the fragment to another fragment within a setOnclick

I have a simple block code and i wish pass this fragment to another fragment with a click in a button.
If anyone could help me I would appreciate it
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio,container,false);
linearLayout = view.findViewById(R.id.clientea);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getActivity(),"Teste",Toast.LENGTH_SHORT).show();
}
});
return view;
}
I will show a simple way with code.
There are one Activity and two Fragments in the code.
First , the MainActivity contains the FragmentOne.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportFragmentManager().beginTransaction().add(R.id.container, new FragmentOne(), "")
.addToBackStack(null)
.commit();
}}
Then FragmentOne, there is a button in it, which will jump FragmentTwo when click.
public class FragmentOne extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_one, container, false);
Button btnClick = view.findViewById(R.id.btn);
btnClick.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getSupportFragmentManager().beginTransaction()
.replace(R.id.container, new FragmentTwo())
.addToBackStack(null)
.commit();
}
});
return view;
}}
The last is the FragmentTwo:
public class FragmentTwo extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_two, container, false);
return view;
}
}
I could give the xml if you want.
From my experience with Android development, the approach that is taken to this problem is to create a functional interface, implement the interface in the Activity that the fragments are managed by and then do the fragment swapping inside the method provided by the interface.
So first create an interface, let's call it IFragmentToSwap.
public interface IFragmentToSwap{
void OnButtonClicked();
}
Then in the Activity that controls fragment transactions you need to implement this interface.
public class MainActivity extends AppCompatActivity implements IFragmentToSwap {
#Override
public void OnButtonClicked()
{
//Swap fragment here
}
}
Now in your fragment in the OnAttach override method you should do the following:
private IFragmentToSwap mListener;
#Override
public void onAttach(Context context)
{
super.onAttach(context);
if (context instanceof IFragmentToSwap)
{
mListener = (MainActivity) context;
}
else
{
throw new RuntimeException(context.toString()
+ " must implement IFragmentToSwap");
}
}
And in the method you provided that is contained inside your fragment you would call the method on the functional interface when a click happens, like this:
#Override
public View onCreateView(final LayoutInflater inflater, final
ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_inicio,container,false);
linearLayout = view.findViewById(R.id.clientea);
linearLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mListener.OnButtonClicked();
Toast.makeText(getActivity(),"Teste",Toast.LENGTH_SHORT).show();
}
});
return view;
}
And don't forget to detach the listener in the OnDetach override:
public void onDetach()
{
super.onDetach();
mListener = null;
}
This is the most common approach I have seen to swapping fragments on Android.

How to start On Boarding activity after Splash Screen activity in android?

I've made a splash screen(Introductory activity) using Cuberto Liquid Pager. My next activity is supposed to be an on boarding activity where 3 fragments are there. The problem is after running the code, Splash screen & 1st fragment from onboarding activity are getting merged. I've tried different solutions but nothing has worked. Please shed some light on this problem.
IntroductoryActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_introductory);
getSupportActionBar().setDisplayShowTitleEnabled(false);
getSupportActionBar().setDisplayShowHomeEnabled(false);
getSupportActionBar().hide();
h1= findViewById(R.id.houseintro);
h2= findViewById(R.id.houseintro2);
bottom= findViewById(R.id.bottom);
txt1= findViewById(R.id.atsingleplace);
txt2= findViewById(R.id.findallprop);
appname= findViewById(R.id.findmehome);
h1.animate().translationY(-2000).setDuration(1000).setStartDelay(6000);
h2.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
bottom.animate().translationY(3000).setDuration(1000).setStartDelay(6000);
txt2.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
txt1.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
appname.animate().translationY(2000).setDuration(1000).setStartDelay(6000);
viewPager=findViewById(R.id.pager);
pagerAdapter=new ScreenSlidePagerAdapter(getSupportFragmentManager(),1);
viewPager.setAdapter(pagerAdapter);
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter{
#SuppressLint("WrongConstant")
public ScreenSlidePagerAdapter(#NonNull FragmentManager fm, int i) {
super(fm, FragmentStatePagerAdapter.BEHAVIOR_RESUME_ONLY_CURRENT_FRAGMENT);
}
#NonNull
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
OnBoardingFragment1 tab1 =new OnBoardingFragment1();
return tab1;
case 1:
OnBoardingFragment2 tab2 =new OnBoardingFragment2();
return tab2;
case 2:
OnBoardingFragment3 tab3 =new OnBoardingFragment3();
return tab3;
}
return null;
}
#Override
public int getCount() {
return NUM_PAGES;
}
}
}
OnBoardingFragment1.java
public class OnBoardingFragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding1,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
OnBoardingFragment2.java
public class OnBoardingFragment2 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding2,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
OnBoardingFragment3
public class OnBoardingFragment3 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
ViewGroup root=(ViewGroup) inflater.inflate(R.layout.fragment_on_boarding3,container,false);
return root;
//return super.onCreateView(inflater, container, savedInstanceState);
}
}
In case you have still not solved it, I think you can use a viewPager for a separate onboarding screen to swipe through the different layout views and either using different class for the splash or attaching the splash style to the onboarding screen to load before the onboarding screen .

i always get error on the button part and getActivity. its my first time using fragment in android

public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.s_1home, null);
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button)view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
}
}
Do like, Create View object like this. Pass container object while inflating layout
View view;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.dashbord, container, false);
Button btn=(Button)view.findViewById(R.id.btn)
return view;
}
You are returning the complete view in the starting itself making the code below return unreachable.
You have given two layouts, If I am not wrong s_1home is layout of your activity where view pager is resting and supply__home__po is your fragment layout. Considering my assumptions are correct please change the code as follows
public class Supplier_Home extends Fragment {
private static final String TAG = "Supplier_Home";
View view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.supply__home__po, container, false);
Button pOrders=(Button) view.findViewById(R.id.pOrders);
pOrders.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d(TAG, "onClick: Clicked login1.");
Intent intent = new Intent(getContext(), Login.class);
}
});
return view;
}
}

Fragment throwing Nullpointer

So I have problem manipulating my TextView inside Fragment.
I have simple callback interface defining only onCallBackReceived(), when my method onCallbackReceived() is called back I want to change text of my TextView, however method setText("onCallback") throws NullPointerException. Exception says that the method is called on null object reference, but I initialized it first using findViewById(). Please help.
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.panel_fragment, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView = (TextView) view.findViewById(R.id.text_view);
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}
Maybe you can findTheViewById in the onCreateView
public class MyFragment extends Fragment implements MySimpleCallback {
TextView textView;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.panel_fragment, container, false);
textView = (TextView) view.findViewById(R.id.text_view);
return view;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
textView.setText("Okay!");
}
#Override
public void onCallbackReceived() {
textView.setText("onCallback");
}
}

Categories