change the height of custom dialog in Dialog Fragment - android

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.

Related

Android layout move item in fragment

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.

Add multiple GridView in the same scrollable layout/fragment

I know there are several questions for this problem already as I have searched for an hour, but neither of those has solved my problem nor the solution is too old.
I'm working on an event app and it displays the attendance of people going to it. It need to has two GridView: YES and NO, depending on the attendance, and it shows the picture of the guests.
I have already made the "YES" GridView and I tried adding a new TextView and GridView to my layout + getting the layout of it in Java for my "NO" GridView but it only shows the first one.
What am I doing wrong? I'm using Fragments for each of my tabs.
This is what I have v.s. what I want:
This is my fragment's layout:
[fragment_one.xml]
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.example.MainActivity.OneFragment">
<TextView android:id="#+id/title_yes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="Yes"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_yes"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_yes"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<TextView android:id="#+id/title_no"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="14dp"
android:text="No"
android:textAllCaps="true"
android:textSize="14sp"
android:fontFamily="sans-serif-regular" />
<GridView android:id="#+id/gridview_no"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/title_no"
android:numColumns="4"
android:verticalSpacing="2dp"
android:horizontalSpacing="2dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
This is my fragment's Java:
public class OneFragment extends Fragment {
public OneFragment() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_one, container, false);
GridView gridViewYes = (GridView) view.findViewById(R.id.gridview_yes);
GridView gridViewNo = (GridView) view.findViewById(R.id.gridview_no);
gridViewYes.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewNo.setAdapter(new ImageAdapter(view.getContext())); // uses the view to get the context instead of getActivity()
gridViewYes.setOnItemClickListener(new AdapterView.OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(getActivity(), "Esta es la imagen " + position + ".", Toast.LENGTH_SHORT).show();
}
});
return view;
}
}
GridView is probably not your best option here. That widget is intended to be used when you have a potentially long list of things to display in a scrolling container. TableView is better when you have a manageable set of items that doesn't need its own scrolling behavior.
Also, you probably don't want a layout_height of match_parent for these things. Use wrap_content to only let them be as tall as their content suggests.
If you need the whole thing to scroll because you aren't certain of the entire height, place the LinearLayout in a ScrollView and let that determine if the collection of tables and text should be scrollable if they're too tall for the space allotted.
Your problem is you have set GridView yes height to match_parent.
android:layout_height="match_parent"
You should change as follows:
android:layout_height="wrap_content"

Android - How to make child LinearLayout match parent RelativeLayout's height

I am putting this LinearLayout inside RelativeLayout.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000"
android:gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
</LinearLayout>
What I am trying to do is - to show which item has been chosen. I set LinearLayout's height and width match_parent. It is only matching parent(RelativeLayout)'s width, not height.
This is what I have intended to do:
This is what I am getting:
LinearLayout is not taking whole height of its parent.
There are 2 reasons, why I am using Relative layout as parent:
The LinearLayout should be in upside(should cover) book information with 40% black color
To show this symbol on the top of book
The whole XML looks like this:
<?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:gravity="center_horizontal">
<LinearLayout
android:id="#+id/llBook"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="6dp"
android:gravity="center_horizontal"
android:orientation="vertical"
android:paddingBottom="5dp"
android:paddingLeft="11dp"
android:paddingRight="2dp"
android:paddingTop="8dp">
<ImageView
android:id="#+id/ivCover"
android:layout_width="95dp"
android:layout_height="146dp" />
<TextView
android:id="#+id/tvTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#232425"
android:textSize="12sp" />
<TextView
android:id="#+id/tvAuthor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:textColor="#848586"
android:textSize="10sp" />
</LinearLayout>
<ImageView
android:layout_width="22dp"
android:layout_height="22dp"
android:layout_alignRight="#id/llBook"
android:layout_marginRight="6dp"
android:background="#drawable/shape_round_book_status"
android:padding="8dp"
android:src="#drawable/icon_new" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#66000000"
android:gravity="center">
<ImageView
android:layout_width="30dp"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
</LinearLayout>
</RelativeLayout>
I am using this RelativeLayout in BaseAdapter class using which I am filling GridView with items.
public class LibraryAdapter extends BaseAdapter {
Context context;
RealmResults<RBook> rBooks;
LayoutInflater inflater;
public LibraryAdapter(Context context, RealmResults<RBook> rBooks) {
this.context = context;
this.rBooks = rBooks;
this.inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return rBooks.size();
}
#Override
public Object getItem(int position) {
return rBooks.get(position);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RBook rBook = (RBook) getItem(position);
View view = convertView;
if(view == null) {
// new row is needed to inflate new row
view = inflater.inflate(R.layout.listitem_library_book, parent, false);
}
Bitmap bitmap = BitmapFactory.decodeByteArray(rBook.getCover() , 0, rBook.getCover().length);
ImageView ivCover = (ImageView) view.findViewById(R.id.ivCover);
ivCover.setImageBitmap(bitmap);
TextView tvTitle = (TextView) view.findViewById(R.id.tvTitle);
tvTitle.setText(rBook.getTitle());
TextView tvAuthor = (TextView) view.findViewById(R.id.tvAuthor);
tvAuthor.setText(rBook.getAuthor());
return view;
}
}
EDIT:
Try1: as cj1098 suggested I have changed child LinearLayout to RelativeLayout. Unfortunately, no effect
Try2: as challenger suggested, I have used this inside my child LinearLayout, but effect was like this:
Try3: as codeMagic suggested, I have changed child LinearLayout's
gravity="center"
to ImageView's
layout_gravity="center"
In result, LinearLayout is not taking whole height.
So my question is how make child LinearLayout match parent RelativeLayout's height?
Try to add this to the LinearLayout:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
Any way use can use this without even using the LinearLayout (with fixing the size of the drawable "big_tick" ):
<ImageView
android:layout_width="30dp"
android:background="#66000000"
android:layout_height="23dp"
android:src="#drawable/big_tick" />
I think that using this layout by itself is fine. It is the fact that you are putting it into a ListView is causing some sort a havoc.
However, you can force the overlay to align itself bottom and top with the book layout:
android:layout_alignBottom="#id/llBook"
android:layout_alignTop="#id/llBook"
After that maybe you still have a few margins to fix, but the two lines above will make the overlay be as big as the book layout.
it is taking the full height of its parent. Get rid of the padding on your child linear layout. Then if that doesn't work. Switch your linearLayout to a relativeLayout. (It's better anyway) another limitation is the actual images you are using. Do they have differing heights?
Remove the LinearLayout totally, use the imageview with src 'tick' as a direct child of the Relative layout with:
andriod:centerInParent=true
and then add View with the desired background transparency that has
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"

Find List View in fragment from Activity

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"!

Custom View using onCreateView in a DialogFragment

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.

Categories

Resources