If we have back stack like A -> B -> C -> D , and from D we need to open A without recreating its instance then we should use Clear top and single top as flags and root activity's (in our case A is root) launch mode should be singleTop or singleTask in our Android manifest file.
Fanciful Android
Monday 25 November 2019
Sunday 22 September 2019
Android Interface Definition Language (AIDL)
- AIDL used to perform IPC (inter process communication). In simple words, when we want to communicate with service of other application then we may use AIDL
- Que: How AIDL different then ContentProvider?
Ans: Using ContentProvider we can access data of other application and we may query on data. While Using AIDL we can do interaction with background Service of other application.
We are going to show one example of AIDL Server and Client application when client app will get string and one student object from server app for demo purpose you can do any process and send result from server app to client app in realtime. Lets start with server application first.
- SERVER APP:
Consider that our app's applicationId (package name) is "com.example.aidl.server".
AIDL is nothing but the interface through client app will get callback from server app.
So Basically in server app we have a Service and a AIDL file (interface).
We need to create interface into main>aidl path of the main (app) module.
Into aidl folder create same package name as our applicationId, and add following aidl file.
Now create following one service at your desired package.
Now you have to rebuild app before move further as android will create abstract class of binder object from aidl file. As you see above in service we have implemented object from MyAidl.Stub. and onBind method of service will return this AIDL.Stub instance (which is actually binder). so Client app will bind this service and get instance of Aidl stub. using this client app can call getStudentObject and getString methods. We can pass custom class using parcelable implementation. We need to create one aidl file too for parcelable class. Following is an example of that.
That's it for server app. - CLIENT APP:
Consider out client app's applicationId (package name) is "com.example.aidl.client".
First of all we need same aidl files as server have so we simply copy paste those files.
Here is one thing need to consider "MyAidl.aidl" file should be in package name same as server. We need "Student.aidl" and "Student.kt" both files same as server. you can place "Student.kt" file at your desired package name and in aidl folder we have to put "Student.aidl" file in same package name as we put "Student.kt" file in our client app. Now we have to rebuild app to move further so android studio will generate Stub classes. We can bind server's service and we can get string and student object from it following way.
Using above activity we have bind service and get data from other app.
That's it for client app.
You can find both app at this github url: https://github.com/k9428/Aidl_demo
So this way we can do interprocess communication using AIDL.
Tuesday 17 September 2019
How to force open english keyboard
- We generally required to open english keyboard by default in password fields. Consider scenario where phone language and app language both are set to other then english. (For ex. set to arabic), Still if we want to open english keyboard for password field then we can use
"android:imeOptions="flagForceAscii" attribute in password field.
Following is full example of edit text.
Friday 13 September 2019
How to use content provider in android
When to use content provider:
- When we want to
share our data, files etc to other application then we can use content provider.
Following diagram illustrate basic of it.
- Let's consider
content provider app as server app and the app who use it called client app.
So to use server app's data, client app needs to use ContentResolver class to perform query to the server app. server app will return cursor to client app so client can iterate through it and read data from cursor. Following diagram illustrate it
-
Lets start server
side (ContentProvider related) Example.
Consider following package and files structure in server app already there so that we can only focus on content provider. It mainly having Database related files using Room library. Following is structure with use of each file- content_provider_demo (package)
- dao (package)
- RecentSearchDao : Dao class to perform query on recent search table
- ContentProviderDemoActivity: demo activity to get result from content provider and print in logs.
- Database: Singleton class to create RoomDatabase object if not exist
- Detail : contains static variables. i.e. Db name etc
- RecentSearch: Table of recent search, made using Room lib
- RecentSearchProvider: Our provider class. we will discuss about it in next section
- ScoscheDatabase: Room database config class contains all Dao and Entity.
- We are going to
provide access of our "Recent search" data to other app using "RecentSearchProvider" class. To do
that first we have to create "RecentSearchProvider" class as following. I have just use insert and
query method for demo purpose. you can use other methods like update, delete etc
How it works
:
- User can use this content provider using content resolver. in content resolver we need to pass
unique URI so that system can find which provider need to use and what table and data need to
access.
- We can construct Uri like following.
Uri = host://authority/table
Here, host is fixed string "content://".
authority is any unique string which we need to define. We can construct it like following.
authority = package_name + "."+ your provider class name
-
We have to register this content provider class in AndroidMenifest.xml file to allow its access to apps. We can do it by adding following code in "application" tab.
Here, "name" contains path of our Content provider class and "authority" contains the authority of this provider.
- So server side (or ContentProvider related) logic done here.
- Client side (ContentResolver related) part start here.
Second part is "How to access data from ContentProvider".
We need to create ContentResolver object to access data from ContentProvider. Android have
getContentResolver() (contentResolver property for Kotlin) method to create object of
ContentResolver. ContentResolver have different methods like "insert","query","update",
"delete" etc to access data. We need to use ContentUri defined previously in content provider
to access its data. To insert data to recent search we can do like following.
- To get records from content provider we can use "query" method of contentResolver. It will return cursor
object so we can iterate through it to fetch data. Following example fetch all recent searches.
- That's it for contentResolver. Second part of code can be found in "ContentProviderDemoActivity".
Client side (ContentResolver related) ends here.
- You can find whole demo in Github. Github Link: https://github.com/k9428/Content-Provider-Demo
Tuesday 27 August 2019
android auto resize textview font not increasing after decrease
I have faced following issue when using textview with autoresize
Following is my layout xml to do auto size
Above code is working fine and did auto complete resize.
but For ex,
if I set value 100
then after some calculation or button click I want to change value to 10000 then text size is too small even if we have space to show this big value.
To solve above problem need to do following.
Following is my layout xml to do auto size
<androidx.appcompat.widget.AppCompatTextView
android:id="@+id/tvAmount"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollHorizontally="true"
android:maxWidth="@dimen/_290sdp"
android:maxHeight="@dimen/_50sdp"
android:gravity="start|bottom"
android:textSize="@dimen/_28ssp"
android:maxLines="1"
android:textColor="@color/charcoal"
android:includeFontPadding="false"
android:layout_toStartOf="@+id/tvCurrency"
tools:text="200"
app:autoSizeTextType="uniform"
app:autoSizeMinTextSize="@dimen/_5ssp"
app:autoSizeMaxTextSize="@dimen/_25ssp"
app:autoSizeStepGranularity="1sp"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toStartOf="@+id/tvCurrency" />
Above code is working fine and did auto complete resize.
but For ex,
if I set value 100
then after some calculation or button click I want to change value to 10000 then text size is too small even if we have space to show this big value.
To solve above problem need to do following.
tvAmount.setHorizontallyScrolling(true) tvAmount.text = value?.getDecimalFormat() tvAmount.setHorizontallyScrolling(false)
I have to enable and disable horizontal scrolling because internally for autosize text calculation, if horizontal scrolling is true text view's available drawing width is "1024 * 1024" and after setting text, we should disable horizontal scroll so text
size may decrease if required.
Subscribe to:
Posts
(
Atom
)