I am using Fragment for Sliding Menu. Now, I want to extend Activity to get reference of layout.xml and many more. But we can't bind Fragment and Activity together. So what is the way to solve this ?
Fragment Code :
public class FindPeopleFragment extends Fragment {
public FindPeopleFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.becomeexpert, container,
false);
return rootView;
}
}
If I am using extends Fragment then I can't use findViewbyId and many more things. So I am confused as I am using Fragment first time. Please help me regarding this.
Since you have the view inflated you can call rootView.findViewById(...)
I think this is what you are after:
public class FindPeopleFragment extends Fragment {
private View rootView;
private View myTextView;
public FindPeopleFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.becomeexpert, container,
false);
myTextView = rootView.findViewById(R.id.my_textview);
return rootView;
}
private void someFunction()
{
View myButton = rootView.findViewById(R.id.my_button);
}
}
You can save a reference to the views that you need before onCreateView returns, or you can save a reference to the rootView and call findViewById on that later on if you need to.
Related
I have 8 fragments which are similar between each other. I want to make a general fragment to manage all of these. I don´t have a clear idea how to acchieve this idea.
My fragments have the same code, except layout, and 2 or 3 methods. So, I think that the best way is to have this in one fragment for all.
What is the best way.
Fragment 1.
public class Fragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.lm_fragment, container, false);
}
public void method(){}
}
Fragment 2.
public class Fragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.lm_fragment, container, false);
}
public void method2(){}
public void method3(){}
}
Fragment X...
Every fragment has the same structure, only changes between them in the methods of each fragment.
I want to do:
General Fragment to manage all cases, with a helper with the methods or something like that.
use any key like int value and pass that value to getInstance() menthod of fragment and then get that value in onCreateView() and inflate respective layout. problem solve
In your base fragment class add an abstract method that is
public int layoutId() {
return R.layout.fragment2_layout;
}
In your subclasses override that method and return the respective layout.
In your onCreateView you will use the layoutId() method.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(layoutId(), container, false);
}
Inheritance.
Eg. Create a BaseFragment which contains all the common methods.
Then all your other specialized fragments classes that have special methods just extends this BaseFragment.
Eg.
public class BaseFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.lm_fragment, container, false);
}
public void commonMethod(){}
}
public class SpecialFragment extends BaseFragment {
public void specialMethod2(){}
public void specialMethod3(){}
}
I'm new to fragments and they are a bit confusing to me right now.
I have read alot and actually built a test app using this tutorial
I was able to add more tabs and so on. But I don't want to display static
content (xmls). I want to be able to modify the UI add listviews, load json data with asynctasck and so on but the very first attempt I made has failed.
So on Tab1:
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;
}
}
I tried to declare a textview and edit it's text with settext but
I get a crash and also findviewbyid is not available as a method.
It didn't throw and error when I typed getView().findViewById but
that's not the problem.
Are fragments limited or very different from traditional activities?
Do I need to make never ending customizations
in order to get a listview loading?
Thank you.
You should declare your TextView as a Member variable for your class and get a reference to that view in onCreateView like:
public class Tab1 extends Fragment {
// your TextView member variable
private TextView mTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1, container, false);
mTextView = (TextView) v.findViewById(R.id.my_text_view);
return v;
}
// It is safe to access the views here, not in onCreate since it is called before onCreateView
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
mTextView.setText("Hello, World!");
}
}
For reference on the Fragment lifecycle
Are fragments limited or very different from traditional activities?
A Fragment has its own lifecycle like that of an Activity but it is dependent upon the lifecycle of its parent Activity, so to answer your question they are not very different, it is just a good practice for modularizing code.
Do I need to make never ending customizations in order to get a listview loading?
A little unclear what you mean by never ending customizations but loading a dynamic ListView is very simple in Android.
For Example:
using fragment_one.xml we have just a FrameLayout
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fl_list_container"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
And the code:
public class Tab1 extends Fragment {
// your FrameLayout member variable
private FrameLayout mFlParent;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, #Nullable Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_one, container, false);
mFlParent = (FrameLayout) v.findViewById(R.id.fl_list_container);
return v;
}
// It is safe to access the views here, not in onCreate since it is called before onCreateView
#Override
public void onActivityCreated(Bundle savedInstanceState){
super.onActivityCreated(savedInstanceState);
// Create a new list, adapter and add a item click listener
ListView myList = new ListView(context); // context needed
myList.setAdapter(new ArrayAdapter<String>(context, android.R.id.simple_list_item_1, new String []{"Item 1", "Item 2", "Item 3", "Item 4"}));
myList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
//... handle clicks on list items as usual
}
});
// add the view to the FrameLayout
mFlParent.addView(myList); // may want to call mFlParent.removeAllViews(); before adding just to be safe
}
}
Good luck and happy coding.
Fragment doesn't have an activity methods.
If you want to use findViewById methods, you should use a class field.
public class Tab1 extends Fragment {
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
mView = inflater.inflate(R.layout.tab_1,container,false);
return mView;
}
#Override
public void onCreate() {
// here you can use `findViewById` method via `mView` variable
mView.findViewById(YOUR_VIEW_ID);
}
}
There is basically 2 method to be use in fragment
1. onCreateView
2. onViewCreated
First one is use to inflate layout on your fragment
Second one is use to find all view used in your layout.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, #Nullable Bundle savedInstanceState) {
View v =inflater.inflate(R.layout.tab_1, container, false);
return v;
}
#Override
public void onViewCreated(View v){
mTextView = (TextView) v.findViewById(R.id.my_text_view);
mTextView.setText("Hello, World!");
}
I am creating a new Tab interface and for every tab I have functionality like:
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) findViewById(R.id.classList);
return rootView;
}
}
But I am getting cannot resolve method findViewById also at another place I am also unable to use runOnUiThread and getting Error:(88, 29) error: cannot find symbol method runOnUiThread(<anonymous Runnable>)
I understand these will work fine if my class was extended from Activity but it’s a tab fragment so how can I use this funcitons?
you need
classListView = (ListView) rootView.findViewById(R.id.classList);
as that is the view you are concerned with
to get runOnUiThred you need to do getActivity().runOnUiThread
You need to reference findViewById using the view,
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) rootView .findViewById(R.id.classList);
return rootView;
}
}
For runOnUiThread is a possible duplicate of Modifying an Android view from a different thread
use its like,
activity.runOnUiThread(new Runnable());
Here is a shore explanation of findViewById method;
findViewById() method will work with the related object that your main layout is bound to. For example, In an activity, when you make a
setContentView(activity_main);
You imply that your Activity will retrieve its layouts from activity_main. So when you make a findViewById() in an activity, that actually means this.findViewById(), where this is your Activity.
So in your case, the layout class_view is bound to the rootView. But when you just write findViewById, it means that this.findViewById. And this is the scope you're in, which is the Fragment in your case. However, I don't think a Fragment has that capability. So you should refer to the rootView to retrieve your views.
rootView.findViewById()
Fragments don't have a findViewById() method like Activity, so you have to call the method of the root view of the fragment. Try doing rootView.findViewById(R.id.classList) instead.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_content_mediaplayer, container, false);
mSeekbar = (SeekBar) v.findViewById(R.id.seekBarMusic);
return v;
Use rootView.findViewById(R.id.classList) instead of findViewById(R.id.classList).
Try this:
public class ClassViewFragment extends Fragment {
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.class_view, container, false);
classListView = (ListView) rootView.findViewById(R.id.classList);
return rootView;
}
}
findViewById must run inside onViewCreated() method
ListView classListView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.class_view, container, false);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
classListView = (ListView) view.findViewById(R.id.classList);
}
}`
In my app initially I created one class "abc.java" which extends Activity and do some functionality..
But now I have decided to implement TabView and abc.java should be first tab in tabview..
My problem is my abc.java class extends activity and now when I change it to fragments , it gives me errors.
If I implement TabActivity , which is deprecated now, it will work fine, but now if I want to use fragment what changes I have to do?
I know I have to make some changes in abc.java but I have no clue how to do that..I am new to android and trying hard to get this done..Any help would be great!!
here is my code for class "abc.java"
public class CountAndMarkPage extends Activity
{
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.count_mark);
//lots of code here
}
}
You'll need to extend Fragment and override onCreateView using the same layout resource. Have a look at this info from docs.
Quick example:
public class CountAndMarkPage extends Fragment {
public void onCreate(Bundle savedInstanceState){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
// Inflate your layout
View myView = inflater.inflate(R.layout.count_mark, container, false);
// Any additional initialization
return myView;
}
}
To access widgets within this layout, you'll need to use findViewById on the inflated root view like so
public class CountAndMarkPage extends Fragment {
public void onCreate(Bundle savedInstanceState){
}
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState){
// Inflate your layout
ViewGroup myView = (ViewGroup) inflater.inflate(R.layout.count_mark, container, false);
// Any additional initialization
TextView myTextView = (TextView) myView.findViewById(R.id.my_view);
// And so on
return myView;
}
}
I am trying to implement Fragments in my Project using the https://github.com/JakeWharton/Android-ViewPagerIndicator plugin.
My Fragment
public final class NearByFragment extends Fragment {
private static String TAG = "bMobile";
public static NearByFragment newInstance(String content) {
NearByFragment fragment = new NearByFragment();
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
Log.d(TAG, "onCreate");
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
return inflater.inflate(R.layout.fragment_search_nearby, null);
}
}
Now I want to execute some code like start start a new thread and download a JSON from the server and then assign a list view from the content I download. In fragments we are assigning views in onCreateView. so where should I write the
listview = (ListView) findViewById(R.id.list_items);
((TextView) findViewById(R.id.title_text))
.setText(R.string.description_blocked);
and other code to generate the Fragment view ?
You can search within the view that you just inflated:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.d(TAG, "onCreateView");
View v = inflater.inflate(R.layout.fragment_search_nearby, null);
listview = (ListView)v.findViewById(R.id.list_items);
((TextView)v.findViewById(R.id.title_text))
.setText(R.string.description_blocked);
return v;
}
You can also use the Fragment.getView() function elsewhere in your code and call that view's findViewById() member.
You can use getActivity().findByView() from the Fragment if you want to have access to the layout elements. Alternatively, you can just put the findByView() call in the main Activity.