Custom NavHostFragment replace framelayout with viewgorup - android

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>

Related

BottomSheetDialogFragment header layout background transparent not working

I have created a BottomSheetFragment.
Everything is fine except the header of the bottom sheet which is not getting transparent.Is there any way to make the background transparent as like as normal bottomsheetFragment works.
I tried by setting the parent background transparent but it is not working at all.
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/layoutContainer"
android:background="#android:color/transparent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/immediateParent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<com.makeramen.roundedimageview.RoundedImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/imageView"
android:layout_width="#dimen/hundrad_twenty"
android:layout_height="#dimen/hundrad_twenty"
android:layout_centerHorizontal="true"
android:layout_marginTop="#dimen/sixty"
app:riv_border_color="#color/transparent"
app:riv_corner_radius="#dimen/twenty"
app:riv_mutate_background="true"
app:riv_oval="false" />
<ImageView
android:layout_width="#dimen/thirty"
android:layout_height="#dimen/thirty"
android:layout_alignParentRight="true"
android:layout_marginTop="#dimen/hundred_fifty"
android:layout_marginRight="100dp"/>
</RelativeLayout>
</android.support.constraint.ConstraintLayout>
I have tired this.
ConstraintLayout layoutContainer;
RelativeLayout immediateParent;
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view = null;
view = inflater.inflate(R.layout.bottom_sheet_dialog, container);
ButterKnife.bind(this, view);
//parent layout transparent not working
layoutContainer.setBackground(getResources().getDrawable(R.color.transparent));
immediateParent.setBackground(getResources().getDrawable(R.color.transparent));
//view transparent
view.setBackgroundColor(getResources().getColor(R.color.transparent));
return view;
}
After trying so many ways I found my solution.
Simply added this code in my fragment.
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
((View) getView().getParent()).setBackgroundColor(Color.TRANSPARENT);
}
BottomSheetDialog with transparent background
Your view is actually transparent (when you use transparent instead of black obviously), problem is that BottomSheetDialogFragment wraps your view into it's own non-transparent container. Here's how to make that container transparent: https://stackoverflow.com/a/46469709/12976196

Display ProgressBar in middle of Screen in android

Here is my layout for Activity. And I want to display android.widget.ProgressBar in the middle of the screen dynamically.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.CoordinatorLayout
android:id="#+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="#+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="#+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
android:layout_toLeftOf="#+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
</android.support.v4.widget.DrawerLayout>
Now In that FrameLayout (id = frameContainer) I replace a Fragment that is BaseFragment.(fragment_base.xml)
MainActivity.java
class MainActivity extends AppCompatActivity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BaseFragment mFileListFragment = new BaseFragment();
FragmentManager manager = getSupportFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
fragment_base.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"
android:orientation="vertical">
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/white"/>
</LinearLayout>
BaseFragment.java
public class BaseFragment extends Fragment{
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_phone_file_base, null);
setLayoutViews(mView);
}
return mView;
}
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
FileListFragment mFileListFragment = new FileListFragment();
FragmentManager manager = getChildFragmentManager();
FragmentTransaction transaction = manager.beginTransaction();
transaction.replace(R.id.frameContainer, mFileListFragment);
transaction.commit();
}
}
Now In FrameLayout(id = frameContainer) of that BaseFragment I open child fragment that is FileListFragment. (fragment_file_list.xml)
fragment_file_list.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.RecyclerView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rvFileList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="visible"/>
</RelativeLayout>
FileListFragment.java
public class FileListFragment extends Fragment{
private View mView;
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
if (mView == null) {
mView = inflater.inflate(R.layout.fragment_file_list, null);
setLayoutViews(mView);
}
return mView;
}
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// Here there is a server call which retrieves File Data from server. When I send server request I start progressBar and after response I hide progressBar programatically.
ProgressBar mProgressBar = new ProgressBar(activity);
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
params.gravity = Gravity.CENTER;
mProgressBar.setLayoutParams(params);
mProgressBar.getIndeterminateDrawable().setColorFilter(activity.getResources().getColor(R.color.colorPrimary), PorterDuff.Mode.MULTIPLY);
FrameLayout listFrame = getParentFragment().getView().findViewById(R.id.frameContainer);
if(listFrame != null) {
listFrame.addView(mProgressBar);
}
}
}
But it doesn't show progressBar in middle of the screen. It displays below of middle of screen like image attached below.
You can try with android:layout_gravity
Standard gravity constant that a child supplies to its parent.
You can align a view in center of the Framelayout by setting the layout_gravity of the child view .
android:layout_gravity="center"
Place the object in the center of its container in both the vertical and horizontal axis, not changing its size .
This is because you adding progressbar in center of framelayout, try this
<android.support.design.widget.CoordinatorLayout
android:id="#+id/myCoordinator"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ProgressBar
android:id="#+id/progressBar2"
style="?android:attr/progressBarStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<include layout="#layout/toolbar" />
</android.support.design.widget.AppBarLayout>
<FrameLayout
android:id="#+id/frameContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<RelativeLayout
android:id="#+id/rlSearchToolContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:background="#color/colorPrimary"
android:gravity="bottom"
android:layout_gravity="bottom"
android:visibility="gone">
<LinearLayout
android:id="#+id/llSearchComponentContainer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/colorPrimary"
android:layout_toLeftOf="#+id/btnDone"
android:gravity="bottom"
android:orientation="horizontal"></LinearLayout>
<Button
android:id="#+id/btnDone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Done"/>
</RelativeLayout>
</android.support.design.widget.CoordinatorLayout>
<FrameLayout
android:id="#+id/frameLeftSlide"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="start" />
Try it using Relativelayout, use Relativelayout as container of progressbar and use centerinparent attribute to center the progressbar in the middle of screen :
RelativeLayout layout = new RelativeLayout(this);
progressBar = new ProgressBar(this);
progressBar.setIndeterminate(true);
progressBar.setVisibility(View.VISIBLE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(100,100);
params.addRule(RelativeLayout.CENTER_IN_PARENT);
layout.addView(progressBar,params);
setContentView(layout);

ListFragment with a custom view hierarchy results with zero width / height children in onViewCreated

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

Layout below Fragment not visible

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.

TextView not adding in fragment layouts

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>

Categories

Resources