Friday, 23 August 2013

Change screen brightness and keep screen awake in Android (Updated with androidx package)

In this tutorial, we will be doing three operations:
1) Increase screen brightness,
2) Decrease screen brightness,
3) Keeping the screen awake (no permissions are needed in manifest)




----------------------------------- play_with_device_screen.xml ---------------------------------- 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center"
android:background="#ffffff">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:orientation="horizontal"
android:weightSum="3"
android:gravity="center">

<Button
android:id="@+id/pwdca_increaseBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="20sp"
android:text="+"
android:padding="5dp"/>

<Button
android:id="@+id/pwdca_awakeBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="20sp"
android:text="Stay Awake"
android:padding="5dp"/>

<Button
android:id="@+id/pwdca_decreaseBTN"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="@color/colorPrimaryDark"
android:textColor="#ffffff"
android:textSize="18sp"
android:text="-"
android:padding="5dp"/>

</LinearLayout>

</LinearLayout>

-------------------------------------------------------------------

------------------------------------- PlayWithDeviceScreenActivity.java ------------------------------------
package com.app.yourappname;

import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class PlayWithDeviceScreenActivity extends AppCompatActivity implements View.OnClickListener {

private WindowManager.LayoutParams winLayoutParam;
private float brightness = 0.4f;
private boolean keepScreenAwake = false;
private Button stayAwakeBTN;

@Override
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.play_with_device_screen_activity);
winLayoutParam = getWindow().getAttributes();

Button increaseBrightnessBTN = findViewById(R.id.pwdca_increaseBTN);
increaseBrightnessBTN.setOnClickListener(this);

Button decreaseBrightnessBTN = findViewById(R.id.pwdca_decreaseBTN);
decreaseBrightnessBTN.setOnClickListener(this);

stayAwakeBTN = findViewById(R.id.pwdca_awakeBTN);
stayAwakeBTN.setOnClickListener(this);
}

@Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.pwdca_awakeBTN:
if(!keepScreenAwake) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON, WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
keepScreenAwake = true;
stayAwakeBTN.setVisibility(View.GONE);
}
break;
case R.id.pwdca_decreaseBTN:
if(brightness>=0)
brightness-=0.1f;
else
brightness=0.0f;
winLayoutParam.screenBrightness = brightness;
getWindow().setAttributes(winLayoutParam);
Toast.makeText(this, "Brightness: "+brightness, Toast.LENGTH_SHORT).show();
break;
case R.id.pwdca_increaseBTN:
if(brightness<=1.0f)
brightness+=0.1f;
else
brightness=1.0f;
winLayoutParam.screenBrightness = brightness;
getWindow().setAttributes(winLayoutParam);
Toast.makeText(this, "Brightness: "+brightness, Toast.LENGTH_SHORT).show();
break;
}
}
}
------------------------------------------------------

-------------------------------------- Declare Activity name in Manifest -------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".PlayWithDeviceScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------



Dial a number with one click (Updated with androidx package)

Suppose you own a business and for its marketing you create an app which has all the details about the business such as product info, address and contact number. Now if the customer wants to call you via your contact number, he/she will expect a simple and quick way to dial the number.

In our app, we will be creating just one click option to take the user to dial a call!


--------------------------------- dial_activity.xml ------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#983428"
android:gravity="center"
android:weightSum="5">

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:textSize="38sp"
android:textColor="#ffffff"
android:gravity="center"
android:text="Bruno Chocolates!"/>

<ImageView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:src="@drawable/chocolate_box"/>

<Button
android:onClick="dialCall"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="0.5"
android:background="#be7e3e"
android:gravity="center"
android:textSize="25sp"
android:textColor="#ffffff"
android:text="Call to order a pack!"/>

</LinearLayout>
-------------------------------------------------------------------------

--------------------------------------------- DialActivity.java --------------------------------------------
package com.app.yourappname;

import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;

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

public class DialActivity extends AppCompatActivity {

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

public void dialCall(View v) {
Intent intent = new Intent(Intent.ACTION_DIAL);
intent.setData(Uri.parse("tel:123456789"));
startActivity(intent);
}
}
-------------------------------------------------------------

------------------------------------- Declare Acvitivy name in Manifest file -------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".DialActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------------

How to display app in Full Screen by removing the title bar and navigation bar...

Developing a game? Then turn your device into a playground...



Please Note: The images are taken from Pixabay.

----------------------------------In the full_screen_activity.xml file-----------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/animalbg"
android:orientation="vertical" >
</LinearLayout>
----------------------------------------------------------------------------------------
--------------------------------------------In FullScreenActivity.java------------------------------------------------
package com.app.yourappname;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

public class FullScreenActivity extends Activity {

@Override
public void onCreate(Bundle b) {
super.onCreate(b);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.full_screen_activity);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY);
}
}
----------------------------------------------------------------------------------------

----------------------------------------Declare the Activity name in Manifest file------------------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FullScreenActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
----------------------------------------------------------------------------------------

Thursday, 22 August 2013

How to use Shared Preferences to make a simple App in Android (updated with androidx)

The simplest way to store data in an app would be only Shared Preferences.
We are going to make an a small and simple app which store data and no need to use any button to save it. Once you close the scree, whatever you have edited, remains as it is.


----------------------------------------- shared_preferences_activity.xml -----------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#F57F17"
android:gravity="center|top">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="8"
android:orientation="vertical">

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#E91E63"
android:background="#FFB300"
android:text="Five Goals for this Year"/>

<EditText
android:id="@+id/spa_goal1ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal2ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal3ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:textStyle="bold"
android:textColor="#ffffff"
android:gravity="center"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal4ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center"
android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<EditText
android:id="@+id/spa_goal5ET"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center" android:textStyle="bold"
android:textColor="#ffffff"
android:textSize="20sp"/>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#FFB300"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1.5"
android:gravity="center"
android:textSize="20sp"
android:textStyle="bold"
android:textColor="#E91E63"
android:text="Progress in %"/>

<EditText
android:id="@+id/spa_progressET"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.5"
android:gravity="center"
android:textSize="20sp"
android:textColor="#ffffff"
android:text="0%"/>

</LinearLayout>
</LinearLayout>
</LinearLayout>
---------------------------------------------------------------
--------------------------------------- SharedPreferencesActivity.java ------------------------------------------
package com.app.yourappname;

import android.content.SharedPreferences;
import android.os.Bundle;
import android.widget.EditText;

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

public class SharedPreferencesActivity extends AppCompatActivity {

private GameDB gdb;
private EditText goal1ET, goal2ET, goal3ET, goal4ET, goal5ET, progressET;

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

gdb = new GameDB();

goal1ET = findViewById(R.id.spa_goal1ET);
goal2ET = findViewById(R.id.spa_goal2ET);
goal3ET = findViewById(R.id.spa_goal3ET);
goal4ET = findViewById(R.id.spa_goal4ET);
goal5ET = findViewById(R.id.spa_goal5ET);
progressET = findViewById(R.id.spa_progressET);

goal1ET.setText(gdb.getString("goal1"));
goal2ET.setText(gdb.getString("goal2"));
goal3ET.setText(gdb.getString("goal3"));
goal4ET.setText(gdb.getString("goal4"));
goal5ET.setText(gdb.getString("goal5"));
progressET.setText(""+gdb.getFloat("progress"));
}

public class GameDB {
private static final String PREF_NAME = "DATA";

public void setString(String key, String value) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putString(key, value);
editor.apply();
}

public String getString(String key) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
return prefs.getString(key,"");
}

public void setFloat(String key, float value) {
SharedPreferences.Editor editor = getSharedPreferences(PREF_NAME, MODE_PRIVATE).edit();
editor.putFloat(key, value);
editor.apply();
}

public float getFloat(String key) {
SharedPreferences prefs = getSharedPreferences(PREF_NAME, MODE_PRIVATE);
return prefs.getFloat(key,0.0f);
}
}

@Override
protected void onPause() {
super.onPause();
gdb.setString("goal1", goal1ET.getText().toString());
gdb.setString("goal2", goal2ET.getText().toString());
gdb.setString("goal3", goal3ET.getText().toString());
gdb.setString("goal4", goal4ET.getText().toString());
gdb.setString("goal5", goal5ET.getText().toString());
gdb.setFloat("progress", Float.parseFloat(progressET.getText().toString()));
}
}
---------------------------------------------------------------

------------------------------------------ Declare Activity name in Manifest file ---------------------------------

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".SharedPreferencesActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
---------------------------------------------------------------

Open Firefox Mozilla browser in Android (Updated with androidx package)

In order to open any website in Firefox Mozilla app, you will need to first download Firefox Mozilla app.



If the app is not installed, this code won't work and the app will rotate to landscape to show error. This is how the output will be:

This is the output when the Firefox Mozilla app is installed.


---------------------------------------firefox_mozilla_activity.xml--------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:gravity="center">

<Button
android:id="@+id/fma_openFirefoxBTN"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="80dp"
android:text="Open Firefox Mozilla"
android:textColor="#ffffff"
android:background="@color/colorPrimary"/>

</LinearLayout>
---------------------------------------
------------------------------- FirefoxOpenerActivity.java ---------------------------
package com.app.yourappname;

import android.content.ComponentName;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.net.Uri;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import androidx.appcompat.app.AppCompatActivity;

public class FirefoxMozillaActivity extends AppCompatActivity {

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.firefox_mozilla_activity);

final Button openFirefoxBtn = findViewById(R.id.fma_openFirefoxBTN);
openFirefoxBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openFirefoxMozilla();
}
});
}

private void openFirefoxMozilla() {
try {
String url = "http://www.java.com";
Intent intent = new Intent(Intent.ACTION_MAIN, null);
intent.addCategory(Intent.CATEGORY_LAUNCHER);
intent.setComponent(new ComponentName("org.mozilla.firefox", "org.mozilla.firefox.App"));
intent.setAction("org.mozilla.gecko.BOOKMARK");
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.setData(Uri.parse(url));
startActivity(intent); }
catch(Exception e) {
Toast.makeText(this,"Error: "+e, Toast.LENGTH_LONG).show();
}
}
}
---------------------------------------
--------------------------- In the manifest file declare Activity name ----------------------
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.app.yourappname">

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".FirefoxMozillaActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
---------------------------------------------