Posts Tagged ‘Programming’

Android: How To Implement Option Menu In Activity or Dialog

Using Option Menu In Activity

To implement Option Menu in an Activity, we need to create a new custom Dialog class. The example code is given below, please see comments in the code for detail.

Step 1: Create a custom Menu Layout (xml file) in Menu Resources (res/menu). I named it my_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu
  xmlns:android="http://schemas.android.com/apk/res/android">
  
  <item 
      android:icon="@android:drawable/ic_menu_share"
      android:title="Option 1" 
      android:id="@+id/menu_option_one" 
   />

  <item 
      android:icon="@android:drawable/ic_menu_preferences" 
      android:title="Option 2" 
      android:id="@+id/menu_option_two" 
  />

  <item 
      android:icon="@android:drawable/ic_menu_info_details" 
      android:title="Option 3" 
      android:id="@+id/menu_option_three" 
  />
  
</menu>
Step 2: Define a custom Dialog class.
package com.chohdry.adnan.androidexamples;

import android.app.Activity;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

public class OptionMenuWithActivity extends Activity {

	/*
	 * Override function onCreateOptionsMenu(Menu menu)
	 * Get menu inflater from activity
	 * Inflate layout (assign custom menu layout)
	 * Call super class's onCreateOptionsMenu(menu)
	 */
	//
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = getMenuInflater();
		inflater.inflate(R.menu.my_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Override function onOptionsItemSelected(MenuItem item)
	 * Identify the item
	 * Call super class's onOptionsItemSelected(MenuItem item)
	 */
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if(item.getItemId()==R.id.menu_option_one){
			//do whatever you want
		}
		return super.onOptionsItemSelected(item);
	}
}

Using Option Menu In Dialog

Step 1 in implementation of Option Menu in a Dialog is same as above. For Step 2, class structure is different because here we need to extend our class from Dialog class rather Activity and also we need to override onMenuItemSelected(int featureId, MenuItem item) instead of onOptionsItemSelected(MenuItem item).

Step 2: Define a custom Dialog class.
package com.chohdry.adnan.androidexamples;

import android.app.Activity;
import android.app.Dialog;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;

//Create a new class extended from default Dialog class
public class OptionMenuWithDialog extends Dialog {

	//Define activity type variable in class
	private Activity mActivity;

	/**
	 * Implement constructor (a custom or default passing Activity)
	 * Set class variable.
	 */
	public OptionMenuWithDialog(Activity activity) {
		super(activity, android.R.style.Theme_Translucent);
		mActivity = activity;

		//Customize your dialog here
	}

	/**
	 * Override function onCreateOptionsMenu(Menu menu)
	 * Get menu inflater from activity
	 * Inflate layout (assign custom menu layout)
	 * Call super class's onCreateOptionsMenu(menu)
	 */
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		MenuInflater inflater = mActivity.getMenuInflater();
		inflater.inflate(R.menu.my_menu, menu);

		return super.onCreateOptionsMenu(menu);
	}

	/**
	 * Override function onMenuItemSelected(int featureId, MenuItem item)
	 * Identify the item
	 * Call super class's onMenuItemSelected(int featureId, MenuItem item)
	 */
	@Override
	public boolean onMenuItemSelected(int featureId, MenuItem item) {

		if(item.getItemId()==R.id.menu_option_one){
			//do whatever you want
		}
		return super.onOptionsItemSelected(item);
	}
}

This code is available for public use on my GitHub. Download (or get using git) sample Android Examples Project from here https://github.com/adnanyousafch/android-examples.

JAVA, .NET, PHP, Android What to Learn?

Dilemma of a Programmer’s Study Life:

My last degree was Masters in Information Technology with specialization in Computer Science. A Programming Geek? Yes I am! And a crazy one too! From first semester till last I got highest grades in all programming language courses. Attitude of “Love to code” made my life very easy in CS oriented degree but made it too difficult for me at the same time about future prospects. I wanted to put my energy and best abilities in the technology and programming language that are promising to my future.

My degree was completely JAVA oriented, the environment and impression student captured was that a qualified, experienced and technically strong JAVA Programmer has best worth after three to four year of industry experience. Many seniors told me that .NET Software Engineers are very demanded and paid higher in the market, with .NET 4.0 in the market enterprise applications are just click away. I wanted to learn .NET a lot but unfortunately it was not in degree. Apart from degree I was doing part-time job at a local web-development company where I did work in PHP/CSS/XHTML and I knew the worth of these technologies and also the fact that one can work on his own with these technologies rather doing job for someone. I selected PHP as Programming Language of my Degree Project and made a Content Management System. Didn’t get much support from any faculty member instead faced some hard criticism because for some reason they all don’t like a Programming Language which is not Object Oriented by birth and is actually a scripting language. It was the era of birth of Mobile Application Development and iPhone was most popular among all technologies. We also heard about something called Android Development but neither Faculty Member nor seniors was too sure to establish an opinion about that.

Dilemma of a Programmer’s Professional Life:

At Degree completion I focused on my existing job, started giving full time and enjoyed a lot working on Programming Language that I was learning from almost two years. Soon after degree it was pouring calls from various software houses for open vacancies luckily I was among toppers. Most of them was for JAVA Developer but I had no interest to work in a technology where I will have to prove myself for 3-4 years to make my worth count (as it was my perception that time). Also got couple of calls for Jr. Android Developer and many for PHP Developer (but not a Jr. one) but one day I couldn’t resist job offer from highest paying software house of my city which was offering the post of “.NET Software Engineer” where interview–to-hiring ration is less than 5% (yes they filter Cream of Market). I joined with a lot enthusiasm but I didn’t know that at Enterprise level things go so slow and volume of Programming is too low in ocean of technologies, as I left job of open source small scale development company so it felt like “break because of a sudden speed breaker” to me. Nonetheless I learned a lot about enterprise systems and enjoyed a lot working on Microsoft technologies. Meanwhile market was growing up in terms of square for mobile application developers mainly for Android. As I told that I was a “Crazy Programmer” so I tried my luck by applying to a software house that was famous for making software engineers do code for long hors and extra shifts. So I shifted my job after almost a year. Here at my new job I am not only working on Android Development but also on JAVA Desktop Applications and JUnit and trust me they all are not much similar.

From PHP to .NET and now on Android, JAVA and JUnit if I think about tough times shifting from programming languages I cannot find any.

What exactly Programmers should learn?

Working on so many Programming Languages made me realize many things. Like all languages have if-elseif-else, Polymorphism, frameworks etc. I shifted many jobs based on technology but programming language never came in the way and my worth was on high node every time. The first question I used to ask from technical interviewer, upon asking solution to a complex program, “in which Programming Language do you want?” and always got the same answer “Any! You may write pseudo code”. It was strange for me for first couple of years but not any more as I have learned a lot and realized that Programming Language is not a thing for which Software Engineers are count worthy, it is problem solving skills, writing perfect solutions, application of design patterns and much more things like it that make us worthy. Programming languages are just used for the application of the ideas.

A good software engineer is independent of Programming Language, You give him problem and he will give you the solution in your desired language.

Software Engineers must not restrict themselves on technology; they should explore more to make themselves System Creator not just coder. Now you may think this post is totally opposite to the title but that’s the way I write. :)

Is Programming Language Totally Irrelevant?

No! Off course not! Being a student one must learn as many technologies and programming languages as one can. As they will proceed with their career they will realize that mastering a language will give the benefit to code less and get more output. But still career should not be focused on Programming languages or technologies. It should be focused on Development of Complex Systems regardless of the implementation technology.

Programming Language is merely a requirement for application of knowledge, it is “Engineering The Software Development Process” which makes one worthy.

about.me
Adnan Chohdry

Adnan Chohdry

IT and Software Professional

Top Viewed Posts
Top Viewed Pages
Get New Posts In Email
Please enter email address
counters