So i tried to create an auto clicker with accessibility service in android studio based on this repository.
I have the following accessibility service config like this.
<?xml version="1.0" encoding="utf-8"?>
<accessibility-service
xmlns:android="http://schemas.android.com/apk/res/android"
android:accessibilityEventTypes="typeAllMask"
android:accessibilityFeedbackType="feedbackGeneric"
android:accessibilityFlags="flagDefault"
android:canPerformGestures="true"
android:canRetrieveWindowContent="true"
android:notificationTimeout="100"
android:description="#string/accessibility_service_description"
/>
These are the kotlin files
FloatingClickService.kt
class FloatingClickService : Service() {
private lateinit var manager: WindowManager
private lateinit var view: View
private lateinit var clickView: View
private lateinit var params: WindowManager.LayoutParams
private lateinit var clickParams: WindowManager.LayoutParams
private var startDragDistance: Int = 0
private var timer: Timer? = null
private lateinit var btnStart: ImageButton
private lateinit var btnSetting: ImageButton
private lateinit var btnMove: ImageButton
override fun onBind(intent: Intent): IBinder? {
return null
}
override fun onCreate() {
super.onCreate()
startDragDistance = dp2px(10f)
manager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
view = createView(R.layout.widget)
clickView = createView(R.layout.edittext_widget)
//setting the layout parameters
params = createLayoutParams()
clickParams = createLayoutParams(true)
//getting windows services and adding the floating view to it
manager.addView(view, params)
manager.addView(clickView, clickParams)
btnStart = view.findViewById(R.id.btn_start)
btnSetting = view.findViewById(R.id.btn_setting)
btnMove = view.findViewById(R.id.btn_move)
updateView()
btnStart.setOnClickListener {
viewOnStart()
}
btnSetting.setOnClickListener {
// setting action
}
setOnTouchListener()
}
private var isOn = false
private fun createLayoutParams(custom: Boolean = false): WindowManager.LayoutParams {
val overlayParam =
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY
} else {
WindowManager.LayoutParams.TYPE_PHONE
}
return WindowManager.LayoutParams(
if (custom) dp2px(48f) else WindowManager.LayoutParams.WRAP_CONTENT,
if (custom) dp2px(48f) else WindowManager.LayoutParams.WRAP_CONTENT,
overlayParam,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE,
PixelFormat.TRANSLUCENT)
}
private fun createView(layoutId: Int): View {
val view = LayoutInflater.from(this).inflate(layoutId, null)
//setting the layout parameters
params = createLayoutParams(false)
clickParams = createLayoutParams(true)
//getting windows services and adding the floating view to it
manager = getSystemService(Context.WINDOW_SERVICE) as WindowManager
return view
}
private fun updateView() {
if (isOn) {
btnSetting.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.settings))
btnSetting.isEnabled = false
btnStart.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.pause))
} else {
btnSetting.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.setting_active))
btnSetting.isEnabled = true
btnStart.setImageDrawable(ContextCompat.getDrawable(this, R.drawable.play))
}
}
private fun viewOnStart() {
if (isOn) {
timer?.cancel()
setOnTouchListener()
} else {
timer = fixedRateTimer(
initialDelay = 0,
period = 3000L
) {
val location = IntArray(2)
clickView.getLocationOnScreen(location)
autoClickService?.click(location[0] + clickView.right, location[1] + clickView.bottom)
}
removeTouchListener()
}
isOn = !isOn
updateView()
}
private fun removeView() {
manager.removeView(view)
manager.removeView(clickView)
}
#SuppressLint("ClickableViewAccessibility")
private fun setOnTouchListener() {
clickView.setOnTouchListener(
TouchAndDragListener(clickParams, startDragDistance) {
manager.updateViewLayout(clickView, clickParams)
}
)
btnMove.setOnTouchListener(
TouchAndDragListener(params, startDragDistance) {
manager.updateViewLayout(view, params)
}
)
}
#SuppressLint("ClickableViewAccessibility")
private fun removeTouchListener() {
clickView.setOnTouchListener(null)
btnMove.setOnTouchListener(null)
}
override fun onDestroy() {
timer?.cancel()
removeView()
super.onDestroy()
}
}
AutoClickService.kt
var autoClickService: AutoClickService? = null
class AutoClickService : AccessibilityService() {
override fun onInterrupt() {
// NO-OP
}
override fun onAccessibilityEvent(event: AccessibilityEvent) {
}
override fun onServiceConnected() {
autoClickService = this
startActivity(
Intent(this, MainActivity::class.java)
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK))
}
fun click(x: Int, y: Int) {
val path = Path()
path.moveTo(x.toFloat(), y.toFloat())
val builder = GestureDescription.Builder()
val gestureDescription = builder
.addStroke(GestureDescription.StrokeDescription(path, 10, 10))
.build()
dispatchGesture(gestureDescription, null, null)
}
}
Starting the service like this
val serviceIntent = Intent(
this#MainActivity,
FloatingClickService::class.java
)
startService(serviceIntent)
The actual result is that, the click is not in the center of the view but instead bottom right of the view. How to make the click happen in the center of the clickView?
I have tried to get the center x and y of the view but it didn't click. My expectation is the clicked tiles should be the same with the dot view. (I want the click happen in the center of the dot view)
class Rove3LiveVideoFragment : BaseFragment(){
#Inject
lateinit var roveR3LiveVideoFragmentViewModel: RoveR3LiveVideoFragmentViewModel
#Inject
lateinit var videoControlWidget: VideoControlWidget
#Inject
lateinit var notConnectedWidget: NotConnectedWidget
#Inject
lateinit var appPreference: AppPreference
#Inject
lateinit var videoFullViewWidget: VideoFullViewWidget
lateinit var customLoader: CustomLoader
private lateinit var onFullScreenListener: OnFullScreenListener
var isChangeScreenButtonClicked = false
var isPortraitMode = true
override fun onAttach(context: Context) {
inject(this)
super.onAttach(context)
if (activity is Rove3MainActivity) {
onFullScreenListener = activity as OnFullScreenListener
} else {
throw ClassCastException(
activity.toString()
+ " must implement MyListFragment.OnItemSelectedListener"
)
}
}
override fun onCreateView(
inflater: LayoutInflater,
container: ViewGroup?,
savedInstanceState: Bundle?
): View? {
return inflater.inflate(R.layout.fragment_rove_r3_live_video, container, false)
}
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
initWidget(view_live_vide_page)
customLoader = CustomLoader(requireContext())
val wifiManager =
requireContext().applicationContext.getSystemService(Context.WIFI_SERVICE) as WifiManager?
wifiManager?.let {
if (wifiManager.isWifiEnabled) {
roveR3LiveVideoFragmentViewModel.apply {
customLoader.show(getString(R.string.title_please_wait))
if (appPreference.isAppGoesBackGroundExceptHomePage) {
appPreference.isAppGoesBackGroundExceptHomePage = false
getRove3CameraConnectionStatus(true)
} else {
getRove3CameraConnectionStatus(false)
}
observe(stateLiveData, ::getR3ConnectedInLiveVideFragment)
}
} else {
notConnectedView()
}
}
}
private fun initWidget(view: View) {
videoControlWidget.apply {
initView(view)
addWidget(this)
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
observe(onClicked, ::onClickedButton)
}
notConnectedWidget.apply {
initView(view)
addWidget(this)
}
videoFullViewWidget.apply {
initView(view)
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
addWidget(this)
observe(onClicked, ::onFullVideoViewControllClick)
}
}
private fun onFullVideoViewControllClick(callToAction: VideoFullViewWidget.CallToAction) {
when (callToAction) {
is VideoFullViewWidget.CallToAction.PortraitMode -> {
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
showVideoView(false)
isChangeScreenButtonClicked = true
}
}
}
private fun onClickedButton(callToAction: VideoControlWidget.CallToAction) {
when (callToAction) {
is VideoControlWidget.CallToAction.LandScapeMode -> {
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
isChangeScreenButtonClicked = true
showVideoView(true)
}
}
}
override fun onPause() {
super.onPause()
Log.d("CALLBACKK", "onPause")
}
private fun getR3ConnectedInLiveVideFragment(state: BaseViewModel.LiveVideFragmentState) {
}
private fun notConnectedView() {
customLoader.hide()
notConnectedWidget.show()
view_surface.visibility = View.GONE
videoControlWidget.hide()
}
private fun showLiveVideoWhenR3Connected() {
notConnectedWidget.hide()
view_surface.visibility = View.VISIBLE
videoControlWidget.show()
}
private fun addWidget(widget: Widget) {
lifecycle.addObserver(widget)
}
private fun showVideoView(isFullVideoView: Boolean) {
onFullScreenListener.onFullSreen(isFullVideoView)
if (isFullVideoView) {
videoControlWidget.hide()
videoFullViewWidget.show()
val params = view_surface.layoutParams as RelativeLayout.LayoutParams
params.width = ViewGroup.LayoutParams.MATCH_PARENT
params.height = ViewGroup.LayoutParams.MATCH_PARENT
view_surface.layoutParams = params
} else {
val params = view_surface.layoutParams as RelativeLayout.LayoutParams
params.width = ViewGroup.LayoutParams.MATCH_PARENT
params.height =
(240 * requireContext().applicationContext.resources.displayMetrics.density).toInt()
view_surface.layoutParams = params
videoControlWidget.show()
videoFullViewWidget.hide()
}
}
override fun onConfigurationChanged(newConfig: Configuration) {
super.onConfigurationChanged(newConfig)
Log.d("ORIENTAIONNNNNN", "ORIENTATIONCHANGE")
requireActivity().let {
if (isChangeScreenButtonClicked) {
Log.d("ORIENTAIONNNNNN", "BUTTONCLICKED")
stopAutoOrientationFor3Seconds()
} else if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
Log.d("ORIENTAIONNNNNN", "LANDSCAPE")
onFullScreenListener.onFullSreen(false)
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
showVideoView(true)
isPortraitMode = false
} else if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) {
Log.d("ORIENTAIONNNNNN", "POTRIAT")
onFullScreenListener.onFullSreen(true)
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
showVideoView(false)
isPortraitMode = true
}
}
}
private fun stopAutoOrientationFor3Seconds() {
val handler = Handler()
handler.postDelayed(object : Runnable {
override fun run() {
run {
isChangeScreenButtonClicked = false
activity?.let {
it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR
}
}
}
}, 5000)
}
}
This is my Activity call back
override fun onFullSreen(fullscreen: Boolean) {
if (fullscreen) {
nav_view.visibility = GONE
window.decorView.systemUiVisibility = (android.view.View.SYSTEM_UI_FLAG_IMMERSIVE
or android.view.View.SYSTEM_UI_FLAG_LAYOUT_STABLE
or android.view.View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
or android.view.View.SYSTEM_UI_FLAG_HIDE_NAVIGATION)
window.addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
if (supportActionBar != null) {
supportActionBar!!.hide()
}
Log.d("ORIENTAIONNNNNN", "FULLVIEW")
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
} else {
nav_view.visibility = VISIBLE
Log.d("ORIENTAIONNNNNN", "HALFVIEW")
window.decorView.systemUiVisibility = android.view.View.SYSTEM_UI_FLAG_VISIBLE
window.clearFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN)
if (supportActionBar != null) {
supportActionBar!!.show()
}
requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
}
}
This is my fragment i have button click for full screen and also screen rotation on sensor on button click its working fine i am able to rotate screen because we are not depending on onConfigurationChanged method while when rotate device manually then first time when i launch app and try to rotate then portrait to landscape then onConfigurationChanged method calling but when i try to rotate to landscape to portrait then onConfigurationChanged method is not calling please help me what i am doing wrong i am following mvvm pattern .
Sorry it was my mistake i got solution for this question .
when i move it.requestedOrientation = ActivityInfo.SCREEN_ORIENTATION_SENSOR this line of code at end in fragment onconfig change method its work fine
I have this app in which I have a parent fragment, which has 2 child fragments in a ViewPager2 object.
One of the child fragments has an interface to communicate changes on its menu to the parent fragment.
The child fragment in question is TasksListFragment.kt and the parent fragment is TodayFragment.kt
When I try to initialize the interface in the child fragment onAttach() function, I get
FATAL EXCEPTION: main Process: com.rajchenbergstudios.hoygenda, PID:
java.lang.ClassCastException: com.rajchenbergstudios.hoygenda.ui.activity.MainActivity cannot be cast to com.rajchenbergstudios.hoygenda.ui.todaylists.taskslist.TasksListFragment$ChildFragmentListener
I don't understand why I get this error, and it says MainActivity, when the parent is a fragment which is the one implementing the interface in the first place, not the MainActivity.
I have everything set up correctly:
I have an interface in the child fragment
The interface is used on the child fragment onCreateMenu to pass the menu object to its interface function onFragmentMenuChanged(menu: Menu)
I override the child fragment's onAttach() and initialize the interface:
override fun onAttach(context: Context) {
super.onAttach(context)
childFragmentListener = context as ChildFragmentListener
}
I write a function called setListener() which is called from the parent fragment to pass its context this to the function parameter which assigns it to the childFragment listener
fun setListener(listener: ChildFragmentListener) {
this.childFragmentListener = listener
}
The parent fragment implements the child fragment listener as seen in the TodayFragment.kt file
Can you tell me what am I doing wrong or how to implement an interface to effectively communicate from child fragment back to its parent fragment?
TasksListFragment.kt
#ExperimentalCoroutinesApi
#AndroidEntryPoint
class TasksListFragment : Fragment(R.layout.fragment_child_tasks_list), TasksListAdapter.OnItemClickListener {
private val viewModel: TasksListViewModel by viewModels()
private lateinit var searchView: SearchView
private lateinit var childFragmentListener: ChildFragmentListener
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
val binding = FragmentChildTasksListBinding.bind(view)
val tasksListAdapter = TasksListAdapter(this)
binding.apply {
tasksListRecyclerview.layoutTasksListRecyclerview.apply {
adapter = tasksListAdapter
layoutManager = LinearLayoutManager(requireContext())
setHasFixedSize(true)
}
ItemTouchHelper(object: ItemTouchHelper.SimpleCallback(0,
ItemTouchHelper.LEFT or ItemTouchHelper.RIGHT){
override fun onMove(
recyclerView: RecyclerView,
viewHolder: RecyclerView.ViewHolder,
target: RecyclerView.ViewHolder
): Boolean {
return false
}
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, direction: Int) {
val task = tasksListAdapter.currentList[viewHolder.adapterPosition]
viewModel.onTaskSwiped(task)
}
}).attachToRecyclerView(tasksListRecyclerview.layoutTasksListRecyclerview)
}
loadObservable(binding, tasksListAdapter)
loadTasksEventCollector()
loadMenu()
}
private fun loadObservable(binding: FragmentChildTasksListBinding, tasksListAdapter: TasksListAdapter) {
viewModel.tasks.observe(viewLifecycleOwner){ tasksList ->
binding.apply {
HGDAViewStateUtils.apply {
if (tasksList.isEmpty()) {
setViewVisibility(tasksListRecyclerview.layoutTasksListRecyclerview, visibility = View.INVISIBLE)
setViewVisibility(tasksListLayoutNoData.layoutNoDataLinearlayout, visibility = View.VISIBLE)
} else {
setViewVisibility(tasksListRecyclerview.layoutTasksListRecyclerview, visibility = View.VISIBLE)
setViewVisibility(tasksListLayoutNoData.layoutNoDataLinearlayout, visibility = View.INVISIBLE)
tasksListAdapter.submitList(tasksList)
}
}
}
}
}
/**
* TasksListViewModel.TaskEvent.ShowUndoDeleteTaskMessage: Stays in this class. It asks for components relevant to this class.
* TasksListViewModel.TaskEvent.NavigateToEditTaskScreen: Stays in this class. The method it overrides comes from task list adapter.
* TasksListViewModel.TaskEvent.NavigateToDeleteAllCompletedScreen: Stays in this class. Relevant to menu which is in this class.
* TasksListViewModel.TaskEvent.NavigateToDeleteAllScreen: Stays in this class. Relevant to menu which is in this class.
*/
private fun loadTasksEventCollector() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
viewModel.tasksEvent.collect { event ->
when (event) {
is TasksListViewModel.TaskEvent.ShowUndoDeleteTaskMessage -> {
Snackbar
.make(requireView(), "Task deleted", Snackbar.LENGTH_LONG)
.setAction("UNDO"){
viewModel.onUndoDeleteClick(event.task)
}
.show()
}
is TasksListViewModel.TaskEvent.NavigateToEditTaskScreen -> {
val action = TodayFragmentDirections
.actionTodayFragmentToTaskAddEditFragment(task = event.task, title = "Edit task", taskinset = null, origin = 1)
findNavController().navigate(action)
}
is TasksListViewModel.TaskEvent.NavigateToAddTaskToSetBottomSheet -> {
val action = TasksListFragmentDirections.actionGlobalSetBottomSheetDialogFragment(task = event.task, origin = 1)
findNavController().navigate(action)
}
is TasksListViewModel.TaskEvent.NavigateToDeleteAllCompletedScreen -> {
val action = TasksListFragmentDirections
.actionGlobalTasksDeleteAllDialogFragment(origin = 1)
findNavController().navigate(action)
}
is TasksListViewModel.TaskEvent.NavigateToDeleteAllScreen -> {
val action = TasksListFragmentDirections
.actionGlobalTasksDeleteAllDialogFragment(origin = 3)
findNavController().navigate(action)
}
}.exhaustive
}
}
}
private fun loadMenu(){
val menuHost: MenuHost = requireActivity()
menuHost.addMenuProvider(object: MenuProvider {
override fun onCreateMenu(menu: Menu, menuInflater: MenuInflater) {
childFragmentListener.onFragmentChanged(menu)
menuInflater.inflate(R.menu.menu_tasks_list_fragment, menu)
val searchItem = menu.findItem(R.id.tasks_list_menu_search)
searchView = searchItem.actionView as SearchView
val pendingQuery = viewModel.searchQuery.value
if (pendingQuery != null && pendingQuery.isNotEmpty()) {
searchItem.expandActionView()
searchView.setQuery(pendingQuery, false)
}
searchView.OnQueryTextChanged{ searchQuery ->
viewModel.searchQuery.value = searchQuery
}
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
menu.findItem(R.id.tasks_list_menu_hide_completed).isChecked =
viewModel.preferencesFlow.first().hideCompleted
}
}
override fun onMenuItemSelected(menuItem: MenuItem): Boolean {
return when (menuItem.itemId) {
R.id.tasks_list_menu_sort_by_date -> {
viewModel.onSortOrderSelected(SortOrder.BY_DATE)
true
}
R.id.tasks_list_menu_sort_by_name -> {
viewModel.onSortOrderSelected(SortOrder.BY_NAME)
true
}
R.id.tasks_list_menu_hide_completed -> {
menuItem.isChecked = !menuItem.isChecked
viewModel.onHideCompletedSelected(menuItem.isChecked)
true
}
R.id.tasks_list_menu_delete_completed -> {
viewModel.onDeleteAllCompletedClick()
true
}
R.id.tasks_list_menu_delete_all -> {
viewModel.onDeleteAllClick()
true
}
else -> false
}
}
}, viewLifecycleOwner, Lifecycle.State.RESUMED)
}
interface ChildFragmentListener {
fun onFragmentChanged(menu: Menu)
}
fun setListener(listener: ChildFragmentListener) {
this.childFragmentListener = listener
}
override fun onItemClick(task: Task) {
viewModel.onTaskSelected(task)
}
override fun onItemLongClick(task: Task) {
viewModel.onTaskLongSelected(task)
}
override fun onCheckboxClick(task: Task, isChecked: Boolean) {
viewModel.onTaskCheckedChanged(task, isChecked)
}
override fun onAttach(context: Context) {
super.onAttach(context)
childFragmentListener = context as ChildFragmentListener
}
override fun onPause() {
super.onPause()
Logger.i(TAG, "onPause", "TasksListFragment paused")
}
override fun onDestroyView() {
super.onDestroyView()
searchView.setOnQueryTextListener(null)
}
}
TodayFragment.kt
#ExperimentalCoroutinesApi
#AndroidEntryPoint
class TodayFragment : Fragment(R.layout.fragment_parent_today), TasksListFragment.ChildFragmentListener {
private val viewModel: TodayViewModel by viewModels()
private lateinit var binding: FragmentParentTodayBinding
private var fabClicked: Boolean = false
private lateinit var tasksListMenu: Menu
private lateinit var viewPager: ViewPager2
private val rotateOpen: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.rotate_open_anim) }
private val rotateClose: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.rotate_close_anim) }
private val fromBottom: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.from_bottom_anim) }
private val toBottom: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.to_bottom_anim) }
private val fadeIn: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.fade_in) }
private val fadeOut: Animation by lazy { AnimationUtils.loadAnimation(requireContext(), R.anim.fade_out) }
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
super.onViewCreated(view, savedInstanceState)
binding = FragmentParentTodayBinding.bind(view)
binding.apply {
tasksListTransparentWhiteScreen.setOnClickListener {
fabAnimationsRollBack(binding)
fabClicked = !fabClicked
}
}
setChildFragmentMenus()
initViewPagerWithTabLayout(binding)
todayDateDisplay(binding)
initFabs(binding)
loadTodayEventCollector()
getFragmentResultListeners()
}
private fun setChildFragmentMenus(){
val tasksListFragment = TasksListFragment()
tasksListFragment.setListener(this)
Logger.i(TAG, "setChildFragmentMenus", "TasksListFragment menu set")
}
private fun getFragmentResultListeners() {
setFragmentResultListener("add_edit_request"){_, bundle ->
val result = bundle.getInt("add_edit_result")
onFragmentResult(result)
}
setFragmentResultListener("create_set_request_2"){_, bundle ->
val result = bundle.getInt("create_set_result_2")
onFragmentResult(result)
}
setFragmentResultListener("task_added_to_set_request"){_, bundle ->
val result = bundle.getInt("task_added_to_set_result")
val message = bundle.getString("task_added_to_set_message")
onFragmentResult(result, message)
}
setFragmentResultListener("task_added_from_set_request"){_, bundle ->
val result = bundle.getInt("task_added_from_set_result")
val message = bundle.getString("task_added_from_set_message")
onFragmentResult(result, message)
}
}
private fun onFragmentResult(result: Int, message: String? = ""){
viewModel.onFragmentResult(result, message)
}
/**
* TodayViewModel.TodayEvent.NavigateToAddTaskScreen: Relevant to this class. Belongs to Fab which are all in this class.
* TodayViewModel.TodayEvent.ShowTaskSavedConfirmationMessage: Relevant to this class. Belongs to onFragmentResultListener which is here.
* TodayViewModel.TodayEvent.ShowTaskSavedInNewOrOldSetConfirmationMessage: Relevant to this class. Belongs to onFragmentResultListener which is here.
* TodayViewModel.TodayEvent.ShowTaskAddedFromSetConfirmationMessage: Relevant to this class. Belongs to onFragmentResultListener which is here.
* TodayViewModel.TodayEvent.NavigateToAddTasksFromSetBottomSheet: Relevant to this class. Belongs to Fab which are all in this class.
*/
private fun loadTodayEventCollector() {
viewLifecycleOwner.lifecycleScope.launchWhenStarted {
viewModel.todayEvent.collect { event ->
when (event) {
is TodayViewModel.TodayEvent.NavigateToAddTaskScreen -> {
val action = TodayFragmentDirections
.actionTodayFragmentToTaskAddEditFragment(task = null, title = "Add task"
, taskinset = null, origin = 1)
findNavController().navigate(action)
}
is TodayViewModel.TodayEvent.ShowTaskSavedConfirmationMessage -> {
Snackbar.make(requireView(), event.msg, Snackbar.LENGTH_LONG).show()
setViewPagerPage(0)
}
is TodayViewModel.TodayEvent.ShowTaskSavedInNewOrOldSetConfirmationMessage -> {
Snackbar.make(requireView(), event.msg.toString(), Snackbar.LENGTH_LONG).show()
}
is TodayViewModel.TodayEvent.ShowTaskAddedFromSetConfirmationMessage -> {
Snackbar.make(requireView(), event.msg.toString(), Snackbar.LENGTH_LONG).show()
fabClicked = true
setFabAnimationsAndViewStates(binding)
setViewPagerPage(0)
}
is TodayViewModel.TodayEvent.NavigateToAddTasksFromSetBottomSheet -> {
val action = TasksListFragmentDirections
.actionGlobalSetBottomSheetDialogFragment(task = null, origin = 2)
findNavController().navigate(action)
}
}.exhaustive
}
}
}
// This will soon be used to be 1
private fun setViewPagerPage(index: Int){
viewModel.postActionWithDelay(300, object: TodayViewModel.PostActionListener{
override fun onDelayFinished() {
viewPager.setCurrentItem(index, true)
}
})
}
private fun todayDateDisplay(binding: FragmentParentTodayBinding) {
binding.apply {
tasksListDateheader.apply {
dateHeaderDayofmonth.text = viewModel.getCurrentDayOfMonth()
dateHeaderMonth.text = viewModel.getCurrentMonth()
dateHeaderYear.text = viewModel.getCurrentYear()
dateHeaderDayofweek.text = viewModel.getCurrentDayOfWeek()
}
}
}
private fun initViewPagerWithTabLayout(binding: FragmentParentTodayBinding) {
viewPager = binding.todayViewpager
val tabLayout: TabLayout = binding.todayTablayout
viewPager.adapter = activity?.let { TodayPagerAdapter(it) }
Logger.i(TAG, "initViewPagerWithTabLayout", "viewPager is not null")
TabLayoutMediator(tabLayout, viewPager) { tab, index ->
tab.text = when (index) {
0 -> "Tasks"
1 -> "Journal"
else -> throw Resources.NotFoundException("Tab not found at position")
}.exhaustive
when (index) {
0 -> {
}
1 -> {
fabClicked = false
}
}
}.attach()
}
private fun initFabs(binding: FragmentParentTodayBinding) {
binding.apply {
tasksListFab.setOnClickListener {
onMainFabClick(binding)
}
tasksListSubFab1.setOnClickListener {
Logger.i(TAG, "initFabs", "Coming soon")
}
tasksListSubFab2.setOnClickListener {
viewModel.onAddTasksFromSetClick()
}
tasksListSubFab3.setOnClickListener {
viewModel.onAddNewTaskClick()
}
}
}
private fun onMainFabClick(binding: FragmentParentTodayBinding) {
setFabAnimationsAndViewStates(binding)
}
private fun setFabAnimationsAndViewStates(binding: FragmentParentTodayBinding) {
setFabAnimationVisibilityAndClickability(binding, fabClicked)
fabClicked = !fabClicked
}
private fun setFabAnimationVisibilityAndClickability(binding: FragmentParentTodayBinding, clicked: Boolean) {
if (!clicked) fabAnimationsRollIn(binding) else fabAnimationsRollBack(binding)
}
private fun fabAnimationsRollIn(binding: FragmentParentTodayBinding) {
binding.apply {
HGDAAnimationUtils.apply {
HGDAViewStateUtils.apply {
setViewAnimation(v1 = tasksListFab, a = rotateOpen)
setViewAnimation(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3, a = fromBottom)
setViewAnimation(v1 = tasksListSubFab1Tv, v2 = tasksListSubFab2Tv, v3 = tasksListSubFab3Tv, a = fromBottom)
setViewAnimation(v1 = tasksListTransparentWhiteScreen, a = fadeIn)
setViewVisibility(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3
, v4 = tasksListSubFab1Tv, v5 = tasksListSubFab2Tv, v6 = tasksListSubFab3Tv, visibility = View.VISIBLE)
setViewVisibility(v1 = tasksListTransparentWhiteScreen, visibility = View.VISIBLE)
setViewClickState(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3, clickable = true)
setViewClickState(v1 = tasksListTransparentWhiteScreen, clickable = true)
}
}
}
}
private fun fabAnimationsRollBack(binding: FragmentParentTodayBinding) {
binding.apply {
HGDAAnimationUtils.apply {
HGDAViewStateUtils.apply {
setViewAnimation(v1 = tasksListFab, a = rotateClose)
setViewAnimation(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3, a = toBottom)
setViewAnimation(v1 = tasksListSubFab1Tv, v2 = tasksListSubFab2Tv, v3 = tasksListSubFab3Tv, a = toBottom)
setViewAnimation(v1 = tasksListTransparentWhiteScreen, a = fadeOut)
setViewVisibility(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3
, v4 = tasksListSubFab1Tv, v5 = tasksListSubFab2Tv, v6 = tasksListSubFab3Tv, visibility = View.INVISIBLE)
setViewVisibility(v1 = tasksListTransparentWhiteScreen, visibility = View.INVISIBLE)
setViewClickState(v1 = tasksListSubFab1, v2 = tasksListSubFab2, v3 = tasksListSubFab3, clickable = false)
setViewClickState(v1 = tasksListTransparentWhiteScreen, clickable = false)
}
}
}
}
override fun onFragmentChanged(menu: Menu) {
tasksListMenu = menu
}
override fun onPause() {
super.onPause()
tasksListMenu.clear()
}
}
Fragment is not a Context i.e fragment is not a child of context .
So when you try to cast context as ChildFragmentListener you are actually casting your Activity to ChildFragmentListener which is giving you this RuntimeException . to make it work you can use childFragmentListener = parentFragment as ChildFragmentListener
Also if your Doing this you do not need setListener method anymore.
On other hand i would suggest you do not use listeners to communicate b/w fragments . I see you already using viewModel so just use a shared one to communicate . You can get a shared ViewModel inside child by creating it with parentFragment.
So I got a problem, When the Main Page is shown, it's supposed to call the API to get the data from it. But, the API never called. What I find strange is, that before Main Page, there is a login page that successfully called the API for login. But, on Main Page, the Retrofit/Okhttp never called the Page to get the data. Do you guys know what's wrong?
Here's my code screenshot:
StoryPageSource
StoryRepository
ViewModel
StoryAdapter
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var bind: ActivityMainBinding
private lateinit var storyAdapter: StoryAdapter
private val timelineStoryViewModel: TimelineStoryViewModel by viewModels {
ViewModelFactory(this, application)
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
bind = ActivityMainBinding.inflate(layoutInflater)
setContentView(bind.root)
showLoadingProcess(true)
displayStories()
bind.floatingActionButton.setOnClickListener {
startActivity(Intent(this, AddStoryActivity::class.java))
}
supportActionBar?.setTitle(R.string.app_name)
}
override fun onCreateOptionsMenu(menu: Menu): Boolean {
val inflaterMenu = menuInflater
inflaterMenu.inflate(R.menu.menus, menu)
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.app_bar_settings -> {
val context = this
val pref = context.getSharedPreferences(
R.string.token_pref.toString(),
Context.MODE_PRIVATE
)
val editor = pref.edit()
editor.remove(R.string.token.toString())
editor.remove(getString(R.string.email))
editor.remove(getString(R.string.password))
editor.apply()
startActivity(Intent(this, LoginActivity::class.java))
}
R.id.app_bar_maps -> {
startActivity(Intent(this, MapsActivity::class.java))
}
}
return super.onOptionsItemSelected(item)
}
private fun displayStories() {
showLoadingProcess(false)
storyAdapter = StoryAdapter()
bind.storyRV.layoutManager = LinearLayoutManager(this)
bind.storyRV.adapter = storyAdapter
timelineStoryViewModel.story().observe(this#MainActivity) { story ->
storyAdapter.submitData(lifecycle, story)
}
storyAdapter.setOnProfileCallback(object : StoryAdapter.OnProfileCallback {
override fun onProfileClicked(data: TimelineStory) {
chosenProfile(data)
}
})
}
private fun chosenProfile(story: TimelineStory) {
var name: TextView = findViewById(R.id.userName)
var storyPict: ImageView = findViewById(R.id.userImageStory)
val optionsCompat: ActivityOptionsCompat =
ActivityOptionsCompat.makeSceneTransitionAnimation(
this,
Pair(storyPict, getString(R.string.imageStoryDetail)),
Pair(name, getString(R.string.nama_pengguna)),
)
val sendData = Intent(this, StoryDetailActivity::class.java)
sendData.putExtra(StoryDetailActivity.EXTRA_STORY, story)
startActivity(sendData, optionsCompat.toBundle())
Toast.makeText(this, "Memuat Story " + story.name, Toast.LENGTH_SHORT).show()
}
private fun showLoadingProcess(isLoading: Boolean) {
if (isLoading) {
bind.loading.visibility = View.VISIBLE
} else {
bind.loading.visibility = View.GONE
}
}
}
I'm trying to build an app that has an intro slider when it's installed. I followed a tutorial.
However, I have an error and the app doesn't even open.
Here is some code:
Class Intro
class Intro : AppCompatActivity(), View.OnClickListener {
lateinit var mPager : ViewPager
var layouts : IntArray = intArrayOf(R.layout.first_slide,R.layout.second_slide,R.layout.third_slide)
lateinit var dotsLayout : LinearLayout
lateinit var dots: Array<ImageView>
lateinit var mAdapter : PageAdapter
lateinit var btnNext: Button
lateinit var btnSkip: Button
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_intro)
if(PrefManager(this).checkPreferences()){goToHomePage()}
if(Build.VERSION.SDK_INT >=19)
{
window.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}else{
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS)
}
mPager=findViewById(R.id.pager)
mAdapter= PageAdapter(layouts,this)
mPager.adapter= mAdapter
dotsLayout = findViewById(R.id.dots)
btnNext = findViewById(R.id.btnNext)
btnSkip=findViewById(R.id.btnSkip)
btnSkip.setOnClickListener { this }
btnNext.setOnClickListener { this }
createDots(0)
mPager.addOnPageChangeListener(object : ViewPager.OnPageChangeListener{
override fun onPageScrollStateChanged(state: Int) {
}
override fun onPageScrolled(
position: Int,
positionOffset: Float,
positionOffsetPixels: Int
) {
}
override fun onPageSelected(position: Int) {
createDots(position)
if(position == layouts.size){
btnNext.setText("DONE")
btnSkip.visibility=View.INVISIBLE
}else{
btnNext.setText("NEXT")
btnSkip.visibility=View.VISIBLE
}
}
})
}
fun createDots(position:Int){
if(dotsLayout!=null){
dotsLayout.removeAllViews()
}
dots = Array(layouts.size,{i -> ImageView(this)})
for(i in 0..layouts.size -1) {
dots[i] = ImageView(this)
if (i == position) {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.active_dots))
} else {
dots[i].setImageDrawable(ContextCompat.getDrawable(this, R.drawable.inactive_dots))
}
var params: LinearLayout.LayoutParams = LinearLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT
)
params.setMargins(4,0,4,0)
dotsLayout.addView(dots[i],params)
}
}
override fun onClick(v: View?) {
when(v!!.id){
R.id.btnSkip ->{
goToHomePage()
PrefManager(this).writeSharedPreferences()
}
R.id.btnNext ->{
loadNextSlide()
}
}
}
private fun goToHomePage() {
startActivity(Intent(this, HomePage::class.java))
finish()
}
private fun loadNextSlide() {
var nextSlide: Int = mPager.currentItem+1
if(nextSlide<layouts.size){
mPager.setCurrentItem(nextSlide)
}
else{
goToHomePage()
PrefManager(this).writeSharedPreferences()
}
}
}
Class PrefManager
class PrefManager {
lateinit var context : Context
lateinit var pref: SharedPreferences
constructor(context: Context) {
this.context = context
getSharedPreferences()
}
private fun getSharedPreferences(){
pref = context.getSharedPreferences(context.getString(R.string.pref_name), Context.MODE_PRIVATE)
}
fun writeSharedPreferences(){
var editor : SharedPreferences.Editor=pref.edit()
editor.putString(context.getString(R.string.pref_key),"NEXT")
editor.commit()
}
fun checkPreferences() : Boolean
{
var status: Boolean = false
status = !pref.getString(context.getString(R.string.pref_key),null).equals("null")
return status
}
fun clearPreferences(){
pref.edit().clear().commit()
context.startActivity(Intent(context, HomePage::class.java))
(context as AppCompatActivity).finish()
}
}
The app works but the buttons next and skip don't respond whe clicked. Why is this happening?
Update: I changed some redundant thing on the code and uncommented the part that I've previous commented, and I don't have the error anymore. But the buttons still don't work