Author Archives: jkoder

Android Widget – ListView

android

Android ListView is a view which groups several items and display them in vertical scrollable list. The list items are automatically inserted to the list using an Adapter that pulls content from a source such as an array or database. An adapter actually bridges between UI components and the data source that fill data into UI Component. Adapter holds the ... Read More »

Android Gestures

android

Android provides special types of touch screen events such as pinch , double tap, scrolls , long presses and flinch. These are all known as gestures. Android provides GestureDetector class to receive motion events and tell us that these events correspond to gestures or not. To use it , you need to create an object of GestureDetector and then extend ... Read More »

Android Creating links using Linkfy

android

Linkfy is a class that lets you create links from TextViews. You can create links not just to web sites, but to also map addresses, emails and even phone numbers. Linking to a web address TextView myWebSite = (TextView) findViewById(R.id.my_web_site); myWebSite.setText("http://http://www.google.com/"); Linkify.addLinks(myWebSite , Linkify.WEB_URLS); Linking to a phone number TextView myPhone = (TextView) findViewById(R.id.my_web_site); myPhone .setText("9999590698"); Linkify.addLinks(myPhone  , Linkify.PHONE_NUMBERS); //map ... Read More »

Android Resource images

android

Resource images are placed under <project_dir>/res/drawable Using a resource image in an image view – XML example <ImageView   android:layout_width="wrap_content"  android:layout_height="wrap_content"  android:tint="#55ff0000"  android:src="@drawable/my_image_name"/> Using a resource image in an image view – Code Example ((ImageView)findViewById(R.id.myImageView)).setImageResource(R.drawable.my_image_name); Turning the resource image to a Bitmap object Bitmap bitmap = BitmapFactory.decodeResource(view.getResources(),R.drawable.my_image_name); Drawing the bitmap on a canvas object canvas.drawBitmap(bitmap, x, y, null); Clearing the bitmap ... Read More »

Android Status-Bar Notifications

android

One of the ways to notify the Android user is by displaying notifications in the status bar. You can show a message, play sound add icon and more. Creating a status bar notification NotificationManager mNotificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); Notification notification = new Notification(R.drawable.icon, "Notification Test", System.currentTimeMillis()); Context context = getApplicationContext(); CharSequence contentTitle = "My notification Title"; CharSequence contentText = "This ... Read More »

Android Alerts/Dialogs

android

Toast A toast notification is a message that pops up on the surface of the window. Toast waitToast = Toast.makeText(getApplicationContext(), "Please wait...", Toast.LENGTH_LONG); waitToast.setGravity(Gravity.TOP, 0, 0); waitToast.show(); AlertDialog AlertDialog.Builder builder = new AlertDialog.Builder(context); builder.setMessage("Are you sure you want to exit?")       .setCancelable(false)       .setPositiveButton("Yes", new DialogInterface.OnClickListener() {           public void onClick(DialogInterface dialog, int id) {                MyActivity.this.finish();           }       })       .setNegativeButton("No", new DialogInterface.OnClickListener() {           public ... Read More »

Android Global Strings

android

A global string, or array of strings are declared in an external xml file in the resource folder:<project>/res/values/strings.xml Declaring a global string <?xml version="1.0" encoding="utf-8"?> <resources>    <string name="hello">Hello World!    <string name="app_name">MyTest </resources> Using a global string while creating a TextView XML example: <TextView    android:layout_width="fill_parent"    android:layout_height="wrap_content"    android:text="@string/hello"    /> Code example: ((TextView)findViewById(R.id.MyTextView)).setText(R.string.hello); Getting a global string String hello = context.getString(R.string.hello); Global String ... Read More »

Android Layouts

android

Layout is a type of a GroupView. It holds views in a certain layout on the screen. Layouts can be declared in two ways: Declare UI elements in XML – Android provides a straightforward XML vocabulary that corresponds to the View classes and subclasses, such as those for widgets and layouts. Instantiate layout elements at runtime – your application can ... Read More »

Android View Groups

android

A ViewGroup is a special view that can contain other views. List view ListView list = new ListView(this); String[] listItems = {"Option 1","Option 2","Option 3"};   list.setAdapter(new ArrayAdapter<String>(                 this,android.R.layout.simple_list_item_1, listItems ));                   list.setOnItemClickListener(new OnItemClickListener() {         @Override       public void onItemClick(AdapterView<?> parent,                     View view, int position, long id) {               Log.d("ListView test", "Item " + id + " was selected.");        } }); ... Read More »

User Interface – Views

android

Creating and adding a new View to a Layout XML example – Adding to a view in a layout xml file <?xml version="1.0" encoding="utf-8"?> <LinearLayout  xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:id="@+id/mainLayout" >     <TextView     android:text="My text view"     android:id="@+id/TextViewExample"     android:layout_width="wrap_content"     android:layout_height="wrap_content"></TextView> </LinearLayout> Code example – Creating and adding a new view to a Layout (implemented in an Activity class). TextView textView = new TextView(this); ... Read More »