Inflating view taking too much time in onCreateView in fragment - android

I've using the Fragment inside the Navigation Drawer.
In Drawer there are 7 more option menus are presents and all other working fine.
Only this fragment tasking 3sec time to show the views.
I've find using debug mode, below this only line taking that much time to execute.
final View view=inflater.inflate(R.layout.fragment_vehicle_inspection,container,false);
Moreover I've using 4 include layout in XML part.
<?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">
<LinearLayout
android:id="#+id/lat_inspection_vendor_name_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="#dimen/margin_05dp"
android:orientation="horizontal">
<TextView
android:id="#+id/txt_inspection_vendor_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:drawableEnd="#drawable/ic_action_edit"
android:gravity="center"
android:maxLines="1"
android:textColor="#color/colorAccent"
android:textSize="#dimen/margin_15sp"
android:textStyle="bold" />
</LinearLayout>
<include
android:id="#+id/include_inspection_function_layout"
layout="#layout/fragment_inspection_function_layout_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/lat_inspection_vendor_name_layout"
android:layout_marginTop="#dimen/margin_05dp"
android:visibility="gone" />
<ScrollView
android:id="#+id/scroll_inspection"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/include_inspection_function_layout"
android:layout_marginTop="#dimen/margin_10dp"
android:focusableInTouchMode="true"
android:focusable="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/fragment_inspection_vendor_details_card" />
<LinearLayout
android:id="#+id/lat_inspection_form"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<include layout="#layout/fragment_inspection_vehicle_form_row_card" />
<include layout="#layout/fragment_inspection_vehicle_features_row_card" />
<include layout="#layout/fragment_inspection_vehicle_form_penalty_row_card" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
I hope include part is not affect inflate view.
I've using 26- EditText, 26-checkbox and 30-TextViews inside the include layout.
My concern is Inflating too much number of views only taking time.
Please assist me to encounter this problem.
Thanks in advance.

It's because too much view's in your layout file and yes include part affect your inflation process. Try to reduce number of view.
Share you layout design image so i can give you a suggestion.

It's because you are showing some high resolution image or any other background resource so it may happen to take some time to draw view in your fragment

Related

RecyclerView not working inside a ScrollView properly

My app and the RecyclerView was working fine until I add the second RecyclerView on to the fragment. Since I added the second fragment, I added a ScrollView to the fragment. Then I faced some issue with the scrolling within the recyclerview, it didn't scroll smoothly. After going through a lot of articles online including StackOverflow, I changed the ScrollView with androidx.core.widget.NestedScrollView. This fixed the issue with scrolling but some major issue occured. The loading of items in the RecyclerView took more time, when I hit the searchbutton, that's there on the menu bar at the top, it took long time to expand the search field and some times app show 'not responding' message. Some articles say add .setNestedScrollingEnabled=false and I added it like the following in onCreateView() of the fragment. But I am still facing the issue.
I am using kotlin
binding.rv_home_items.isNestedScrollingEnabled=false
binding.rv_home_categories.isNestedScrollingEnabled=false
Following is the xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorOffWhite"
android:orientation="vertical"
tools:context=".ui.fragments.HomeFragment">
<FrameLayout
android:id="#+id/framelayout_category"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_home_categories"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="horizontal"
android:paddingBottom="15dp" />
<ImageButton
android:id="#+id/ibutton_show_category"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|right"
android:layout_margin="5dp"
android:background="#drawable/ic_show_category" />
</FrameLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/framelayout_category">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<com.denzcoskun.imageslider.ImageSlider
android:id="#+id/image_slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:visibility="gone"
app:iss_auto_cycle="true"
app:iss_corner_radius="5"
app:iss_delay="0"
app:iss_error_image="#color/colorDarkGrey"
app:iss_period="2500"
app:iss_placeholder="#color/colorDarkGrey"
app:iss_selected_dot="#drawable/default_selected_dot"
app:iss_unselected_dot="#drawable/default_unselected_dot"
tools:visibility="visible" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_home_items"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/image_slider"
android:layout_marginBottom="50dp" />
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
</RelativeLayout>
I am completely stuck on my project, any help with this is highly appreciated
Edit:
When I launch the app, in a few seconds it's shows a message that the app is not responding.
If you have more than one RecyclerView and even all of them have same scroll direction, seems ConcatAdapter can be preferred solution.
ConcatAdapter enables sequentially combine multiple adapters to a single RecyclerView.

Not able to set background color to a view in Kotlin

Edit: everything is fine with the code, Android Studio is not deploying changes to app version 4.1.3
Im trying to change the background color of view(Relative layout and rootView) at runtime
but its not working at all here is the code
rootview = findViewById(R.id.rootView)
rootview.setBackgroundColor(resources.getColor(R.color.backgroundColor2))
Layout code/file, I'm using relativeLayout as the main layout here which is overridden by another view completely
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:id="#+id/rootView"
android:layout_height="match_parent"
tools:context=".MainActivity">
<LinearLayout
android:id="#+id/banner_container"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
/>
<at.markushi.ui.CircleButton
android:id="#+id/button"
android:layout_width="250dp"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="#drawable/ic_launcher_foreground"
app:cb_color="#color/mainButtonColor1"
app:cb_pressedRingWidth="8dip" />
<at.markushi.ui.CircleButton
android:layout_width="64dp"
android:elevation="8dp"
android:layout_height="64dp"
android:src="#drawable/ic_launcher_foreground"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_marginEnd="30dp"
android:layout_marginBottom="30dp"
app:cb_color="#B71C1C"
app:cb_pressedRingWidth="8dip" />
</RelativeLayout>
If I set the background in XML it works fine
I think because you are using a Relative layout. So maybe, your relative layout is overridden or covered by another View (like view 2 is covered by view 1 and view 3 ). And when you change the background of view 2, this function worked but you don't see it because it is being covered by view 1 and view 3. You should try Liner Layout to replay.

How can I have one unique button in multiple components of a layout in android?

So I have an xml that consists of a linear layout containing a Button and a TextView like this:
<?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="100dp"
android:orientation="vertical">
<Button
android:id="#+id/btnCell"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:paddingLeft="40dp"
android:text="Button"
android:textColor="#color/blueText" />
<View
android:height="wrap_content"
android:width="wrap_content"
android:background="#drawable/Test"/>
</LinearLayout>
And I want to use this same layout inside other layouts in a different xml. I need the same button at every time, so I reuse it by including it in the two layouts (both layouts are in the same xml, but ones is hidden):
First one
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include layout="#layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
Second One:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include layout="#layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
So I show the first layout and hide the second one at the beginning of the app , as the user moves within the interface, the layouts exchange so that the other one is shown and the first one hidden.
The thing is that I declare the Button in my java activity class like this:
btnCell = (Button) thirdView.findViewById(R.id.btnCell);
btnCell.setOnClickListener(this);
And implemented the listener.
#Override
public void onClick(View v) {
if (v == btnCell) {
System.out.println("entered if");
}
System.out.println("entered function");
}
The problem is that when I click the button when the first view is shown and the second hidden, the button works just fine, but when I unhide the second layout, hide the first one, and proceed to click the button, that should be the same as the first one but in a different layout, nothing happens. I searched and find out, that this happens because the id is assigned only to the button shown in the first layout because of view hierarchy, but not the one in the second layout. How can I make both buttons react to the same action, without declaring a new button in each layout but instead reusing it?
I have used this type of layout. you can create Id different for both and inflate that view and give different name so You can differentiate both thing.
<?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">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include android:id="+id/firstOne" layout="#layout/buttonLayout"/>
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
android second one is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:visibility="gone"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image"
/>
<include android:id="+id/secondTwo" layout="#layout/buttonLayout"/>
<CheckBox
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
The Problem is both the layout are include in same layout file and the id of that
button are same so whenever you click on any of the button at the same time event will fire on both button like both are clicked.
So, you have to give the different id for both the button I hope it works fine..
You can add a different Id for each included layout:
<include android:id="+id/layout_a" layout="#layout/buttonLayout"/>
and
<include android:id="+id/layout_b" layout="#layout/buttonLayout"/>
and then use two findViewById to reach them:
btnCellA = (Button)thirdView.findViewById(R.id.layout_a).findViewById(R.id.btnCell);
btnCellB = (Button)thirdView.findViewById(R.id.layout_b).findViewById(R.id.btnCell);

Make programmatically added fragment clickable, uniquely identifiable

I'm running into some problems trying to figure out how to make fragments that I have programmatically added into a LinearLayout clickable. I'm using fragments because they will be in multiple activities and it was a good way for me to create a layout like this:
http://i.stack.imgur.com/P4lOG.png
But, if there's a better way to do this that would make the process of making it clickable, I'm certainly open to changing things up.
Anyway, I'm adding the fragments to the LinearLayout, jobsList, like so:
FragmentManager fragmentManager = getFragmentManager();
int count = 2;
for (int i = 0; i < count; i++) {
FragmentTransaction fragmentTransaction = fragmentManager
.beginTransaction();
Bundle bundle = new Bundle();
bundle.putInt("jobID", i + 1);
jobFragment job = new jobFragment();
job.setArguments(bundle);
fragmentTransaction.add(R.id.jobsList, job, Integer.toString(i));
fragmentTransaction.commit();
}
The count of 2 is just a placeholder for now, later there will be an arbitrary number of jobs.
Here is the layout, it's a bit of a mess but I got all the specific weights the way I wanted this way.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicPort"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Port" />
<TextView
android:id="#+id/basicLoadOrEmpty"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="Load/Empty" />
<TextView
android:id="#+id/basicInOrOut"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/background"
android:text="In/Out" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicContainerNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Container #" />
<TextView
android:id="#+id/basicChassisNum"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Chassis #" />
<TextView
android:id="#+id/basicContainerType"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Cont. Type" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicDirectionArrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".2"
android:background="#drawable/background"
android:text="Direction" />
<TextView
android:id="#+id/basicCustomer"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".8"
android:background="#drawable/background"
android:text="Customer Location" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".333" >
<TextView
android:id="#+id/basicSteamshipLine"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".4"
android:background="#drawable/background"
android:text="Steamship Line" />
<TextView
android:id="#+id/basicBKG_BOL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight=".6"
android:background="#drawable/background"
android:text="BKG-BOL#" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".2" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/basicStatus1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 1" />
<TextView
android:id="#+id/basicStatus2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".5"
android:background="#drawable/background"
android:text="Status 2" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
<Space
android:layout_width="match_parent"
android:layout_height="20dp" />
</LinearLayout>
My question to you is, is it possible to make these fragments clickable and uniquely identifiable, and if so, what is the best way to go about doing that?
Thank you for your help!
What you could do is:
Modify the layout of your fragment, so it uses the RelativeLayout (now its probably LinearLayout right?). By modify I mean either use RelativeLayout INSTEAD of LinearLayout (more complicated but i think a bit better), or put your LinearLayout INSIDE RelativeLayout.
Place a view that matches in size the size of the fragment, on top of your textViews. Add an onClickListener to it that performes the code on clicks.
First - fragments are not clickable, layouts and their children are. A Fragment can handle the click action for things that occur in its layout but you do not "click" the fragment.
Second - sounds like you're confused about how to use fragments and what a Fragment actually does. A Fragment is like an Activity with some parts missing. A Fragment has its own lifecycle and can be used to perform many of the same tasks as an Activity the difference being that (1) a Fragment is "hosted" by an activity that provides the missing functions and does not run on its own (2) a Fragment can run in the background with no UI (I do not know of a background activity yet).
You do not need to/should not be using Fragments for what you are trying to do.
Your layout is really just a series of TextViews. You should be able to make that layout in XML reuse that layout file - by simply getting a LayoutInflater anywhere else in your app that you want to. If things got really crazy you could create a custom TextView class to provide detailed functionality but you should not be using 10 fragments, each with one TextView to make that layout work.
I just wanted to follow up on things in case anybody stumbles upon this answer in the future. It turns out using fragments was a bad idea. While I was able to make them clickable and identifiable through a bit of a hack job, I ran into problems later on when I was trying to fix layout issues. So, thanks to #Rarw I looked into using a LayoutInflater instead.
And that was much easier! I just made the layout xml file, used the LayoutInflater to add it to a LinearLayout and added that to the list. Makes things much easier down the road:
LayoutInflater inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
LinearLayout container = new LinearLayout(this);
inflater.inflate(R.layout.basic, container, true);
container.setId(i);
list.addView(container, params);
container.setClickable(true);
container.setOnClickListener(this);
A hacky solution is to place a transparent button on top of the entire fragment, have the fragment extend onClickListener, and set the button's onClickListener to this.
One way to create such a button:
Use a constraintLayout as the root layout for the xml.
As the last of child of the constraintLayout, create a button, constrain it to occupy entire fragment by setting layout_constrainXXX to corner elements.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
<!-- The remaining properties of the layout -->
<!-- All the children of the layout -->
<Button
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
android:background="#00000000"
android:id="#+id/fragment_button" />
</android.support.constraint.ConstraintLayout>
Note that you can do this other ways too, such as in a RelativeLayout
Then in the java/kotlin file, extend onClickListener.
override fun onClick(p0: View?) {
// Whatever you want to do here
}
In method onActivityCreated, set the transparent button's listener as:
var button : Button = fragment_button
button.setOnClickListener(this)

Strange behaviour when trying to restrict a view in RelativeLayout

I am trying to add a dynamic Bounding box to my camera layout,I have the following xml code in which I am trying to keep the bounding box from reaching the ShutterButton,I find that the box goes underneath the ShutterButton,I think it is because overlapping of views is allowed in RelativeLayout...
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:layout_alignParentTop="true"
android:layout_above="#+id/btn_capture"
/>
<com.example.mycameraapp.RectangleView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/btn_capture"
android:id="#+id/bound_box"
/>
<com.example.mycameraapp.ShutterButton android:id="#id/btn_capture"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
I would like it to not work in the region where ShutterButton specifically is,such that the ShutterButton remains the top Z element followed by the RectangleView on top of the CameraPreview which is simply a SurfaceView
Your btn_capture was not created before you referenced it. Whenever you assign ids use #+id and whenever you refer ids use #id for avoiding errors
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<com.example.mycameraapp.ShutterButton android:id="#+id/btn_capture"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<FrameLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/container"
android:layout_alignParentTop="true"
android:layout_above="#id/btn_capture"
/>
<com.example.mycameraapp.RectangleView android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#id/btn_capture"
android:id="#+id/bound_box"
/>
Adding the RectangleView in my Java code as a child of the FrameLayout helped.Now,it draws itself behind the ShutterButton and allows me to click the Button even if it is drawn behind the ShutterButton.
mPreview=new CameraPreview(this,mCamera);
mBox=new RectangleView(this);
container.addView(mPreview);
container.addView(mBox);
In onPause,container.removeAllViews(); ensures that the RectangleView is removed as well

Categories

Resources