Tuesday, July 9, 2013

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.

Thursday, June 27, 2013

How to stop EditText from gaining focus at Activity startup? - Android


I have an activity with EditText at the top and a ListView under it.. EditText takes focus as soon as the Activity starts.. and also the soft keyboard comes up..

To fix this problem, add the following code in your enclosing layout of EditText, ListView,

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  android:id="@+id/mainLayout"
  android:descendantFocusability="beforeDescendants"
  android:focusableInTouchMode="true" >
and also remove

 <requestFocus />
from EditText.

How to use custom fonts in Android XML file?


Add the .ttf file in your assets/fonts folder. To use it in TextView the best way is to extend the TextView and have to set the font.  Here is the code of the extended TextView,

package naveendroid.fontstest;

import android.content.Context;
import android.graphics.Typeface;
import android.util.AttributeSet;
import android.widget.TextView;

public class RobotTextView extends TextView {
    public RobotTextView(Context context, AttributeSet attrs, int defStyle) {        super(context, attrs, defStyle);
        init();
    }

    public RobotTextView(Context context, AttributeSet attrs) {        super(context, attrs);
        init();
    }

    public RobotTextView(Context context) {        super(context);
        init();
    }

    private void init() {
        Typeface tf = Typeface.createFromAsset(getContext().getAssets(),
                                               "Roboto-Regular.ttf");
        setTypeface(tf);
    }

}

Then use this in your .xml file as shown below,

<naveendroid.fontstest.MyTextView
    android:id="@+id/sometext"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center"
    android:layout_weight="1"
    android:text="TextView with the font that you had set in RobotTextView class "
    android:textSize="20dip"
    android:textColor="#000"
   >