Pages

Monday, March 31, 2014

Back to Home Screen directly using ACTION MAIN with category CATEGORY HOME

If you want to go to Home Screen directly, using the code:


Intent intent = new Intent(Intent.ACTION_MAIN);
intent.addCategory(Intent.CATEGORY_HOME);
startActivity(intent);



Read More..

How to create Auto Complete text in android

just drop auto-complete textview from widget to your layout or copy below code:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical">

    <AutoCompleteTextView
        android:id="@+id/autoCompleteTextView1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
      >

        <requestFocus />
    </AutoCompleteTextView>

</LinearLayout>
 

Now open java file and paste below code


package sel.listG; //package name

import android.os.Bundle;
importandroid.widget.ArrayAdapter;
importandroid.widget.AutoCompleteTextView;
importandroid.app.Activity;

public class MainActivity extends Activity {
      String values[]={"January", "July", "June", "Ravi", "Ravinder", "Abhishek", "Abhinav", "Harkishore", "Hardev"};

      @Override
      protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.activity_main);
            AutoCompleteTextView lv=(AutoCompleteTextView)findViewById(R.id.autoCompleteTextView1); 
//adapter will adapt the string and pass the string to lv object
            ArrayAdapter<?> adapter=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, values);
            lv.setAdapter(adapter);
            
      }
  }
 

Now run your code..
Read More..

Sunday, March 30, 2014

Custom Expandable List In Android

This example shows how to create custom expandable list in android.
Algorithm:
1.)  Create a new project by File-> New -> Android Project name it CustomExpandableList.
2.)  Run for output.

Steps:
1.) Create a project named CustomExpandableList and set the information as stated in the image.
Build Target: Android 4.0
Application Name: CustomExpandableList
Package Name: com. example. CustomExpandableList
Activity Name: CustomExpandableList
Min SDK Version: 14
2.) Open CustomExpandableList.java file and write following code there:
package com.example.CustomExpandableList;
import android.app.ExpandableListActivity;
import android.os.Bundle;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AbsListView;
import android.widget.BaseExpandableListAdapter;
import android.widget.ExpandableListAdapter;
import android.widget.ExpandableListView;
import android.widget.ExpandableListView.ExpandableListContextMenuInfo;
import android.widget.TextView;
import android.widget.Toast;
public class CustomExpandableList extends ExpandableListActivity {
    ExpandableListAdapter mAdapter;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Set up our adapter
        mAdapter = new MyExpandableListAdapter();
        setListAdapter(mAdapter);
        registerForContextMenu(getExpandableListView());
    }
    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.setHeaderTitle("Sample menu");
        menu.add(000"Sample action");
    }
 
    @Override
    public boolean onContextItemSelected(MenuItem item) {
        ExpandableListContextMenuInfo info = (ExpandableListContextMenuInfo)item.getMenuInfo();
        String title = ((TextView) info.targetView).getText().toString();
     
        int type = ExpandableListView.getPackedPositionType(info.packedPosition);
        if (type == ExpandableListView.PACKED_POSITION_TYPE_CHILD) {
            int groupPos =ExpandableListView.getPackedPositionGroup(info.packedPosition);
            int childPos =ExpandableListView.getPackedPositionChild(info.packedPosition);
            Toast.makeText(this, title + ": Child " + childPos + " clicked in group " + groupPos,
                    Toast.LENGTH_SHORT).show();
            return true;
        } else if (type == ExpandableListView.PACKED_POSITION_TYPE_GROUP) {
            int groupPos =ExpandableListView.getPackedPositionGroup(info.packedPosition);
            Toast.makeText(this, title + ": Group " + groupPos + " clicked", Toast.LENGTH_SHORT).show();
            return true;
        }
     
        return false;
    }
    public class MyExpandableListAdapter extends BaseExpandableListAdapter {
        private String[] groups = { "Android Versions""Android Phones" };
        private String[][] children = {
                { "IceCream Sandwitch""Gingerboard""Android 2.0""Android 1.6" },
                { "Razr""Sony Ericsson""Galaxy Tab""Galaxy" }
        };
     
        public Object getChild(int groupPosition, int childPosition) {
            return children[groupPosition][childPosition];
        }
        public long getChildId(int groupPosition, int childPosition) {
            return childPosition;
        }
        public int getChildrenCount(int groupPosition) {
            return children[groupPosition].length;
        }
        public TextView getGenericView() {
            AbsListView.LayoutParams lp = new AbsListView.LayoutParams(
                    ViewGroup.LayoutParams.MATCH_PARENT, 64);
            TextView textView = new TextView(CustomExpandableList.this);
            textView.setLayoutParams(lp);
            textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
            textView.setPadding(36, 0, 0, 0);
            return textView;
        }
     
        public View getChildView(int groupPosition, int childPosition, booleanisLastChild,
                View convertView, ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getChild(groupPosition, childPosition).toString());
            return textView;
        }
        public Object getGroup(int groupPosition) {
            return groups[groupPosition];
        }
        public int getGroupCount() {
            return groups.length;
        }
        public long getGroupId(int groupPosition) {
            return groupPosition;
        }
        public View getGroupView(int groupPosition, boolean isExpanded, ViewconvertView,
                ViewGroup parent) {
            TextView textView = getGenericView();
            textView.setText(getGroup(groupPosition).toString());
            return textView;
        }
        public boolean isChildSelectable(int groupPosition, int childPosition) {
            return true;
        }
        public boolean hasStableIds() {
            return true;
        }
    }
}
3.) Compile and build the project.
Output





Read More..