Been trying to figure this out for a while now:
I'm trying to create a custom dialog using the standard approach shown on the Dev site.
public class CustomDialog extends DialogFragment {
private OnDialogResultListener mOnDialogResultListener = null;
public void setOnDialogResultListener(OnDialogResultListener dialogResultListener) {
mOnDialogResultListener = dialogResultListener;
}
public static CustomDialog newInstance(OnDialogResultListener dialogResultListener) {
CustomDialog frag = new CustomDialog();
frag.setOnDialogResultListener(dialogResultListener);
return frag;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
getDialog().setTitle(getString(R.string.Dialog_CustomDialog_Title));
View v = inflater.inflate(R.layout.customdialog, container, false);
return v;
}
}
With the XML being:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
<EditText android:hint="#string/Dialog.CustomDialog.OldPassword" android:layout_marginBottom="#dimen/normalMargin" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText02" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.NewPassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/EditText01" android:layout_width="fill_parent"></EditText>
<EditText android:hint="#string/Dialog.CustomDialog.RetypePassword" android:layout_height="wrap_content" android:inputType="textPassword" android:id="#+id/editText1" android:layout_width="fill_parent">
<requestFocus></requestFocus>
</EditText>
<LinearLayout android:layout_height="wrap_content" android:id="#+id/linearLayout1" android:layout_width="fill_parent">
<Button android:layout_height="wrap_content" android:text="#string/save" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_save"></Button>
<Button android:layout_height="wrap_content" android:text="#string/cancel" android:layout_width="#dimen/normalButtonWidth" android:id="#+id/btn_Custom_cancel"></Button>
</LinearLayout>
</LinearLayout>
And after creating and showing the dialog, I'm left with:
The white background has been applied to emphasize the unexpected and unwanted behavior.
Any ideas? I've tried changing width/heights, using weights in a horizontal LinearLayout, setting width/height programatically - all to no avail.
I found this tricks:
getDialog().getWindow().setBackgroundDrawableResource(R.color.white);
Looks like better solution.
I did come up with a cheap fix, which I would rather not have to use.
Adding a 0px height, 2000dp (some huge number) width view anywhere in the layout causes it to fill the dialog frame.
With the DialogFragment the width and height are null. Create an extra sub view group with fill parent and it should work for you.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent"
android:layout_width="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_height="fill_parent" android:background="#color/white" android:layout_margin="0px" android:layout_width="fill_parent">
.....
If you set attachToRoot = true during inflater.inflate it should show dialog correctly.
View v = inflater.inflate(R.layout.customdialog, container, true);
I used a relativeLayout as the root view in my case and it actually filled out the dialog windows.
Related
I create a Fragment, but I can't even move item.
If item is in the middle of my Fragment everything is correct, but if I try to add something that thing is invisible in my fragment.
Some pictures:
This is what I need
but I get something like that:
As you can see, button is different and if I try to move(button) it become invisible
Code
fragment_user_account_details.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
style="#style/Theme.Design.NoActionBar"
>
<Button
android:id="#+id/btnPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="78dp"
android:layout_marginEnd="74dp"
android:text="Pass"
android:textSize="30sp" />
<TextView
android:id="#+id/profileTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Profile Fragment"
android:textSize="30sp" />
</RelativeLayout>
UserAccountDetails class
public class UserAccountDetails extends Fragment
{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState)
{
View v = inflater.inflate(R.layout.fragment_user_account_details, container, false);
String bundle = getArguments().getString("UserDetails");
UserLogInData userLogInData = new Gson().fromJson(bundle, UserLogInData.class);
TextView txt = v.findViewById(R.id.profileTextView);
txt.setText(userLogInData.getUsername());
Button btnPass = v.findViewById(R.id.btnPassword);
btnPass.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View x) {
startActivity(new Intent(UserAccountDetails.this.getContext(), ChangePasswordPop.class));
}
});
return v;
}
}
I use this tutorial:
https://codinginflow.com/tutorials/android/navigation-drawer/part-3-fragments
Everything except name of my Class is the same.
Let me know if you need more info
The Root layout in your fragment is a Relative Layout, as the name suggests, this layout will set the position of the items either relative to itself or other child items in the layout,
If you want to move something then change its position with respect to another item in the RelativeLayout.
for example:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_green_light"
style="#style/Theme.Design.NoActionBar">
<Button
android:layout_below="#+id/profileTextView"
android:id="#+id/btnPassword"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="78dp"
android:layout_marginEnd="74dp"
android:text="Pass"
android:textSize="30sp" />
<TextView
android:id="#+id/profileTextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Profile Fragment"
android:textSize="30sp" />
</RelativeLayout>
Here the button will move below the text, similarly you can make it go above
Relative Layout takes tags like android:layout_above="#id/yourID, android:layout_below='#id/yourID' and there are more tags to place your Buttons or TextViews you want to place with them as it is used to place things in relation to other layouts or Buttons and TextViews, etc.
I have a very simple Dialog:
On click of the button I would like to toggle visibility of the second EditText:
public class SomeDialog extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View root = inflater.inflate(R.layout.some_dialog, container, false);
EditText editSecond = root.findViewById(R.id.text_second);
Button buttonOk = root.findViewById(R.id.button_ok);
buttonOk.setOnClickListener(v -> {
editSecond.setVisibility(editSecond.getVisibility() == View.VISIBLE ? View.GONE : View.VISIBLE);
});
return root;
}
}
I would like it to be animated smoothly, so I set "animateLayoutChanges" to "true":
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:animateLayoutChanges="true"
>
<TextView
android:id="#+id/titleText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:padding="8dp"
android:text="Title"
android:textAppearance="#style/TextAppearance.AppCompat.Large.Inverse"
android:textSize="18sp"
/>
<EditText
android:id="#+id/text_first"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="First"
/>
<EditText
android:id="#+id/text_second"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Second"
/>
<Button
android:id="#+id/button_ok"
style="#style/Widget.AppCompat.Button.Colored"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginTop="8dp"
android:text="Ok"
/>
</LinearLayout>
The dialog gets animated, but unfortunately not in the correct order.
What happens:
the TextEdit vanishes
the Dialog height snaps (instantly) to the smaller height
the Button moves up (animated)
So between 2) and 3) the button is not visible and slowly slides into the (already smaller) dialog from offscreen (bottom).
What can be done about this?
(Code Sample works and can be copy/pasted into Android Studio as is)
I'm new to Android, I have a fragment which have this XML code
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="16dp"
android:orientation="vertical"
android:id="#+id/linearRoot"
>
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/cv"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_name"
android:layout_alignParentTop="true"
android:textSize="30sp"
android:text="bonjour"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/person_age"
android:layout_below="#+id/person_name"
android:text="ce test a réussi"
/>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
</ScrollView>
With this one, I want to add several CardView Dynamically.So, as a test, I'm trying to add a second CardView in the LinearLayout this way :
public class TopRatedFragment extends Fragment {
LinearLayout ll;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_top_rated, container, false);
ll = (LinearLayout)rootView.findViewById(R.id.linearRoot);
CardView cv = new CardView(getActivity().getApplicationContext());
ll.addView(cv);
return rootView;
}
}
but it does nothing at all. I'm wondering what is the right way to do this.
you have to add LayoutParams (width, height) in order to actually show your CardView
cv.setLayoutParams(new ViewGroup.LayoutParams(20,20));
for example with 20dp width and height
in my application I use SherlockActionBar with tab navigation.
In the activity MyFragmentActivity that extend SherlockFragmentActivity I add 3 tab with the fragment
mTabsAdapter.addTab(
bar.newTab().setText(getString(R.string.filmtab)),
FragmentFilm.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.cinematab)),
FragmentCinema.class, null);
mTabsAdapter.addTab(
bar.newTab()
.setText(getString(R.string.dintornitab)),
FragmentPdi.class, null);
The FragmentPdi class have this code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_pdi_info, container, false);
return view;
}
and the layout is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/list_places"
android:layout_width="wrap_content"
android:layout_height="fill_parent" >
</ListView>
</RelativeLayout>
In the MyFragmentActivity I want find the listview for populate it in this way:
final ListView elencoPlaces = (ListView)findViewById(R.id.list_places);
....
but I have NullPointerException!
Why??
In the other fragment (FragmentFilm and FragmentCinema) I have other view like textview without this
problem!!
Please help me!
Thank you.
P.S. Is this the correct way for populate dinamically the views in fragment, or I must populate the view in the fragment class?? If the second how I can pass varible from the activity to the fragment??
if all you have in the layout file is a listview, don't bother overriding oncreateview. the default layout will give you that much. then you can use the methods built into the ListFragment - like getListView() and setListAdapter().
Ok, but I dont undestand why I have another fragment the FragmentCinema where I have this xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:paddingTop="10dip" android:id="#+id/otherCinemas" android:layout_width="fill_parent" android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout android:orientation="horizontal" android:id="#+id/plotSeparatorOtherCin" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_below="#id/txtProgrammation">
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="20.0dip" android:layout_height="wrap_content" android:layout_marginRight="5.0dip" />
<TextView android:layout_gravity="center_vertical" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="#string/altricinema" android:textStyle="bold" />
<ImageView android:id="#+id/showOtherCinemas"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_action_freccia_cinema"
/>
<ImageView android:layout_gravity="center_vertical" android:background="#drawable/palm_divider_line" android:layout_width="fill_parent" android:layout_height="wrap_content" />
</LinearLayout>
<ListView
android:id="#+id/listOtherCinemas"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_below="#id/plotSeparatorOtherCin"
android:visibility="gone" >
</ListView>
</RelativeLayout>
and this code of class:
public class FragmentCinema extends SherlockFragment
{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.tab_cinema_info, container, false);
return view;
}
}
and when I use the findViewbyId
final ListView cinemasOther = (ListView)findViewById(R.id.listOtherCinemas);
it works! But in FragmentPdi dont work!!
If you want to use ListView in ListFragments, your ListView must have id android:id="#+id/android:list"!
I have created a custom dialog, the code is below. The problem is that, the height of the dialog is becoming wrap_content, i.e it is independent of what ever the height i mention in the xml. I have checked other questions, they din't help me.
public class PointsDialogFragment extends DialogFragment{
private static final String TAG = PointsDialogFragment.class.getSimpleName();
public static PointsDialogFragment newInstance(){
PointsDialogFragment pdf = new PointsDialogFragment();
Bundle newBundle = new Bundle();
pdf.setArguments(newBundle);
return pdf;
}
private View mRoot;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
getDialog().requestWindowFeature(STYLE_NO_TITLE);
mRoot = inflater.inflate(R.layout.fragment_points_dialog, null);
return mRoot;
}
}
and the xml for fragment_points_dialog.xml is
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="313dp"
android:layout_width="fill_parent"
android:background="#color/green_gradient_start"
>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="242dp"
android:background="#drawable/reward_box"
android:id="#+id/reward_box"
android:layout_alignParentLeft="true"
>
<TextView
android:layout_centerInParent="true"
style="#style/WallSectionHeading"
android:text="5"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="173dp"
android:layout_height="250dp"
android:background="#drawable/reward_mascot"
android:layout_alignParentRight="true"
>
<LinearLayout
android:id="#+id/reward_cloud"
android:layout_width="wrap_content"
android:layout_height="100dp"
android:background="#drawable/reward_cloud"
android:layout_alignParentBottom="true"
>
<TextView
android:layout_gravity="center_vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="You have gained 5 Delight Points"
android:textColor="#color/white"
android:textStyle="italic"
android:paddingLeft="15dp"
android:paddingRight="10dp"
/>
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
I am showing dialog like this..
PointsDialogFragment pdf = PointsDialogFragment.newInstance();
pdf.show(getFragmentManager(), "dialog");
I would like know a way i can change the height of the dialog with in the dialog fragment.
I think the problem is that when you inflate the View, you're not supplying the ViewParent. When you don't supply that parameter, any "layout_X" parameters in the root of your View will be ignored, as those values are supplied to the parent (which is null in your current situation). What you can do is either supply a ViewParent when inflating or wrap your View XML in another ViewGroup, leaving the absolute layout parameters in the 2nd level.
Sample code:
mRoot = inflater.inflate(R.layout.fragment_points_dialog, container, false);
In your DialogFragment xml, instead of using LinearLayout use a RelativeLayout and set the layout_height to wrap_content it will resize automatically the view.
Make sure to not use align parent bottom otherwise it will fill the screen.