fragment does not go to activity [Android] - android

It's been less than a week since I learned Android. trying to create a project, but there are some things that don't work.
Problem: Does not move from fragment to activity
Tried: Browse the document on Google. and I used #Override for onclickListener in HomeFragment.java A red line popped up and woke up remove, but still don't know what the error is.
HomeFragment.java
public class HomeFragment extends Fragment {
private HomeViewModel homeViewModel;
private Button btn_today;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
homeViewModel = new ViewModelProvider(this).get(HomeViewModel.class);
View root = inflater.inflate(R.layout.fragment_home, container, false);
ViewGroup root_view = (ViewGroup)inflater.inflate(R.layout.fragment_home, container, false);
Button btn_today = (Button)root_view.findViewById(R.id.btn_today);
btn_today.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(getActivity(), HomeActivity.class);
startActivity(intent);
}
});
return root;
}
}
used the Navigation Drawer Activity when I first created the project.
what I want to do is click on the button btn_today to go to the activity page (in xml file)
I'm a beginner or I don't know what the problem is. appreciate any help
doing it all day, but I'm not sure.

You should be use root variable for finding button view id.
Button btn_today = (Button)root.findViewById(R.id.btn_today);

Related

Fragment onClickListener Not registering Click

I've gone through all the search results on SO and I can't find one that solves my issue. I have a fragment and I am trying to use an ImageButton to close the fragment. But my onClickListener will not register a click. I placed a break-point on the line and it was never hit.
ImageButton closeButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
if (getArguments().getBoolean("invert") == true) {
rootView = inflater.inflate(R.layout.popup_inverted_header_and_text, container, false);
}else {
rootView = inflater.inflate(R.layout.popup_header_and_text, container, false);
}
rootView.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
closeButton = (ImageButton) rootView.findViewById(R.id.closeButton);
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getFragmentManager().popBackStackImmediate();
}
});
return rootView;
}
From my research, the way you add a fragment to a view could affect this, so here is the code where I add the fragment.
FragmentManager fragmentManager = getFragmentManager();
headerAndTextFragment hatf = headerAndTextFragment
.newInstance("Information on GlassFrogg review process",
"Here is some information about the review process that you probably didn't actually want to know",
true);
FragmentTransaction transaction = fragmentManager.beginTransaction();
transaction.replace(R.id.popupContainer, hatf);
transaction.addToBackStack(null).commit();
Please make sure you are using same id 's in both the layouts since you have a single ImageButton defined in the class on which the listener is registered.
Since you are inflating two different layouts. Are you sure both the Layout has that ImageButton ? and both have same ID ?
when you open the fragment, you don't add the fragment to backStack, so when you click to imagebutton, there is no fragment in the backStack to pop.
use this
closeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
getActivity().getFragmentManager().beginTransaction().remove(YOUR_FRAGMENT_NAME.this).commit();
}
});

Using Scandit BarcodeScanner in 2 fragment of an application

I develop an application for scanning barcode. My application is an Android TabView application with 2 tabs. And I want to add barcode scanner in both of them.
My code is following:
Fragment1:
public class WebServiceFragment extends Fragment {
RelativeLayout scanServiceView;
ScanditSDKAutoAdjustingBarcodePicker barcodePicker;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null) {
return null;
}
View v = inflater.inflate(R.layout.webservice_layout, container, false);
scanServiceView = (RelativeLayout)v.findViewById(R.id.scanserviceview);
barcodePicker = new ScanditSDKAutoAdjustingBarcodePicker(getActivity(), "<My Scandit SDK Key 1>", ScanditSDKAutoAdjustingBarcodePicker.CAMERA_FACING_BACK);
barcodePicker.startScanning();
scanServiceView.addView(barcodePicker);
return v;
}
}
Fragment2:
public class CMCSMOFragment extends Fragment {
RelativeLayout scanSMOView;
ScanditSDKAutoAdjustingBarcodePicker barcodePicker;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(container == null) {
return null;
}
View v = inflater.inflate(R.layout.cmcsmo_layout, container, false);
scanSMOView = (RelativeLayout)v.findViewById(R.id.scansmoview);
barcodePicker = new ScanditSDKAutoAdjustingBarcodePicker(getActivity(), "<My Scandit SDK Key 2>", ScanditSDKAutoAdjustingBarcodePicker.CAMERA_FACING_BACK);
barcodePicker.startScanning();
scanSMOView.addView(barcodePicker);
return v;
}
}
But when I build, only Fragment2 display camera view for scanning. Fragment1 display black screen. If I remove Fragment2 code which call Scandit SDK, Fragment1 would work normally.
How should I do with this situation? Image below is the issue screen:
Fragment1:
Fragment2:
As pointed out in the comments you will need to stop the scanning when navigating away from the Fragment.
This is due to the Fragment is not having its view destroyed when simply navigating away. So your reference to the barcodePicker does not get collected as long as you are holding on to that reference.
The camera does not allow multiple sources to connect to it, hence you will have to disconnect from it before attempting to use it somewhere else.

Referring to Views from different Fragments in same Activity

Let say the target application is built from 3 fragments which are all in the same activity public class MainActivity extends android.support.v4.app.FragmentActivity implements ActionBar.TabListener. Starting fragment is public class ButtonSectionFragment extends Fragment where there is a Button:
public class ButtonSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View rootView = inflater.inflate(R.layout.btn, container, false);
Button mybutton = (Button) rootView.findViewById(R.id.mybutton);
mybutton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
????????????????????
}
});
}
There are ?? in the onClick method, I will get to that. And there is another fragment like this:
public static class TextSectionFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tv, container, false);
TextView textv = (TextView) rootView.findViewById(R.id.texty);
}
Both of the fragments are using different layouts so this is why rootView is being used right in front of findViewById.
The outcome I would like to achieve is: by Button from 1st fragment click set the TextView from 2nd fragment to Hello. What should I put in the ??? part to make it all work?
You need to use interface as callback to the activity then set the textview in fragment.
Two framgents should never communicate directly.
http://developer.android.com/training/basics/fragments/communicating.html.
Framgent1 -->Activity -->Fragment2.
You can comunicate value from fragment2 to activity first then from activity to fragment2. Then set text in fragment2
You just have to use getActivity().findViewById() instead of getView().findViewById()
final TextView tv = (TextView) getActivity().findViewById(R.id.texty);
if (tv != null)
tv.setText("Hello");
Fragments are just branches inside the common layout tree of activity. All fragment views of a common activity can be accessed through Activity.findViewByXXX(). The only complication is that fragments can be dynamically added, removed, replaced, etc. So you to be sure that the needed fragment is already inflated into the layout hierarchy. You can make initialization of the UI in onViewCreated() of the other fragment. That guarantees you the layout has been loaded already.
Fragment frag1 = new ButtonSectionFragment ();
Fragment frag2 = new TextSectionFragment();
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.add(layout, frag1);
ft.add(layout, frag2);
ft.commit();
View frag1RootView = frag1.getView();
View frag2RootView = frag2.getView();
Button btn = (Button)frag1RootView.findViewById(id);
TextView tv = (TextView)frag2RootView.findViewById(id);
untested but... I think that would do it...
EDIT: You should get the root views onActivityCreated(); or it'll throw you a null...
In continuation to Raghunandan's answer, you could check similar implementation in the link.
update TextView in fragment A when clicking button in fragment B
Do not forget to get a reference of the textview of the TextSectionFragment into the MainActivity.

Check if fragment displayed as dialog to change click listener behavior

I am creating an app in which the user should be able add people for meetings.
The structure consists of several fragments managed in the same activity (list_people, person_detail, create_meeting).
I would like to reuse the fragment showing the list of people as a dialog in the create_meeting fragment. And add a person to a meeting by clicking on the person item.
When the list_people fragment is embedded in the view, a click on a person item replace the list_people fragment with a person_detail fragment. This behavior is already implemented with an interface for the main activity.
I am looking for a solution to change the behavior of the click listener whether the list_people fragment is displayed as an embedded fragment or as a dialog. Any ideas of how I could do that?
Any help would be greatly appreciated.Thanks.
Ok I have found a solution. It is to use a constructor (newInstance) for the fragment in which you can pass variables.
public class ListPeopleFragment extends Fragment {
public static ListPeopleFragment newInstance(boolean nested){
ListPeopleFragment f = new ListPeopleFragment();
Bundle args = new Bundle();
args.putBoolean("nested", nested);
f.setArguments(args);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View view = inflater.inflate(R.layout.fragment_list_people, container, false);
boolean nested = false;
Bundle arguments = getArguments();
if (arguments != null)
{
nested = getArguments().getBoolean("nested");
}
displayListViewPeople(view, nested);
return view;
}
}
The displayListViewPeople set the click listener depending on the value of nested.
You instantiate the fragment this way:
ListPeopleFragment nestedFrag = ListPeopleFragment.newInstance(true);

Are UI elements initiated in Fragment or FragmentActivity?

I am porting Activity-type app to Fragments-type app. I am a bit confused where I am supposed to initiate UI elements of the fragment.
For example, if I initiate a button from Fragment class in FragmentActivity class, I get no error.
btnOne = (Button) findViewById(R.id.btn_one);
But, if I try to initiate onClick listener, then I get error of type java.lang.NullPointerException.
Then again, I cannot find method findViewById in the Fragment class.
Am I really forced to initiate in FragmentActivity and to specify listeners in Fragment?
You initiate your button in the fragment usually. An example would be:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View fragmentView = inflater.inflate(R.layout.yourlayout, container, false);
Button yourbutton = (Button) fragmentView.findViewById(R.id.button);
yourbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
//Do your thing
}
}
return fragmentView;
}

Categories

Resources