Strange behaviour when trying to restrict a view in RelativeLayout - android

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

Related

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.

Inflating view taking too much time in onCreateView in fragment

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

android log info is “W/OpenGLRenderer: Incorrectly called buildLayer on View”,what does it mean?

this is the view of this android fragment
and this is the float button part code bellow:
<FrameLayout
android:id="#+id/dialpad_floating_action_button_container"
android:background="#drawable/fab_green"
android:layout_width="#dimen/floating_action_button_width"
android:layout_height="#dimen/floating_action_button_height"
android:layout_above="#id/dialpad_floating_action_button_margin_bottom"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/dialpad_floating_action_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/floating_action_button"
android:contentDescription="#string/description_dial_button"
android:src="#drawable/fab_ic_call"/>
</FrameLayout>
But, the Android studio print the info:
W/OpenGLRenderer: Incorrectly called buildLayer on View: FrameLayout, destroying layer...
W/OpenGLRenderer: Incorrectly called buildLayer on View: AppCompatImageButton, destroying layer...
and the floatbutton does not show...
I've tried a lot, but still have no idea how it may happens,and how to check the reason. can anyone help me with it? Thanks a lot!
At last,I solved this problem.
There is a mistake with the layout xml file,this is the wrong file content:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="6">
<include layout="#layout/dialpad_view"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:id="#+id/dialpad_floating_action_button_container"/>
<!-- "Dialpad chooser" UI, shown only when the user brings up the
Dialer while a call is already in progress.
When this UI is visible, the other Dialer elements
(the textfield/button and the dialpad) are hidden. -->
<ListView android:id="#+id/dialpadChooser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_dialer_light"
android:visibility="gone" />
<!-- Margin bottom and alignParentBottom don't work well together, so use a Space instead. -->
<Space android:id="#+id/dialpad_floating_action_button_margin_bottom"
android:layout_width="match_parent"
android:layout_height="#dimen/floating_action_button_margin_bottom"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/dialpad_floating_action_button_container"
android:background="#drawable/fab_green"
android:layout_width="#dimen/floating_action_button_width"
android:layout_height="#dimen/floating_action_button_height"
android:layout_above="#id/dialpad_floating_action_button_margin_bottom"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/dialpad_floating_action_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/floating_action_button"
android:contentDescription="#string/description_dial_button"
android:src="#drawable/fab_ic_call"/>
</FrameLayout>
</RelativeLayout>
The "" tag has a id witch is the same as the id of the "" bellow.And I realized that maybe I had copied this id to the wrong place.
So, I just removed this id,and the floatbutton shows as I expected;
There is the right content bellow:
<RelativeLayout
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="6">
<include layout="#layout/dialpad_view"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
<!-- "Dialpad chooser" UI, shown only when the user brings up the
Dialer while a call is already in progress.
When this UI is visible, the other Dialer elements
(the textfield/button and the dialpad) are hidden. -->
<ListView android:id="#+id/dialpadChooser"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/background_dialer_light"
android:visibility="gone" />
<!-- Margin bottom and alignParentBottom don't work well together, so use a Space instead. -->
<Space android:id="#+id/dialpad_floating_action_button_margin_bottom"
android:layout_width="match_parent"
android:layout_height="#dimen/floating_action_button_margin_bottom"
android:layout_alignParentBottom="true" />
<FrameLayout
android:id="#+id/dialpad_floating_action_button_container"
android:background="#drawable/fab_green"
android:layout_width="#dimen/floating_action_button_width"
android:layout_height="#dimen/floating_action_button_height"
android:layout_above="#id/dialpad_floating_action_button_margin_bottom"
android:layout_centerHorizontal="true">
<ImageButton
android:id="#+id/dialpad_floating_action_button"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/floating_action_button"
android:contentDescription="#string/description_dial_button"
android:src="#drawable/fab_ic_call"/>
</FrameLayout>
</RelativeLayout>

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);

Adding buttons below GraphView, all inside a Fragment

I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout 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"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout 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"
tools:context="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.

Categories

Resources