Android animate view outside fragment is getting clipped - android

I have a view in a fragment. This fragment is within a FrameLayout. I want to animate this view moving outside the fragment borders. However, the view always get clipped when crossing the border. I have tried by setting android:clipChildren="false" and android:clipToPadding="false" to everything, but I can't get it to work.
Is this even possible to do?

I had the similar problem with fragments and put android:clipChildren="false" and android:clipToPadding="false" on all the levels in my hierarchy, but it was still not working. The trick that worked for me was to add this override in my fragment code:
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup vg = (ViewGroup) view;
vg.setClipChildren(false);
vg.setClipToPadding(false);
}
Hope this helps...
P.S. the credit should go to this answer

frangulyan You're missing the other bit of code from your credited answer. The below solution worked for me.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
LinearLayout rootView = (LinearLayout) inflater.inflate(R.layout.fragment_main, container, false);
//This can be done in XML
rootView.setClipChildren(false);
rootView.setClipToPadding(false);
return rootView;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
//This is the NoSaveStateFrameLayout - force it to not clip
FrameLayout frameLayout = (FrameLayout) getView();
frameLayout.setClipChildren(false);
frameLayout.setClipToPadding(false);
}

The answers already given by #frangulyan and #Barrie Galitzky were both on the right track, but I had to add this loop to setClipping false for all parents in the hierarchy before it would work properly
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
ViewGroup vg = (ViewGroup) view;
while(vg != null) {
vg.setClipChildren(false);
vg.setClipToPadding(false);
vg = vg.getParent() instanceof ViewGroup ? (ViewGroup) vg.getParent() : null;
}
}

I know this is a bit old, and correct answers have been given, but I wanted to add that you can do the same thing as frangulyan does inside onCreateView using the ViewGroup. I.e.:
#Override
public View onCreateView(LayoutInflater, ViewGroup container, Bundle savedInstanceState) {
container.setClipChildren(false);
container.setClipToPadding(false);
// Whatever other setup you like
return View;
}

Related

Which one of these is the best way to access android fragment views?

How are these methods different from each other when trying to get the view?
First:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
listView = view.findViewById(R.id.listview);
return view;
}
Second:
#Override
public void onActivityCreated(#Nullable Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
listView = getActivity().findViewById(R.id.listview); }
* some say this is used to get activity views but i used it for getting fragment views(which didn't existed in the activity) and it worked fine.
Third:
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
listView = getView().findViewById(R.id.listview);
}
Three methods are good. In the onCreateView you create the view (!), it's the really first time you can use what you inflated. Then onViewCreated is called with the view you returned in the onCreateView, You can directly use the view given as parameter, it is the same the getView() provides. My advice is to initialise your UI variables here
For onActivityCreated, it is the best place to modify your UI elements. It is called when fragment creation is complete and when fragment is re-attached. There you can use the variables you initialised before, without having to get the activity just for that purpose.

if (view == null) issues in fragment

I have updated my Sdk Oreo 8.0 When i come my current fragment to my previous fragment its crash because i am using if (view == null)
if i removed this condition it 'll work fine but have to load again view.
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
if (view == null) {
view = inflater.inflate(R.layout.home_fragement_layout, container, false);
ButterKnife.bind(this, view);
bundle=getArguments();
registrationResponse=(RegistrationResponse)bundle.getSerializable("registerresponse");
setadapter();
hitUserFavouriteOutfitsapi();
swipeRefreshLayout.setOnRefreshListener(this);
}
toolBarTopChangeState =((ToolBarTopChangeState)context);
toolBarTopChangeState.stateChangeToolBarIcon(0);
toolBarTopChangeState.stateChangeToolBarText(getString(R.string.app_name));
return view;
}
Use this code:
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container,
#Nullable Bundle savedInstanceState) {
view = inflater.inflate(R.layout.home_fragement_layout, container, false);
ButterKnife.bind(this, view);
bundle=getArguments();
registrationResponse=(RegistrationResponse)bundle.getSerializable("registerresponse");
setadapter();
hitUserFavouriteOutfitsapi();
swipeRefreshLayout.setOnRefreshListener(this);
toolBarTopChangeState =((ToolBarTopChangeState)context);
toolBarTopChangeState.stateChangeToolBarIcon(0);
toolBarTopChangeState.stateChangeToolBarText(getString(R.string.app_name));
return view;
}
On the lists is a good practice to check if the view is already created and if it's to use it further, but on the activities and fragments you should left the OS to decide when should inflate again the view. You can have a look over android lifecycle to understand better how it's works
it 'll happen because i am adding animation when fragment change and return to back.. This problem facing because of Android 8.0(Oreo) rest os working fine so I removed the animation during fragment change it 'll solved.

how to Bind dynamic views using butterknife

i am creating the multiple dynamic views in android and my fragment using the layout of parent fragment and in child fragment i am creating the dynamic views but i am not able to bind those dynamic views using Butterknife.
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this is the on create
// Inflate the layout for this fragment
View view = super.onCreateView(inflater, container, savedInstanceState); this.container.addView(inflater.inflate(R.layout.overall_sport_performance_row, null), 5);
i have one textview in the overall_sport_performance layout and i want to use that using butterknife
I don't really understand your solution, if your Fragment's view is overall_spot_performance.xml, do like this:
#BindView(R.id.your_text_view_id)
protected TextView textView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = View.inflate(getContext(), R.layout.overall_sport_performance_row, null);
Butterknife.bind(this, view);
return view;
}
No need to bind the TextView dynamically. However if you still need to bind it dynamically, you can use findById method. I did not test it but should work like this:
View dynamicView = inflater.inflate(R.layout.overall_sport_performance_row, null);
this.container.addView(dynamicView, 5);
TextView textView = ButterKnife.findById(dynamicView, R.id.your_text_view_id);

getting a view using getResources()

I want to set text of a TextView that is inside a layout file,
I tried setContentView() but it isnt working since i am using fragments.
I tried using getResources().getLayout(R.layout.abc);
It returns null
I tried setContentView() but it isnt working since i am using
fragments
That's wrong. You have to override onCreateView and inflate and return the layout you want to show, and you can use onCreateView, and use its first parameter, View view, to call findViewById and access the widgets in your layout. You can read more here and here
inside fragment you can set view inside function onCreateView(), use below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
View view = (LinearLayout) inflater.inflate(R.layout.my_layout, container, false);
return view;
}
here my_layout should be the name of layout file,
now you can get view of it inside function onviewcreated()
public void onViewCreated(View view, Bundle savedInstanceState){
// here you can get your textview and set its value
}
thumbs up, if you find my answer correct
You can inflate your layout like this:
ViewGroup group = LayouInflate.from(context).inflate(R.layout.abc,null);
TextView tv = group.findViewById(R.id.xxx);
you have to inflate the layout through the onCreateView method and then return the View.
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
TextView yourTextView = (TextView)rootView.findViewById(R.id.yourTextViewId);
return rootView;
}

Grid View in a fragment

I try to set up a grid view like in this tutorial : http://developer.android.com/guide/topics/ui/layout/gridview.html
But I want a fragment and setContentView in a fragment is not working. What I have to change to make it works ?
Thanks.
Override the onCreateView() method in your fragment like so:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedState) {
return inflater.inflate(R.layout.fragment_layout, container, false);
}

Categories

Resources