Posts

Android camera intent using (StartActivityForResult)

private static final int CAMERA_CODE = 0; // This method is used to call the camera activity. private void cameraIntent(){ final Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(getFile(this)) ); startActivityForResult(intent, CAMERA_CODE); } //create a temporary file in sd card path. private File getFile(Context context){ final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() ); if(!path.exists()){ path.mkdir(); } return new File(path, "image.tmp"); } //This method is used to getresult from another activity. @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { if (resultCode == CAMERA_CODE) { final File file = getFile(this); try { Bitmap captureBmp = Media.getBitmap(getContentResolver(), Uri.fromFile(file) ); // do whatever you want with the bitmap (Resize, Rename, Add ...

Android datepicker update current day

private DatePickerDialog datePicker; private TextView _my_text_view; private Calendar _calendar = Calendar.getInstance(); @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); // on start up it will show the date picker dialog. // you can call this method anywhere in your activitiy showDialog(DATE_DIALOG_ID); } /** * Create the date picker Dialog. */ @Override protected Dialog onCreateDialog(int id) { switch (id) { case DATE_DIALOG_ID:   datePicker = new DatePickerDialog (this,  dateSetListener, year, month, day);   return datePicker; } return null; } /** * Get the values from the datepicker. */ private DatePickerDialog.OnDateSetListener dateSetListener =  new DatePickerDialog.OnDateSetListener() { public void onDateSet(DatePicker view, int year, int monthOfYear,int dayOfMonth){ year = year; month = monthOfYear; day = dayOfMonth;...

Input filter (space not allowed) for EditText

This example which prevent the user to enter blank space in edit text field in Android app  EditText.setFilters(new InputFilter[] { filter }); InputFilter filter = new InputFilter() { public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { for (int i = start; i < end; i++) { if (Character.isSpaceChar(source.charAt(i))) { return ""; } } return null; } }

Android get date after 7 days

Date currentDay = new Date(); // Add milliseconds to your current day Date seventhDay = new Date  (currentDay.getTime + 604800000L ); // 7*24*60*60*1000 or Use the calendar api Calendar calendar = Calendar.getInstance(); calendar.setTime(myDate); calendar.add(Calendar.DAY_OF_YEAR, 7); Date seventhDay = calendar.getTime();

Android button with background

Image
     If you want to create a background for UI components like Buttons or Edit Text etc., First you have to create xml file in your drawable folder. yellow_border_gradient.xml <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" android:shape="rectangle"> <gradient android:startColor="#FFEC8B" android:endColor="#FFFFFF" android:angle="90"/> <stroke android:width="1dip" android:color="#8E8E8E" /> <corners android:radius="5dip" /> <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> </shape>      After creating the folder you can use the above code for designing, after saving your xml. you can call your xml in your button or any UI component. for example shown below <Button android:textSize="14...

Android create temporary file in sd card

private File getTemporaryFile(Context context){ //it will return /sdcard/yourImage.tmp final File path = new File( Environment.getExternalStorageDirectory(), context.getPackageName() ); if(!path.exists()){ path.mkdir(); } return new File(path, "yourImage.tmp"); }

Android Database Example

Database Class import java.util.ArrayList; import java.util.List; import android.content.Context; import android.database.Cursor; import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteOpenHelper; import android.database.sqlite.SQLiteStatement; import android.util.Log; public class DatabaseHelper { private static final String DATABASE_NAME = "firstdatabase.db"; private static final int DATABASE_VERSION = 1; private static final String TABLE_NAME = "mytable"; private Context context; private SQLiteDatabase sqlDb; private SQLiteStatement insertStatement; private static final String INSERT = "insert into " + TABLE_NAME + "(name) values (?)"; public DatabaseHelper(Context context) { this.context = context; OpenHelper openHelper = new OpenHelper(this.context); this.sqlDb = openHelper.getWritableDatabase(); this.insertStatement = this.sqlDb.compileStatement(INSERT); } public long insert(Strin...

Java Singleton Class

The Singleton class’s default constructor is made private,  which prevents the direct instantiation of the object by other classes using the new keyword. The Singleton is a useful Design Pattern for allowing only one instance of your class, but common mistakes can inadvertently allow more than one instance to be created.

JSON Object

JSON (JavaScript Object Notation) is a lightweight computer data interchange format.  It is a text-based, human-readable format for representing simple data structures and  associative arrays (called objects).  The JSON format is often used for transmitting structured data over a network connection  in a process called serialization. Its main application is in AJAX web application  programming, where it serves as an alternative to the traditional use of the XML format. Supported Data types are ( String, Number, Boolean,Array,  Object (Collection of key/value Pairs), null ). Example import net.sf.json.JSONObject; public class MyJSON { public static void main(String args[]){ JSONObject object=new JSONObject(); object.put("name","your Name"); object.put("Max.Marks",new Integer(100)); object.put("Min.Marks",new Double(35)); object.put("Scored",new Double(86.67)); object.put("nickname","your...

Change Apache Tomcat Port Number

 Tomcat runs on port 8080. But there can be situations where there are some other servers running on this same port, forcing you to change the port of one of the servers. Go to your tomcat installed path. C:\..............\Tomcat 6.0\conf\server.xml In the server.xml file; locate the following segment. <Connector port="8080" … /> By Changing this 8080 port number to your new port number.

Android Notes

Activity: Your applications presentation layer.  Every screen in your application will be an extension of activity.  It is nothing but a visible screen. (Layouts, Views) Intent: An inter-application message-passing framework. Intent is an abstract description of an operation  to be performed. Intent provides a facility for performing late runtime binding between the codes  in different applications. Intent Filter: An intent filter declares the capabilities of its parent component. Broadcast receiver: Application - receive and respond to a global event, such as the phone ringing or an  incoming text message. Broadcast Receivers will automatically start your application to  respond to an Incoming Intent, making them perfect for creating event-driven applications. Layouts: Template of view objects.Some Layouts are Absolute Layout, Frame Layout, Linear Layout,  Relative Layout. Android manifest.xml This XML file describes where your appl...

Android menu creation and implementation

To create a menu resource, create an yourmenu.xml  file inside your project's  res/menu/  directory yourmenu.xml   <?xml version="1.0" encoding="utf-8"?> <menu xmlns:android="http://schemas.android.com/apk/res/android"> <item android:id="@+id/menu_next" android:icon="@drawable/icon_next" android:title="Next" /> <item android:id="@+id/menu_back" android:icon="@drawable/icon_back" android:title="Back" /> </menu> Creation   @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.yourmenu, menu); return true; } If menu item selected.. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.menu_next: //your code here.. return true; default: return super.onOptionsItemSelected(item)...

Android Progress Dialog using runnable

Following code will create a progress dialog and run using ruunable interface. final ProgressDialog dialog = ProgressDialog.show(Activity.this, "Your Message", "Status Message",true); Handler handler = new Handler(); Runnable runnable = new Runnable() { @Override public void run() { startActivity(new Intent(MyActivity.this, YourActivity.class)); dialog.dismiss(); } }; handler.postDelayed(runnable, 3000);

Android Button onClickListener

Button button = (Button) findViewById(R.id.your_button); button.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { Toast.makeText(firstActivity.this, "Message by Donor", Toast.LENGTH_SHORT).show(); } });

Android Intent

Intents are used as a message passing mechanism. It works within your application, between the applications.  One of the most common uses for intents is to start new activities. It is either explicitly (by specifying the class to load) or implicitly (by requesting that an action be performed on a piece of data). Explicit Intent startActivity(new Intent(FirstActivity.this,SecondActivity.class)); Implicit Intent Intent intent = new Intent (Intent.ACTION_DIAL, Uri.parse ("tel: 123-4567")); StartActivity (intent);

Android Custom List View Creation

import java.util.ArrayList; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.Window; import android.widget.BaseAdapter; import android.widget.ListView; import android.widget.TextView; public class MyActivity extends Activity { public ArrayList<String> oneList = new ArrayList<String>(); public ArrayList<String> twoList = new ArrayList<String>(); private oneAdapter oneAdapter; private ListView oneListView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); setContentView(R.layout.yourXml); } @Override protected void onResume() { super.onResume(); setContentView(R.layout.yourXml); listCreation(); oneListView = (ListView) findViewById(R.id.list_medic...

SwapNumbersWithoutThirdVariable - JAVA

public class SwapNumbersWithoutThirdVariable { public static void main(String[] args) { int num1 = 10; int num2 = 20; System.out.println("Before Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); //add both the numbers and assign it to first num1 = num1 + num2; num2 = num1 - num2; num1 = num1 - num2; System.out.println("After Swapping"); System.out.println("Value of num1 is :" + num1); System.out.println("Value of num2 is :" +num2); } }

What is .shtml?

SHTML  stands for Server-parsed HyperText Markup Language. It is a file extension identifying a particular type of HyperText Markup Language (HTML) file. Basic HTML files usually provide the text and formatting for web pages.  SHTML  files generally perform the same function, but they also allow the use of simple server commands.

Android Screen sizes and folder struture

Image
Low density Small screens QVGA 240x320 res/layout-small-ldpi res/layout-small-land-ldpi Low density Normal screens WVGA400 240x400 (x432) res/layout-ldpi res/layout-land-ldpi Medium density Normal screens HVGA 320x480 res/layout-mdpi res/layout-land-mdpi Medium density Large screens HVGA 320x480 res/layout-large-mdpi res/layout-large-land-mdpi High density Normal screens WVGA800 480x800 (x854) res/layout-hdpi res/layout-land-hdpi Xoom (medium density large but 1280x800 res) res/layout-xlarge res/layout-xlarge-land

Android layout border with rounded corners

Create the file layout_border.xml in your drawables directory and put in the following content. layout_border.xml <?xml version="1.0" encoding="UTF-8"?> <shape xmlns:android="http://schemas.android.com/apk/res/android"> <solid android:color="#FFFFFF"/> <stroke android:width="3dip" android:color="#B1BCBE" /> <corners android:radius="10dip"/> <padding android:left="0dip" android:top="0dip" android:right="0dip" android:bottom="0dip" /> </shape> Implementation you have to call the above xml in your border property of your (Relative or Linear or etc.,) layout: android:background="@drawable/layout_border"