In activity I can associate layout xml as following
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
}
how in the onCreate() of Fragment class load an Activity?
I have fragement class as in the following
public class TestFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle saved) {
}
}
If you want to inflate the xml layout inside the fragment you can do it easily with the LayoutInflater:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.your_layout, container, false);
}
For further information read the offical Fragment Reference.
Related
I am learning android developing(novice).I want to add a fragment to the mainActivity with a textview and change the text. This code works perfectly:
public class Fragment1 extends Fragment {
//#Nullable
//#Override
TextView textt;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
((TextView)view.findViewById(R.id.textview)).setText("some text");
return view;
}
}
But this codes invokes null pointer exception:
public class Fragment1 extends Fragment {
//#Nullable
//#Override
TextView textt;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
textt = (TextView)view.findViewById(R.id.textview);
return view;
}
public void updateText() {
textt.setText("some text");
}
}
and:
public class Fragment1 extends Fragment {
//#Nullable
//#Override
View view;
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_fragment1, container, false);
return view;
}
public void updateText() {
((TextView)view.findViewById(R.id.textview)).setText("some text");
}
}
MainActivity:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
Fragment1 frg1 = new Fragment1();
ft.replace(R.id.fragment_container, frg1);
ft.addToBackStack(null);
ft.commit();
frg1.updateText();
}
}
why these codes providing different outcome?
my project
frg1.updateText(); is called before the onCreateView lifecycle method of your fragment is called.. You should call the method after you set
textt = (TextView)view.findViewById(R.id.textview);
View view = inflater.inflate(R.layout.fragment_fragment1, container, false);
textt = (TextView)view.findViewById(R.id.textview);
this.updateText();//here
return view;
Check out fragment lifecycle docs regarding onCreateView
The system calls this when it's time for the fragment to draw its user interface for the first time.
This will happen when fragment is loaded not when it is created in your activity.
Here, the textview is not created when you call the updateText function.
Do such calls inside your fragment. Not on the activity which houses the fragment.
In my application,i have a Fragment class for Facebook login.I want to get this inflated layout from class in my Activity class so that i can display the facebook data in my SideMenu.How to get the layout in Activity class ?
This is my onCreate method
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
fa = super.getActivity();
ll = (LinearLayout) inflater.inflate(R.layout.login_notifications, container, false);
login();
return ll;
}
This is the code for showing layout in fragment:-
private static View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
rootView = inflater.inflate(R.layout.location_layout, container,false);
return rootView;
}
While adding user interface to a fragment it is shown to inflate the view each an every call to onCreateView:
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);
}
}
reference: http://developer.android.com/guide/components/fragments.html#UI
What if I cache the view inflated and return it on next calls such as:
public static class ExampleFragment extends Fragment {
private View mView = null;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if(mView == null){
mView = inflater.inflate(R.layout.example_fragment, container, false);
}else{
//detach mView from previous parent if exist
...
}
return mView;
}
}
Fragment manager already handles such optimizations for you.
onCreateView() is only called when the fragment view needs to be created. When it's already created in a usable state, onCreateView() won't be called.
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.
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;
}
}