Saturday, August 17, 2013

ClassCastException in FrameLayout - changing


I'd spent quite a lot of time to find out the root cause of ClasscastException when we change the order of views in layout xml..

And found that if we do "Project --> Clean" in Eclipse the problem will be resolved.

Eclipse - is the culprit :)

Tuesday, July 9, 2013

Admob error in Eclipse? - Android


Eclipse is showing an error in android:configChanges line in my AndroidManifest.xml:

<activity android:name="com.google.ads.AdActivity"
              android:configChanges="keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize"></activity>
and the error is,
error: Error: String types not allowed (at 'configChanges' with value 'keyboard|keyboardHidden|orientation|screenLayout|uiMode|screenSize|smallestScreenSize').

I resolved it by setting target=android-13 in project.properties file.. After changing it don't forget to do Project-> Clean 

If you would like to know how to make use of Admob in your Android app, you can take a look at this tutorial..

http://www.monkeycoder.co.nz/Community/posts.php?topic=1121

APK signed with a certificate that is not yet valid - Android?

While publishing the app I got the below error,

"You uploaded an APK signed with a certificate that is not yet valid. You need to sign your APK with a certificate that is currently valid."

After several Google searches, I resolved this issue by changing the system date of the computer.. I changed my system date to some earlier date, generated a new keystore and uploaded the app.. it worked like a charm :)


Tuesday, July 2, 2013

How to start an activity if the user clicks on OK button in AlertDialog - Android?

Make use of this code to start an activity if the user clicks on OK button in AlertDialog,

AlertDialog.Builder alertDialog = new AlertDialog.Builder(Someclass.this);
    builder.setMessage("Are you sure you want to quit?")    alertDialog.setPositiveButton("OK", new DialogInterface.OnClickListener() {  
    public void onClick(DialogInterface dialog, int which) { 
        Intent myIntent = new Intent(((Dialog) dialog).getContext(), Someclass2.class);        startActivity(myIntent);    
        return;  
        }        
    })
    .setNegativeButton("Cancel", new DialogInterface.OnClickListener(){
        public void onClick(DialogInterface dialog, int which) {
             // Do Nothing
     }

    alertDialog.show();

Saturday, June 29, 2013

How to add common OnClick event for ImageView for every activity? - Android

If your layout contains one common header (with an ImageView) across multiple activities.. you may need to write an OnClick event for the imageView in every single activity..

Instead of writing the same code in every activity you can write a class the extends OnClickListener and the OnClick method like below,

public class ImageViewOnclickListener implements OnClickListener {
  private Context context;
  public ImageViewOnclickListener(Context context) {    this.context = context;
  }

  @Override
  public void onClick(View arg0) {
    Intent intent = new Intent(context, SomeActivity.class);
    context.startActivity(intent);
  }
}

In your activities:
protected void onCreate(...) {
  setContentView(...);
  ((ImageView) findViewById(R.id.mybutton)).setOnClickListener(new ImageViewOnclickListener(this));
}
Hope this helps :)

Draw dotted/dashed line in Android?


You can make use of the below code to draw a dotted or dashed line in Android.
drawable/dotted.xml:
<shape xmlns:android="http://schemas.android.com/apk/res/android"
android:shape="line">
    <stroke
       android:color="#C7B299"
       android:dashWidth="10px"
       android:dashGap="10px" />
</shape>
somefile.xml:
<ImageView
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:src="@drawable/dotted" />

How to specify percentage width for TextView in Linearlayout - Android?

I was able to achieve this with the following XML,

   <LinearLayout
    android:layout_width="match_parent" 
    android:layout_height="wrap_content">

        <TextView
            android:text="left" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".60" /> 

        <TextView
            android:text="right" 
            android:layout_width="0dp" 
            android:layout_height="wrap_content" 
            android:layout_weight=".40" />

    </LinearLayout>
You can replace the TextView with EditText or Button or whatever elements that fit your needs..
Note: Make sure to set the  layout_width to 0dpor your views may not be scaled properly.