Posts

Showing posts from 2012

Android detect Android Version

This is a code I extracted from active project called HideBar . This code logs the AndroidVersion and returns AndroidVersion. public enum AndroidVersion { HC, ICS, JB, UNKNOWN }; public AndroidVersion getAndroidVersion() { Log.v(LOG_TAG, "getAndroidVersion called"); int sdk = android.os.Build.VERSION.SDK_INT; if (11 <= sdk && sdk <= 13) { Log.v(LOG_TAG, "We are running on HoneyComb"); return AndroidVersion.HC; } else if (14 <= sdk && sdk <= 15) { Log.v(LOG_TAG, "We are running on IceCreamSandwich"); return AndroidVersion.ICS; } else if (16 == sdk) { Log.v(LOG_TAG, "We are running on JellyBean"); return AndroidVersion.JB; } else { Log.v(LOG_TAG, "We don't know what we are running on"); return AndroidVersion.UNKNOWN; } }

Android setting the default zoom controls.

Here is how to zoom controls in webview. WebView webview = new WebView(this); webview.getSettings().setBuiltInZoomControls(true);

How to restart android Activty?

Here is how to restart an activity in Android. public void reload() {     Intent intent = getIntent();     overridePendingTransition(0, 0);     intent.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION);     finish();     overridePendingTransition(0, 0);     startActivity(intent); } But Since API level 11, you can use recreate() method. The recreate() method acts just like a configuration change, so your onSaveInstanceState() and onRestoreInstanceState() methods are also called, if applicable.

Android: Dynamically add View at the runtime

Here is the coding to add a view such a as a button, layout etc. at the runtime. LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); ViewGroup parent = (ViewGroup)findViewById(R.id.where_you_want_to_insert); inflater.inflate(R.layout.the_child_view, parent);

How to hide Android Soft Keyboard

If you want to hide the Android Soft Keyboard, you can use following methods to do that. Method 1 InputMethodManager imm = (InputMethodManager)getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(myEditText.getWindowToken(), 0); Method 2 getWindow().setSoftInputMode(       WindowManager.LayoutParams.SOFT_INPUT_STATE_ALWAYS_HIDDEN); Method 3 Add following code into the AndroidManifest.xml file, inside the Activity tag. android:windowSoftInputMode="stateHidden" Method 4 Try below code inside onCreate() method in your activity class. EditText edtView=(EditText)findViewById(R.id.editTextConvertValue); edtView.setInputType(0); or editView.setInputType(InputType.TYPE_NULL); ***This method works as a means of getting around the "can't hide the soft keyboard" bug in 2.0 and 2.1 as described in code.google.com/p/android/issues/detail?id=7115 Method 5 If you work with TabHost and you want to hide the keyboard on onT

Java : Get size and orientation of android device

// First, you need to get the Display from the WindowManager Display display = ((WindowManager) getSystemService(WINDOW_SERVICE)).getDefaultDisplay(); // Now you can call all display-related methods int width = display.getWidth(); int height = display.getHeight(); int orientation = display.getOrientation();

How to get icons of Installed Applications in Android

I wanted to get icons of running activities And here I done it. List<PackageInfo> packs = getPackageManager().getInstalledPackages(0); Drawable[] icons=new Drawable[packs.size]; for(int i=0;i<packs.size();i++) {     PackageInfo p = packs.get(i);     icons[i]= p.applicationInfo.loadIcon(getPackageManager());  } And you may need to convert the return values to Bitmap.

Android: How to add static header to the top of a ListActivity

I wanted to add static header to my ListView and here how I done it. In my ListActivity Class. @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); ListView listView = getListView(); LayoutInflater inflater = getLayoutInflater(); ViewGroup header =  (ViewGroup)inflater.inflate(R.layout.header, lv, false); listView.addHeaderView(header, null, false); String[] values = getResources().getStringArray(R.array.reg_dates); ArrayAdapter adapter = new ArrayAdapter (this, android.R.layout.simple_list_item_1, values); setListAdapter(adapter); } And here is my header.xml file. <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <Button androi

Generate Android key hash For Facebook App in Windows

You need to download Openssl-for-windows library. And you need to create a folder in C:\ drive or(folder you want). And unzip all the files from openssl-for-windows library zip file. And go to where your Java installation resides, and execute the following code. (C:\Program Files\Java\jdk1.7.0_03\bin) keytool -export -alias myAlias -keystore C:\Users\Isuru\.android\myKeyStore | C:\openssl\bin\openssl sha1 -binary | C:\openssl\bin\openssl enc -a -e     Hit Enter, and type your password.

CATHOLICISM Faith Formation Part II

Image

How to change Eclipse Theme?

I wanted to change the Eclipse theme or template here how I did it. There is a good website called http://eclipsecolorthemes.org . You can download Eclipse Preferences(epf) file for chosen theme. And you have to import that file. Go to File -> Import -> General -> Preferences -> Browse(your downloaded epf file.) How to import Eclipse Preferences (*.epf) from Roger Dudler on Vimeo .

Creating and Showing an Android System Notification

A Android system notifications are normally used for sending notifications by Android services. Here how you create simple notification. public void showNotification(CharSequence message) { final int notifyRef = 1; final int notifyIcon = R.drawable.icon; final long notifyWhen = System.currentTimeMillis(); final String notifyService = Context.NOTIFICATION_SERVICE;   NotificationManager notifyManager = (NotificationManager)getSystemService(notifyService); Notification notification = new Notification( notifyIcon, message, notifyWhen);   Context context = getApplicationContext(); CharSequence notifyTitle = message; CharSequence notifyText = "You saved this message."; Intent notifyIntent = new Intent( this, MyAndroidSdkAppActivity2.class); PendingIntent contentIntent = PendingIntent.getActivity( this, 0, notifyIntent, 0); notification.setLatestEventInfo( context, notifyTitle, notifyText

Showing an Android Alert Dialog

Following code will show Android Alert Dialog. public void showOkAlertDialog(CharSequence message) { new AlertDialog.Builder(this) .setMessage(message) .setPositiveButton("OK", null) .show(); } Message Must be something you need to show to user.

Exiting an Application Activity

I didn't check this, because normally in android you not exit from the app, android os take the garbage collection for the app. So here is the code for existing from an activity. exitButton = (Button)findViewById(R.id.exit_button); exitButton.setOnClickListener( new Button.OnClickListener() { public void onClick (View v){ Log.d(TAG, v.toString() + ": Leaving activity..."); Runtime.getRuntime().exit(0); } } );

Install JDownloader in Ubuntu

JDownloader is the best Download manager I worked with, here how you install it in Ubuntu. Go to terminal, and type. sudo add-apt-repository ppa:jd-team/jdownloader sudo apt-get update && sudo apt-get install jdownloader

Ubuntu Bluetooth disabled by switch error.

I got this error and couldn't start bluetooth of my PC. But restarting bluetooth service solved the problem. # sudo /etc/init.d/bluetooth restart  To check device added. #  dmesg |tail