Sunday, 3 March 2013

Dialog Fragments

Honeycomb introduced Fragments to support reusing portions of UI and logic across multiple activities in an app. In parallel, the showDialog / dismissDialog methods in Activity are being deprecated in favor of DialogFragments.

In this post, I’ll show how to use DialogFragments with the v4 support library (for backward compatibility on pre-Honeycomb devices) to show a simple edit dialog and return a result to the calling Activity using an interface.
 For design guidelines around Dialogs, see the Android Design site.

The Layout

Here’s the layout for the dialog in a file named fragment_edit_name.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/edit_name"
    android:layout_width="wrap_content" android:layout_height="wrap_content"
    android:layout_gravity="center" android:orientation="vertical"  >

    <TextView
        android:id="@+id/lbl_your_name" android:text="Your name" 
        android:layout_width="wrap_content" android:layout_height="wrap_content" />
      
    <EditText
        android:id="@+id/txt_your_name"
        android:layout_width="match_parent"  android:layout_height="wrap_content" 
        android:inputType=”text”
        android:imeOptions="actionDone" />
</LinearLayout>
Note the use of two optional attributes. In conjunction with android:inputType=”text”,android:imeOptions=”actionDone” configures the soft keyboard to show a Done key in place of the Enter key.

The Dialog Code

The dialog extends DialogFragment, and since we want backward compatibility, we’ll import it from the v4 support library. (To add the support library to an Eclipse project, right-click on the project and choose Android Tools | Add Support Library).
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialog extends DialogFragment {

    private EditText mEditText;

    public EditNameDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_name, container);
        mEditText = (EditText) view.findViewById(R.id.txt_your_name);
        getDialog().setTitle("Hello");

        return view;
    }
}
The dialog extends DialogFragment and includes the required empty constructor. Fragments implement theonCreateView() method to actually load the view using the provided LayoutInflater.

Showing the Dialog

Now we need some code in our Activity to show the dialog. Here is a simple example that immediately shows the EditNameDialog to enter the user’s name. On completion, it shows a Toast with the entered text.
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
// ...
public class FragmentDialogDemo extends FragmentActivity implements EditNameDialogListener {

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        showEditDialog();
    }

    private void showEditDialog() {
        FragmentManager fm = getSupportFragmentManager();
        EditNameDialog editNameDialog = new EditNameDialog();
        editNameDialog.show(fm, "fragment_edit_name");
    }

    @Override
    public void onFinishEditDialog(String inputText) {
        Toast.makeText(this, "Hi, " + inputText, Toast.LENGTH_SHORT).show();
    }
}
There are a few things to notice here. First, because we’re using the support library for backward compatibility with the Fragment API, our Activity extends FragmentActivity from the support library. Because we’re using the support library, we callgetSupportFragmentManager() instead of getFragmentManager().
After loading the initial view, the activity immediately shows the EditNameDialog by calling its show() method. This allows the DialogFragment to ensure that what is happening with the Dialog and Fragment states remains consistent. By default, the back button will dismiss the dialog without any additional code.

Using the Dialog

Next, let’s enhance EditNameDialog so it can return a result string to the Activity.
import android.support.v4.app.DialogFragment;
// ...
public class EditNameDialog extends DialogFragment implements OnEditorActionListener {

    public interface EditNameDialogListener {
        void onFinishEditDialog(String inputText);
    }

    private EditText mEditText;

    public EditNameDialog() {
        // Empty constructor required for DialogFragment
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        View view = inflater.inflate(R.layout.fragment_edit_name, container);
        mEditText = (EditText) view.findViewById(R.id.txt_your_name);
        getDialog().setTitle("Hello");

        // Show soft keyboard automatically
        mEditText.requestFocus();
        getDialog().getWindow().setSoftInputMode(
                LayoutParams.SOFT_INPUT_STATE_VISIBLE);
        mEditText.setOnEditorActionListener(this);

        return view;
    }

    @Override
    public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
        if (EditorInfo.IME_ACTION_DONE == actionId) {
            // Return input text to activity
            EditNameDialogListener activity = (EditNameDialogListener) getActivity();
            activity.onFinishEditDialog(mEditText.getText().toString());
            this.dismiss();
            return true;
        }
        return false;
    }
}
For user convenience, we programmatically focus on the EditText with mEditText.requestFocus(). Alternatively, we could have used the <requestFocus/> tag in the layout XML to do this; however, in some cases it’s preferable to request focus programmatically. For example, an OnFocusChangeListener added in the Fragment’s onCreateView() method won’t get called if you request focus in the layout XML.
If the user focuses on an EditText, the soft keyboard will automatically appear. In order to force this to happen with our programmatic focus, we call getDialog().getWindow().setSoftInputMode(). Note that many Window operations you might have done previously in a Dialog can still be done in a DialogFragment, but you have to callgetDialog().getWindow() instead of just getWindow(). The resulting dialog is shown on both a handset and tablet (not to scale):

The onEditorAction() method handles the callback when the user presses the Done key. It gets invoked because we’ve set an OnEditorActionListener on the EditText. It calls back to the Activity to send the entered text. To do this, EditNameDialog declares an interface EditNameDialogListener that is implemented by the Activity. This enables the dialog to be reused by many Activities. To invoke the callback method onFinishEditDialog(), it obtains a reference to the Activity which launched the dialog by calling getActivity(), which all Fragments provide, and then casts it to the interface type. In MVC architecture, this is a common pattern for allowing a view to communicate with a controller.
We can dismiss the dialog one of two ways. Here we are calling dismiss() within the Dialog class itself. It could also be called from the Activity like the show() method.

Wednesday, 27 February 2013

How To Create New Java Project In Eclipse

Step1: 

File >> New >> Java Project



Step2:

Give the project name and click next button.




Step3:

Click finish button.
Then create class file in the src folder and enjoy coding.

Sunday, 24 February 2013

how to create new android project in eclipse





Step 1. File >> New >> Project





Step 2. Select the option Android Application Project.
And click next button.




Step 3. a) Type Application Name, Project Name and Package Name.

b) Select Minimum Required SDK, Target SDK  Compile With and Theme According to your project requirements.

c) Click next button.




Step 4. Choose your project storage location where you want to store. And click next button.



Step 5. Give your project an icon. And click next button.




Step 6. Choose an activity kind. And Click next button.




Step 7. Give activity name and layout name.
Click finish to finally create a new blank android project.




 Enjoy Coding...


Saturday, 23 February 2013

Unlock Root - An Utility That Roots Almost Any Android Smartphone

Rooting is the process of gaining administrative (superuser) access on your handset which allows you to customize and optimize your device according to your needs. If you are using an android smartphone, then you are likely to consider rooting your phone because you want to optimize this and that on your device. This is where Unlock Root steps in. Unlock Root supports over 200 smartphones running Android version 2.1, 2.2 and 2.3. This handy software comes as small setup package for Windows XP, Vista and Windows7 platform that can be installed easily with one click. After it is installed on your PC, this utility will let you root almost any android device within just few clicks.



UnlockRoot is probably the easiest way to get root access on almost any android smartphone. It supports rooting of many handsets including HTC Sensation, Galaxy Nexus (i9250), LG Optimus 3D (P920), HTC Wildfire S, Galaxy Note (I9220), LG Optimus 2x, HTC Desire S, Galaxy S II (I9100), LG Optimus LET, HTC Incredible S, Galaxy S (I9000), LG Optimus Black , Galaxy Mini GT-S5570Galaxy Ace GT-S5830 and many others from various manufacturers such as Samsung, Sony Ericsson, LG, ZTE, HTC, Lenovo, Acer, Huawei, Motorola, etc. If you doubt your device is supported or not then you can view a complete list of supported devices here. Also, even if you device is not listed in the list above, you still can give this a try since it is claimed to be safe and there's a high probability you'll get your device rooted.

Well then, without further delay let's move on to rooting your android handset. If you are still unsure about rooting your android smartphone then read this article where I've pointed out some of the advantages and disadvantages of Rooting. Else, follow the simple instructions from below carefully and your device shall be rooted in no time.

UPDATE!!! 2012/11/08
Unlock root version 3.0 has been released with major improvements and support for a lot of new devices. New Unlock Root version 3.0 now supports rooting of Android 4.0.0, 4.0.1, 4.0.2 and 4.0.3 versions along with regular 2.2 and 2.3 versions. With this latest version support for Samsung Galaxy Note 2, Galaxy S3 (with Android 4.1.1),Samsung Galaxy Note, Galaxy S2, Amazon's Kindle fire and many more devices have been added. Below is a short change-log of the latest update since version 2.3.1.



Update Log : v3.0.0
  • Support for Samsung Galaxy Note2/SIII 4.1.1
  • Support Samsung Galaxy Note/SII 4.0.3
  • Support Samsung Amazon Kindle Fire 6.3.1
  • Support Android 2.2-2.3,4.0.0-4.0.3
  • Add Kernel Root, support Samsung Galaxy Note/S2/S.
  • Fixed driver inf error.

    Note : With latest version, Unlock root comes as an online installer package so I've included it seperately below. Download it and install and follow the rest of the instructions from below.
    Application Name : UnlockRoot_downloader_by_UnlockRoot.exe
    Size : 151 KB
    Version : v3.0.0
    Release Date : 2012/11/08
    Platform (OS) : WinXP/Vista/Win7
    Download : Link 1 

    Note: This is just a online installer that will download latest Unlockroot and install on your PC. 

    Disclaimer : Rooting Voids Manufacturer's Warranty. Do It At Your Own Risk!

    1. Download Unlock Root (Latest version from above) setup application to your PC or Laptop from below:
      Application Name : UnlockRoot23.exe
      Size : 9 MB
      Version : v2.3.1 
      Release Date : 2012/04/01
      Platform (OS) : WinXP/Vista/Win7
      Download : Link 1  | MediaFire Mirror  
    2. Install the Application on your System.
      • Double Click on UnlockRoot23.exe file.
      • Press Next Button and Click on Install button.
      • Wait till the package installs all required files.
      • When done, make sure Run Unlock Root 2.31 option is checked. 
      • Press on Finish button and Unlock Root will start up.
  1. Once installed, run the Unlock Root application (if it not running already) by navigating to StartMenu=>All Programs=>Unlock Root=>Unlock Root. You will be presented with the following interface.



  2. Now, Enable USB Debugging on your android smartphone.
    • Navigate to Settings=>Applications=>Development in your android smartphone.
    • Check USB Debugging option.
  3. Make sure you have proper drivers installed on your PC. You can install Samsung Kies, which will install the drivers for you. This is needed to make sure your device is recognized by the system.
  4. Connect your smartphone to your PC or Laptop via USB Cable.
  5. Once connected, click on Root Button within the Unlock Root application.
  6. It will then, present you with a Choose a connected device window, which lists all devices connected to the PC.


  7. Select your device from the list and wait for the app to root your handset.
  8. When successfully rooted, it will prompt you to reboot your device. Go ahead and press Yes, so that the changes can take effect.


  9. That's all. Your device is now rooted and you may disconnect it from the PC as well as close theUnlock Root application.
App Drawer with Superuser app.

So, now that you have successfully rooted your android smartphone, you should see a superuser app icon on your App Drawer (as shown in the image beside). This app is used to manage root access to various root applications which you can search and download from Play Store. A list of few root apps that are useful can be found at this article. They are quite useful ones so check them out.



From now on, whenever you run a app that requires root access you will be prompted to Allow or Deny the request by the superuser app. Be careful when allowing the access since some applications might collect important data from your device and send them to their servers without your knowledge (In case you want to be extra cautious about this issue , PDroid Privacy Protection app is your best bet. You can learn more about it here). Also, in case you want to revert back to the original secure state, you can Unroot your device with the same app as mentioned above. Run the app and then click onUnroot button and your device will be unrooted within no time.

How to install Android Sdk for windows,mac and linux.


Download the Android SDK corresponding to the system which you are using
           http://dl.google.com/android/installer_r15-windows.exe

1) Prepare your development computer and ensure it meets the system requirements.
a) Need to install the JDK, if you don't have it already.
    (http://java.sun.com/javase/downloads/index.jsp)

b) If you will be developing in Eclipse with the Android Development Tools (ADT) Plugin
    (http://www.eclipse.org/downloads/)

  The "Eclipse Classic" version is recommended. Otherwise, a Java or RCP version of Eclipse is recommended.



2) Install the SDK starter package .
  a) If you downloaded a .zip or .tgz package (instead of the SDK installer), unpack it to a safe location on your machine. By default, the SDK files are unpacked into a directory named android-sdk-<machine-platform>.

   b) If you downloaded the Windows installer (.exe file), run it now and it will check whether the proper Java SE Development Kit(JDK) is installed (installing it, if necessary), then install the SDK Tools into a default location (which you can modify).

   c) Make a note of the name and location of the SDK directory on your systemyou will need to refe to the SDK directory later, when setting up the ADT plugin and when using the SDK tools from the command line.



3) Install the ADT Plugin for Eclipse (if you'll be developing in Eclipse, You can download the ADT plugin from http://developer.android.com/sdk/eclipse-adt.html#installing).
   a) Follow these steps to download the ADT plugin and install it in your Eclipse environment.

        i) Start Eclipse, then select Help > Install New Software....

        ii) Click Add, in the top-right corner.

        iii) In the Add Repository dialog that appears, enter "ADT Plugin" for the Name and the following URL for the Location: 
        https://dl-ssl.google.com/android/eclipse/
            (If you have trouble acquiring the plugin, try using "http" in the Location URL, instead of "https"(https is preferred for security reasons).

        iv) In the Available Software dialog, select the checkbox next to Developer Tools and click Next.

        v) In the next window, you'll see a list of the tools to be downloaded. Click Next. 

        vi) Read and accept the license agreements, then click Finish. 

  b) Configuring the ADT Plugin - the next step is to modify your ADT preferences in Eclipse to point to the Android SDK directory:

    i) Select Window > Preferences... to open the Preferences panel (Mac OS X: Eclipse > Preferences)

    ii) Select Android from the left panel.

    iii) For the SDK Location in the main panel, click Browse and locate your downloaded SDK director Click Apply, then OK.



4) Add Android platforms and other components to your SDK.
   a) You can launch the Android SDK and AVD Manager in one of the following ways:

        i) From within Eclipse, select Window > Android SDK and AVD Manager.

        ii) On Windows, double-click the SDK Manager.exe file at the root of the Android SDK directory.

        iii) On Mac or Linux, open a terminal and navigate to the tools/ directory in the Android SDK, the execute: android

   b) To download components, use the graphical UI of the Android SDK and AVD Manager to browse the SDK repository and select new or
       updated components, The Android SDK and AVD Manager installs the selected components in your SDK environment