I am facing one issue,I am inflating my fragment using inflater.inflate(id, container, false). If I inflate it using inflater.inflate(id, null) then it is showing up.
I can't use second method for inflating as I have defined theme in manifest file and need a different background for this specific screen. After using second method, background image is not covering complete screen.
Please find below my xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/home_blackgroundpng"
android:orientation="vertical" >
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="wrap_content"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/divider_color1" >
</View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_marginLeft="25dp" >
<include layout="#layout/report_header"/>
</LinearLayout>
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#color/divider_color1" >
</View>
<ExpandableListView
android:id="#+id/reportList"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
</HorizontalScrollView>
Code to inflate it is:
public class SalesReportSTLFragment extends Fragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
ViewGroup root = (ViewGroup) inflater.inflate(R.layout.report_stl, container, false);
fillList();
return root;
}
}
Related
There are some views and one FrameLayout in the layout. I want to custom NavHostFragment replace FrameLayout with RelativeLayout, it works but the views hierarchy is wrong. The ImageView should be top, but it is in the bottom.
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
and my custom NavHostFragment:
public abstract class BaseModuleFragment extends NavHostFragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = null;
if (getContentViewId() > 0) {
view = inflater.inflate(getContentViewId(), container, false);
View frameLayout = view.findViewById(R.id.lb_nav_host_container);
if (!(frameLayout instanceof FrameLayout)) {
throw new RuntimeException("frame layout must exist");
}
frameLayout.setId(getId());
} else {
view = super.onCreateView(inflater, container, savedInstanceState);
}
return view;
}
}
Was the method wrong, or how to solve?
Change your layout to this one. It should be ok
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:layout_below="#+id/ivTop"
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/ivTop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
These pictures show:
Wrong:
FrameLayout cover ImageView, it is wrong
Right
ImageView should be top, this is right
FrameLayout top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
ImageView top
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#id/lb_nav_host_container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/logo" />
</RelativeLayout>
I am really fresher in fragment,this is the first time I try it.Here is what I met,I dont know how to fix that,please help me.This is my left_fragment.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Button"/>
</LinearLayout>
This is my right_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00ff00">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="20sp"
android:text="This is right fragment"/>
</LinearLayout>
They looks in the preview is,Left:
right:
And this is the LeftFragment.class:
public class LeftFragment extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.left_fragment,container,false);
return view;
}
}
This is the RightFrament.class:
public class RightFrament extends Fragment{
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.right_fragment,container,false);
return view;
}
}
This is the layout file activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<fragment
android:id="#+id/left_fragment"
android:layout_width="0dp"
android:name="com.example.gaby.fragmenttest.LeftFragment"
android:layout_weight="1"
android:layout_height="match_parent" />
<fragment
android:id="#+id/right_fragment"
android:name="com.example.gaby.fragmenttest.RightFrament"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
this layout file's preview is like this:
I find it odd,I open it in Genymotion nexus7,It seems right.that means the android studio layout preview went wrong,How to solve that?
In your activity_main.xml use the tools:layout="..." attribute.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
...
>
<fragment
android:id="#+id/left_fragment"
....
tools:layout="#layout/left_fragment"
/>
<fragment
android:id="#+id/right_fragment"
...
tools:layout="#layout/right_fragment"
/>
</LinearLayout>
I'm inflating View for ListFragment with:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_home, container, false);
}
In public void onViewCreated(View view, Bundle savedInstanceState) I'm checking the view children's width/height and it seems that they are always 0. Why?
This is my fragment_home.xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<FrameLayout
android:id="#+id/home_fragment_map"
android:layout_width="match_parent"
android:layout_height="#dimen/home_map_h" />
<LinearLayout
android:id="#id/android:empty"
android:background="#color/white"
android:layout_width="match_parent"
android:layout_height="#dimen/event_height"
android:gravity="center">
<FrameLayout
android:layout_width="50dp"
android:layout_height="match_parent">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/location_icon_grey"
android:layout_gravity="center" />
</FrameLayout>
<com.gigaset.location.ui.custom.TextViewMuseo
android:layout_width="match_parent"
android:text="#string/home_no_events"
android:layout_height="#dimen/home_latest_pos_h"
android:gravity="center_vertical" />
</LinearLayout>
<ListView
android:id="#id/android:list"
android:divider="#null"
android:dividerHeight="0dp"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
</LinearLayout>
Views will not take on any size until it is laid out on the screen. Aka, usually after onResume(). If you wish to obtain it's measurements when known, you can use a layout listener. For more details and an example: Android - get height of a view before it´s drawn
I have the follwing layout:
<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" >
<View
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
Programatically I'm replacing the view "content" with my Fragment:
View view = inflater.inflate(R.layout.kwiz_game, container, false);
contentView = view.findViewById(R.id.content);
currentFragment = new KwizCardBackFragment(android.R.drawable.btn_plus);
getFragmentManager().beginTransaction().replace(R.id.content,
currentFragment);
However the fragment is taking all the space and the Layout with the TextView is not shown.
I also tried to use RelativeLayouts with alignParentBottom=true and other stuff...
View of the Fragment which replaces the content:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/background_kwiz_item" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="5dp"
android:scaleType="centerCrop" />
</RelativeLayout>
OnCreateView of the fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_card_back, container,
false);
ImageView imageView = (ImageView) view.findViewById(R.id.image);
if (drawable != null) {
imageView.setImageDrawable(drawable);
} else {
imageView.setBackgroundResource(drawableId);
}
return view;
}
After hours of debugging I finally found the mistake... Some of my layouts had views with the id "content". I thought when I look for R.id.content that the "nearest" element will be used... that's not the case!
Thanks anyone!
I modified your layout a little bit and this seems to achieve what you want:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_above="#+id/bottomNav" />
<LinearLayout
android:id="#+id/bottomNav"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Username: " />
</LinearLayout>
</RelativeLayout>
And a small side note, I see you used fill_parent in your layout, but that's deprecated since API level 8, so you should use match_parent instead :)
Hope this helps, cheers!
You should use FrameLayout as your fragment container, and put (and then replace) your fragments into that FrameLayout container.
I have a master-detail activity with fragments. I try to add TextView dynamically in detail fragment. But it's not adding to detail fragment layouts. But the layouts are displayed properly.
Fragment code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View rootView = inflater.inflate(
R.layout.fragment_configuration_detail, container, false);
RelativeLayout cdf_detail_part1=((RelativeLayout) rootView.findViewById(
R.id.cdf_detail_part1));
Log.i("config", "mItem1->"+mItem1);
if (mItem1 != null) {
TextView name_text = (TextView) cdf_detail_part1.findViewById(
R.id.cdf_tv_name);
name_text.setText(name);
}
return rootView;
}
My layout XMl file:
<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:baselineAligned="false"
android:orientation="horizontal"
tools:context="com.x.y.configurations.ConfigurationDetailFragment" >
<RelativeLayout
android:id="#+id/cdf_detail_part1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#00FF00" >
<TextView
android:id="#+id/cdf_tv_name"
style="?android:attr/textAppearanceLarge"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/cdf_detail_part2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#0000FF" >
</RelativeLayout>
</LinearLayout>