Skip to main content

Posts

Showing posts from June, 2014

Rotate Animation to ImageView

Rotate the image at center axis, by using Rotate Animation.

RotateAnimation anim = new RotateAnimation(0.0f, 360.0f, Animation.RELATIVE_TO_SELF, 0.5f,Animation.RELATIVE_TO_SELF, 0.5f);
        anim.setInterpolator(new LinearInterpolator());
        anim.setRepeatCount(Animation.INFINITE);
        anim.setDuration(1500);

and set into image like this

imageView.startAnimation(anim);

Change the font of ActionBar title

Change the font of ActionBar title using SpannableString.

Just follow the code.

SpannableString s = new SpannableString("It's a new font");
s.setSpan(new TypefaceSpan(this, "arial.ttf"), 0, s.length(),
       Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
getActionBar().setTitle(s);

Back Navigation in action bar

Sometimes most of developers now using ActionBar. They need to navigate button on top, if they need just add few lines in the code.

getActionBar().setDisplayHomeAsUpEnabled(true);

and

public boolean onOptionsItemSelected(MenuItem item) {
// Take appropriate action for each action item click
switch (item.getItemId()) {
case android.R.id.home:
onBackPressed();
return true;
default:
return super.onOptionsItemSelected(item);
}
}


Using Color HEX codes in android

This is transparent level codes of hex codes.

100% — FF
95% — F2
90% — E6
85% — D9
80% — CC
75% — BF
70% — B3
65% — A6
60% — 99
55% — 8C
50% — 80
45% — 73
40% — 66
35% — 59
30% — 4D
25% — 40
20% — 33
15% — 26
10% — 1A
5% — 0D
0% — 00

If you provide 6 hex digits, that means RGB (2 hex digits for each value of red, green and blue).

If you provide 8 hex digits, it's an ARGB (2 hex digits for each value of alpha, red, green and blue respectively).

So actually you're changing from R=B4, G=55, B=55 to A=B5, R=55, G=55, B=55 which is why it's a mostly-transparent grey colour.

http://en.wikipedia.org/wiki/ARGB
http://developer.android.com/guide/topics/resources/more-resources.html#Color

if you want to some transparent level of color using in layout or widgets, just use like that
android:background:"#80FFFFFF"

Current Weather Demo

I'm so happy today, because today is my first web based application released in google play. previously i'm developed the some web based applications, but that's not in google play or any public markets. all are third party usage applications. Now it's time to move some small steps.

Current Weather Demo

Get the Current Weather Status by City ID and City Name

Before that, just signup and get the APPID from

http://openweathermap.org/register

Get the City ID and City name information from

http://openweathermap.org/help/city_list.txt

This is just a demo application, soon release the full functionality app.

You can download the source from Github

Download the App from Google Play



Check the connectivity status in android

Many of web based applications, check the internet connections, before done the web services.

You can set the permission for network state, like this

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

Code:

public boolean networkAvailability() {
ConnectivityManager connectivity = (ConnectivityManager) context
.getSystemService(Context.CONNECTIVITY_SERVICE);
if (connectivity != null) {
NetworkInfo[] info = connectivity.getAllNetworkInfo();
if (info != null)
for (int i = 0; i < info.length; i++)
if (info[i].getState() == NetworkInfo.State.CONNECTED) {
return true;
}

}
return false;
}


Convert the Kelvin into celsius

celsius= kelvin-273.15;

subtract 273.15 from kelvin value, you got the celsius value.

public String kevinToCelsius(String kelvin) {
double kel, celsius;
kel = Double.parseDouble(kelvin);
celsius = kel - 273.15;
return String.valueOf(celsius).substring(0, 4);
}

Change the Unix timestamp into date format

It's very interesting. Sometimes you got the result as unix timestamp from any backend apis or some results. It's simple method to convert the unix timestamp into date format.

// Unix timestamp into time format
public String getDateFormat(String dateformat) {
long dt = Long.parseLong(dateformat);
Date date = new Date(dt * 1000L); // *1000 is to convert seconds to
// milliseconds
SimpleDateFormat sdf = new SimpleDateFormat("yyyy/MM/dd HH:mm"); // the
sdf.setTimeZone(TimeZone.getTimeZone("GMT-4"));
String formattedDate = sdf.format(date);
return formattedDate;
}

Slide Navigation

Slide Navigation

Most of new android applications uses the slide navigation. you can do left to right and right to left slide navigations. Like an gmail or facebook app.


I develop the slide navigation app demo.
Click Here to download from Google Play
Also you can get the source from GitHub
App and Source include the Colored ListView, Numbers to Words, Coloring textview examples..

Change the TextView color Continuosly using UIThread

Have a nice day. Now i write the code for changing the textview continuosly using UIThread.

Just try out.

handler = new Handler();
new Thread(new Runnable() {

@Override
public void run() {
final int totalCount=1000;
// TODO Auto-generated method stub
for(int i=0;i<totalCount;i++){
try {
runOnUiThread(new Runnable() {
                       public void run() {                        
Random r = new Random();
colorText.setTextColor(Color.rgb(r.nextInt(256),
r.nextInt(256), r.nextInt(256)));                          
                       }                       
                   });

Thread.sleep(totalCount);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}

}).start();


Textview is continuously changing the textcolor. I'm set the 1000 milliseconds (1 Second).

Set other font to textview

To set the other fonts into textview, using Typeface.

Copy the fonts into assets folder,

Typeface type = Typeface.createFromAsset(getAssets(),"fonts/consolas.ttf");

textInfo.setTypeface(type);


Copy the Database into SDCARD

To copy the Database into SDCARD, you can use following code to do that,

InputStream myInput = new FileInputStream("/data/data/pack_name/databases/databasename.db");

File directory = new File("/sdcard/some_folder");
if (!directory.exists()) {
    directory.mkdirs();
}

OutputStream myOutput = new FileOutputStream(directory.getPath() + "/AppName.backup");

byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer)) > 0) {
    myOutput.write(buffer, 0, length);
}

myOutput.flush();
myOutput.close();
myInput.close();

Creating the Database in SDCARD

public class MyDataBase extends SQLiteOpenHelper {

    MyDataBase (final Context context) {        super(context, Environment.getExternalStorageDirectory()
                + File.separator + "/DataBase/" + File.separator
                + DATABASE_NAME, null, DATABASE_VERSION);
    }

Set the permission for READ and WRITE ACCESS.
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>

Creating or Formatting the JSON.

JSON - JavaScript Object Notation is lightweight interchange format. JSON is more comfortable than XML. Also fast than the parsing. Most of web services is achieved through this JSON.

Some peoples are struggling to create and formatting the JSON strings and arrays. Here i give the links, its very useful for formatting and creating JSON. Just try it and give your feedback.

http://jsonviewer.stack.hu/ --> My Best Option.

http://jsonformat.com/

http://www.jsoneditoronline.org/

http://jsonformatter.curiousconcept.com/