Posts

Showing posts from April, 2020

Android - Optimize and Publish Apps

Before developers publish apps, the following things should be done to prevent the Google Play Console from annoying us. 1. Change the package name com.example.<Name of project> to com.<Name of organization>.<Name of project> in the build.gradle file. 2. Check and adjust versionCode and versionName in the build.gradle file properly. 3. Reduce your app size by using android { // Other settings buildTypes { release { minifyEnabled true shrinkResources true proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro' } } } in the build.gradle file. 4. Convert images to WebP by right clicking on an image file or an image folder, and then click convert to WebP. 5. Prepare and upload one 512 x 512 32-bits PNG icon file and one 1024 x 500 24-bits PNG feature graphic file. References: https://developer.android.com/topic/performance/

Android - Clipboard and Keyboard

1. Copy text to the clipboard. In Activity, use val clipboard = getSystemService(ClipboardManager::class.java) val clipData = ClipData.newPlainText("The first copied text.", "Hello World!") clipboard.setPrimaryClip(clipData) In Fragment, use val clipboard = requireActivity().getSystemService(ClipboardManager::class.java) val clipData = ClipData.newPlainText("The first copied text.", "Hello World!") clipboard.setPrimaryClip(clipData) 2. Close the keyboard and the cursor. In Activity, use getSystemService(InputMethodManager::class.java) .hideSoftInputFromWindow(<The View object of scope>.windowToken, 0) currentFocus?.clearFocus() In Fragment, use requireActivity().getSystemService(InputMethodManager::class.java) .hideSoftInputFromWindow(<The View object of scope>.windowToken, 0) requireActivity().currentFocus?.clearFocus()

Android - Scrollable TextView in ViewPager2

1. Check the scrollbars in the attributes of TextView. 2. Use textView.movementMethod = ScrollingMovementMethod() 3. Use textView.setOnTouchListener { _, _ -> textView.parent.requestDisallowInterceptTouchEvent(true) false // It must return false to make TextView scrollable in ViewPager2. } Reference: https://stackoverflow.com/questions/29471842/allow-scrolling-edittext-and-swiping-viewpager

Android - ViewPager2, TabLayout, and Fragment

1. Prerequisites: Before using ViewPager2 and TabLayout, append implementation 'com.google.android.material:material:$material_version' to the build.gradle file. 2. Usage: a. Use ViewPager2 and TabLayout in the activity_main layout. b. Create Fragment classes and their layout files. Warning: All subclasses of Fragment must include a public no-argument constructor. class FragmentA: Fragment() { override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? ): View? { return inflater.inflate(R.layout.fragment_a, container, false) } override fun onViewCreated(view: View, savedInstanceState: Bundle?) { super.onViewCreated(view, savedInstanceState) // Change UI and use listeners here. } } c. Connect Fragment to TabLayout in MainActivity. viewPager.adapter = object : FragmentStateAdapter(this) { override fun getItemCount(): Int {