Sunday, 1 March 2015

How to Post Admob's banner ad at the bottom of a custom view/ surface view (Programmeticaly).

In this post we will see how you can put Admob's banner ad at the very bottom of your custom view or SurfaceView Programmeticaly with the latest way of integrating Admob.

I hope you already know how to add a normal banner ad. If not please hit this link to know super easy way.


------------------------------------ build.gradle ------------------------------------
apply plugin: 'com.android.application'

android {
compileSdkVersion
29

defaultConfig {
applicationId
"com.app.yourappname"
minSdkVersion 16
targetSdkVersion 29
versionCode 1
versionName "1.0"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

buildTypes {
release {
minifyEnabled
false
proguardFiles getDefaultProguardFile('proguard-android-optimize.txt'), 'proguard-rules.pro'
}
}
}

dependencies {
implementation fileTree(dir:
"libs", include: ["*.jar"])
implementation
'androidx.appcompat:appcompat:1.1.0'
implementation 'androidx.constraintlayout:constraintlayout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'androidx.test.ext:junit:1.1.1'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.2.0'
implementation 'com.google.android.gms:play-services-ads:19.2.0'
}
 ----------------------------------------------------------------

Follow these steps for easy understanding:
1) Initialize the Admob ads.
2) Initialize your custom View by extending View to it
3) Create a RelativeLayout
4) Add your custom View to RelativeLayout
5) Add the admob banner ad to RelativeLayout
6) Set content view to RelativLayout
------------------------------------------ CustomViewActivity.java -------------------------------------
package com.app.yourappname;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.os.Bundle;
import android.view.View;
import android.widget.RelativeLayout;

import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.initialization.InitializationStatus;
import com.google.android.gms.ads.initialization.OnInitializationCompleteListener;

import androidx.annotation.Nullable;

public class CustomViewActivity extends Activity {

@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

MobileAds.initialize(this, new OnInitializationCompleteListener() {
@Override
public void onInitializationComplete(InitializationStatus initializationStatus) {}
});

MySimpleView myView = new MySimpleView(this);

AdView adView = new AdView(this);
adView.setAdSize(AdSize.BANNER);
adView.setAdUnitId("ca-app-pub-3940256099942544/6300978111"); //AD_UNIT_ID is your banner ad id.

AdRequest adRequest = new AdRequest.Builder().build();
adView.loadAd(adRequest);
RelativeLayout rl = new RelativeLayout(this);
RelativeLayout.LayoutParams rl_lp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);

rl_lp.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
rl.addView(myView);
rl.addView(adView, rl_lp);
setContentView(rl);
}

public class MySimpleView extends View {

private Paint paint = new Paint();

public MySimpleView (Context context) {
super(context);
}

@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
paint.setColor(Color.rgb(0,255,0));
canvas.drawRect(new Rect(0,0,canvas.getWidth(), canvas.getHeight()), paint);
}
}
}
--------------------------------------------------------------------------

----Add the internet permissions, app id and declare Activity name with landscape orientation  ------
<?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">

<uses-permission android:name="android.permission.INTERNET"/>
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"
/>

<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">
<meta-data
android:name="com.google.android.gms.ads.APPLICATION_ID"
android:value="ca-app-pub-3940256099942544~3347511713"
/>
<activity android:name=".CustomViewActivity" android:screenOrientation="landscape">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
-------------------------------------------------------------------------- 

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>


ElevenLabs For Audio Models