How can I change the fragment in running? - android

Hy, I have this code in my Fragment:
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fr_menu,container,false);
final ViewGroup dismissableContainer = (ViewGroup)view.findViewById(R.id.dismissable_container);
for (int i = 0; i < 3; i++) {
final Button dismissableButton = new Button(getActivity());
dismissableButton.setLayoutParams(new ViewGroup.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
dismissableButton.setText("Button " + (i + 1));
dismissableContainer.addView(dismissableButton);
}
return view;
}
in the same fragment I have a method that receives an object from the outside
public void sendObject(MyObject t){
Button b=new Button(getActivity());
b.setText(t.getNum_tav());
// Now I want to put **b** under button1 button2 and button3
}
in the method sendobject ... How can I change the fragment?
I have to put the button with the other 3 button

Just add it to the same ViewGroup, assuming it's a vertical LinearLayout:
getView().findViewById(R.id.dismissable_container).addView(b);
If it's not, then you'll have to work out the layout to prepare a suitable position for it, but it should be conceptually similar.

Related

Android - Getting Objects in Multiple RootView

public class OccuMechanical extends android.support.v4.app.Fragment {
private View v_schedule;
private LinearLayout layout_schedule;
private ImageButton iv_add_schedule;
private int i = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_occu_mechanical, container, false);
layout_schedule = (LinearLayout) v.findViewById(R.id.layout_mech_schedule);
iv_add_schedule = (ImageButton) v.findViewById(R.id.iv_add_schedule);
iv_add_schedule.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
i++;
v_schedule = LayoutInflater.from(getActivity()).inflate(R.layout.layout_mech_schedule, null);
layout_schedule.addView(v_schedule);
EditText et = (EditText) layout_schedule.getRootView().findViewById(R.id.layout_description);
et.setText("Test: " + String.valueOf(i));
}
});
return v;
}
}
idk if my term is right in the title but here's what im doing...
when I click the 'Add more equipment' it adds layout to my app so
I want to get all the EditText inside the layout i added in the RootView,
I tried setting the text to test it but the first editText of the first rootView is the only one updating. All the codes in my fragment listed above. attached image is the result of this code.
image result
I've done this task by using *RecyclerView as #hjchin suggested. Thank You!
RecyclerView Tutorial # Developer.Android

How to display custom dialog with dynamic table layout?

I want to create the table layout that is created dynamically inside the custom dialog. Can anyone tell me how to do this?
I did some thing but nothing is showing within the dialog. I had no table layout within that dialog.
Can anyone tell me where I made the mistake, please?
This is my activity:
LayoutInflater inflater = getActivity().getLayoutInflater();
View dialog_view = inflater.inflate(R.layout.progressdialog, null);
AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
dialog.setView(dialog_view);
TableLayout table_layout = (TableLayout)dialog_view.findViewById(R.id.Table_view_in_dialog);
for (int i = 1; i <= 4; i++) {
TableRow row = new TableRow(getActivity());
row.setLayoutParams(new LayoutParams(LayoutParams.MATCH_PARENT,
LayoutParams.WRAP_CONTENT));
// inner for loop
for (int j = 1; j <= 4; j++) {
TextView tv = new TextView(getActivity());
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tv.setBackgroundResource(R.drawable.cell_shape);
tv.setPadding(5, 5, 5, 5);
tv.setText("R " + i + ", C" + j);
row.addView(tv);
}
table_layout.addView(row);
}
AlertDialog alert = dialog.create();
alert.show();
Why don't you use dialog fragment , http://developer.android.com/reference/android/app/DialogFragment.html
This is best to use as you can manage this like fragment as this has own UI
as
public class SearchWeeklyAlertDialog extends DialogFragment
{
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.NewDialog);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.dialog_serach_days, container);
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
// your stuff like find ids and perform operations
}
}
and You can call this as
SearchWeeklyAlertDialog dialog = new SearchWeeklyAlertDialog();
dialog.show(getChildFragmentManager(), DIALOG_FRAGMENT);

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

setting the background colour from program rather than from xml for fragments

I need some help in setting the background colour using code for my current fragment. The fragment is created on selecting a particular tab.
Below is my main activity code
public class TabActionBarActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set the text and background colour
setTheme(R.style.MyTheme);
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
String label6 = getResources().getString(R.string.label6);
tab = actionBar.newTab();
tab.setText(label6);
TabListener<Tab6Fragment> tl6 = new TabListener<Tab6Fragment>(this,
label6, Tab6Fragment.class);
tab.setTabListener(tl6);
actionBar.addTab(tab);
String label7 = getResources().getString(R.string.label7);
tab = actionBar.newTab();
tab.setText(label7);
TabListener<Tab7Fragment> tl7 = new TabListener<Tab7Fragment>(this,
label7, Tab7Fragment.class);
tab.setTabListener(tl7);
actionBar.addTab(tab);
}
Now the code for the Tab7Fragment class looks like this
public class Tab7Fragment extends Fragment {
TextView tv1;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
ScrollView sv = new ScrollView(this.getActivity());
LinearLayout ll = new LinearLayout(this.getActivity());
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
TextView tv = new TextView(this.getActivity());
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
EditText et = new EditText(this.getActivity());
et.setText("weeeeeeeeeee~!");
ll.addView(et);
Button b = new Button(this.getActivity());
b.setText("I don't do anything, but I was added dynamically. :)");
ll.addView(b);
for(int i = 0; i < 20; i++) {
CheckBox cb = new CheckBox(this.getActivity());
cb.setText("I'm dynamic!");
ll.addView(cb);
}
return this.getView();
}
}
Now how can i set the view for this fragment?
this.getActivity().setContentView(sv);
I know the above method is incorrect. But i need to set the content view with the scrollview layout. and another question is how do i set the background colour for this view?(using setBackgroundColor()?
Try this code in your onCreateView():
View view = inflater.inflate(R.layout.your_fragment_layout, container, false);
Then dont forget to return the view that you just inflated to the android runtime.
Remove return this.getView(); and add return view;
To set background color :
sv.setBackgroundColor(getRessources().getColors(R.color.yourcolor));
To inflate layout into a fragment, you need to return a View from onCreateView :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.your layout, container, false);
}

What to do in the Fragment onCreateView Method?

What I have are 3 Layouts.
fragment_tag, fragment_stunde and fragment_fach
fach should be in stunde and stunde in tag.
And in fach are some TextViews I want to set the text.
So my code so far is:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
database = new Database(context);
database.open();
for (int j = 1; j < 12; j++) {
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
String[][] vplanDaten = database.getVplan(Integer.valueOf(DateHelper.get(DateHelper.WEEK_OF_YEAR)), index + 1, j);
for (int k = 0; k < vplanDaten.length; k++) {
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
TextView fach_lehrer = (TextView) fach.findViewById(R.id.textView_lehrer);
TextView fach_fach = (TextView) fach.findViewById(R.id.textView_fach);
TextView fach_raum = (TextView) fach.findViewById(R.id.textView_raum);
fach_lehrer.setText(vplanDaten[k][0]);
fach_fach.setText(vplanDaten[k][1]);
fach_raum.setText(vplanDaten[k][2]);
}
}
database.close();
return tag;
}
But this gives me an Error:
java.lang.ClassCastException: android.support.v4.view.ViewPager cannot be cast to android.widget.LinearLayout
at de.MayerhoferSimon.Vertretungsplan.TagFragment.onCreateView(TagFragment.java:56)
So how do I have to change the code that that what I want to do happens?
Try making the following changes:
LinearLayout tag = (LinearLayout)inflater.inflate(R.layout.fragment_tag, container, true);
to:
LinearLayout tag = (LinearLayout)(inflater.inflate(R.layout.fragment_tag, null)).findViewById(R.id.idOfTag);
Similarly:
LinearLayout stunde = (LinearLayout) inflater.inflate(R.layout.fragment_stunde, tag, true);
to:
LinearLayout stunde = (LinearLayout)(inflater.inflate(R.layout.fragment_stunde, null)).findViewById(R.id.idOfStunde);
And:
LinearLayout fach = (LinearLayout) inflater.inflate(R.layout.fragment_fach, stunde, true);
to:
LinearLayout fach = (LinearLayout)(inflater.inflate(R.layout.fragment_fach, null)).findViewById(R.id.idOfFach);
Change the LinearLayout to a View like such:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View tag = inflater.inflate(R.layout.fragment_tag, container, true);
//Everything else...
return tag;
}
Also I believe you can only inflate one view per onCreateView otherwise the layouts will replace one another. You need to have three separate fragments that each inflate its own view and call these fragments individually from the top parent activity.

Categories

Resources