AndroidSketchpad
Imagine Learn And Apply
Imagine Learn And Apply
Home All posts
<item android:id="@+id/action_share"
android:title="@string/share"
app:showAsAction="ifRoom"
app:actionProviderClass="android.support.v7.widget.ShareActionProvider"/>
android.support.v7.widget.ShareActionProvider
ShareActionProvider myShareActionProvider;Now come on onCreateOptionsMenu() method initializes this by using getActionProvider() method class of MenuItemCompat
myShareActionProvider = (ShareActionProvider) MenuItemCompat.getActionProvider(shareItem);set a share intent
myShareActionProvider.setShareIntent(createShareIntent());Creta a method that's return Intent
private Intent createShareIntent() { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); String s = "welocme to android sketchPad"; shareIntent.putExtra(Intent.EXTRA_TEXT, s); return shareIntent; }Now we create another method that name update share action provider: note: don't forget to cheek shareActionProvider is not null
private void resetShareActionProvider(String word) { if (myShareActionProvider != null) { Intent shareIntent = new Intent(Intent.ACTION_SEND); shareIntent.setType("text/plain"); shareIntent.putExtra(Intent.EXTRA_TEXT, word); myShareActionProvider.setShareIntent(shareIntent); } }Now you can use this method anywhere you want and update intent for this share action provider.
Intent shareIntent = new Intent(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
-keep class android.support.v7.widget.ShareActionProvider { *; }
<?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"> <ImageView android: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" /> <TextView android: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" /> <TextView android: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>and ic_lover.xml it as vector drawable, code here
<vector android:height="24dp" android:viewportHeight="512.0" android:viewportWidth="512.0" android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android"> <path android:fillColor="#FF6464" android:pathData="M0,173.5C0,96 62.9,33.1 140.4,33.1c34.4,0 65.9,12.4 90.3,32.9c14.7,12.3 36,12.3 50.7,0c24.4,-20.5 55.9,-32.9 90.3,-32.9C449.1,33.1 512,96 512,173.5v-4.1c0,136.1 -165.6,258.9 -230.4,301.8c-15.5,10.3 -35.7,10.3 -51.2,0C165.6,428.3 0,305.5 0,169.4" /> <path android:fillColor="#D2555A" android:pathData="M0,173.5c0,-0.6 0.1,-1.2 0.1,-1.8C0.1,171 0,170.2 0,169.4V173.5z" /> <path android:fillColor="#D2555A" android:pathData="M114.8,182.7c-0,0.6 -0.1,1.3 -0.1,1.9v-4.5C114.8,181 114.8,181.9 114.8,182.7c0.5,-66.9 29.4,-123.3 69.2,-142.5c-13.7,-4.5 -28.4,-7.1 -43.6,-7.1c-76.9,0 -139.3,61.9 -140.3,138.6c1.9,135.2 165.9,256.8 230.3,299.4c6.6,4.4 14.1,6.8 21.6,7.4C198.9,418.8 116,306 114.8,182.7z" /> <path android:fillColor="#FF8B8B" android:pathData="M449.5,153.9m-32,0a32,32 0,1 1,64.1 0a32,32 0,1 1,-64.1 0" /> <path android:fillColor="#FFC1C1" android:pathData="M408.4,92.8m-20,0a20,20 0,1 1,40.1 0a20,20 0,1 1,-40.1 0" /> </vector>
public class SplashScreen extends AppCompatActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); }
Thread checkForData = new Thread(){ @Override public void run() { super.run(); try{ sleep(1000); } catch (Exception e) { e.printStackTrace(); } finally { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } } };start this thread by calling start method
checkForData.start();That's it. Splash Screen is created successfully. You can run a test for see result.
compile 'com.android.support:support-vector-drawable:25.3.1'Note: change the version according to you compile SDK version. See mine-
compileSdkVersion 25 buildToolsVersion "25.0.3"One more thing to go. add below code in your activity class
static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); }ok, run the app again.
<style name="splashTheme" parent="AppTheme.NoActionBar"> <item name="android:windowBackground">@color/colorPrimary</item> </style>use this theme on the splash screen activity. Open manifest file and override below code-
<activity android:name=".SplashScreen" android:label="@string/app_name" android:theme="@style/splashTheme"> <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER" /> </intent-filter> </activity>and my color code is
<color name="colorPrimary">#3F51B5</color>rerun your app and see the change.
<alpha
xmlns:android="http://schemas.android.com/apk/res/android"
android:fromAlpha="0"
android:toAlpha="1"
android:duration = "1000" />
Now load this animation, see code-Animation set = AnimationUtils.loadAnimation(this, R.anim.splash_screen_animation);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()
ImageView imageView = (ImageView) findViewById(R.id.splash_imageView); TextView app = (TextView) findViewById(R.id.splash_app); TextView appAbout = (TextView) findViewById(R.id.splash_appAbout);set animation with those views and also start the animation
//set animation imageView.setAnimation(set); app.setAnimation(set); appAbout.setAnimation(set); //start animation set.start();That's it now run the app again and see the animation on your splash screen.
public class SplashScreen extends AppCompatActivity { static { AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_splash); ImageView imageView = (ImageView) findViewById(R.id.splash_imageView); TextView app = (TextView) findViewById(R.id.splash_app); TextView appAbout = (TextView) findViewById(R.id.splash_appAbout); Animation set = AnimationUtils.loadAnimation(this, R.anim.splash_screen_animation); //set animation imageView.setAnimation(set); app.setAnimation(set); appAbout.setAnimation(set); //start animation set.start(); Thread checkForData = new Thread(){ @Override public void run() { super.run(); try{ sleep(1000); } catch (Exception e) { e.printStackTrace(); } finally { startActivity(new Intent(SplashActivity.this, MainActivity.class)); finish(); } } }; checkForData.start(); } }
public class DetailsActivity extends AppCompatActivity implements TextToSpeech.OnInitListener { }
private TextToSpeech toSpeech;
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 = new Intent(); installIntent.setAction( TextToSpeech.Engine.ACTION_INSTALL_TTS_DATA); startActivity(installIntent); }if you missing something check the full code of onInit() method
@Override public void 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 = new Intent(); 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,
private void setTTS(String selectedWord) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { toSpeech.speak(selectedWord, TextToSpeech.QUEUE_FLUSH, null, null); } else { toSpeech.speak(selectedWord, TextToSpeech.QUEUE_FLUSH, null); } }Note: below methods is deprecated. So check if the device is lollipop and higher then use utterance as an extra parameter.
@Override protected void onDestroy() { super.onDestroy(); if (toSpeech != null) { toSpeech.stop(); toSpeech.shutdown(); } }
{ "rules": { ".read": "auth != null", ".write": "auth != null" } }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.
implementation 'com.google.firebase:firebase-auth:11.2.0'
Now create an instance of FirebaseAuth classval 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
val nullValue:String? = nullif you want to create a variable but want to assign a value later then use
var name:String? name = "shudipto"now print the value
println("Name: $name")output:
Name: shudiptonow assign this value to null
name = nullprint this new value
println(name)Output:
null
Note: if you want to set this variable must have value then use below code-print(name!!)now we create a function that returns null
//null return function fun returnNull():String? { return null }print this value:
println("Function: ${returnNull()}")Output:
println("Function: ${returnNull()}")now we use the force operator to assign the return value of this methods
val returnNull1:String ?= returnNull()now if we want to learn the length of this variable we can use length method
//val returnNull2 = returnNull1.lengthbut this line shows an error because the value of the variable is null. This is the advantage of kotlin. If you are in java the here this line program will be crashed. but in kotlin, we can do by this way without any crash
val returnNull2 = returnNull1?.length
Now, this program will not crash. but you can do another thing that is if the value is null then you can save a string or default value I am using a stringval returnNull2 = returnNull1?.length ?: "value is null"print the value
println("ReturnNull2: $returnNull2")output:
ReturnNull2: value is nullsee the variable value is null so the default value is assigned. you can also use this method for the specific data type variable:
val returnNull4:String = returnNull() ?: "No name"print the value:
println(returnNull4)Output:
No nameThat's it. Hope you understand the concept. This is very important in Android App development. It prevent some unexpected crash.
welcome to kotlin series. Kotlin Series In this series, we are basically going through some basic and advanced tutorial about kotlin ...