How to load layout in code from xml file - android

I'm using ViewPageIndicator and I have a problem with using XML layout in code. Usually I'm using setContentView(R.layout.activity_main).
Of course I can create layout in code but I prefer to use XML files.
public final class FragmentOne extends Fragment {
private static final String KEY_CONTENT = "FragmentOne";
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if ((savedInstanceState != null) && savedInstanceState.containsKey(KEY_CONTENT)) {
mContent = savedInstanceState.getString(KEY_CONTENT);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText("some text");
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
LinearLayout layout = new LinearLayout(getActivity());
layout.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
layout.setGravity(Gravity.CENTER);
layout.addView(text);
return layout;
}
}

In onCreateView() you can use the LayoutInflater passed in as the first argument to inflate the layout.
Your code would look something like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.my_fragment_layout, container, false);
}

Related

Button array in Fragment

I am new to Android Programming. I want to add an array of buttons fragment layout dynamically.
Activity:
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_list_dir,
container, false);
LinearLayout linearLayout=new LinearLayout(getActivity());
LayoutParams param=new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(param);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LayoutParams param2=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
Button dirButton[]=new Button[5];
for(int i=0;i<5;i++)
{
dirButton[i].setLayoutParams(param2);
linearLayout.addView(dirButton[i]);
}
ViewGroup vg=(ViewGroup)rootView;
vg.addView(linearLayout);
return rootView;
}
}
But this is causing Null pointer exception. But if only one button is used then it works like:
Button dirButton=new Button(getActivity());
Is there way to achieve the button array?
Arrays don't work like that. Try something like this:
Button[] buttonArray = new Button[5];
for(int i = 0; i < 5; i++)
{
buttonArray[i] = new Button(getActivity());
buttonArray[i].setLayoutParams(params);
linearLayout.addView(buttonArray[i]);
}
public static class PlaceholderFragment extends Fragment {
private static final String LOG_TAG = PlaceholderFragment.class.getSimpleName();
public PlaceholderFragment() { }
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_my, container, false);
LinearLayout linearLayout = new LinearLayout(getActivity());
LinearLayout.LayoutParams param = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(param);
linearLayout.setOrientation(LinearLayout.VERTICAL);
LinearLayout.LayoutParams param2 = new LinearLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
Button dirButton[] = new Button[5];
for(int i = 0; i < 5; i++) {
dirButton[i] = new Button(getActivity());
dirButton[i].setLayoutParams(param2);
linearLayout.addView(dirButton[i]);
}
ViewGroup vg = (ViewGroup) rootView;
vg.addView(linearLayout);
return rootView;
}
}
When you have an array of objects, the default value is null . You forgot to actually create the Button. Here is the part you needed to change :
dirButton[i] = new Button(getActivity());
dirButton[i].setLayoutParams(param2);

Can't inflate layout properly for a fragment in Android application

In first place this is the code of the fragment:
public class MyFragment extends Fragment {
private View FragmentView;
public static Fragment newInstance() {
MyFragment NewFragment = new MyFragment();
return NewFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
FragmentView = inflater.inflate(
R.layout.tasks,
new LinearLayout(MainActivity.MainContext)
);
FragmentView.setLayoutParams(
new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.MATCH_PARENT
)
);
addText();
return FragmentView;
}
private void addText() {
TasksView.setBackgroundResource(R.drawable.bg_image);
TextView MyTitle = new TextView(MainActivity.MainContext);
MyTitle.setLayoutParams(
new LayoutParams(
LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT
)
);
MyTitle.setText(R.string.mytitle);
((LinearLayout) FragmentView).addView(MyTitle );
}
}
MainActivity.MainContext is stored as public static in the main activity's class and is retrieved when creating the activity by getApplicationContext().
Now, the problem lays here:
FragmentView = inflater.inflate(
R.layout.tasks,
new LinearLayout(MainActivity.MainContext)
);
Because of this, the MyTitle text view is not displayed inside the fragment. If I do this:
FragmentView = inflater.inflate(R.layout.tasks, null);
it is displayed but I get a warning:
Avoid passing null as the view root (needed to resolve layout parameters on the inflated layout's root element)
I would like do it properly and not get any warnings and I don't find a way. I also tried:
FragmentView = inflater.inflate(R.layout.tasks, container);
but the application crashes.
You should pass container as a parent.
FragmentView = inflater.inflate(
R.layout.tasks,
container,
false
);
Also you should specify false, so LayoutInflater does not attach the inflated view to parent. The inflated view is attached to parent by FragmentManager.

fragments use assets with swipe views appcompat

I am using fragments and I canĀ“t use the assets to change my letter. I am using swipe page with appcompat. Here is my code:
public class AcercaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.acerca, container, false);
TextView logouno = (TextView) getView().findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
return view;
}
}
Try with this little change:
public class AcercaFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.acerca, container, false);
TextView logouno = (TextView) view.findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity()
.getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
return view;
}
}
The way you applied your font was ok, but you must get your TextView from the View view.
You can't access views from getView() in your onCreateView method. In here, you should only return an inflated view:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
return inflater.inflate(R.layout.acerca, container, false);
}
Then, initialize the UI elements like so:
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
TextView logouno =(TextView) getView().findViewById(R.id.logouno);
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/Roboto-Thin.ttf");
logouno.setTypeface(typeface);
}

Null Pointing when adding to a fragment's layout

I am getting a Null Pointer Exception when trying to add a textView programmatically to a fragment.
I think the context is coming back null but I could be wrong. The crash is happening at startMenu.addView(tv);
public class TabFragment1 extends Fragment {
View inflatedView;
Context context = null;
private Bundle mBundle;
private LinearLayout startMenull ;
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
if (inflatedView != null) {
ViewGroup parent = (ViewGroup) inflatedView.getParent();
if (parent != null)
parent.removeView(inflatedView);
}
try {
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
//startMenu = (LinearLayout) getActivity().findViewById(R.id.start_menull);
startMenu = (LinearLayout) getLayoutInflater(mBundle).inflate(R.id.start_menull, null);
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenu.addView(tv);
} catch (InflateException e) {
}
return inflatedView;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mBundle = savedInstanceState;
}
}
Updated above code 9/16 3:48
Solved:
in the try/catch
inflatedView = inflater.inflate(R.layout.start_menu, container,
false);
context = getActivity().getApplicationContext();
startMenull =((LinearLayout) inflatedView.findViewById(R.id.start_menull));
TextView tv = new TextView(context);
tv.setId(1);
tv.setText("Here is the text Box");
startMenull.addView(tv);
context cant be null at that point, since he is using applicationContext.
but start Menu IS! null since onCreate is called before onCreateView. So you would have to inflate the View first, which you should do in onCreateView not onCreate.
Just Noticed you are inflating the View in onCreateView, but still you cant Access it in onCreate as it is called before onCreateView.
See Android Life Cycle. http://developer.android.com/images/fragment_lifecycle.png
public class TabFragment1 extends Fragment {
View inflatedView;
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
inflatedView = inflater.inflate(R.layout.start_menu, container,false);
LinearLayout startMenu = (LinearLayout) inflatedView.findViewById(R.id.start_menu);
TextView tv = new TextView(getActivity());
tv.setId(1);
tv.setText("Here is the text view");
startMenu.addView(tv);
return inflatedView;
}
}
If i wanted to do something like you are doing, this would b how i'd do it. Why would u check if container is null? or why would you assume that inflatedView is != null?
onCreate calls before onCreateView. Read about lifecycle.

Fragmentation Layout inflater issue

hello guys..
i want to add two layout in my fragmentation .one layout already inflated after that second layout inflate on first layout button click...well i tried to remove first layout on button click of first layout button and inflate second layout but my button click does not response any type of reaction..please help me
public static LinearLayout mLL_MyNotes,mLin_myNotes,mLL_UploadNotes;
public static ViewFlipper mVF_MyNotes,mViewUploadMyNotes;
public static View mViewMyNotes;
#Override
public View onCreateView(final LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setTextSize(20 * getResources().getDisplayMetrics().density);
text.setPadding(20, 20, 20, 20);
if(mContent.equalsIgnoreCase("My Notes")){
mViewMyNotes = inflater.inflate(R.layout.activity_myspace_mynotes_list, container, false);
mLin_myNotes=(LinearLayout)mViewMyNotes.findViewById(R.id.mainLin_MyNotes);
mVF_MyNotes = (ViewFlipper) mViewMyNotes.findViewById(R.id.vf_MySpace_MyNotes);
mbtn_Upload_MyNotes = (Button) mViewMyNotes.findViewById(R.id.btnUpload_MySpace_MyNotes);
mWV_MyNotes = (WebView) mViewMyNotes.findViewById(R.id.wv_MySpace_MyNotes);
mLV_MyNotes = (ListView) mViewMyNotes.findViewById(R.id.lv_MySpace_MyNotes);
mLV_MyNotes.setAdapter(new MySpace_MyNotesAdapter(getActivity(),R.layout.activity_myspace_mynotes_listitem));
mbtn_Upload_MyNotes.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
LayoutInflater Newinflater = (LayoutInflater) getActivity().
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mViewUploadMyNotes==Newinflater.inflate(R.layout.activity_myspace_mynotes_uploades,null);
mLL_UploadNotes = (LinearLayout) mViewUploadMyNotes.findViewById(R.id.llUpload_MyNotes);
((ViewGroup) mLin_myNotes.getParent()).removeView(mViewUploadMyNotes);
mLin_myNotes.addView(mViewUploadMyNotes);
}
});
return mViewMyNotes;

Categories

Resources