How to use base activity in fragment? - java

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.

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,

Are Android Fragments View or ViewGroup

As the question reads, Fragments in android are View or ViewGroup. Can anyone explain
Here is the onCreateView method of Fragment from docs
public static class ExampleFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
The container parameter passed to onCreateView() is the parent
ViewGroup (from the activity's layout) in which your fragment layout
will be inserted
And
To draw a UI for your fragment, you must return a View from this
method that is the root of your fragment's layout.
You cannot define fragments as views or viewgroups.Fragments are more than that.The easiest way to visualize fragment is to think of them as chunks of an activity having own xml appearance,own behaviour with own life cycle callbacks.They always run on top of an activity allowing you to perform operations such as add,replace etc on them at run time.This way you can switch between your layouts effectively and efficiently.
To understand onCreateView method,consider the following explanation:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState){
View view =inflater.inflate(R.layout.example_fragment, container, false);
return view;
}
this returns a single View object, always a ViewGroup, with the set of View object that defines the Fragment's UI. The Activity calls this event handler when it is time for the Fragment to provide its UI for display.
Neither. Fragment is a base class.
From https://developer.android.com/guide/components/fragments.html
A Fragment represents a behavior or a portion of user interface in
an Activity
Fragment contains a field:
// The View generated for this fragment.
View mView;
that is generated in onCreateView which has the implementation:
#Nullable
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
Bundle savedInstanceState) {
return null;
}
So if you want a Fragment with a View then #Override that method. And then the Fragment can be shown to the user if you use the appropriate fragment transaction from an Activity or nested Fragment.
fragment is a wrapper over a custom view thats tied to host activity lifecycle.
Fragments are a kind of Activity that don't take full screen,it can just split the screen into many fragments,so it is well used for phone and tablet .
i think is no,activity(fragment) control what the view shows,viewGroup extends view and implements ViewManger.Only Activity loads related Views can the phone show the layout.Activity is a Group,view can work normally in this container

How to acces SlidingTabLayout XML views on Android?

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;
}
}

How to get string array from xml to fragment's java file?

Below code shows null pointer exception..
public class MyFragment extends Fragment {
private String[] eArray = getActivity()
.getResources()
.getStringArray(R.array.English);
What are the other options, searching in stackoverflow did not give me success..
I am using FragmentPagerAdapter also.. which is not related to this eArray.. but it contains data for images.
getActivity() is returning null because you are using it before fragment get attached to Activity.
You need to initialize this at onCreateView
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
eArray = getActivity().getResources().getStringArray(R.array.English);
}

Trying to create a fragment class

I'm trying to create a fragment class following from this:
And they give you the code for a fragment class, but I'm getting
ExampleFragments.java
package com.example.learn.fragments;
public static class ExampleFragments extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.blue_pill_frag, container, false);
}
}
But I'm getting an error on line 9, where I declare the name of the class.
Illegal modifier for the class ExampleFragments; only public, abstract & final are permitted
I'm sure it's something basic that I'm not understanding, thanks.
You can't have a static top-level class. Change
public static class ExampleFragments extends Fragment {
to
public class ExampleFragments extends Fragment {
In the example you're following, the static modifier is used because the Fragment is a nested class.
To get rid of the error you can include that fragment into an existing class, or remove the static modifier.

Categories