Wednesday 23 July 2014

Convert HashSet to Arraylist in Android

Set<String> set = new HashSet<String>();
List<String> sample=new ArrayList<String>(set);

Convert Arraylist to HashSet Android

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist name);

Store and Retrieve Array list using shared preferences in Android

Store Arraylist Using  Shared Preferences
------------------------------------------------------

SharedPreferences prefs=this.getSharedPreferences("yourPrefsKey",Context.MODE_PRIVATE);
Editor edit=prefs.edit();

Set<String> set = new HashSet<String>();
set.addAll(your Arraylist Name);
edit.putStringSet("yourKey", set);
edit.commit();

Retrieve Arraylist from Shared Preferences
-----------------------------------------------------

Set<String> set = prefs.getStringSet("yourKey", null);
List<String> sample=new ArrayList<String>(set);

Tuesday 22 July 2014

Sorting ArrayList in Android

Sorting Ascenting
----------------------------

Collections.sort(arraylist Name);


Sorting Descending
----------------------------------

Collections.sort(arraylist Name,Collections.reverseOrder());

Thursday 17 July 2014

android.app.RemoteServiceException

in android.app.ActivityThread$H.handleMessage
[10:59:21 AM] Manu Mohan: android.app.RemoteServiceException: Bad notification posted from package *************: Couldn't create icon: StatusBarIcon(pkg=**********user=0 id=0x7f02007b level=0 visible=true num=0 )
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1531)
at android.os.Handler.dispatchMessage(Handler.java:107)
at android.os.Looper.loop(Looper.java:194)
at android.app.ActivityThread.main(ActivityThread.java:5371)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:525)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:833)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:600)
at dalvik.system.NativeStart.main(Native Method)







Make sure the image or drawables specified in R.java with the given id is in all drawable folders.

find a random number between particular digits in Android

   Random r=new Random();
       
  int s=r.nextInt(endnumber)+startnumber;

Android Rotate Animation

Create a 'anim' folder under res folder
Create a rotate.xml file under anim folder

add below code in rotate.xml
-------------------------------------------------------


<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <rotate android:fromDegrees="0"
        android:toDegrees="180"
        android:pivotX="50%"
        android:pivotY="50%"
        android:duration="600"
     
        android:interpolator="@android:anim/cycle_interpolator"/>

</set>


open your main.xml file and add an ImageView in it
-----------------------------------------------------------

 <ImageView 
                        android:id="@+id/swapImage"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"
                        android:background="@drawable/ic_launcher"/>



open your MainActivity.java file
---------------------------------------------------

public class MainActivity extends Activity implements AnimationListener{

Animation animRotate;
        ImageView swapImage;
        public void onCreate(Bundle savedInstanceState)
 {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);

swapImage=(ImageView)findViewById(R.id.swapImage);

  animRotate = AnimationUtils.loadAnimation(getApplicationContext(),
               R.anim.rotate);
     
     
       animRotate.setAnimationListener(this);

 swapImage.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// TODO Auto-generated method stub
swapImage.startAnimation(animRotate);

}
});


}


@Override
public void onAnimationEnd(Animation animation) {
// TODO Auto-generated method stub

}






@Override
public void onAnimationRepeat(Animation animation) {
// TODO Auto-generated method stub

}






@Override
public void onAnimationStart(Animation animation) {
// TODO Auto-generated method stub

}





}


Wednesday 16 July 2014

Text change listner on EditText

your xml file contains EditText as
--------------------------------------------------


  <EditText
                        android:id="@+id/fromrate"
                        android:layout_width="wrap_content"
                        android:layout_height="wrap_content"/>



change your java page
---------------------------------------------

EditText txt_from=(EditText)findViewById(R.id.fromrate);

 txt_from.addTextChangedListener(new TextWatcher() {

@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
// TODO Auto-generated method stub

Toast.makeText(getApplicationContext(), "called when a text added or deleted",200).show();

}

@Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub

}

@Override
public void afterTextChanged(Editable s) {
// TODO Auto-generated method stub
Toast.makeText(getApplicationContext(), "called After  a text added or deleted",200).show();
}
});

Admob using google play service library

import google play service lib from  folder where your android sdk placed-> extras-> google -> google play services -> lib project ->google play services lib

add below code in your .java file
--------------------------------------------

  AdRequest request = new AdRequest.Builder()
   .addTestDevice(AdRequest.DEVICE_ID_EMULATOR)
   .build();


AdView adView = new AdView(this);
adView.setAdUnitId("your add id");
adView.setAdSize(AdSize.BANNER);

adView.loadAd(request);

LinearLayout lay=(LinearLayout)findViewById(R.id.adView);
lay.addView(adView);



add below code in your xml file
-----------------------------------------------

 <LinearLayout
        android:id="@+id/adView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_gravity="top|left"
        android:orientation="vertical">
           
         </LinearLayout>

Proguard Changes while using google play service library

add the below code in your proguard-project.txt file

-keep class com.google.android.gms.** { *; }
-dontwarn com.google.android.gms.**

Proguard changes for using Admob SDK

add         -dontwarn com.google.ads.**  in your proguard-project.txt  folder

Beginig Android Application Developement by developing simple calculator


File-> New-> Android Application Project

Application Name: Calculator

Project Name: Calculator

Package name: com.calculator.scientific

Minimum Required SDK:  API 8
Target SDK: API 15

click next and then click finish;

open MainActivity.java from src->comcom.calculator.scientific from your project and replace with following code.
//--------------------------------------------------------------
package com.calculator.scientific

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

public class MainActivity extends Activity implements OnClickListener {

final int MENU_RESET_ID = 1;
final int MENU_QUIT_ID = 2;



  EditText etNum1;
  EditText etNum2;

  Button btnAdd;
  Button btnSub;
  Button btnMult;
  Button btnDiv;

  TextView tvResult;

  String oper = "";

  /** Called when the activity is first created. */
  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // find the elements
    etNum1 = (EditText) findViewById(R.id.etNum1);
    etNum2 = (EditText) findViewById(R.id.etNum2);

    btnAdd = (Button) findViewById(R.id.btnAdd);
    btnSub = (Button) findViewById(R.id.btnSub);
    btnMult = (Button) findViewById(R.id.btnMult);
    btnDiv = (Button) findViewById(R.id.btnDiv);

    tvResult = (TextView) findViewById(R.id.tvResult);

    // set a listener
    btnAdd.setOnClickListener(this);
    btnSub.setOnClickListener(this);
    btnMult.setOnClickListener(this);
    btnDiv.setOnClickListener(this);

  }

  @Override
  public void onClick(View v) {
    // TODO Auto-generated method stub
    float num1 = 0;
    float num2 = 0;
    float result = 0;

    // check if the fields are empty
    if (TextUtils.isEmpty(etNum1.getText().toString())
        || TextUtils.isEmpty(etNum2.getText().toString())) {
      return;
    }

    // read EditText and fill variables with numbers
    num1 = Float.parseFloat(etNum1.getText().toString());
    num2 = Float.parseFloat(etNum2.getText().toString());

    // defines the button that has been clicked and performs the corresponding operation
    // write operation into oper, we will use it later for output
    switch (v.getId()) {
    case R.id.btnAdd:
      oper = "+";
      result = num1 + num2;
      break;
    case R.id.btnSub:
      oper = "-";
      result = num1 - num2;
      break;
    case R.id.btnMult:
      oper = "*";
      result = num1 * num2;
      break;
    case R.id.btnDiv:
      oper = "/";
      result = num1 / num2;
      break;
    default:
      break;
    }

    // form the output line
    tvResult.setText(num1 + " " + oper + " " + num2 + " = " + result);
  }

  @Override
  public boolean onCreateOptionsMenu(Menu menu) {
    // TODO Auto-generated method stub
    menu.add(0, MENU_RESET_ID, 0, "Reset");
    menu.add(0, MENU_QUIT_ID, 0, "Quit");
    return super.onCreateOptionsMenu(menu);
  }

  // process menu item clicks
  @Override
  public boolean onOptionsItemSelected(MenuItem item) {
    // TODO Auto-generated method stub
    switch (item.getItemId()) {
    case MENU_RESET_ID:
      // clear the fields
      etNum1.setText("");
      etNum2.setText("");
      tvResult.setText("");
      break;
    case MENU_QUIT_ID:
      // exit the application
      finish();
      break;
    }
    return super.onOptionsItemSelected(item);
  }

}

//--------------------------------------------------------------------------

Open activity_main.xml  from res->layout from your project and paste the following code

//----------------------------------------------------------


<?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:orientation="vertical" >

    <LinearLayout

        android:id="@+id/linearLayout1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="10pt"
        android:layout_marginRight="10pt"
        android:layout_marginTop="3pt" >

        <EditText

            android:id="@+id/etNum1"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginRight="5pt"
            android:layout_weight="1"
            android:inputType="numberDecimal" >
        </EditText>

        <EditText

            android:id="@+id/etNum2"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="5pt"
            android:layout_weight="1"
            android:inputType="numberDecimal" >
        </EditText>
    </LinearLayout>

    <LinearLayout

        android:id="@+id/linearLayout2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:layout_marginTop="3pt" >

        <Button

            android:id="@+id/btnAdd"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="+"
            android:textSize="8pt" >
        </Button>

        <Button

            android:id="@+id/btnSub"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="-"
            android:textSize="8pt" >
        </Button>

        <Button

            android:id="@+id/btnMult"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="*"
            android:textSize="8pt" >
        </Button>

        <Button

            android:id="@+id/btnDiv"
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_weight="1"
            android:text="/"
            android:textSize="8pt" >
        </Button>
    </LinearLayout>

    <TextView

        android:id="@+id/tvResult"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_marginLeft="5pt"
        android:layout_marginRight="5pt"
        android:layout_marginTop="3pt"
        android:gravity="center_horizontal"
        android:textSize="12pt" >
    </TextView>

</LinearLayout>






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

Run The Application by Right Click on the project and RunAs-> Android Application