Found 1631 Articles for Android

How to handle a back button in an android activity?

Azhar
Updated on 31-Jul-2019 09:09:06

2K+ Views

This example demonstrates how do I handle back button in an android activity.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.javaimport android.os.Bundle; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {    Button button;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       button = findViewById(R.id.button);     ... Read More

How does Activity.finish() work in Android?

Azhar
Updated on 31-Jul-2019 09:04:01

5K+ Views

This example demonstrates how Activity.finish() work in android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.javaimport android.content.Intent; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       Button button = (Button) findViewById(R.id.button);       button.setOnClickListener(new View.OnClickListener() { ... Read More

How do I prevent an Android device from going to sleep mode?

Azhar
Updated on 31-Jul-2019 08:57:40

732 Views

This example demonstrates how do I prevent an Android device from going to sleep mode.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javaimport android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.WindowManager; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);    } }Step 4 − Add the following code ... Read More

What is difference between onCreate() and onStart() on Android?

Azhar
Updated on 31-Jul-2019 08:52:48

2K+ Views

This example demonstrates the difference between onCreate() and onStart() in Android.Note −onCreate() is called when the when the activity is first created.onStart() is called when the activity is becoming visible to the user.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javaimport android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.util.Log; public class MainActivity extends AppCompatActivity {    public static final String TAG = "Have a nice day!"; ... Read More

How to convert pixels to DP’s in Android app?

Azhar
Updated on 31-Jul-2019 08:48:34

186 Views

This example demonstrates about How do I convert Pixels to DP’s in Android.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.         Step 3 − Add the following code to src/MainActivity.javaimport android.content.Context; import android.content.res.Resources; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; import android.widget.Button; import android.widget.RelativeLayout; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       ... Read More

How to limit Decimal Places in Android EditText?

Azhar
Updated on 31-Jul-2019 08:31:28

4K+ Views

This example demonstrates about How to limit Decimal Places in Android EditText.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.text.InputFilter; import android.text.Spanned; import android.widget.EditText; import java.util.regex.Matcher; import java.util.regex.Pattern; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       EditText etText = ... Read More

How to detect airplane mode is on or off in Android?

Azhar
Updated on 01-Aug-2019 07:11:19

947 Views

This example demonstrates about How to detect airplane mode is on or off in AndroidStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.content.Context; import android.os.Bundle; import android.provider.Settings; import android.widget.TextView; public class MainActivity extends AppCompatActivity {    TextView textView;    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       textView ... Read More

How to play a custom ringtone/alarm sound in Android?

Azhar
Updated on 31-Jul-2019 08:05:39

1K+ Views

This example demonstrates about How to play a custom ringtone/alarm sound in AndroidStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.media.Ringtone; import android.media.RingtoneManager; import android.net.Uri; import android.os.Bundle; import android.view.View; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);    }    public void playRingtone(View view) ... Read More

How do I display the current date and time in an Android application?

Azhar
Updated on 31-Jul-2019 07:58:47

638 Views

This example demonstrates about How do I display the current date and time in an Android applicationStep 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; import android.widget.TextView; import java.text.DateFormat; import java.util.Date; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);       TextView tvDateTime ... Read More

How to set background color of a view in Android App

Azhar
Updated on 31-Jul-2019 07:51:32

2K+ Views

This example demonstrates about How do I change the color of Button in Android when clicked.Step 1 − Create a new project in Android Studio, go to File ⇒ New Project and fill all required details to create a new project.Step 2 − Add the following code to res/layout/activity_main.xml.     Step 3 − Add the following code to src/MainActivity.javapackage app.com.sample; import androidx.appcompat.app.AppCompatActivity; import android.os.Bundle; public class MainActivity extends AppCompatActivity {    @Override    protected void onCreate(Bundle savedInstanceState) {       super.onCreate(savedInstanceState);       setContentView(R.layout.activity_main);    } }Step 4 − Add the following code to androidManifest.xml ... Read More

Advertisements