Friday 22 March 2013

Android Admob Programmatically.

Hi Friends Today we are going to see about how to add admob in our Android Application.

Ad Mob released Ad Mob SDK v6.0.1 for android a few days ago. 

Download the latest Google Admob Ads SDK from following link

https://developers.google.com/mobile-ads-sdk/download
and paste it in your project libs folder.

Your Manifest file should contain the following permissions

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

And the following activity

<activity android:name="com.google.ads.AdActivity"/>

Your Activity will be look like something below.

import android.app.Activity;
import android.os.Bundle;
import android.widget.LinearLayout;
import android.widget.LinearLayout.LayoutParams;

import com.google.ads.*;

public class AdmobExample extends Activity {
    /** Called when the activity is first created. */  
    private AdView myAdView;
   
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        myAdView = new AdView(this, AdSize.BANNER, "youradmob id. ");
       
        //get layoutView
        LinearLayout rootView = (LinearLayout)this.findViewById(R.id.rootViewGroup);
        LinearLayout.LayoutParams layoutParams = new LayoutParams(480, 75);
       
        rootView.addView(myAdView, 0, layoutParams);       
       
        AdRequest re = new AdRequest();
        re.setGender(AdRequest.Gender.FEMALE);      
       
        myAdView.loadAd(re);
    }
}

Your xml file

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:gravity="center_horizontal"
  
    >
   
   
    <LinearLayout  android:layout_width="fill_parent"
        android:layout_alignParentBottom="true"
         android:id="@+id/rootViewGroup"
        android:layout_height="wrap_content" >
       
       
    </LinearLayout>
   

</RelativeLayout>



Sunday 17 March 2013

Simple Expandable List view

The following code explains you people's how to create the simple expandable list view.

first The Launcher Activity:
1.MyActivity.Java

import android.app.Activity;
import android.app.ExpandableListActivity;
import android.content.Context;
import android.os.Bundle;
import android.widget.ExpandableListView;

import java.util.ArrayList;

public class MyActivity extends Activity {
    private ExpandableListView mExpandableList;

    Context context;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        context=this;
mExpandableList = (ExpandableListView)findViewById(R.id.expandable_list);
 mExpandableList.setGroupIndicator(getResources().getDrawable(R.drawable.expandiconempty));
  ArrayList<Parent> arrayParents = new ArrayList<Parent>();
 ArrayList<String> arrayChildren = new ArrayList<String>();
//here we set the parents and the children
 for (int i = 0; i < 2; i++){
   //for each "i" create a new Parent object to set the title and the children
 Parent parent = new Parent();
 parent.setTitle("Parent " + i);
arrayChildren = new ArrayList<String>();
 for (int j = 0; j < 5; j++) {
arrayChildren.add("Child " + j);
}
parent.setArrayChildren(arrayChildren);
//in this array we add the Parent object. We will use the arrayParents at the setAdapter
 arrayParents.add(parent);
}
//sets the adapter that provides data to the list.
mExpandableList.setAdapter(new MyCustomAdapter(MyActivity.this,arrayParents));
mExpandableList.expandGroup(0);
mExpandableList.expandGroup(1);

    }
}
 
 
The custom Adapter class which binds the parent list-view ad child list-view with the adapters.  

import android.content.Context;
import android.database.DataSetObserver;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseExpandableListAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MyCustomAdapter extends BaseExpandableListAdapter {
    private LayoutInflater inflater;
    private ArrayList<Parent> mParent;
    public MyCustomAdapter(Context context, ArrayList<Parent> parent){
        mParent = parent;
        inflater = LayoutInflater.from(context);
    }
    @Override
    //counts the number of group/parent items so the list knows how many times calls getGroupView() method
    public int getGroupCount() {
        return mParent.size();
    }

    @Override
    //counts the number of children items so the list knows how many times calls getChildView() method
    public int getChildrenCount(int i) {
        return mParent.get(i).getArrayChildren().size();
    }

    @Override
    //gets the title of each parent/group
    public Object getGroup(int i) {
        return mParent.get(i).getTitle();
    }
    @Override
    //gets the name of each item
    public Object getChild(int i, int i1) {
        return mParent.get(i).getArrayChildren().get(i1);
    }
    @Override
    public long getGroupId(int i) {
        return i;
    }
    @Override
    public long getChildId(int i, int i1) {
        return i1;
    }
    @Override
    public boolean hasStableIds() {
        return true;
    }
    @Override
    //in this method you must set the text to see the parent/group on the list
    public View getGroupView(int i, boolean b, View view, ViewGroup viewGroup) {

        if (view == null)
  {
            view = inflater.inflate(R.layout.parent, viewGroup,false);
        }


        TextView textView = (TextView) view.findViewById(R.id.list_item_text_view);
        //"i" is the position of the parent/group in the list
        textView.setText(getGroup(i).toString());

        //return the entire view
        return view;
    }

    @Override
    //in this method you must set the text to see the children on the list
    public View getChildView(int i, int i1, boolean b, View view, ViewGroup viewGroup) {
        if (view == null) {
            view = inflater.inflate(R.layout.child, viewGroup,false);
        }

        TextView textView = (TextView) view.findViewById(R.id.list_item_text_child);
        //"i" is the position of the parent/group in the list and 
        //"i1" is the position of the child
        textView.setText(mParent.get(i).getArrayChildren().get(i1));

        //return the entire view
        return view;
    }

    @Override
    public boolean isChildSelectable(int i, int i1) {
        return true;
    }

    @Override
    public void registerDataSetObserver(DataSetObserver observer) {
        /* used to make the notifyDataSetChanged() method work */
        super.registerDataSetObserver(observer);
    }
}
 
Parent Class with  setter and getter methods. 

import java.util.ArrayList;

public class Parent {
    private String mTitle;
    private ArrayList<String> mArrayChildren;

    public String getTitle() {
        return mTitle;
    }

    public void setTitle(String mTitle) {
        this.mTitle = mTitle;
    }

    public ArrayList<String> getArrayChildren() {
        return mArrayChildren;
    }

    public void setArrayChildren(ArrayList<String> mArrayChildren) {
        this.mArrayChildren = mArrayChildren;
    }
}
 
main design file .
 
1.main.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"
        >
    <ExpandableListView android:id="@+id/expandable_list"
                        android:layout_width="fill_parent"
                        android:layout_height="fill_parent"
                        android:transcriptMode="alwaysScroll"
                        android:cacheColorHint="#00000000"

                        android:listSelector="@android:color/transparent"/>
</LinearLayout>
 
2. child.xml will be look like this . 
 
<?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:id="@+id/list_item_child"
        android:gravity="center_vertical">


    <TextView android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:id="@+id/list_item_text_child"
              android:textSize="20sp"
              android:padding="10dp"
              android:layout_marginLeft="5dp"/>


</LinearLayout>  
 
3. Parent.xml will be like this .
 
<?xml version="1.0" encoding="utf-8"?>

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="@+id/list_item">

<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="@+id/list_item_text_view"
android:textSize="20sp"
android:padding="10dp"
android:layout_weight="1"
android:layout_marginLeft="35dp" />

<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="button"
android:focusable="false"
android:layout_gravity="right"
android:focusableInTouchMode="false"
android:id="@+id/button"/>

</LinearLayout>
 
 
one more thing both  child and parent .xml files will be your own custom files. 
so you can design with you . 
 happy coding....