Wednesday, 16 September 2015

How to change Action Bar color in Android


     

     <resources>

    <style name="MyTheme" parent="@android:style/Theme.Holo.Light.DarkActionBar">
        <item name="android:actionBarStyle">@style/MyActionBar</item>
        <item name="android:actionMenuTextColor">@color/textColor</item>
    </style>

    <style name="MyActionBar" parent="@android:style/Widget.Holo.Light.ActionBar">
        <item name="android:titleTextStyle">@style/TitleBarTextColor</item>
        <item name="android:background">@color/actionBar</item>
    </style>

    <style name="TitleBarTextColor"   parent="@style/TextAppearance.AppCompat.Widget.ActionBar.Title">
        <item name="android:actionBarSize">120dp</item>
        <item name="actionBarSize">120dp</item>
        <item name="android:textColor">@color/textColor</item>
    </style>

</resources>



And also mention the theme name in your Manifest file

   android:theme="@style/MyTheme" >


Happy Coding:)

Tuesday, 15 September 2015

Ordered JSON String in Android


JSON
   
             JSON   object is an unordered set of name/value pairs.So the Android changes the order of JSON ,To keep the values order use the below code


Map obj=new LinkedHashMap();

   obj.put("name","Dhinakaran");

   obj.put("Blog","AndroidDhina");

   obj.put("Number","19");

   obj.put("Job","S/W");

   StringWriter out = new StringWriter();

   JSONValue.writeJSONString(obj, out);

   String jsonText = out.toString();


And Download "json simple 1.1 jar" from this link

And add the jar file to your lib folder

Happy Coding:)

Friday, 11 September 2015

How to Download a PDF from URL in Android



          This Tutorial teach you  how to download a PDF file from URL

 private class LongOperation extends AsyncTask<String, Void, String> {

String Filepath;

@Override
protected String doInBackground(String... params) 
    { 
    try {
String extStorageDirectory=Environment.getExternalStorageDirectory().toString();
File folder = new File(extStorageDirectory, "IFIN-PDF");
folder.mkdir();
Filepath = "ISFL-" + new Date().getDate()+newDate().getMonth()+
                                        new Date().getYear()+".pdf";
   File file = new File(folder, Filepath);
   try {
  file.createNewFile();

    catch (IOException e1) 
   {
e1.printStackTrace();
}
     Downloader.DownloadFile
  ("http://xxxxxxxxxxxxx/pdffile.pdf", file);//Paste your url here
    }
    catch (Exception e) {}
    return Filepath;
   }

@Override
protected void onPostExecute(String result) {

ProgressClass.progressClose();

File file = new File(Environment.getExternalStorageDirectory()
+ "/IFIN-PDF/" + result);

PackageManager packageManager = getPackageManager();
Intent pdfIntent = new Intent(Intent.ACTION_VIEW);
pdfIntent .setType("application/pdf");
List list = packageManager.queryIntentActivities(pdfIntent ,
PackageManager.MATCH_DEFAULT_ONLY);
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
Uri uri = Uri.fromFile(file);
intent.setDataAndType(uri, "application/pdf");
startActivity(intent);

}

@Override
protected void onPreExecute() {

ProgressClass.progressShow(downloadPDF.this, "Connecting");
}

}

public static class Downloader {

public static void DownloadFile(String fileURL, File directory) {
try {
   FileOutputStream file = new FileOutputStream(directory);
  URL url = new URL(fileURL);
HttpURLConnection connection = (HttpURLConnection) url .openConnection();
  connection .setRequestMethod("GET");
connection .setDoOutput(true);
connection .connect();
   InputStream input connection .getInputStream();
   byte[] buffer = new byte[1024];
   int len = 0;
 while ((len input .read(buffer)) > 0) {
file .write(buffer, 0, len );
}
file .close();
} catch (Exception e) {
e.printStackTrace();
}

}
}

Happy Coding:)


Thursday, 10 September 2015

Add Currency Symbol in Android Textview


       Here I am going to explain how to add a currency symbol in Android textview
                 


   Step 1)strings.xml


  <?xml version="1.0" encoding="utf-8"?>
  <resources>

    <string name="app_name">Currency</string>
    <string name="us">\u0024</string>
    <string name="Rs">\u20B9</string>

 </resources>


 Step 2)activity_main.xml

  <RelativeLayout                                                             xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <TextView
        android:id="@+id/textview"
        android:layout_width="wrap_content"
        android:textColor="#e6443c"
        android:textSize="wrap_content
        android:layout_height="100dp"
        android:layout_centerInParent="true" />

    <TextView
        android:id="@+id/textview1"
        android:layout_width="wrap_content"
        android:textColor="#e6443c"
        android:layout_height="wrap_content"
        android:textSize="25dp
        android:layout_below="@+id/textview"
        android:layout_centerInParent="true" />

</RelativeLayout>

 Step 3)MainActivity.java


 import android.app.Activity;
 import android.os.Bundle;
 import android.widget.TextView;

  public class MainActivity extends Activity {

TextView t, t1;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
t = (TextView) findViewById(R.id.textview);
t1 = (TextView) findViewById(R.id.textview1);

String rupee = getResources().getString(R.string.Rs);
String dollar = getResources().getString(R.string.us);
t.setText(rupee +   "   05   " + "AndroidDhina");
t1.setText(dollar + "   19   " + "AndroidDhina");
}

}

Happy Coding:)



Wednesday, 9 September 2015

How to add an image inside a button in android


   Here I am going to explain how to add an image inside a button


                          

Step 1) drawable-home_button.xml


  
  <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270"
        android:endColor="#E8f2fe"
        android:startColor="E8f2fe" />

    <corners android:radius="3dp" />

    <stroke
        android:width="2px"
        android:color="#486e9d" />

</shape>

Step 2) .Add drawable to button


      <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:background="@drawable/home_button"
                android:drawableLeft="@android:drawable/ic_menu_edit"
                android:drawablePadding="6dp"
                android:gravity="left|center"
                android:height="60dp"
                android:padding="6dp"
                android:text="AndroidDhina"
                android:textColor="#000"
                android:textStyle="bold" />

Happy Coding :)

Border Color on Android Button


                    Here I am going to explain how to draw a border for android button


                          

Step 1) drawable-home_button.xml

  
  <?xml version="1.0" encoding="utf-8"?>
 <shape xmlns:android="http://schemas.android.com/apk/res/android"
    android:shape="rectangle" >

    <gradient
        android:angle="270"
        android:endColor="#E8f2fe"
        android:startColor="E8f2fe" />

    <corners android:radius="3dp" />

    <stroke
        android:width="2px"
        android:color="#486e9d" />

</shape>

Step 2) Add drawable to button


      <Button
                android:layout_width="0dp"
                android:layout_weight="1"
                android:background="@drawable/home_button"
                android:drawableLeft="@android:drawable/ic_menu_edit"
                android:drawablePadding="6dp"
                android:gravity="left|center"
                android:height="60dp"
                android:padding="6dp"
                android:text="AndroidDhina"
                android:textColor="#000"
                android:textStyle="bold" />


Thursday, 3 September 2015

Android Preferences Activity Example

Applications often include settings that allow users to modify app features and behaviors. For example, some apps allow users to specify whether notifications are enabled or specify how often the application syncs data with the cloud.
If you want to provide settings for your app, you should use Android'sPreference APIs to build an interface that's consistent with the user experience in other Android apps (including the system settings). Here I am going to  explain how to build your app settings using Preference APIs.

Step.1)MainActivity.java
package com.preference;
import android.os.Bundle;
import android.preference.PreferenceActivity;
public class MainActivity extends PreferenceActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
addPreferencesFromResource(R.layout.preferences);
}
}
Step.2)preferences.xml
<?xml version="1.0" encoding="utf-8"?>
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android" ><EditTextPreference
        android:key="username"
        android:summary="Please provide your username"
        android:title="Your Name" >
    </EditTextPreference>
    <CheckBoxPreference
        android:defaultValue="false"
        android:key="Location"
        android:summary="let apps that have asked your permission use your                                            location permission."
        android:title="Access to my Location" />
     <ListPreference
        android:defaultValue="1"
        android:entries="@array/simArray"
        android:entryValues="@array/simValues"
        android:key="sim"
        android:summary="Select the SIM"
        android:title="Select SIM" />
  </PreferenceScreen>

Step.3)array.xml

  <?xml version="1.0" encoding="utf-8"?>
  <resources>
  <string-array name="simArray">
<item>SIM1</item>
<item>SIM2</item>
</string-array>
 <string-array name="simValues">
    <item>1</item>
    <item>2</item>
   
 </string-array>
 </resources>


To read the preferences values in any part of your application, you can use the code that is shown below:
SharedPreferences SP = PreferenceManager.getDefaultSharedPreferences(getBaseContext());
String UserName = SP.getString("username", "Dhina");
boolean LocationSP.getBoolean("Location",false);
String SIM= SP.getString("sim","1");



Happy Coding :)