Android how to animate alpha and then translation? - android

I want to make 2 animations, I have one view that will disappear once its touched(the one with emojis and "How do you feel today?"), with an alpha animation(from 1 to 0 alpha, at the end its visibility will be View.GONE), and then all the other views in the layout should slide up instead of them going up instantly. I have already done the alpha animation, the transition one is the one I can't achieve.
I am using Constraint Layout so what happens is as soon as the first view disappears changing its visibility to GONE, the others move instantly up. I have tried many ways, but I can´t find how to achieve this. I don't know what type of transition or animation should I use.
This is the code I am using fot the alpha animation:
fun hideEmojiView(view: View) {
val fadeOut: Animation = AlphaAnimation(1f, 0f)
fadeOut.interpolator = AccelerateInterpolator()
fadeOut.duration = 500
fadeOut.setAnimationListener(object : Animation.AnimationListener {
override fun onAnimationEnd(animation: Animation) {
view.visibility = View.GONE
}
override fun onAnimationRepeat(animation: Animation) {}
override fun onAnimationStart(animation: Animation) {}
})
view.startAnimation(fadeOut)
}
UPDATE: SOLUTION
I ended up making the suggestion #Tenfour04 made. The problem was that I was adding android:animateLayoutChanges="true" to a ScrollView and it needs to be added to a ConstraintLayout, and it needs to be the direct parent of the view you are dissapearing. So here is my Layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:animateLayoutChanges="true"
tools:context=".fragments.maincontent.TodayFragment">
<TextView
android:id="#+id/textView7"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/subtitle_font"
android:text="Upcoming"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_daily_picks" />
<FrameLayout
android:id="#+id/frameLayout7"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView6"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView6"
app:layout_constraintTop_toTopOf="#+id/textView6">
</FrameLayout>
<FrameLayout
android:id="#+id/frameLayout6"
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView5"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView5"
app:layout_constraintTop_toTopOf="#+id/textView5">
</FrameLayout>
<TextView
android:id="#+id/tv_welcome"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:fontFamily="#font/title_font"
android:text="Welcome, Sebastián"
android:textColor="#color/white"
android:textSize="24sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:fontFamily="#font/subtitle_font"
android:text="Stories"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/emoji_view" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_stories"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView5">
</androidx.recyclerview.widget.RecyclerView>
<FrameLayout
android:layout_width="0dp"
android:layout_height="2dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:background="#color/white"
app:layout_constraintBottom_toBottomOf="#+id/textView7"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/textView7"
app:layout_constraintTop_toTopOf="#+id/textView7">
</FrameLayout>
<TextView
android:id="#+id/textView6"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:fontFamily="#font/subtitle_font"
android:text="Daily picks"
android:textColor="#color/white"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/rv_stories" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_daily_picks"
android:layout_width="0dp"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView6">
</androidx.recyclerview.widget.RecyclerView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/rv_upcoming"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView7" />
<ImageView
android:layout_width="0dp"
android:layout_height="200dp"
android:layout_marginStart="8dp"
android:layout_marginEnd="8dp"
android:layout_marginBottom="8dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/rv_upcoming" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/emoji_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="16dp"
android:background="#drawable/round_white_30alpha_background"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tv_welcome">
<TextView
android:id="#+id/textView13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:fontFamily="#font/subtitle_font"
android:text="How do you feel today?"
android:textColor="#color/white"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/textView13">
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji1"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="😡"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji2"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🙁"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji3"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🤢"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji4"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="😃"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/emoji5"
android:layout_width="45dp"
android:layout_height="45dp"
android:layout_gravity="center"
android:layout_weight="1"
android:background="#drawable/circle_white"
android:text="🤩"
android:textAllCaps="false"
android:textSize="26sp" />
</FrameLayout>
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</ScrollView>
and my code:
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
// Inflate the layout for this fragment
val binding = FragmentTodayBinding.inflate(layoutInflater)
val user = AppController.get<UserModel>(UserModel.key)
binding.tvWelcome.text = "Welcome, " + user!!.name
binding.emoji1.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji2.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji3.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji4.setOnClickListener{ binding.emojiView.visibility = View.GONE }
binding.emoji5.setOnClickListener{ binding.emojiView.visibility = View.GONE }
return binding.root
}

Related

Button Click and Change Text with Sliding Root Navigation Drawer

I am trying to create a logout button on the Sliding Root Navigation which I found here
This is my main activity
class HomeScreenActivity : AppCompatActivity() {
private lateinit var binding: ActivityHomeScreenBinding
private lateinit var leftDrawerBinding: MenuLeftDrawerBinding
private lateinit var homeScreenViewModel: HomeScreenViewModel
private var slidingRootNav: SlidingRootNav? = null
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivityHomeScreenBinding.inflate(layoutInflater)
leftDrawerBinding = MenuLeftDrawerBinding.inflate(layoutInflater)
setContentView(binding.root)
setSupportActionBar(binding.slToolbar)
title = ""
slidingRootSetup(savedInstanceState)
setUpBottomNavBar()
homeScreenViewModel = ViewModelProvider(this)[HomeScreenViewModel::class.java]
replaceFragment(InGymFragment())
// leftDrawerBinding.logoutFab.setOnClickListener(listener)
}
// Side Panel
private fun slidingRootSetup(savedInstanceState: Bundle?) {
slidingRootNav = SlidingRootNavBuilder(this)
.withToolbarMenuToggle(binding.slToolbar)
.withMenuOpened(false)
.withGravity(SlideGravity.LEFT)
.withContentClickableWhenMenuOpened(true)
.withSavedState(savedInstanceState)
.withMenuLayout(R.layout.menu_left_drawer)
.inject()
}
private val listener = View.OnClickListener {
when (it.id) {
leftDrawerBinding.logoutFab.id -> logout()
}
}
private fun logout() {
Toast.makeText(this, "logout clicked!", Toast.LENGTH_LONG).show()
// homeScreenViewModel.logoutUser
// startActivity(Intent(this,AuthenticationScreenActivity::class.java))
}
This is the menu left drawer
Screenshot of menu left drawer
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:backgroundTint="#color/colorPrimaryDark"
android:orientation="vertical">
<TextView
android:id="#+id/user_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/name"
android:textSize="40sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/user_name">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout2">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout3">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:baselineAligned="false"
android:gravity="center"
android:orientation="vertical"
android:paddingLeft="24dp"
android:paddingRight="24dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/linearLayout4">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/current_membership"
android:textSize="25sp" />
<TextView
android:id="#+id/membership_status5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/temp"
android:textSize="50sp" />
</LinearLayout>
<com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
android:id="#+id/logout_fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:backgroundTint="#color/white"
android:focusableInTouchMode="true"
android:text="#string/logout"
tools:viewBindingType="com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton"
android:textColor="#color/black"
app:icon="#android:drawable/ic_lock_power_off"
app:iconTint="#color/black"
app:layout_constraintBottom_toTopOf="#id/app_version"
app:layout_constraintEnd_toEndOf="parent" />
<TextView
android:id="#+id/app_version"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="center"
android:padding="20dp"
android:text="#string/app_creator_with_version"
android:textSize="20sp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
This is the main activity layout
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".view.ui.HomeScreenActivity">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_blue">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/appBarLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/rounded_bottom_appbar"
app:elevation="0dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="20dp">
<androidx.appcompat.widget.Toolbar
android:id="#+id/sl_toolbar"
android:layout_width="match_parent"
android:layout_height="86dp"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ProgressBar
android:id="#+id/id_progress_bar"
android:layout_width="65dp"
android:layout_height="65dp"
android:indeterminateDrawable="#drawable/progress_background"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
<de.hdodenhof.circleimageview.CircleImageView
android:id="#+id/profile_image"
android:layout_width="48dp"
android:layout_height="48dp"
android:src="#drawable/profile"
android:layout_marginStart="8.5dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/welcomeMessage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="12dp"
android:text="#string/app_name"
android:textColor="#ECE8E8"
android:textSize="16sp"
app:layout_constraintStart_toEndOf="#+id/profile_image"
app:layout_constraintTop_toTopOf="#+id/profile_image" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/today_s_date"
android:textColor="#FFFFFF"
android:textSize="20sp"
app:layout_constraintStart_toStartOf="#+id/welcomeMessage"
app:layout_constraintTop_toBottomOf="#+id/welcomeMessage" />
<ImageView
android:id="#+id/notificationsImage"
android:layout_width="30dp"
android:layout_height="30dp"
android:src="#drawable/ic_notification"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#+id/profile_image"
android:contentDescription="#string/notification" />
</androidx.constraintlayout.widget.ConstraintLayout>
</com.google.android.material.appbar.AppBarLayout>
<FrameLayout
android:id="#+id/main_frame_layout"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#id/bottom_nav_bar"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/appBarLayout" />
<com.ismaeldivita.chipnavigation.ChipNavigationBar
android:id="#+id/bottom_nav_bar"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_gravity="bottom"
android:background="#drawable/rounded_top_bottomnav"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:cnb_menuResource="#menu/bottom_menu" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.drawerlayout.widget.DrawerLayout>
I don't know how to add an adapter to this. I tried searching for anyone already done that before but didn't find any clue.
Please See: Adding an android:onClick="logout" with fun logout(view: View) { Toast.makeText(this, "logout clicked via xml!", Toast.LENGTH_LONG).show() } works. But I wish to change the text too how am I suppose to get a hold to it?
You should include some relevant code of what you have tried so far, in order to better understand the question.
Anyway, if I get that correctly you need to add a Button in the menu.xml file, then in your fragment/activity.kt you can set a onClickListener on said button to implement the logic for logging out.

How to use onFocusChangeListener inside a recycler view adapter?

I am currently trying to expand a view when it is focused and contract it when unfocused. But, I am unable to get the events for onFocusChangeListener. I tried adding that for all of the views inside the parent view but that didn't work either. Is there anything like reyclerview takes all the focus changes to itself not allowing its children? This is my item code:
<?xml version="1.0" encoding="utf-8"?>
<androidx.cardview.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_marginBottom="20dp"
app:cardCornerRadius="6dp"
app:cardElevation="3dp">
<LinearLayout
android:id="#+id/selected"
android:layout_width="7dp"
android:layout_height="match_parent"
android:background="#167ED1"
android:orientation="horizontal" />
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/largeItemView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible"
android:focusable="true"
android:focusableInTouchMode="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="80dp"
android:background="#color/primary"
android:alpha="0.5"
app:layout_constraintTop_toTopOf="parent"/>
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintTop_toTopOf="parent">
<EditText
android:id="#+id/questionTitle"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:hint="Question"
android:maxLines="1"
android:padding="20dp"
android:theme="#style/SelectedColorBlue"
app:layout_constraintEnd_toStartOf="#+id/settings"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
android:focusable="true"
android:focusableInTouchMode="true"/>
<ImageView
android:id="#+id/settings"
android:layout_width="23dp"
android:layout_height="23dp"
android:layout_marginHorizontal="10dp"
android:background="?selectableItemBackgroundBorderless"
android:src="#drawable/more_vert"
app:layout_constraintBottom_toBottomOf="#id/questionTitle"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="#id/questionTitle"
app:tint="#color/white" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/optionsRV"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical"
android:padding="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
app:layout_constrainedHeight="true"
app:layout_constraintBottom_toTopOf="#id/addOption"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/questionTitle"
tools:itemCount="2"
tools:listitem="#layout/add_form_option_item" />
<TextView
android:id="#+id/points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="20dp"
android:text="1 Point"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#id/addOption"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/addOption" />
<TextView
android:id="#+id/editPoints"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="10dp"
android:text="#string/editHtmlUnderline"
android:textColor="#color/primary"
android:textSize="16sp"
app:layout_constraintBottom_toBottomOf="#+id/points"
app:layout_constraintStart_toEndOf="#+id/points"
app:layout_constraintTop_toTopOf="#+id/points" />
<Button
android:id="#+id/addOption"
android:layout_width="wrap_content"
android:layout_height="57dp"
android:layout_margin="20dp"
android:drawableEnd="#drawable/plus"
android:text="Add Option"
app:cornerRadius="18dp"
app:layout_constraintBottom_toTopOf="#id/required"
app:layout_constraintEnd_toEndOf="parent" />
<ImageView
android:id="#+id/delete"
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_margin="10dp"
android:padding="3dp"
android:src="#drawable/delete"
app:layout_constraintBottom_toBottomOf="#+id/required"
app:layout_constraintEnd_toStartOf="#+id/imageView"
app:layout_constraintTop_toTopOf="#+id/required"
app:tint="#color/gray" />
<ImageView
android:id="#+id/imageView"
android:layout_width="1dp"
android:layout_height="35dp"
android:layout_marginHorizontal="10dp"
android:padding="3dp"
android:src="#color/gray"
app:layout_constraintBottom_toBottomOf="#+id/required"
app:layout_constraintEnd_toStartOf="#id/required"
app:layout_constraintTop_toTopOf="#+id/required"
app:tint="#color/gray" />
<CheckBox
android:id="#+id/required"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="20dp"
android:buttonTint="#color/primary"
android:checked="true"
android:text="Required"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
<androidx.constraintlayout.widget.Group
android:id="#+id/group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:constraint_referenced_ids="addOption,points,editPoints,delete,imageView,required" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
and this is my code for adding the focus listsner inside the onBIndViewHolder:
binding.largeItemView.setOnFocusChangeListener { _: View, hasFocus: Boolean ->
Toast.makeText(itemView.context,"Focus change $hasFocus", Toast.LENGTH_SHORT).show()
binding.selected.visibility = if(hasFocus) VISIBLE else View.GONE
binding.group.visibility = if(hasFocus) VISIBLE else View.GONE
}
binding.largeItemView.children.forEach {
it.setOnFocusChangeListener { _, hasFocus ->
Toast.makeText(itemView.context,"Focus change $hasFocus", Toast.LENGTH_SHORT).show()
binding.selected.visibility = if(hasFocus) VISIBLE else View.GONE
binding.group.visibility = if(hasFocus) VISIBLE else View.GONE
}
}
and this is my reycler view:
<ScrollView>
<NestedScrollView>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/optionsRV"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:paddingHorizontal="10dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"
tools:itemCount="10"
tools:listitem="#layout/item_question_add_item" />
</NestedScrollView>
</ScrollView>
Thanks in advance

Recyclerview animation (layout change in size and margins after click)

Iam working on an app using recyclerview in which if I click an item, it will change in layout height(view will be visible) and margins changes.
How to animate like google dialer app when I click recent contact will show elevation layout and shrink margins?
I use notifyitemchange(position) and work in animate shift of point (left and top of layout).
but I want to animate shrink layout margins not shift view like google app dialer recent contacts [google app dialer].
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/groups_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipToPadding="false"
android:paddingTop="10dp"
android:paddingBottom="180dp"
app:layoutManager="androidx.recyclerview.widget.LinearLayoutManager"/>
</LinearLayout>
viewholder layout
<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<data>
<variable
name="group"
type="com.example.gtt.database.Group" />
<variable
name="clickListener"
type="com.example.gtt.start.GroupListener" />
<variable
name="viewModel"
type="com.example.gtt.start.StartViewModel" />
</data>
<androidx.cardview.widget.CardView
android:id="#+id/cardView"
layoutMarginGroup="#{group}"
layoutMarginGroupId="#{viewModel.hideShowId}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="6dp"
app:cardCornerRadius="6dp">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/itemLayout"
layoutBackColor="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/cLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="?android:attr/selectableItemBackground"
android:onClick="#{() -> viewModel.onGroupHideShowClicked(group.groupId)}"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:id="#+id/groupView"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_marginStart="#dimen/layoutMargin"
android:layout_marginTop="#dimen/layoutMargin"
android:layout_marginBottom="#dimen/layoutMargin"
android:background="#drawable/colored_circle"
android:gravity="center_horizontal|center_vertical"
android:onClick="#{() -> clickListener.onClick(group)}"
android:text="#{group.groupName.length >=2 ? group.groupName.substring(0,2).toUpperCase() : group.groupName.toUpperCase() }"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#color/white"
app:layout_constraintEnd_toStartOf="#id/groupsTextView"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<!-- android:text="#string/g" -->
<TextView
android:id="#+id/groupsTextView"
groupNameText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="10dp"
android:paddingRight="10dp"
android:textColor="#color/colorBlue"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#id/materialTextView"
app:layout_constraintEnd_toStartOf="#+id/cml"
app:layout_constraintStart_toEndOf="#id/groupView"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/materialTextView"
groupMaterialText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textColor="#color/colorBlue"
android:textSize="22sp"
app:layout_constraintBottom_toTopOf="#id/appTextView"
app:layout_constraintEnd_toEndOf="#id/groupsTextView"
app:layout_constraintStart_toStartOf="#id/groupsTextView"
app:layout_constraintTop_toBottomOf="#+id/groupsTextView" />
<TextView
android:id="#+id/appTextView"
appText="#{group}"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:paddingBottom="10dp"
android:textColor="#color/colorBlue"
android:textSize="16sp"
app:layout_constraintEnd_toEndOf="#+id/groupsTextView"
app:layout_constraintStart_toStartOf="#id/groupsTextView"
app:layout_constraintTop_toBottomOf="#+id/materialTextView" />
<LinearLayout
android:id="#+id/cml"
cmlBackColor="#{group}"
android:layout_width="wrap_content"
android:minWidth="70dp"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:gravity="center"
android:orientation="vertical"
app:layout_constraintBottom_toBottomOf="#+id/appTextView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/groupsTextView"
app:layout_constraintTop_toTopOf="parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/numberOfClasses"
android:textAlignment="center"
android:textSize="12sp" />
<TextView
android:id="#+id/classTextView"
classText="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAlignment="center"
android:textColor="#color/colorBlue"
android:textSize="18sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:text="#string/numberOfMonths"
android:textAlignment="center"
android:textSize="12sp" />
<TextView
android:id="#+id/monthTextView"
monthText="#{group}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:textAlignment="center"
android:textColor="#color/colorBlue"
android:textSize="18sp" />
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
<androidx.constraintlayout.widget.ConstraintLayout
android:id="#+id/constraintLayout2"
layoutVisibilityGroup="#{group}"
layoutVisibilityGroupId="#{viewModel.hideShowId}"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/cLayout">
<!-- android:visibility="gone"
android:background="#drawable/shadow_border"
-->
<View
android:id="#+id/divider9"
android:layout_width="0dp"
android:layout_height="1dp"
android:background="?android:attr/listDivider"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/addClassButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:drawableTop="#drawable/ic_add_class"
android:onClick="#{() -> viewModel.addClass()}"
android:text="#string/c"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/delClassButton"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/delClassButton"
style="#style/Widget.MaterialComponents.Button.TextButton"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:drawableTop="#drawable/ic_del_class"
android:onClick="#{() -> viewModel.delClass()}"
android:text="#string/c"
android:textColor="#color/black"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#id/addClassButton"
app:layout_constraintTop_toTopOf="#+id/addClassButton" />
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
</androidx.cardview.widget.CardView>
</layout>
binding adapter to change data and layout
#BindingAdapter("layoutVisibilityGroup", "layoutVisibilityGroupId")
fun ConstraintLayout.setLayoutVisibility(item: Group, id: Long) {
visibility = if (item.groupId == id) {
View.VISIBLE
} else {
View.GONE
}
}
#BindingAdapter("layoutMarginGroup","layoutMarginGroupId")
fun setLayoutMargin(view: CardView, item: Group, id: Long) {
if (view.layoutParams is ViewGroup.MarginLayoutParams) {
val p = view.layoutParams as ViewGroup.MarginLayoutParams
if (item.groupId == id) {
val npx = dpToPixel(10f).toInt()
p.setMargins(npx, p.topMargin, npx, p.bottomMargin)
} else {
p.setMargins(0, p.topMargin,0, p.bottomMargin)
}
//view.layoutParams = p
}
view.cardElevation = if (item.groupId == id) {
dpToPixel(8f)
} else {
0f
}
}
and I use this code to animate layout changes
adapter.notifyItemChanged(oldPosition)
adapter.notifyItemChanged(newPosition)

Re-constraining views in Constraint Layout programmatically

I have a constraint layout with a number of views and I wanted to sort them programmatically. I tried using ConstraintSet but it's not working as expected.
I created a sample app to demonstrate the problem
Here's the activity_main.xml file.
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/grid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingTop="5dp"
android:theme="#style/Theme.MaterialComponents.DayNight">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/leftGuide"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.02" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/rightGuide"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.98" />
<androidx.constraintlayout.widget.Guideline
android:id="#+id/topGuide"
android:layout_width="0dp"
android:layout_height="0dp"
android:orientation="horizontal"
app:layout_constraintGuide_percent="0.02" />
<com.google.android.material.card.MaterialCardView
android:id="#+id/myApp"
android:layout_width="0dp"
android:layout_height="117dp"
app:cardBackgroundColor="#FFFDE7"
app:cardCornerRadius="22dp"
app:cardElevation="3dp"
app:layout_constraintEnd_toStartOf="#id/cat"
app:layout_constraintHorizontal_chainStyle="spread_inside"
app:layout_constraintStart_toEndOf="#id/leftGuide"
app:layout_constraintTop_toBottomOf="#id/topGuide"
app:layout_constraintWidth_percent="0.3">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="8dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher_background" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="3dp"
android:maxWidth="100dp"
android:maxHeight="60dp"
android:text="#string/app_name"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#EB2B2313"
android:textSize="14sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/cat"
android:layout_width="0dp"
android:layout_height="117dp"
app:cardBackgroundColor="#FFFDE7"
app:cardCornerRadius="22dp"
app:cardElevation="3dp"
app:layout_constraintEnd_toStartOf="#id/kicks"
app:layout_constraintStart_toEndOf="#id/myApp"
app:layout_constraintTop_toTopOf="#id/topGuide"
app:layout_constraintWidth_percent="0.3">
<ImageView
android:layout_width="57dp"
android:layout_height="57dp"
android:layout_gravity="center"
android:layout_marginBottom="6dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher_foreground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="3dp"
android:maxWidth="100dp"
android:maxHeight="60dp"
android:text="cat"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#EB2B2313"
android:textSize="14sp" />
</com.google.android.material.card.MaterialCardView>
<com.google.android.material.card.MaterialCardView
android:id="#+id/kicks"
android:layout_width="0dp"
android:layout_height="117dp"
app:cardBackgroundColor="#FFFDE7"
app:cardCornerRadius="22dp"
app:cardElevation="3dp"
app:layout_constraintEnd_toStartOf="#id/rightGuide"
app:layout_constraintStart_toEndOf="#id/cat"
app:layout_constraintTop_toTopOf="#id/topGuide"
app:layout_constraintWidth_percent="0.3">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:layout_marginBottom="6dp"
android:contentDescription="#string/app_name"
android:src="#drawable/ic_launcher_foreground" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="3dp"
android:maxWidth="100dp"
android:maxHeight="60dp"
android:text="kicks"
android:textAppearance="#style/TextAppearance.AppCompat.Medium"
android:textColor="#EB2B2313"
android:textSize="14sp" />
</com.google.android.material.card.MaterialCardView>
</androidx.constraintlayout.widget.ConstraintLayout>
Here's how it looks like.
But i want to change the constraints programmatically to look like this
This is what I've tried
class MainActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
myApp.setOnClickListener {
val constrain = ConstraintSet()
constrain.apply {
clone(this#MainActivity,R.layout.activity_main)
connect(myApp.id,ConstraintSet.END,rightGuide.id,ConstraintSet.START,0)
connect(myApp.id,ConstraintSet.START,kicks.id,ConstraintSet.END,0)
connect(cat.id,ConstraintSet.START,leftGuide.id,ConstraintSet.END,0)
connect(cat.id,ConstraintSet.END,kicks.id,ConstraintSet.START,0)
connect(kicks.id,ConstraintSet.END,myApp.id,ConstraintSet.START,0)
connect(kicks.id,ConstraintSet.START,cat.id,ConstraintSet.END,0)
applyTo(grid)
}
}
}
}
But this is what I get
It seems it's a bug with constraint layout 2.0.0.beta2.
After upgrading to beta 4 (latest version currently), it produces the expected outcome.

Identify view being touched

I have a layout that looks something like this:
<?xml version="1.0" encoding="utf-8"?>
<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/main_read_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.project.books.flipview.FlipView
android:id="#+id/flip_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/color_black"
app:orientation="horizontal"
tools:context=".ReadActivity" >
</com.project.books.flipview.FlipView>
<include
android:id="#+id/readingtoolbar"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
layout="#layout/readingbar_layout" />
</android.support.constraint.ConstraintLayout>
The FlipView is a custom Framelayout that has a page turn animation like a book. The user is also able to pinch zoom on each page spread.
The Toolbar below has a selection of crayons. Once a user clicks on a color, an invisible fragment is launched and the user is able to draw on top of the page spread.
This is the toolbar Layout
<android.support.v7.widget.Toolbar
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/readbar"
android:layout_width="match_parent"
android:layout_height="?android:attr/actionBarSize"
android:background="#color/defaultdark_color"
app:layout_constraintBottom_toBottomOf="parent"
android:elevation="4dp">
<android.support.constraint.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/tabtools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:src="#drawable/icontools"
android:scaleType="fitXY"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/tabdictionary"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:scaleType="fitXY"
android:layout_marginStart="16dp"
android:src="#drawable/icondictionary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/tabtools"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/tabassessment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:alpha="0.3"
android:scaleType="fitXY"
android:src="#drawable/iconassesment"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/tabdictionary"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/tabexit"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:scaleType="fitXY"
android:src="#drawable/iconclose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/eraser"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginTop="4dp"
android:src="#drawable/drawingeraser"
android:scaleType="fitXY"
android:visibility="gone"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#id/guide_eraser"
app:layout_constraintBottom_toBottomOf="parent" />
<ImageView
android:id="#+id/crayon_black"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingblack"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_blue"
app:layout_constraintStart_toEndOf="#+id/guide_eraser" />
<ImageView
android:id="#+id/crayon_blue"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingblue"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_green"
app:layout_constraintStart_toEndOf="#+id/crayon_black" />
<ImageView
android:id="#+id/crayon_green"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawinggreen"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_orange"
app:layout_constraintStart_toEndOf="#+id/crayon_blue" />
<ImageView
android:id="#+id/crayon_orange"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingorange"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_pink"
app:layout_constraintStart_toEndOf="#+id/crayon_green" />
<ImageView
android:id="#+id/crayon_pink"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingpink"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_red"
app:layout_constraintStart_toEndOf="#+id/crayon_orange" />
<ImageView
android:id="#+id/crayon_red"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingred"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_violet"
app:layout_constraintStart_toEndOf="#+id/crayon_pink" />
<ImageView
android:id="#+id/crayon_violet"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingviolet"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_white"
app:layout_constraintStart_toEndOf="#+id/crayon_red" />
<ImageView
android:id="#+id/crayon_white"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:src="#drawable/drawingwhite"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#id/crayon_yellow"
app:layout_constraintStart_toEndOf="#+id/crayon_violet" />
<ImageView
android:id="#+id/crayon_yellow"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="8dp"
android:onClick="buttonClicked"
android:scaleType="fitXY"
android:visibility="gone"
android:layout_marginTop="4dp"
android:src="#drawable/drawingyellow"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#id/crayon_white"
app:layout_constraintEnd_toStartOf="#id/guide_center"/>
<ImageView
android:id="#+id/clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/iconreset"
android:visibility="gone"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintStart_toEndOf="#+id/guide_center"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/share"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:visibility="gone"
android:actionProviderClass="android.widget.ShareActionProvider"
android:showAsAction="ifRoom"
android:src="#drawable/iconshare"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="#+id/tabexit2"
app:layout_constraintTop_toTopOf="parent" />
<ImageView
android:id="#+id/tabexit2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:visibility="gone"
android:src="#drawable/iconclose"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.constraint.Guideline
android:id="#+id/guide_eraser"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.1"/>
<android.support.constraint.Guideline
android:id="#+id/guide_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent="0.6"/>
</android.support.constraint.ConstraintLayout>
</android.support.v7.widget.Toolbar>
The toolbar initially starts out as invisible. If you click anywhere on the page, it should slide out from the bottom. I am able to capture the click event by using the dispatchTouchEvent
override fun dispatchTouchEvent(ev: MotionEvent?): Boolean {
if (ev!!.action == MotionEvent.ACTION_DOWN) {
startX = ev.x
startY = ev.y
}
if (ev.action == MotionEvent.ACTION_UP){
val endX = ev.x
val endY = ev.y
if (isAClick(startX, endX, startY, endY)) {
onSlideViewButtonClick(reading_toolbar!!)
return true
} else {
return super.dispatchTouchEvent(ev)
}
}
return super.dispatchTouchEvent(ev)
}
So the toolbar slides out but if I click on any crayon on the toolbar, it slides back down which it isn't supposed to do. Basically, what I want is that if the user clicks anywhere else EXCEPT on the toolbar itself, then it should slide back down.
I tried using onTouch so I can pass the view instead of dispatchTouchEvent but I can't seem to capture clicks even with the same logic. I can capture a drag event, though.
class ReadActivity : Activity(), FlipViewAdapter.Callback, FlipView.OnFlipListener, FlipView.OnOverFlipListener, View.OnTouchListener {
...
}
setting
flip_view.setOnTouchListener(this)
testing the click here
override fun onTouch(v: View?, ev: MotionEvent?): Boolean {
println("clicking")
// put actual clicking logic
return true
}

Categories

Resources