Tuesday, January 7, 2014

How to set image button click effect ?

I have one simple solution using that you can set default press effect to button.

// Create button and add event
Button addImageView = (Button) bottomView.findViewById(R.id.addImage);
addImageView.setOnClickListener(new OnClickListener() {

@Override
public void onClick(View v) {
// click event
}
});


//Using this code set touch listener

addImageView.setOnTouchListener(touchEffect);

//touch listner
OnTouchListener touchEffect = new OnTouchListener() {

        public boolean onTouch(View v, MotionEvent event) {
            switch (event.getAction()) {
                case MotionEvent.ACTION_DOWN: {
                    v.getBackground().setColorFilter(0xe0f47521,PorterDuff.Mode.SRC_ATOP);
                    v.invalidate();
                    break;
                }
                case MotionEvent.ACTION_UP: {
                    v.getBackground().clearColorFilter();
                    v.invalidate();
                    break;
                }
            }
            return false;
        }

    };

Thursday, January 31, 2013

Date compare in java android


private boolean checkDateLimit() {

long CurrentDateInMilisecond = System.currentTimeMillis(); // Date 1
long Date1InMilisecond = Date1.getTimeInMillis(); //Date2

if (CurrentDateInMilisecond <= Date1InMilisecond) {
return true;
} else {
return false;
}

}


/*
Date1 and Date2 convert into milisecond check its value.

/*

Thursday, November 1, 2012

set Title to Activity

In create method just set param to setTitle method.
public class MyActivity extends Activity {
    @Override
    public void onCreate(Bundle savedInstanceState) {

        setTitle("MyActivity");
       

    }
}

Thursday, October 25, 2012

Tuesday, October 9, 2012

Date picker android





Button toDate = (Button) findViewById(R.id.doc_dateto);
toDate.setOnClickListener(new OnClickListener() {

private int month;
private int year;
private int day;

@Override
public void onClick(View v) {

OnDateSetListener datePickerListener = null;

datePickerListener = new DatePickerDialog.OnDateSetListener() {



public void onDateSet(DatePicker view,
int selectedYear, int selectedMonth,
int selectedDay) {
year = selectedYear;
month = selectedMonth;
day = selectedDay;

Calendar c = Calendar.getInstance();
c.set(year, month, day);
toDate.setText(c.getTime().toGMTString());
}
};


final Calendar c = Calendar.getInstance();
year = c.get(Calendar.YEAR);
month = c.get(Calendar.MONTH);
day = c.get(Calendar.DAY_OF_MONTH);
DatePickerDialog datePicker = new DatePickerDialog(
SUP101SampleActivity.this, datePickerListener, year,
month, day);
datePicker.show();

}
});

Friday, August 17, 2012

Simple and sort Listview example (Android)


private View getChapterLayout() {
ListView listView = new ListView(this);
listView.setAdapter(new BaseAdapter() {

public int getCount() {
return chapterdata.size();
}

public Object getItem(int position) {
return chapterdata.get(position);
}

public long getItemId(int addressList) {
return 0;
}

public View getView(int position, View convertView,
ViewGroup parent) {

inflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.button_chapter, null);

TextView textview = (TextView) convertView
.findViewById(R.id.button);
textview.setText(chapterdata.get(position).getName());
return convertView;
}



});
return listView;
}


// chapterdata is an arrayList


// Add Event to each Row



 listView.setOnItemClickListener(new OnItemClickListener() {

public void onItemClick(AdapterView<?> parent, View view,
final int position, long id) {
try {
runOnUiThread(new Runnable() {

@Override
public void run() {

}
});
} catch (Exception e) {
// TODO: handle exception
}



}
});




Tuesday, August 14, 2012

Listview display black while scrolling.

Simply add following tag in listview's xml file.


    <ListView
        android:id="@+id/list"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
      android:cacheColorHint="#00000000"
       >
    </ListView>

Monday, July 30, 2012

File path to Uri convert android or Uri to file

Convert file path to Uri


File externalFile = new File(Environment.getExternalStorageDirectory(),"image.png");
Uri external = Uri.fromFile(externalFile);




Uri to file convert.


File f = new File(uri.toString());

Wednesday, July 11, 2012

How to set costume fonts in android


1. create assets/fonts/ and put your TTF files in there:
2.set view's properties.
 TextView tv=(TextView)findViewById(R.id.custom); 
  Typeface face=Typeface.createFromAsset(getAssets(), "fonts/ARIAL.ttf"); 
  tv.setTypeface(face); 

Tuesday, July 10, 2012

Set Dynamic Image Resource to android Image button android.




// 1.Create button
// 2 .Assign background
ImageView button = (ImageView) bottomView.findViewById(R.id.button);
button.setId(i);
button.setBackgroundResource(getResources().getIdentifier(
"image1", "drawable", getPackageName()));

Thursday, June 7, 2012

Google Map Api key for Android, JDK6 and JDK 7. and Android Invalid Map API Key

How to get Google Api key?

1.Getting the MD5 Fingerprint of the SDK Debug Certificate
 open commnad prompt.
Using cd command reach java bin folder ex. cd C:\Program Files\Java\jdk1.7.0_04\bin>

2. Type following command.
For windows 
JDK 1.7
keytool -v -list -alias androiddebugkey -keystore "C:\Users\virens\.android\debug.keystore" -storepass android -keypass android

jdk 1.6

keytool  -list -alias androiddebugkey -keystore "C:\Users\virens\.android\debug.keystore" -storepass android -keypass android
other platform

  • Windows Vista: C:\Users\<user>\.android\debug.keystore
  • Windows XP: C:\Documents and Settings\<user>\.android\debug.keystore
  • OS X and Linux: ~/.android/debug.keystore


3.Open below url and create account.Paste that md5 signature. 






Note : 

Android Invalid Map API Key


I'm willing to bet that you have JDK 7. JDK 7 seems to be returning the SHA1 finger print. If you want thMD5 has, throw a -v in there.

Tuesday, April 10, 2012

Get cursor position of Edittext android or How to insert text at cursor position.


You can get the Cursor position using the getSelectionStart() and getSelectionEnd() methods. If no text is highlighted, both getSelectionStart() and getSelectionEnd() return the position of the cursor. myEditText.getSelectionStart();
or
myEditText.getSelectionEnd();

How to insert text at cursor position.
int curPos = editText.getSelectionStart();
String str = editText.getText().toString();
String str1 = (String) str.substring(0, curPos);
String str2 = (String) str.substring(curPos);
editText.setText(str1+"<br/>"+ list1[which] +str2);

Thursday, March 29, 2012

How to increasing heap size of Eclipse Windows & Mac

Windows


Go eclipse installed package using file explorer,
Open Eclipse.ini file.
Modify following detail.

Minsize:  -Xms24m
Maxsize: -Xmx125m
increase min & max size according to your pc configuration.

Mac OS
Go Right click on eclipse icon -> seletct show package contains -> package using file explorer,
Open contains/macOS/Eclipse.ini file.
Modify following detail.
Minsize:  -Xms24m
Maxsize: -Xmx125m
-startup
../../../plugins/org.eclipse.equinox.launcher_1.0.200.v20090520.jar
--launcher.library
../../../plugins/org.eclipse.equinox.launcher.cocoa.macosx_1.0.0.v20090519
-product
org.eclipse.epp.package.java.product
-showsplash
org.eclipse.platform
--launcher.XXMaxPermSize
256m
-vmargs
-Dosgi.requiredJavaVersion=1.5
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts
-XX:MaxPermSize=256m
-Xms21m  //change min size
-Xmx64m  // change max size
-Xdock:icon=../Resources/Eclipse.icns
-XstartOnFirstThread
-Dorg.eclipse.swt.internal.carbon.smallFonts


Friday, February 24, 2012

How to Create animated drawable.

This is simple example which rotate frame by frame , just you need to create each rotate image ans play one by one.
http://developer.android.com/guide/topics/graphics/drawable-animation.html


1.Create animated drawable.
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
     android:oneshot="true">
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
 </animation-list>


2.
AnimationDrawable rocketAnimation;
public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 
  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
   rocketImage.setBackgroundResource(R.drawable.rocket_thrust);
   rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
 }
public boolean onTouchEvent(MotionEvent event) {
   if (event.getAction() == MotionEvent.ACTION_DOWN) {
     rocketAnimation.start();
    return true;
   }
  return super.onTouchEvent(event);
 }

Wednesday, February 15, 2012

Store array of String or int or list in SharedPreferences android



 SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
        JSONArray arr = new JSONArray();
        arr.put("Viren");
        arr.put("Android");
        prefs.edit().putString("key", arr.toString());
        prefs.edit().commit();
        try {
            arr = new JSONArray(prefs.getString("key", "{}"));
            System.out.println(arr.getString(1)); 
        } catch (JSONException e) {
            e.printStackTrace();
        }
content -->