Showing posts with label Android App Features. Show all posts
Showing posts with label Android App Features. Show all posts

May 15, 2021

How To Add Exit Button In Android Studio - Androidpro.in

In this post we will learn about how to implement exit button in android studio projects app. Here provide java code which code paste in the home activity up to last Carli brasses after implement onBackPressed Method. For more explain watch video which below here.

Home activity code 

@Override
public void onBackPressed() {
    AlertDialog.Builder builder =
new AlertDialog.Builder(this);
    builder.setMessage(
"Do You Want To Exit ? ")
            .setCancelable(
false)
            .setPositiveButton(
"Yes", new DialogInterface.OnClickListener() {
               
@Override
                
public void onClick(DialogInterface dialogInterface, int i) {
                    HomeActivity.
super.onBackPressed();
                }
            })
            .setNegativeButton(
"No", new DialogInterface.OnClickListener() {
               
@Override
               
public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();

 

Share:

May 4, 2021

Floating Action Button Android Studio Example With Source Code - Androidpro.in

Xml code

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">

    <!--This will be the parent Floating Action Button-->
    <!--After the implementation the Floating Action Button at
        the bottom right corner as we have constrained accordingly-->
    <!--After clicking the above button the
        following two buttons will pop up.
        So this button is considered as parent FAB-->
    <!--After opening the application it looks like regular
        FAB but after user clicks it, it extends-->
    <!--This functionality has been handled in the MainActivity.java-->
    <com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton
        android:id="@+id/add_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="end"
        android:layout_marginEnd="16dp"
        android:layout_marginBottom="16dp"
        android:backgroundTint="@color/colorAccent"
        android:text="Actions"
        app:icon="@drawable/ic_baseline_add_24"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent" />

    <!--Floating action button for add alarm-->
    <!--Make sure that you are constraining this
        button to the parent button-->
    <!--Make sure to add backgroundTint attribute to match
        the entire application color-->
    <!--because after changing the base application theme the color
        of the FAB is set Blue as default-->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_alarm_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:backgroundTint="@color/colorAccent"
        app:fabSize="normal"
        app:layout_constraintBottom_toTopOf="@+id/add_fab"
        app:layout_constraintEnd_toEndOf="@+id/add_fab"
        app:srcCompat="@drawable/ic_baseline_add_24" />

    <!--Action name text for the add alarm button-->
    <!--Make sure that you are constraining this
        Text to the add Alarm FAB button-->
    <TextView
        android:id="@+id/add_alarm_action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Add Alarm"
        app:layout_constraintBottom_toBottomOf="@+id/add_alarm_fab"
        app:layout_constraintEnd_toStartOf="@+id/add_alarm_fab"
        app:layout_constraintTop_toTopOf="@+id/add_alarm_fab" />

    <!--Floating action button for add person-->
    <!--Make sure that you are constraining this button
        to the add Alarm FAB button-->
    <!--Make sure to add backgroundTint attribute to match
        the entire application color-->
    <!--because after changing the base application theme
        the color of the FAB is set Blue as default-->
    <com.google.android.material.floatingactionbutton.FloatingActionButton
        android:id="@+id/add_person_fab"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginBottom="24dp"
        android:backgroundTint="@color/colorAccent"
        app:fabSize="normal"
        app:layout_constraintBottom_toTopOf="@+id/add_alarm_fab"
        app:layout_constraintEnd_toEndOf="@+id/add_alarm_fab"
        app:layout_constraintStart_toStartOf="@+id/add_alarm_fab"
        app:srcCompat="@drawable/ic_baseline_add_24" />

    <!--Action name text for the add person button-->
    <!--Make sure that you are constraining this Text
        to the add Person FAB button-->
    <TextView
        android:id="@+id/add_person_action_text"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginEnd="8dp"
        android:text="Add Person"
        app:layout_constraintBottom_toBottomOf="@+id/add_person_fab"
        app:layout_constraintEnd_toStartOf="@+id/add_person_fab"
        app:layout_constraintTop_toTopOf="@+id/add_person_fab" />


</androidx.constraintlayout.widget.ConstraintLayout>

Java Code

package com.app.androidpro.my;

import androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;

import com.google.android.material.floatingactionbutton.ExtendedFloatingActionButton;
import com.google.android.material.floatingactionbutton.FloatingActionButton;

public class MainActivity extends AppCompatActivity {

    FloatingActionButton mAddAlarmFab, mAddPersonFab;
    ExtendedFloatingActionButton mAddFab;
    TextView addAlarmActionText, addPersonActionText;
    Boolean isAllFabsVisible;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        mAddFab = findViewById(R.id.add_fab);
        mAddAlarmFab = findViewById(R.id.add_alarm_fab);
        mAddPersonFab = findViewById(R.id.add_person_fab);

        addAlarmActionText = findViewById(R.id.add_alarm_action_text);
        addPersonActionText = findViewById(R.id.add_person_action_text);

        mAddAlarmFab.setVisibility(View.GONE);
        mAddPersonFab.setVisibility(View.GONE);
        addAlarmActionText.setVisibility(View.GONE);
        addPersonActionText.setVisibility(View.GONE);


        isAllFabsVisible = false;

        mAddFab.shrink();
        
        mAddFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        if (!isAllFabsVisible) {


                            mAddAlarmFab.show();
                            mAddPersonFab.show();
                            addAlarmActionText.setVisibility(View.VISIBLE);
                            addPersonActionText.setVisibility(View.VISIBLE);

                            mAddFab.extend();


                            isAllFabsVisible = true;
                        } else {

                            mAddAlarmFab.hide();
                            mAddPersonFab.hide();
                            addAlarmActionText.setVisibility(View.GONE);
                            addPersonActionText.setVisibility(View.GONE);

                            mAddFab.shrink();

                            isAllFabsVisible = false;
                        }
                    }
                });


        mAddPersonFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Person Added", Toast.LENGTH_SHORT).show();
                    }
                });


        mAddAlarmFab.setOnClickListener(
                new View.OnClickListener() {
                    @Override
                    public void onClick(View view) {
                        Toast.makeText(MainActivity.this, "Alarm Added", Toast.LENGTH_SHORT).show();
                    }
                });


    }
}

Share:

Apr 24, 2021

How add Share Button In Toolbar - Android Studio - Androidpro.in

MainActivity.Java

package in.app.androidpro.myapplication;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.content.Intent;
import android.content.pm.ApplicationInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;

import java.io.File;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       getMenuInflater().inflate(R.menu.menu,menu);
       return true;
    }

    @Override
    public boolean onOptionsItemSelected(@NonNull MenuItem item) {
       int id = item.getItemId();
       if (id == R.id.share){
           ApplicationInfo api = getApplicationContext().getApplicationInfo();
           String apkpath = api.sourceDir;
           Intent intent = new Intent(Intent.ACTION_SEND);
           intent.setType("aplication/vnd.android.package-archive");
           intent.putExtra(Intent.EXTRA_STREAM, Uri.fromFile(new File(apkpath)));
           startActivity(Intent.createChooser(intent,"ShareVia"));
       }
       return true;
    }
}

Menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/share"
    android:title="Share"
    android:icon="@drawable/ic_baseline_share_24"
    app:showAsAction="always"/>
</menu>

Share:

Apr 16, 2021

How To Add Splash Screen In Android Studio - Androidpro.in

 In this post show how to implement splash screen in android studio projects.

Step 1 : Create a new project

Step 2 : Create a new Activity Name Home Activity Or Any Other Name

Step 3:  add following code in activity_main.xml (or) main.xml:

<ImageView
        android:id="@+id/imageView"
        android:layout_width="112dp"
        android:layout_height="112dp"
        android:src="@drawable/icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/imageView" />


activity_main.xml (or) main.xml: Code Show Like This 

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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"
    tools:context=".MainActivity">
    <ImageView
        android:id="@+id/imageView"
        android:layout_width="112dp"
        android:layout_height="112dp"
        android:src="@drawable/icon"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />


    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:indeterminate="true"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        android:layout_marginTop="8dp"
        app:layout_constraintTop_toBottomOf="@id/imageView" />


</androidx.constraintlayout.widget.ConstraintLayout>


Step 4 : Open Main Activity (Means Java File Of Xml) Where Paste Xml Code And add following Java  code.

Thread background = new Thread() {
            public void run() {
                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),HomeActivity.class);
                    startActivity(i);

                    //Remove activity
                    finish();
                } catch (Exception e) {
                }
            }
        };
        // start thread
        background.start();

Java File Show Like This
package com.example.computershortcutekeybybajaranggroup;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Intent;
import android.os.Bundle;

public class MainActivity extends AppCompatActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Thread background = new Thread() {
            public void run() {
                try {
                    // Thread will sleep for 5 seconds
                    sleep(5*1000);

                    // After 5 seconds redirect to another intent
                    Intent i=new Intent(getBaseContext(),HomeActivity.class);
                    startActivity(i);

                    //Remove activity
                    finish();
                } catch (Exception e) {
                }
            }
        };
        // start thread
        background.start();

    }
}
 


Now your project is ready for show splash screen 
Share:

Apr 8, 2021

How to add exit button in android studio - Androidpro.in

In this post shows how to implement exit button in android app project in android studio.

Press back button and when you reach home page on your android app then if press again exit for quit from app then ask from user you want to exit.

Step 1 Go to main activity where you want to show exit button (Exit From App) 

Step 2 Open java file and paste code which code provide below down the main section of activity.

@Override
public void onBackPressed() {
    AlertDialog.Builder builder = new AlertDialog.Builder(this);
    builder.setMessage("Do You Want To Exit ? ")
            .setCancelable(false)
            .setPositiveButton("Yes", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    HomeActivity.super.onBackPressed(); 
                }
            })
            .setNegativeButton("No", new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialogInterface, int i) {
                    dialogInterface.cancel();
                }
            });
    AlertDialog alertDialog = builder.create();
    alertDialog.show();



Share:

Mar 21, 2021

How to implement back button in android studio - Androidpro.in

In this post we will read to how to add/set back button in android app projects in action bar in android studio.

Here provide a android studio code which code we use in activity of android studio projects, paste this code where you want to show the back button for go back after clicked, for example we create a story app and we want to  show back button in details story page then we need to paste this java code in the details story activity in java file in the main section. For more explain I suggest to you watch video which below here.

Step 1 Create a project

Step 2 Create new activity (for go next page)

Step 3 paste code in the second activity (new activity) which below

Java Code

package in.app.androidpro.makemoneyonline;

import
androidx.appcompat.app.ActionBar;
import
androidx.appcompat.app.AppCompatActivity;

import android.os.Bundle;
import
android.view.Menu;
import
android.view.MenuItem;
import
android.webkit.WebView;

public class
Card1 extends AppCompatActivity {

   
@Override
   
protected void onCreate(Bundle savedInstanceState) {
       
super.onCreate(savedInstanceState);
       
setContentView(R.layout.activity_card1);


       
ActionBar actionBar = getSupportActionBar();
        if
(actionBar != null) {
            actionBar.setDisplayHomeAsUpEnabled(
true);
       
}

            WebView webView
;
           
webView = findViewById(R.id.webview);
           
webView.loadUrl("file:///android_asset/Start A YouTube Channel.html");
       
}

   
public boolean onOptionsItemSelected(MenuItem item){
       
switch (item.getItemId()) {
           
case android.R.id.home:
                finish()
;
                return true;
       
}
       
return super.onOptionsItemSelected(item);
   
}

   
public boolean onCreateOptionsMenu(Menu menu) {
       
return true;

   
}
}

 

Share: