Search This Blog

Showing posts with label Toast. Show all posts
Showing posts with label Toast. Show all posts

Friday, October 14, 2016

Pick images from default gallery intent

Pick image from default gallery images... from android


final PackageManager packageManager = getPackageManager();
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

List<ResolveInfo> list = packageManager.queryIntentActivities(intent,
                                                      PackageManager.MATCH_DEFAULT_ONLY);
if (list.size() > 0) {
         startActivityForResult(intent, PICK_IMAGE_REQUEST);
} else {
          Toast.makeText(UserProfileEditActivity.this, "No app can perform this action",                        Toast.LENGTH_LONG).show();
           return;
}

Sunday, March 31, 2013

DialogFragment

android.app.DialogFragment, was introduced from API Level 11 (Android 3.0), displays a dialog window, floating on top of its activity's window.


public class MyDialogFragment extends DialogFragment {

            static MyDialogFragment newInstance() {

            String title = "My Fragment";

            MyDialogFragment f = new MyDialogFragment();
            Bundle args = new Bundle();
            args.putString("title", title);
            f.setArguments(args);
            return f;
            }

            @Override
            public Dialog onCreateDialog(Bundle savedInstanceState) {
                            String title = getArguments().getString("title");
                            Dialog myDialog = new AlertDialog.Builder(getActivity())
                                                   .setIcon(R.drawable.ic_launcher)
                                                   .setTitle(title)
                                                   .setPositiveButton("OK", 

                                                      new DialogInterface.OnClickListener() {
                     @Override
                     public void onClick(DialogInterface dialog, int which) {
                                      ((AndroidDialogFragmentActivity)getActivity()).okClicked();
                    }
                })
                                                  .setNegativeButton("Cancel",
new DialogInterface.OnClickListener() {

                   @Override
                   public void onClick(DialogInterface dialog, int which) {
                                      ((AndroidDialogFragmentActivity)getActivity()).cancelClicked();
                   }
               })
              .create();

return myDialog;
}

}


in main.xml modify the following code.


<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >

<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/hello" />
<Button
android:id="@+id/opendialog"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Open Dialog" />

</LinearLayout>


now in your activity just change the following code and see the dialog

public class DialogFragmentActivity extends Activity {
                          /** Called when the activity is first created. */
                         @Override
                         public void onCreate(Bundle savedInstanceState) {
                                      super.onCreate(savedInstanceState);
                                      setContentView(R.layout.main);


                                Button buttonOpenDialog = (Button)findViewById(R.id.opendialog);
                                buttonOpenDialog.setOnClickListener(new Button.OnClickListener(){

                               @Override
                               public void onClick(View arg0) {
                                                  OpenDialog();
                                }});

                  }

                  void OpenDialog(){
                         MyDialogFragment myDialogFragment = MyDialogFragment.newInstance();
                         myDialogFragment.show(getFragmentManager(), "myDialogFragment");
                  }

                 public void okClicked() {
                          Toast.makeText(AndroidDialogFragmentActivity.this,
                                           "OK Clicked!",
                                           Toast.LENGTH_LONG).show();
                 }

                 public void cancelClicked() {
                                  Toast.makeText(AndroidDialogFragmentActivity.this,
                                                 "Cancel Clicked!",  Toast.LENGTH_LONG).show();
                  }
}

Monday, February 13, 2012

Gallery, Using Gallery widget

Here is a exercise to implement a picture view using Gallery, a view that shows items in a center-locked, horizontally scrolling list.
Basically, this exercise follow the Android Tutorial, Hello Gallery.Create a Android Application, HelloGallery. 

Copy some images, in .png format, into res/drawable/ directory. Named a,b,...,e.png in my case.
Android supports bitmap resource files in a few different formats: png (preferred), jpg (acceptable), gif (discouraged). The bitmap file itself is compiled and referenced by the file name without the extension (so res/drawable/my_picture.png would be referenced as R.drawable.my_picture).

Create res/values/attrs.xml to define Theme_android_galleryItemBackground.

<?xml version="1.0" encoding="utf-8"?>
<resources>
<declare-styleable name="Theme">
<attr name="android:galleryItemBackground" />
</declare-styleable>
</resources>



Modify main.xml to have a Gallery in the screen.

<?xml version="1.0" encoding="utf-8"?>
<Gallery xmlns:android="http://schemas.android.com/apk/res/android"
android:id="@+id/gallery"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>


Now open the java file and insert the following code into it.


public class HellogalleryActivity extends Activity {

/** Called when the activity is first created. */

@Override

public void onCreate(Bundle savedInstanceState) {
   super.onCreate(savedInstanceState);
   setContentView(R.layout.main);

   Gallery g = (Gallery) findViewById(R.id.gallery);
   g.setAdapter(new ImageAdapter(this));

   g.setOnItemClickListener(new OnItemClickListener() {
       public void onItemClick(AdapterView<?> parent,
           View v, int position, long id) {
           Toast.makeText(HellogalleryActivity.this, "" + position,
               Toast.LENGTH_SHORT).show();
       }
   });
}

public class ImageAdapter extends BaseAdapter {
   int mGalleryItemBackground;
   private Context mContext;

   private Integer[] mImageIds = {
           R.drawable.a,
R.drawable.b,
           R.drawable.c,
R.drawable.d,
           R.drawable.e,R.drawable.f,
           R.drawable.g,R.drawable.h
   };

   public ImageAdapter(Context c) {
       mContext = c;
       TypedArray a = obtainStyledAttributes(R.styleable.Theme);
       mGalleryItemBackground = a.getResourceId(                  
                   

R.styleable.Theme_android_galleryItemBackground,
                   0);
       a.recycle();
   }

   public int getCount() {
       return mImageIds.length;
   }

   public Object getItem(int position) {
       return position;
   }

   public long getItemId(int position) {
       return position;
   }

   public View getView(int position,View convertView, ViewGroup parent) {
       
       ImageView i = new ImageView(mContext);

       i.setImageResource(mImageIds[position]);
       i.setLayoutParams(new Gallery.LayoutParams(150, 100));
       i.setScaleType(ImageView.ScaleType.FIT_XY);
       i.setBackgroundResource(mGalleryItemBackground);

       return i;
   }
}
}



Saturday, January 21, 2012

How to Show Alert Dialog in Android

Creating alert dialog is very easy. In this tutorial i will be discussing about creating different alert dialogues with one button(ok button), two buttons(yes or no buttons) and three buttons(yes, no and cancel buttons).

Android alert dialog with One button

The following code will create a simple alert dialog with one button. In the following code setTitle() method is used for set Title to alert dialog. setMessage() is used for setting message to alert dialog. setIcon() is to set icon to alert dialog.


AlertDialog alertDialog = new AlertDialog.Builder(
AlertDialogActivity.this).create();
// Setting Dialog Title

alertDialog.setTitle("Alert Dialog");
// Setting Dialog Message
alertDialog.setMessage("Welcome to typicaljava.blogspot.com");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.tick);
// Setting OK Button
alertDialog.setButton("OK", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
                // Write your code here to execute after dialog closed
                Toast.makeText(getApplicationContext(), "You clicked on OK",                                          
                                                                Toast.LENGTH_SHORT).show();
                }
        });
// Showing Alert Message
alertDialog.show();


Android alert dialog with two button

The following code will create alert dialog with two button. setPositiveButton() is used to create a positive button in alert dialog and setNegativeButton() is used to invoke negative button to alert dialog.

AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Confirm Delete...");
// Setting Dialog Message
alertDialog.setMessage("Are you sure you want delete this?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.delete);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog,int which) {
// Write your code here to invoke YES event
                   Toast.makeText(getApplicationContext(), "You clicked on YES", 
                                                       Toast.LENGTH_SHORT).show();
                        }
            });
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// Write your code here to invoke NO event
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
dialog.cancel();
}
});
// Showing Alert Message
alertDialog.show();



Android alert dialog with three button

Here setNeutralButton() is used to create a neutral cancel button

AlertDialog.Builder alertDialog = new AlertDialog.Builder(AlertDialogActivity.this);
// Setting Dialog Title
alertDialog.setTitle("Save File...");
// Setting Dialog Message
alertDialog.setMessage("Do you want to save this file?");
// Setting Icon to Dialog
alertDialog.setIcon(R.drawable.save);
// Setting Positive "Yes" Button
alertDialog.setPositiveButton("YES", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed YES button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on YES",
Toast.LENGTH_SHORT).show();
}
});
// Setting Negative "NO" Button
alertDialog.setNegativeButton("NO", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed No button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on NO", Toast.LENGTH_SHORT).show();
}
});
// Setting Netural "Cancel" Button
alertDialog.setNeutralButton("Cancel", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
// User pressed Cancel button. Write Logic Here
Toast.makeText(getApplicationContext(), "You clicked on Cancel",
Toast.LENGTH_SHORT).show();
}
});
// Showing Alert Message
alertDialog.show();




After making the proper screenshot of this code i will be uploading on it....

Monday, January 2, 2012

SPINNER ACTIVITY WITH ANDROID

First of all create the spinnerlayout.cml in android and put thr following code into it.

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

<textview android:id="@+id/TV1"
               android:layout_height="wrap_content"
               android:layout_width="fill_parent"
               android:text="Enter the class:">

<spinner
              android:drawselectorontop="true"
              android:entries="@array/items"
              android:id="@+id/spinner1"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:prompt="@string/prompt">
<spinner android:drawselectorontop="true"
              android:id="@+id/spinner2"
              android:layout_height="wrap_content"
              android:layout_width="fill_parent"
              android:prompt="@string/Face">

</spinner>
</spinner>
</textview>
</linearlayout>


NOW edit the string file and insert the following into it :


<string-array name="items">
    <item>ItemA</item>
    <item>ItemB</item>
    <item>ItemC</item>
    <item>ItemD</item>
    <item>ItemE</item>
    <item>ItemF</item>
    <item>ItemG</item>
    <item>ItemH</item>
    <item>ItemI</item>
    </string-array>


<string name="prompt">Select from categories</string>
<string name="Face">SELECT THIS</string>



Now create the java class and update the following in that java file....

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Spinner;
import android.widget.Toast;






public class SpinnerDemoActivity extends Activity implements AdapterView.OnItemSelectedListener {




/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.spinnerlayout);
//TextView selection= (TextView)findViewById(R.id.TV1);
Spinner spin=(Spinner)findViewById(R.id.spinner2);


Toast.makeText(this, "Redirecting to the Fourth Tutorial", Toast.LENGTH_LONG).show();


ArrayAdapter aa=ArrayAdapter.createFromResource(this, R.array.types, android.R.layout.simple_spinner_dropdown_item);


spin.setAdapter(aa);
}
public void onItemSelected(AdapterView arg0, View arg1, int arg2,long arg3) {
// TODO Auto-generated method stub


}
public void onNothingSelected(AdapterView arg0) {
// TODO Auto-generated method stub


}
}