Thursday, 23 January 2014

Build your app for all screen sizes.

Here is how you will build your app compatible for each and every screen size in the world.


  


There are various ways to make your app compatible for all screen sizes. Here are some of the techniques described in easy way:

1) Add a small entry in Manifest: The first and most easiest way is to write the following in your manifest file.
<supports-screens
android:smallScreens="true"
android:normalScreens="true"
android:largeScreens="true"
android:xlargeScreens="true"
android:anyDensity="true" />
This will make your app's display fit to devices from small to extra large screens. Also it supports all densities.

2) Use weights and wrap_content & match_parent in layouts: In Android, weights are very important useful layout attributes. A weight is nothing but the percentage of area that needs to be covered. It works just like wrap_content & match_parent and automatically expands & shrinks according to the layout. Our job is just to specify the weight value.

Suppose you want to develop an app that has such a UI which should cover all the area of the screen proportionally as shown in the screen-shot, then weight does a big work as it suits for portrait as well as landscape screen orientation:


Here is how you modify your layout file:
<?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:layout_weight="2">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="vertical"
android:weightSum="3">

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="2"
android:background="#9940fd"/>

<TextView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#9965fd"/>
</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:weightSum="3"
android:orientation="horizontal">

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="2"
android:background="#9940fd"/>

<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#9965fd"/>

</LinearLayout>

</LinearLayout>

3) Create separate layouts: There are devices which have very high screen resolutions. The layouts made in the 'layout' folder cannot be applied for those as it won't look stunning. So in order to do so we have to create some extra separate individual layouts for those devices.
Here is the screen-shot how to make it.




Sunday, 5 January 2014

Keep holding on... by implementing onLongClickListener (Updated with androidx package)

With the help of onLongClickListener method one can use the button for two different purposes.




-------------This goes in the main.xml file--------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#018786"
android:gravity="center">

<Button
android:text="Hold me for a long time!"
android:id="@+id/lca_longClickBtn"
android:layout_marginTop="30dp"
android:gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"/>

<TextView
android:id="@+id/lca_resultTV"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="20dp"
android:textColor="#ffffff"/>
</LinearLayout>
----------------------------------------------------------

-----------------MyActivity.java file's content----------------
package com.app.yourappname;

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

import androidx.appcompat.app.AppCompatActivity;

public class LongClickActivity extends AppCompatActivity implements View.OnClickListener, View.OnLongClickListener {
private TextView tv;
private Button btn;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.long_click_activity);
tv = findViewById(R.id.lca_resultTV);
btn = findViewById(R.id.lca_longClickBtn);
btn.setOnClickListener(this);
btn.setOnLongClickListener(this);
}

@Override
public boolean onLongClick(View v) {
tv.setText("This is a LONG click!");
return true;
}

@Override
public void onClick(View v) {
tv.setText("This is a SIMPLE click!");
}
}
-------------------------------------------------------------

In the manifest, declare the Activity:
<?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=".LongClickActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>


Friday, 3 January 2014

Play the Media Sound! (Background music)

Can you Imagine a life without music? Please don't imagine 😊

This tutorial is all about implementing background music in your apps and especially games.


Keep the music.mp3 file in main->res->raw folder.
The guitar or any image you wish should be kept in the drawable folder.
I downloaded the image from Pixabay.

------------------------- play_media_activity.xml --------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#ff9e22"
android:weightSum="10">

<ImageView
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="9"
android:src="@drawable/guitar"/>

<Button
android:text="Start Background Music"
android:id="@+id/pma_play_music_BTN"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:background="#EE6002"
android:textColor="#ffffff"
android:textSize="16sp"
android:layout_gravity="center"/>

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

----------------------The java code---------------
package com.app.yourappname;

import android.media.MediaPlayer;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

public class PlayMediaActivity extends AppCompatActivity {

private MediaPlayer mp = null;
private Button playBTN;

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.play_media_activity);
initMedia();
playBTN = findViewById(R.id.pma_play_music_BTN);
playBTN.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {

if(mp!=null && !mp.isPlaying()) {
mp.start();
playBTN.setText("Background Music Started!");
}
else {
mp.pause();
mp.seekTo(0);
playBTN.setText("Background Music is not Started");
}
}
});
}

private void initMedia() {
try{
mp = new MediaPlayer();
mp.setOnPreparedListener(new MediaPlayer.OnPreparedListener() {
public void onPrepared(MediaPlayer mp)
{}
});

mp.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
public void onCompletion(MediaPlayer mp)
{ mp.reset(); }
});
mp = MediaPlayer.create(this, R.raw.music);
//mp.setVolume(100.0f, 1000.0f);
// mp.prepareAsync();
} catch(Throwable t) {
android.util.Log.e("PlayMediaActivity", "Exception in playing audio", t);
Toast.makeText(this, "Media Player Exception...", Toast.LENGTH_LONG).show();
}
}

@Override
protected void onResume() {
super.onResume();
if(mp!=null) {
if(mp.isPlaying()) {
playBTN.setText("Background Music Started!");
}
else {
playBTN.setText("Background Music is not Started");
}
}
}

@Override
public void onPause() {
super.onPause();
if(mp!=null) {
mp.pause();
mp.seekTo(0);
}
}
}
--------------------------------------------------------

------------------------------------ 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=".PlayMediaActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------

Thursday, 2 January 2014

Listen without Listeners. Detect click without onClickListener (Updated with androidx package)

Hi there! Today we will be doing an interesting and easy task where we will be making the button to function without implementing our general onClickListener() method.

This is achieved by just adding one more attribute in the xml file and the button will listen to us.

Here we go....




-------------Our xml file will have the following-------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#F06292"
android:gravity="center">
<Button
android:text="Click"
android:id="@+id/button1"
android:layout_marginTop="30dp"
android:layout_width="match_parent"
android:layout_height="80dp"
android:onClick="clicked"
android:textSize="25sp"
android:textStyle="bold"
android:textColor="#ffffff"
android:background="#EC407A"
android:layout_gravity="center"/>

<TextView
android:id="@+id/textView1"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:background="#F8BBD0"
android:gravity="center"
android:textColor="#000000"
android:textStyle="bold"
android:textSize="25dp"
android:text=""/>
</LinearLayout>
------------------------------------------------------------------

-----------------The OnClickXmlAcivity.java code----------------------
package com.app.yourappname;

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

import androidx.appcompat.app.AppCompatActivity;

public class OnClickXmlActivity extends AppCompatActivity {

private TextView tv;

@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.on_click_xml_activity);
tv = findViewById(R.id.textView1);
}

public void clicked(View v) {
tv.setText("Clicked!");
}
}
-----------------------------------------------------

--------------------- 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=".OnClickXmlActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>

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