Bypassing stacked views with click listeners - java

I have a RelativeLayout with both an ImageView and a ViewPager. It seems as though the ViewPager sits on top of the ImageView because I had an onClick listener set up for my ImageView and when I implemented the ViewPager the ImageView no longer responds to clicks. Is there any way to "click through" to the ImageView below? In general, is it possible to ignore the "top" view and allow the "bottom" view to respond to click events?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="15dp"
android:layout_gravity="center_horizontal">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:contentDescription="#string/feed_image_content_description"
android:scaleType="centerCrop" />
<android.support.v4.view.ViewPager
android:id="#+id/view_pager"
android:layout_width="match_parent"
android:layout_height="500dp"
android:scaleType="centerCrop">
<android.support.v4.view.PagerTitleStrip
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="#style/custom_viewpagertitlestrip"
android:layout_gravity="top" />
</android.support.v4.view.ViewPager>
</RelativeLayout>

You may want to try to tell ViewPage (and its children) they are not clickable by setting layout attribute android:clickable="false" (I'd also perhapss set android:focusable="false" and android:focusableInTouchMode="false" from XML or call setClickable(false) from code.

Related

Android View below another in FrameLayout not work

I have such code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.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:background="#000">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:padding="15dp"
android:background="#drawable/button"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#FFF" />
<pl.jawegiel.endlessblow.other.GameSurface
android:id="#+id/gameSurface"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
(...)
</RelativeLayout>
</FrameLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/left_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="start"
android:background="#22FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/right_rv"
android:layout_width="200dp"
android:layout_height="match_parent"
android:layout_gravity="end"
android:background="#99FFFFFF"
android:choiceMode="singleChoice"
android:divider="#080"
android:dividerHeight="2dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager" />
</androidx.drawerlayout.widget.DrawerLayout>
The problem is that my Button is not below GameSurface.
I put this Button as first one counting on that it will be below GameSurface but it does not work.
How to achieve that?
You need to change FrameLayout to another kind of ViewGroup. FrameLayout is a a type of ViewGroup where one View is on top of another one. Like Layers in a cake.
Putting one below another is not possible in FrameLayout
The easiest solution might be to change FrameLayout with LinearLayout and of course, remember to add android:orientation attribute to it.
NOTE: I suggest you to learn ConstraintLayout, which can replace nearly every type of nested ViewGroups including FrameLayout and RelativeLayout :-)
This you can't achieve with FrameLayout, this is because FrameLayout is usually used to show only one child. Even tho it can show more child views they are just placed on top of each other. This is explained in the answer above. Each ViewGroup has its own pros and cons and the best Layout for this is LinearLayout. LinearLayout can place your child's one below another or one next to another, based on android:orientation attribute you can use. Values are vertical and horizontal. You understand from that which one should be used here.
Therefore, what you need to do is simply change your FrameLayout to LinearLayout. So this is how it looks in my XML:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
After that, you just place your child views in order you want to show them. So, in my case I have an ImageView and a Button, placed in that order, like this:
<LinearLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:gravity="center_vertical"
android:orientation="vertical"
android:layout_height="match_parent">
<ImageView
android:id="#+id/gameSurface"
android:src="#drawable/ic_user"
android:layout_width="200dp"
android:layout_gravity="center_horizontal"
android:layout_height="200dp" />
<com.google.android.material.button.MaterialButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="center map"
android:layout_marginTop="20dp"
android:padding="15dp"
android:layout_gravity="center_horizontal|center_vertical"
android:textColor="#000" />
</LinearLayout>
Then I get this:

Scrollview's last bottom contents hidden by the bottom navigation view

Hi coders I have a constraint layout, in it is a bottom navigation view
a tool bar on top and a scrollview which has lots of buttons going down vertically.
The problem is that the scrollview's last buttons are hiding behind the bottom navigation view after I scroll all the way to bottom how can this be solved
On the actual code there are plenty of buttons in scrollview through here I add only a few here.
<android.support.constraint.ConstraintLayout 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:id="#+id/container"
android:background="#ffffff"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/tutorialsinclude"
layout="#layout/app_bar_main"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toBottomOf="#id/tutorialsinclude"
android:id="#+id/webtut">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"
android:layout_marginTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="web "
android:id="#+id/tutorialsButton1"/>
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Tutorials 1 "
android:id="#+id/tutorialsButton1"/>
</LinearLayout>
</ScrollView>
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigationbb"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="0dp"
android:layout_marginEnd="0dp"
android:background="#ffffff"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
You can just put a footer in the end to fix this issue. Just make a View below the last button. Something like this:
<View android:layout_width="match_parent"
android:layout_height="32dp" />
You just have to adjust the height to make it work for your layout. Also, you really shouldn't manually put all these buttons in a scrollView. should use a RecyclerView instead.
Set constraint to bottom of scrollview to top of bottom navigation view and set height as 0dp which will make height to match the constraint
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintTop_toBottomOf="#id/tutorialsinclude"
app:layout_constraintBottom_toTopOf="#id/navigationbb"
android:id="#+id/webtut">

Widget ListView loading view

I'm trying to display a dynamic list on a widget and so far i've been able to display my 3 headers (which are simply textview inside a linearlayout) but for my items (2 textview in one linearlayout inside another linear layout) the view displays Loading... instead of the content (a task label).
I've already bumped getViewTypeCount to 2 in my RemoteViewsFactory to match the number of views returned by the getViewAt.
I can't figure out what is wrong inside this layout that would cause a problem:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="3dp"
android:clickable="true"
android:focusable="true"
style="#style/SelectableItemBackground"
android:id="#+id/widget_item"
>
<LinearLayout
android:paddingLeft="22dp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="•"
android:paddingRight="6dp"
android:textSize="17dp"
android:textColor="#color/colorText" />
<TextView
android:id="#+id/widget_item_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17dp"
android:textColor="#color/colorText" />
</LinearLayout>
Thank you for reading :)
To anyone reading this, i have been able to solve it by suppressing the style attribute

How can show bottom navigation view at a top of the app?

I want to show the bottom navigation view at the top of my app like facebook, i try this but its not showing anything.
Use TabLayout and you will have something like that.
Here below is a video which I think it will help you.
You need to use Fragments and TabLayout
https://codinginflow.com/tutorials/android/tab-layout-with-fragments
It does not matter where the bottom navigation view is placed, you can just place it at top of your layout.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_navigation"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
app:itemIconTint="#color/bottom_navigation_colors"
app:itemTextColor="#color/black"
app:menu="#menu/bottom_navigation_menu"
app:labelVisibilityMode="labeled"/>
<androidx.viewpager.widget.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" />
</LinearLayout>

Toggle child TextView inside CardView based on visibility

I have the following CardView:
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="expandCollapse">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="20dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textSize="18sp"
android:text="Heading Goes Here" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:srcCompat="#drawable/ic_action_down" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Description Goes Here"
android:visibility="gone" />
</LinearLayout>
</android.support.v7.widget.CardView>
Now, there are many of these cardviews, so I want to make my expandCollapse function work with all of them.
So basically, when the CardView is clicked, I want it to show or hide the Description Goes Here TextView depending on it's current visibility. It will either be GONE or VISIBLE. I also want it to change the ImageView arrow based on the current visibility.
Basically I just want this card to expand/collapse the textview inside of it. I know how do to this, but I don't know how to select the child TextView dynamically inside of my function.
How exactly do I select this TextView when the card is clicked, considering I will not be specifying an id for it, since I want this to work with many different CardView having the exact same layout?
I figured it out.
I simply added a tag to the TextView with android:tag="desc"
Then I used TextView textView = view.findViewWithTag("desc"); in my onClick function.

Categories