Posts

Create custom button in android

Image
Download Code   Create custom button in android Maybe you would like to create custom button. We can use custom android buttons for a better layout. Here I’m going to show how to customize Android buttons. In the following image you can see how our buttons will look like. 1) Create button_disable_yellow.xml file in drawable folder.       <?xml version="1.0" encoding="utf-8"?>     <shape xmlns:android="http://schemas.android.com/apk/res/android" >         <corners android:radius="25dp" />         <solid android:color="#ACAFAF" />     </shape> 2) Create button_enable_yellow.xml file in drawable folder.       <selector xmlns:android="http://schemas.android.com/apk/res/android">         <item><layer-list>              ...

Create custom ListView in android

Image
Download code I have done custom list view in android with custom button and custom text view. 1) In this example I have create database, adapter, model, custom control class and main activity class and also create two xml files one for item of ListView and second for ListView, Button and TextView. 2) In this example I use colors.xml, ids.xml and strings.xml files. 3) Bellow I attached screen shoot of my custom ListView. Download code

How to hide Soft Keyboard when activity starts ?

In the AndroidManifest.xml <activity android:name="com.your.package.ActivityName"       android:windowSoftInputMode="stateHidden"  /> Also you can use the following functions to show/hide the keyboard /**  * Hides the soft keyboard  */ public void hideSoftKeyboard() {     if(getCurrentFocus()!=null)     {         InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);         inputMethodManager.hideSoftInputFromWindow(getCurrentFocus().getWindowToken(), 0);     } } /**  * Shows the soft keyboard  */ public void showSoftKeyboard(View view) {     InputMethodManager inputMethodManager = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);     view.requestFocus();     inputMethodManager.showSoftInput(view, 0); }

How can set list view height based on children ?

- Use this function to change listview size dynamically..... /**     * Sets the list view height based on children.     *     * @param listView     * the new list view height based on children     */     private void setListViewHeightBasedOnChildren(ListView listView)     {         ListAdapter listAdapter = listView.getAdapter();         if (listAdapter == null)             return;         int desiredWidth = MeasureSpec.makeMeasureSpec(listView.getWidth(), MeasureSpec.UNSPECIFIED);         int totalHeight = 0;         View view = null;         for (int i = 0; i < listAdapter.getCount(); i++)         {      ...

How can remove duplicate values from ArrayList

- This is solution for remove duplicate values from ArrayList Set<String> set = new LinkedHashSet<String>(); for (ArrayList<String> list:yourList) {     set.addAll (list); } ArrayList<String> uniqueList = new ArrayList<String>(set);

String to ArrayList

How to convert comma separated String value to ArrayList ? Solution : private List<String> items; private String orderedItem = "1,2,3,4,5,6,7,8,9,"; items = new ArrayList<String>(Arrays.asList(orderedItem.split(",")));

Shared Preferences

----- Initialization : ----- SharedPreferences pref = getApplicationContext().getSharedPreferences("MyPref", 0); // 0 - for private mode Editor editor = pref.edit(); ----- Storing Data : ----- editor.putBoolean("key_name", true); // Storing boolean - true/false editor.putString("key_name", "string value"); // Storing string editor.putInt("key_name", "int value"); // Storing integer editor.putFloat("key_name", "float value"); // Storing float editor.putLong("key_name", "long value"); // Storing long editor.commit(); // commit changes ----- Retrieving Data : ----- // returns stored preference value // If value is not present return second param value - In this case null pref.getString("key_name", null); // getting String pref.getInt("key_name", null); // getting Integer pref.getFloat("key_name", null); // getting Float pref.getLong("key_name", null)...