View

Wednesday, 17 August 2011

How to create contextmenu in android

creating a context menu is very simple,Here i use Textview name as press me on long,when the textview is pressed for long,its open a context menu.In that context menu i use New,edit,copy,paste and delete.
Inside of the onCreate Method,we have to registerForcontextmenu(controls name)
Example: press=(TextView)findViewById(R.id.press_tv);
               registerForContextMenu(press); 
Then we  need to override the onCreateContextMenu method to create the menu:
 @Override 
   public void onCreateContextMenu(ContextMenu menu, View v,ContextMenuInfo menuInfo) {  
  super.onCreateContextMenu(menu, v, menuInfo);  
   menu.setHeaderTitle("Context Menu");  
   menu.add(0, v.getId(), 0, "New");  
   menu.add(0, v.getId(), 0, "Edit");  
   menu.add(0, v.getId(), 0, "copy");  
   menu.add(0, v.getId(), 0, "Paste");  
   menu.add(0, v.getId(), 0, "Delete");  
when the item is selected,that items action can perform here,using onContextItemSelected method.
Here i use function for only "New" just passing the Intent,If u want some functions mean, you write a else if () and perform some more actions there.
 @Override 
  public boolean onContextItemSelected(MenuItem item) {  
   if(item.getTitle()=="New"){
    Intent np=new Intent(getApplicationContext(),NewPage.class);
    startActivity(np);
    function1(item.getItemId());}  
   else if(item.getTitle()=="Save"){
    //Some funtion perform here//
    function2(item.getItemId());}  
    else {return false;}  
  return true;  
 }
create a method for function1.... function n,
private void function2(int itemId) {
  // TODO Auto-generated method stub
 
 }
 private void function1(int itemId) {
  // TODO Auto-generated method stub
 
 } 
Result is:
           

pic1

pic2

 
  

pic3

pic4
Download full source code 

Friday, 5 August 2011

How to create FeedBack Form inside of the customDialogBox?

How to create a form in dialog box,Here i use sample feed back form inside of the custom dialogbox,First u have to create a button in main.xml file,Because when the button is press ,FeedBack Form can open in customdialogbox,so thats why i had put a Button(Press Dialog).After that you also create a new xml file name as dialog.xml,In that xml ,i use Name,Mail,Comment and two buttons like send and cancel.
code for dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
  xmlns:android="http://schemas.android.com/apk/res/android"
  android:layout_width="match_parent"
  android:orientation="vertical" android:layout_height="wrap_content">
    <LinearLayout android:id="@+id/linearLayout1" android:layout_height="wrap_content" android:layout_width="match_parent">
        <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Name" android:textSize="15dp" android:layout_marginLeft="10dp" android:layout_marginTop="5dp"></TextView>
        <EditText android:id="@+id/editText1" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_width="200dp" android:layout_marginLeft="16dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout2">
        <TextView android:id="@+id/textView2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Mail Id:" android:layout_marginTop="5dp" android:layout_marginLeft="5dp"></TextView>
        <EditText android:id="@+id/editText2" android:layout_height="wrap_content" android:layout_marginTop="5dp" android:layout_width="200dp" android:layout_marginLeft="16dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout3">
        <TextView android:id="@+id/textView3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Comment" android:layout_marginLeft="5dp" android:layout_marginTop="5dp"></TextView>
        <EditText android:id="@+id/editText3" android:layout_height="100dp" android:layout_width="200dp"></EditText>
    </LinearLayout>
    <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="@+id/linearLayout4">
        <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Send" android:layout_marginLeft="70dp"></Button>
        <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Cancel"></Button>
    </LinearLayout>
</LinearLayout>
Then i have to create a main class name as FeedbackFormusingInsideDialogBox.java
static final int CUSTOM_DIALOG_ID = 0;
Button customDialog_Dismiss;
The class have to declare as a
its a very few line code to creat a dialogbox.Just button press click listener,Dialog onCreateDialog and cancel button listener.
here i put full code .just check it

public class FeedbackFormusingInsideDialogBox extends Activity {
 static final int CUSTOM_DIALOG_ID = 0;
 Button customDialog_Dismiss;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);      
        Button buttonStartDialog = (Button)findViewById(R.id.startdialog);
        buttonStartDialog.setOnClickListener(new Button.OnClickListener(){
   @Override
   public void onClick(View arg0) {
    // TODO Auto-generated method stub
    showDialog(CUSTOM_DIALOG_ID);
   }
        });
              
    }     
    private Button.OnClickListener customDialog_DismissOnClickListener  = new Button.OnClickListener(){ 
  @Override
  public void onClick(View arg0) {
   // TODO Auto-generated method stub
   dismissDialog(CUSTOM_DIALOG_ID);
  }    
    };   
 @Override
 protected Dialog onCreateDialog(int id) {
  // TODO Auto-generated method stub
  Dialog dialog = null;;
     switch(id) {
     case CUSTOM_DIALOG_ID:
      dialog = new Dialog(this);
      dialog.setContentView(R.layout.dialogbox);
      dialog.setTitle("Feed Back Form");
             customDialog_Dismiss = (Button)dialog.findViewById(R.id.button2);
             customDialog_Dismiss.setOnClickListener(customDialog_DismissOnClickListener);
         break;
     }
     return dialog;
 } 
 }
Result :




Tuesday, 2 August 2011

How to use swipe in android?

Swipe is otherwise called  as a Fling. Its a touch free movement in android.
Before that we have set some SWIPE MIN,MAX Distance,Threshold_velosity and as well as path.
 private static final int SWIPE_MIN_DISTANCE = 120;
  private static final int SWIPE_MAX_OFF_PATH = 250;
 private static final int SWIPE_THRESHOLD_VELOCITY = 200;
 private GestureDetector gestureDetector;
 View.OnTouchListener gestureListener;
 private Animation slideLeftIn;
 private Animation slideLeftOut;
 private Animation slideRightIn;
        private Animation slideRightOut;
        private ViewFlipper viewFlipper;
Next step is to write a code inside of the onCreate Method
        setContentView(R.layout.main);
        viewFlipper = (ViewFlipper)findViewById(R.id.flipper);
        slideLeftIn = AnimationUtils.loadAnimation(this, R.anim.slide_left_in);
        slideLeftOut = AnimationUtils.loadAnimation(this, R.anim.slide_left_out);
        slideRightIn = AnimationUtils.loadAnimation(this, R.anim.slide_right_in);
        slideRightOut = AnimationUtils.loadAnimation(this, R.anim.slide_right_out);
       
        gestureDetector = new GestureDetector(new MyGestureDetector());
        gestureListener = new View.OnTouchListener() {
            public boolean onTouch(View v, MotionEvent event) {
                if (gestureDetector.onTouchEvent(event)) {
                    return true;
                }
                return false;
            }
        };
You will need to extend SimpleOnGestureListener to implement your own handling on swipe/fling action:

 class MyGestureDetector extends SimpleOnGestureListener {
        @Override
        public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
            try {
                if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)
                    return false;
                // right to left swipe
                if(e1.getX() - e2.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 viewFlipper.setInAnimation(slideLeftIn);
                    viewFlipper.setOutAnimation(slideLeftOut);
                 viewFlipper.showNext();
                }  else if (e2.getX() - e1.getX() > SWIPE_MIN_DISTANCE && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {
                 viewFlipper.setInAnimation(slideRightIn);
                    viewFlipper.setOutAnimation(slideRightOut);
                 viewFlipper.showPrevious();
                }
            } catch (Exception e) {
                // nothing
            }
            return false;
        }
    }
At last, you need to make sure in your activity, you catch the gesture event by overriding onTouch() method:
 
@Override
    public boolean onTouchEvent(MotionEvent event) {
        if (gestureDetector.onTouchEvent(event))
         return true;
     else
      return false;
    }
Here i use viewflipper,two LinearLayouts and two Buttons in main.xml file you have to set as <ViewFlipper xmlns:android="http://......" like this

<?xml version="1.0" encoding="utf-8"?>
<ViewFlipper xmlns:android="http://schemas.android.com/apk/res/android"
     android:id="@+id/flipper"
     android:layout_width="fill_parent"
     android:layout_height="fill_parent">
   
 <LinearLayout
  android:layout_width="fill_parent" android:layout_height="70dp" android:background="#E2A9F3">
  <TextView android:id="@+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="30dp" android:layout_marginTop="10dp" android:text="Swipe Me" android:textSize="20dp" android:textColor="#DF0101"></TextView>
    </LinearLayout>  
    <LinearLayout
  android:layout_width="fill_parent" android:layout_height="60dp" android:background="#585858">
     <Button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Back"></Button>
     <Button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Next"></Button>
    </LinearLayout>  
</ViewFlipper>
At last thing u have to create a new folder and set name as anim or what u wish ,u give it.In that folder you have to  create a xml for Left_in,Left_out,Right_in and right_out and set the values.
name:slide_left_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="100%p" android:toXDelta="0" android:duration="800"/>
</set>
name:slide_left_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="-100%p" android:duration="800"/>
</set>
name:slide_right_in.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="-100%p" android:toXDelta="0" android:duration="800"/>
</set>
name:slide_right_out.xml
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate android:fromXDelta="0" android:toXDelta="100%p" android:duration="800"/>
</set>

Result:
 

Monday, 1 August 2011

How to use Menu Option in Android?

Its a very simple way to use menu option in android, just only four line to write it. first u have to  create new folder and name as menu
example res/menu/sample.xml.
give some options what u want ,Here i gave file,open,save and edit

<?
<
xml version="1.0" encoding="utf-8"?>menu
xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@+id/file" android:title="@string/file" />
<item android:id="@+id/open" android:title="@string/open"/> <item android:id="@+id/save" android:title="@string/save" />
<item android:id="@+id/edit" android:title="@string/edit"/> </menu>
after that we have to set a values in strings.xml
example:values/strings.xml
<?
<
xml version="1.0" encoding="utf-8"?>resources>
<string name="hello">Hello World, MenuScreen!</string>
<string name="app_name">SampleMenu</string>
<string name="file">File</string>
<string name="open">Open</string>
<string name="save">Save</string>
</
<string name="edit">Edit</string>resources>
now you have to write a code to show the menu options.
@Override
    public boolean onCreateOptionsMenu(Menu menu) {
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.sample, menu);         
        return true;
    }
Result is: