to support Java 8 add following line on the build.gradle file
// Keep the following configuration in order to target Java 8.
compileOptions {
sourceCompatibility JavaVersion.VERSION_1_8
targetCompatibility JavaVersion.VERSION_1_8
}
Welcome to my Blog.
In this post, I am going to talk about android share action provider.
What is Share Action Provider?
Share action provider is just a provider for share some action. we just create a that enable data sharing and also to show a submenu with sharing activities if the hosting item is placed on the overflow menu.
In this post, I will show you how can you add this feature in your app. Let's start What we need to do for adding share action provider? It's very simple step. We just need to create a view on menu.xml file add reference it from the java class. That's it.
Code analysis:
1. android:id -> we need an id for the call this view
2. android:title -> set a title, for this view3. app:showAsAction -> here I use "If room". Also available some other action too.
here a short list of action and their work
If room -> if there is enough space on action bar then it will
show always -> it will show this view always in the action bar
never -> for this action, this view will never show
collapseActionView -> This item's action view collapses to a normal menu item.
with text -> this will show the icon with text that defines on android: title on the XML file.
4. actionProviderClass -> here we need to set action provider class. We are working on Share action provider, so we need to set-
android.support.v7.widget.ShareActionProvider
Time for java code-
Create a global variable of Share action Provider Class;
ShareActionProvider myShareActionProvider;
Now come on onCreateOptionsMenu() method initializes this by using getActionProvider() method class of MenuItemCompat
Now you can use this method anywhere you want and update intent for this share action provider.
we just finished all code for adding share action prover. But ...
we can do this also various way, here is another one,
Create a global Variable of ShareActionProvider and initialize it on the on Create menu option (same as previous) now create an Intent where you have data and bind your data on and set this intent to ShareActionProvider variable
see the code:
Intent shareIntent =newIntent(Intent.ACTION_SEND);
shareIntent.setType("text/plain");String s ="welome to Android SketchPad";
shareIntent.putExtra(Intent.EXTRA_TEXT, s);if (myShareActionProvider !=null) {
myShareActionProvider.setShareIntent(shareInten);
}
If you are using loader manager then do this task on the onLoadFinished() method
Fix app crashing:
If you use proguard in your project, then your app will be crashed on release mode. To fix this you need to add below line on the proguard-project.txt
That's it:
That's the ending of today code. now you can add this option to your app. In this post, I just show you a basic tutorial about share action provider. In share action provider I just share text, but you can share image, document, video, audio and much more.
Welcome to this post. In this post, I am going to discuss on Android Splash Screen.
In this whole post, I gonna cover those topics
1. How to create android Splash Screen
2. How to override default color when app launching (windows background)
When you click app icon, the app takes some time to start. To overcome this option android by default show a color until the activity is launched. By default, it is white in color. So later in this post, I will show you how to overcome this situation and how to change this default color.
Let's start-
Create an Activity named it as your choice
now design your layout file. I just provide my XML code.
<?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:layout_width="match_parent"android:layout_height="match_parent"app:layout_behavior="@string/appbar_scrolling_view_behavior"tools:context="com.blogspot.shudiptotrafder.soilscience.SplashActivity"tools:showIn="@layout/activity_splash"android:background="?attr/colorPrimary"><ImageViewandroid:id="@+id/splash_imageView"android:layout_width="match_parent"android:layout_height="200dp"app:srcCompat="@drawable/ic_lover"app:layout_constraintStart_toStartOf="parent"android:layout_marginStart="8dp"app:layout_constraintEnd_toEndOf="parent"android:layout_marginEnd="8dp"android:contentDescription="app icon"app:layout_constraintTop_toTopOf="parent"android:layout_marginTop="80dp"tools:ignore="HardcodedText"/><TextViewandroid:id="@+id/splash_app"android:layout_width="wrap_content"android:layout_height="wrap_content"android:text="App Name"app:layout_constraintStart_toStartOf="parent"android:layout_marginStart="8dp"app:layout_constraintEnd_toEndOf="parent"android:layout_marginEnd="8dp"android:layout_marginTop="16dp"android:textSize="30sp"app:layout_constraintTop_toBottomOf="@+id/splash_imageView"/><TextViewandroid:layout_width="wrap_content"android:layout_height="wrap_content"app:layout_constraintTop_toBottomOf="@+id/splash_app"app:layout_constraintStart_toStartOf="parent"android:layout_marginStart="8dp"android:layout_marginEnd="8dp"android:layout_marginTop="16dp"android:id="@+id/splash_appAbout"android:text="This is a complete app"android:textSize="16sp"app:layout_constraintEnd_toEndOf="parent"/></android.support.constraint.ConstraintLayout>
now create an instance of Thread class and in the run methods call sleep methods and pass value 1000 (1000 for 1s, the value of Mille seconds). Don't forget to put this sleep method on try-catch block. and finally, block on try-catch, create an Intent that brings us to MainActivity. At list call finish() method and this activity will be destroyed.
See the code-
That's it. Splash Screen is created successfully. You can run a test for see result.
oh! Your app is not working. I forget one thing that we are using vector drawable. If your app minimum API is 21 then you have no headache. but if you targeting lower API you need to use support library to support vector drawable for lower API. To do that add below line in your build.gradle file
A Bonus for you
I also show you how to animate this splash screen. This is the pretty basic animation example.
Let's do this
create an XML file on res -> anim folder.
splash_screen_animation.xml
In the upper activity code, we don't create view variable, because we don't need that time. But now we need those views add set this animation with those views. Create view and assign it by calling findViewById()
That's it
now you can create a splash screen and also override the loading screen. In this example, I use just the only color but you can also use drawable in the loading time. So it's your choice what you used un your project.
That finished this post.
Why you wasting your time, create an awesome splash screen for your existing or next project?
Welcome to this post. In this post, we are going to discuss android text to speech.
Before the start, check the official documentation of Android text to speech.
In my case, I use this option with a fab (Floating action button). When the user clicks on this fab button this text to speech is triggered and user heard the speech.
Let's start,
Implement TextToSpeech.OnIntListener on your activity class.
override onIint() methods and follow below steps:
1. First, we need to check if Text to speech engine is installed or not
if (status != TextToSpeech.ERROR) {
}
2. If the status is not erroring then we set a default language, we set here the US as default.
int result = toSpeech.setLanguage(Locale.US);
3. now check language data is missing or not supported or not installed then we start for install this text to speech engine.
if (result ==TextToSpeech.LANG_MISSING_DATA||
result ==TextToSpeech.LANG_NOT_SUPPORTED||
result ==TextToSpeech.ERROR_NOT_INSTALLED_YET) {
Intent installIntent =newIntent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
if you missing something check the full code of onInit() method
@Overridepublicvoid onInit(int status) {
if (status !=TextToSpeech.ERROR) {
int result = toSpeech.setLanguage(Locale.US);if (result ==TextToSpeech.LANG_MISSING_DATA||
result ==TextToSpeech.LANG_NOT_SUPPORTED||
result ==TextToSpeech.ERROR_NOT_INSTALLED_YET) {
Intent installIntent =newIntent();
installIntent.setAction(TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA);
startActivity(installIntent);
}
}
}
Create a method and add a single parameter, word as String. In my case, I named it as setTTS() and parameter is word as String,
setTTS(String selectedWord)
Note: below methods is deprecated. So check if the device is lollipop and higher then use utterance as an extra parameter.
One more thing to go.
We need to shut down this TextToSpeech engine when the activity is destroyed. but before we need to check textToSpeech is null or not to avoid any error. If it is not null the call stop and shutdown method. see the code
That's it
you finished writing coding. Now call setTTS() method from anywhere in your project. You can use it as Button onClick or TextView onClick. Just pass a string parameter and call the method. As I told you, at the beginning of this post I am using this option in a fab button. you can that code by the following link. But don't be confused. This is the full activity code, so there is a lot of things with TextToSpeech. Click GitHub link.
Now you can use this code. Hope you understand this TextToSpeech logic. and you will be you use this option in your next or existing project.
In this post, we are going to learn about how to connect firebase from the android studio. This is the easiest methods ever.
we can directly connect firebase through the android studio.
Let's start-
open any project or create the new project
I am going throw picture by picture. Follow picture to picture for completing this process.
In android studio click tool and select firebase.
after click that it will open this window
Now click any option. In my case, I use analytics
click log on any event. After it will open this window
Click connect to firebase
after the process is finished it will open this window.
Edit project name (this is for firebase project. it will show in the firebase only) change your location.
click on connect to the firebase
after the finish of this process, it will show will connect firebase
succesfully connected now click add analytics to your app
click accept. it will change your gradle file
wait for finishing the gradle build.
after gradle finish, this will show Dependices set up correctly.
you can add more function by clicking the same process.
Now go to the firebase console
this is our newly created process.
That's it:
we successfully connected our project to firebase. This is the easiest methods ever. you don't need to add JSON file manually. All the file automatically add to your project.
In this post, we are going learn about how to sign in anonymously in firebase.
But before starting, I am providing you some ideas why we need sign in anonymously?
If you are using real-time database and storage you need to create a rule for access database and storage.
By default the rule is
here auth means Authentication. So if your app hasn't any login option you sign in anonymously sign in from your app. So by doing this, that database and storage can be access only from your app. so you can protect your database. Or if you change the rule to access everyone there is a possibility to lose your database. anyone can delete your database with in a single request.
So we sign in anonymously in Firebase from our app to protect database and storage. Let's start,
you already know how to connect Firebase for your project.
now go to firebase console and select authentication.
click this to go directly Firebase Console.
Now go to sign in method
and click anonymous and enable it.
that's the ending of the configuration of firebase console
now time for coding Note: I use kotlin as a default language.
Now create an instance of FirebaseAuth class
see the code-
val mAuth =FirebaseAuth.getInstance()
use signInAnonymously() method and add a complete listener. if the task is successful we show a message that Authentication is successful and also user id. if failed then we will show a fail message.
mAuth.signInAnonymously()
.addOnCompleteListener(this, { task ->if (task.isSuccessful) {
val user = mAuth.currentUser
Toast.makeText(this, "Authentication successful. user id ${user.uid}",
Toast.LENGTH_SHORT).show()
} else {
// If the sign in fails displays a message to the user.
Toast.makeText(this, "Authentication failed.",
Toast.LENGTH_SHORT).show()
}
})
That's it coding see the final screen short in the firebase console
you find some details about your user
1. creation date
2. signed in date
3. user id
That's it from this post hope this will help you to understand this concept.
and you will implement anonymous login option for your app and make a better security for your Firebase database and storage.
Happy coding