Lock Fragment rotation to Portrait? - java

I've tried to put this code which was found here into my Class code but I couldn't get it to work. Here's the code I'm working with:
public class Details extends Fragment {
public void onCreate (Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.details, container, false);
return rootView;
}
}
If someone could guide me how/where to put the code in the Hyperlink to work with the code above in order for the Fragment to not rotate and stay in Portrait that would be extremely helpful for me to learn.
Thank you!

its been a while since this is posted, but here is answer which helped me in my case.
In your onCreateView for each Fragment you want to be Portrait add
getActivity().setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
If you need Landscape just change SCREEN_ORIENTATION_PORTRAIT to SCREEN_ORIENTATION_LANDSCAPE
Hope it helps

Related

Best way to handle signUp and Edit Sign up Functionality

I am working on Android. I'm just trying to optimizing my code. Generally I have noticed so many issue which is optimize but due to some limited time we are not optimizing that. Here I have share my problem. basically in all application I have facing this same Issue.
Problem
What is the best way to handle signup Fragment and EditSignUp Fragment?
Description
Generally in my case I am making one single registration.xml and making two different Fragment RegistrationFragment.java and EditRegistrationFragment.java. Just like below.
RegistrationFragment.java
public class RagistrationFragment extends BaseFragment {
private TextView tvEmail;
private TextView tvLeaveIn;
private TextView tvUserName;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.registration, container, false);
return view;
}
public void inilialization()
{
tvUserName=(TextView)getView().findViewById(R.id.tvUserName);
tvLeaveIn=(TextView)getView().findViewById(R.id.tvLeaveIn);
tvEmail=(TextView)getView().findViewById(R.id.tvEmail);
}
EditRegistration.java
public class EditRagistrationFragment extends BaseFragment {
private TextView tvEmail;
private TextView tvLeaveIn;
private TextView tvUserName;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.registration, container, false);
return view;
}
public void inilialization()
{
tvUserName=(TextView)getView().findViewById(R.id.tvUserName);
tvLeaveIn=(TextView)getView().findViewById(R.id.tvLeaveIn);
tvEmail=(TextView)getView().findViewById(R.id.tvEmail);
tvEmail.setEnabled(false);
}
So here code is not reused. It's just a Example.Here I have define one method for initializing UI but this method is not reused. I know you can reuse this kind of method putting in BaseFragment but it's not proper way because baseFragment will extend by all Fragment in Application.initialization() is just one example.
I hope you are clear with my problem
Please give me proper suggestion.
Thanks,

Adding calculator to navigation drawer

I've got a problem. For school project I have to create app in Android Studio with few activities so i decided to add Navigation Drawer, but when i try to create simple calculator to app i can't even reference button or textview. This is code for activity where I want to create calculator. Also I have to add Map Activity and SQLite Database.
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
return rootview;
}
}
Use rootView to find your item when referencing in a Fragment:
public class Calculator_main extends Fragment {
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootview = inflater.inflate(R.layout.calculator, container, false);
//Notice the TextView being referenced with rootView in front of the findViewById statement
TextView namefield = (TextView) rootView.findViewById(R.id.namefield);
return rootview;
}
}
You need to do this when using Fragment, as this is the only way your app can reference an item in your Fragment's XML view
This is however not necessary in an Activity and should not be confused with the method of referencing in a Fragment

Trouble with Fragments in Android Studio

I am trying to make a fragment Java file. The xml fragment name is firstscreenlayout. This is the code.
public class FirstScreenFrag extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
View view = inflater.inflate(R.layout.firstscreenlayout, container, false);
return view;
}
}
The line View view = inflater.inflate(R.layout.firstscreenlayout, container, false); is underlined and says unreachable statement when hovered over. I don't know how to fix this.
I am very new to coding, but am learning in my free time as a student. Please help me! Thanks!
Remove below line
return super.onCreateView(inflater, container, savedInstanceState);
You can return either your inflated view or do super.onCreateView(inflater, container, savedInstanceState);
return only one of them depending upon your requirement
return means your method finished, and the code will not execute after return.
try to remove this.
return super.onCreateView(inflater, container, savedInstanceState);

FragmentStatePagerAdapter NullException

I have two fragments that contains ViewPager. The first fragment that contain viewPager works very well. As I come to second fragment that contain ViewPager, I start to get FragmentStatePagerAdapter NullException. I knew what is the problem, I need to implement onDetach function to get rid this problem. I search around the internet, there is a lots of solutions. And I'm currently using API 19. So maybe this solution very suitable for me. But I don't know how to use the solution.
getSupportFragmentManager().beginTransaction().detach(mFragment1).replace(R.id.main, mFragment2).attach(mFragment2).addToBackStack(null).commit();
What is exactly mFragment1 and mFragment2 and R.id.main ?
Fragment 1
public class fragment1 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_restaurantprocess, container, false);
adapter = new RestaurantProcessPageAdapter(getActivity().getSupportFragmentManager());
...
}
}
Fragment 2
public class fragment2 extends fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
super.onCreateView(inflater, container, savedInstanceState);
View v = inflater.inflate(R.layout.fragment_feedpage, container, false);
adapter = new RestaurantFeedPageFragment(getActivity().getSupportFragmentManager());
...
}
}
I'm using slide animation to fragment
public void changeFragment_toRight(Fragment frag) /* Slide to right screen */ {
if (getSupportFragmentManager().findFragmentByTag("frag")!=null)
getSupportFragmentManager().beginTransaction().setCustomAnimations(R.anim.right_slide_in, R.anim.right_slide_out).replace(R.id.container, frag, "frag").commit();
else
getSupportFragmentManager().beginTransaction().add(R.id.container, frag, "frag").commit();
}
changeFragment_toRight(new fragment1());
changeFragment_toRight(new fragment2());
Source: Click here to view the source
It looks like the problem is that you are manually using a FragmentTransaction to change fragments. A ViewPager will do this automatically when you supply it with an adapter, so when you change fragments, you are trying to remove a fragment that has already been removed.

Android, Java: Bundles & Views for Fragments

I have a fragment, that i want to put a textview which will have a 3 strings stored in it from the 'Main Activity'
The strings that I need to pass will come from the Main Activity and will be passed into FragmentClass
The issue is that its not recognising findViewById, i understand this is something to do with the views? Could someone advise me on how i could get this editText working and then how i can pass a bundle of strings from the MainActivity to the FragmentClass.
#SuppressLint("NewApi")
public class FragmentClass extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(
R.layout.new_fragment, container, false);
// View rootView = inflater.inflate(R.layout.get_data, container, false);
TextView FragmentTextView = (TextView)findViewById(R.id.FragmentTextView);
}
}
Change to
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View view =inflater.inflate(R.layout.new_fragment, container, false);
TextView FragmentTextView = (TextView)view.findViewById(R.id.FragmentTextView);
return view;
}
Also you have #SuppressLint("NewApi") you have suppressed lint warning. Probably indicates that some features are available in new api's only. Do check your min sdk version in manifest

Categories