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,
Related
Here i implemented a emoji-keyboard.
For the use of this library , activity extended to EmojiCompatActivity.
prepareKeyboard(EmojiCompatActivity activity, EmojiEditText input
This is working well in activity. While using in fragment i tried this one
prepareKeyboard((ActivityName)getActivity,input)
Here is my MainActivity
public class MainActivity extends EmojiCompatActivity {}
And fregment class is
public class PagerEmojKeyboard extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.emoji_keyboard, container, false);
EmojiEditText userMessageInput = getActivity().findViewById(R.id.input_message);
EmojiKeyboardLayout emojiKeyboardLayout=(EmojiKeyboardLayout) v.findViewById(R.id.keyboard_emoj);
emojiKeyboardLayout.prepareKeyboard((MainActivity) this.getActivity(),userMessageInput);
return v;
}
}
You can refer to the base activity as this.activity.
Sometimes, while using fragments, the above method also works.
If you are working in the method onCreateView you shouldn't have problems but if you have other method there is a View variable that you must have as a global for use throughout the class.
the code below named Presenteter is my main class, and I am replacing the Fragment according to questions.
public class Presenteter extends AppCompatActivity {
private final Questions question1Fragment = new Questions();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.present);
FragmentTransaction ft=getSupportFragmentManager().beginTransaction();
ft.setCustomAnimations(android.R.anim.fade_in,android.R.anim.fade_out);
ft.replace(R.id.flPersonalization, question1Fragment);
ft.commit();
}
This is my Questions class. It is getting the questions from REST server. All questions are listed in in one Spinner. I didn't write them here, in order not to confuse you.
public class Questions extends Fragment implements AdapterView.OnItemSelectedListener {
SearchableSpinner spinner;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.question, container, false);
LinearLayout ln=(LinearLayout)view.findViewById(R.id.listLayout);
spinner=(SearchableSpinner)view.findViewById(R.id.spinner1);
}
Finally I want to use Spinner's onItemSelected() event in my Presenter class.
How can I do this?
Thanks in advance.
You could do something like this in your Fragment:
Presenteter p = (Presenteter) getActivity();
// ...
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
p.yourMethod(); // call a method of your Presenteter class
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
I'm glad your question is answered but there is a better way for doing this.
If you are familiar with Rx stuff, that's one way to do it.
If Rx is not your cup of tea, you can use EventBus which simplifies exactly this kind of communication.
Link:
EventBus
I would suggest going for the latter if you are not sure.
my name is Augusto, I'm developing a Messaging/Social app for Android on Android Studio and I implemented SlidingTabLayout (com.google.samples.apps.iosched.ui.widget) with 2 tabs (Chats and Friends ).
I wish to know how to access the views inside the Tabs' xml files on my activity.
I have:
MainActivity.class with activity_main.xml as contentview and the tabs's layouts(friendsview.xml and chatsview.xml) for the tabs.
I want to access tabs' layouts view (e.g: EditText, ImageView) from MainActivity.class, How do I "import" them to the activity?
Thanks!
SlidingTabLayout it's only a layout. To manage content use fragments with FragmentPagerAdapter for your tabs.
Here (link) is nice tutorial for you.
If you follow this example tutorial, you will have
public class Tab1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1,container,false);
return v;
}
}
and to access EditText and other widget inside tab, you need to have reference from view, so change above code to:
public class Tab1 extends Fragment {
private EditText editText;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.chatsview,container,false);
editText = v.findViewById(R.id.YOUR_EDIT_TEXT_ID);
return v;
}
}
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.
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