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);
Related
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);
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);
}
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.
I have an app with fragments working ok,
now I need to implement some pop up views,
Im following this tutorial Example of using PopupWindow
all seems fine, but The pop up is not showing yet, as I have a doubt on how to show the "popupWindow"
my .java:
public class Tab2HeadH1 extends Fragment implements OnClickListener {
//testeo popero
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.tab_2_head_buttons, container,false);
//Buttons
Button buttonNose = (Button) view.findViewById(R.id.button_pop_nose);
buttonNose.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
//aqui tus tareas,,
//LayoutInflater layoutInflater = (LayoutInflater)getBaseContext().getSystemService(LAYOUT_INFLATER_SERVICE); //old errors
LayoutInflater layoutInflater = (LayoutInflater)getActivity().getBaseContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View popupView = layoutInflater.inflate(R.layout.popup, null);
final PopupWindow popupWindow = new PopupWindow(
popupView, LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT);
}
});
Button buttonEye = (Button) view.findViewById(R.id.button_pop_eye);
buttonEye.setOnClickListener(new OnClickListener() {
#Override
public void onClick(final View v) {
// onLoginClicked(v);
Toast.makeText(getActivity(), "ss9 eye",
Toast.LENGTH_SHORT).show();
}
});
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
((TabActivity)getActivity()).setHeader("TAPING APPLICATION");
}
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
switch (v.getId()) {
}
}
}
So what is missing to show my pop up?
thanks a lot!
If you want to generate Custom pop up window you can do this use AlertDialog.Builder
private AlertDialog.Builder scheduleBuilder;
private AlertDialog dialog_Creater;
LayoutInflater myLayoutInflater = getLayoutInflater();
View v = myLayoutInflater.inflate(R.layout.schedule_creator, null); //Replace your layout R.layout.youLayout
scheduleBuilder = new AlertDialog.Builder(v.getContext());
scheduleBuilder.setTitle("Edit your Schedule Memo");
scheduleBuilder.setView(v); //set view
dialog_Creater = scheduleBuilder.create();
dialog_Creater.show();
While you setup the Popup fine, you aren't actually calling any method to show it anywhere. Call whichever one of the following methods suits your needs to open it:
showAtLocation()
showAsDropDown()
showAsDropDown() (Different from #2)
try this:-
PopupWindow pw;
View layout = inflater.inflate(R.layout.popnumber,
(ViewGroup) findViewById(R.id.lytpopuppoppop));
// your popup layout and id of root layout
pw = new PopupWindow(layout, LayoutParams.FILL_PARENT,
LayoutParams.WRAP_CONTENT, true);
// your main layout where you display popup
pw.showAtLocation(findViewById(R.id.lytboard), Gravity.CENTER,
0, 0);
I need to create four different layouts, each one to a page of ViewPagerIndicator.
How can I do this? The ViewPagerIndicator is already working, but I'm using the sample from http://viewpagerindicator.com and there is created a simple TextView for all pages.
See the sample (TestFragment.java):
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
TextView text = new TextView(getActivity());
text.setGravity(Gravity.CENTER);
text.setText(mContent);
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;
}
I need to identify the current page (position) and refer to a related resource layout (XML). Is it possible?
I also need to ensure that all Views of all pages to be loaded at once only one time when I create the activity, allowing values to be updated later.
I appreciate any help!!
Thanks
I found a simple solution which works for me. Here is what I did.
TestFragment.class
public static TestFragment newInstance(String content) {
TestFragment fragment = new TestFragment();
//
// StringBuilder builder = new StringBuilder();
// for (int i = 0; i < 20; i++) {
// builder.append(content).append(" ");
// }
// builder.deleteCharAt(builder.length() - 1);
fragment.mContent = content;
return fragment;
}
Comment out the for loop here. Just get the mContent which we are going to use as a Flag.
Now in your onCreateView() change it as follows,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// TextView text = new TextView(getActivity());
// text.setGravity(Gravity.CENTER);
// text.setText(mContent);
// 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);
Log.i("mContent",mContent);
View view=null;
if(mContent.equalsIgnoreCase("title1"))
{
view = inflater.inflate(R.layout.one, container, false);
}
else if(mContent.equalsIgnoreCase("title2"))
{
view = inflater.inflate(R.layout.two, container, false);
}
else if(mContent.equalsIgnoreCase("title3"))
{
view = inflater.inflate(R.layout.three, container, false);
}
return view;
}
That is all it takes. Now you will be able to inflate your views based on the Title name which we have used as the flag.