Showing posts with label Android. Show all posts
Showing posts with label Android. Show all posts

Saturday, August 9, 2014

How to host a website on your Android device

,

android_stock6_720w
As you may already know, there are plenty of hosting companies out there, some expensive, some not. Some are dependable, some have terrible up time, and some simply shouldn’t be allowed to offer hosting. Even after looking through a list of some of the best options, finding the perfect hosting company can be a rather difficult thing to do. So why not host your site yourself?

Thanks to your Android device’s capabilities you can host a website by yourself. Note this is not meant to be the perfect solution for anyone and everyone. But, it’s helpful for developers who want to test their website or people who want to learn and test capabilities of their Android.
Here’s how to host a website on your Android device:

Setting up the Server

1. Install and run KSWEB on your Andriod device. The app allows a 6-day free trial, after which you will be required to pay $2.99 for the full version in-order to keep your site running. Other alternatives include My FTP Server (Free) and Pixel Media Server (Free). For the purpose of this article we will use KSWEB.

Screenshot_2014-04-02-01-52-00

2. Once opened, locate the IP Address given on the KSWEB and access it through your computer, or access the ‘localhost:8080′ link through your phone.

Screenshot_2014-04-02-01-56-10

3. Go to “Tools” and click on Start, your browser should now open. The default username and password is ‘admin’ for both.

 
4. (Optional but Recommended) Select ‘system settings’ and change password from ‘admin’ to something else. Do this again for MySQL. Go on KSWEB settings and enter a MySQL Password.

Screenshot_2014-04-03-18-19-48

Making your Server accessible though the Web

1. Enter your router’s IP Address in a browser to access it’s admin page.
Untitled

2. Create a Static IP available for your Android Device. Access your DHCP settings and select an IP range that your router allows you to use.
Untitled3

3. Access ‘port forwarding’ and change the ports to 80 and 8080. This allows your router to communicate with your Android device and it’s server.
untitles 3
4. Access the WiFi settings on your Android Device, and long press the name of your WiFi network until you see the pop-up. Click ‘Modify network,’ and check advanced settings. Change IP Settings to Static and IP Address to the Static IP set in step 2.

Screenshot_2014-04-02-02-36-35
5. Find out your IP Address. An easy way to this is by Googling “What is my IP.” People will use the IP Address to access your site so make sure to capture it correctly.

Untitled1
For now your site will show blank; you have to give out IP to people in-order for them to access your site. Also, your Android device has to be on at all times in order for this to work.
By installing Keep Screen On Free you can allow your server to be on almost always. As for the content of your site, install FTP Server, you can now add files to your site after you change the ports for FTP. This method obviously doesn’t even come close to some of the best hosting services available, but it’s “free” and simple to set-up.
Have you hosted a website from your Android before? Did we miss anything? What would you recommend to others hoping to do something similar?

(androidguys)

Friday, August 8, 2014

Hidden codes for your Android device you probably didn't know

,


Baked deep into Android is a bunch of secret codes that will enable of a variety of statistics and settings on your device that you probably didn’t know existed, as well as some diagnostic tests you can perform on the hardware.
These settings are not enabled by default, and that’s for a reason. Take care when using these and we’re not responsible if you mess your device up.
1. Get information from your phone and battery, including usage and statistics.
*#*#4636#*#*
2. Factory format your device
*2767*3855#
3. Get information your camera
*#*#34971539#*#*
4. Open a file copy screen to backup your media
*#*#273283*255*663282*#*#*
5. Enter service mode
*#*#197328640#*#*
6. Perform various tests on your device
*#*#232339#*#*- WLAN test
*#*#232338#*#* - Shows WiFi MAC address
*#*#1472365#*#* - GPS test
*#*#1575#*#* - Another GPS test
*#*#232331#*#* - Bluetooth test
*#*#2664#*#* – Touch screen test
Drop us a comment in the section below if you found out any useful information about your device that you didn’t know before by using these codes.

Saturday, September 21, 2013

Top 5 Android App That Make your Smartphone A Wireless Webcam

,
Smart Phone have a great innovation of 21 century.Its Change our life style.Now more and more updation make our life more and more success. We Use Android Smartphone. Android Phone is Best smartphone for me Because It has a losts of app store with free and premium app. You can download apps from google play store . But many times we don’t know which app is best. If you are looking for Android App that make your android phone a wireless webcam. Then here we publish top 5 Best Android Apps that make your Smartphone a Wireless Webcam.

Smartphone camera

Monday, July 1, 2013

Don’t Need to Install a Task Manager: How to Manage Running Apps on Android

,

android-robot

Google Play is full of task managers for Android. These utilities can show you apps running in the background, kill running apps, and otherwise manage your apps — but you don’t need to install any third-party software to do this.
We’ll show you how to quickly and easily kill and manage your running apps using only the software included with your Android phone. Third-party task managers are unnecessary and many include harmful features, like task killers.

Automatic Task Killing

Task managers and task killers are often one and the same. A task killer promises to speed up your phone by automatically killing apps running in the background. It stays running in the background, automatically removing apps from memory when you’re done using them.
However, we’ve already explained why you shouldn’t use a task killer on Android. In a nutshell, Android doesn’t manage processes like Windows does. Properly behaved apps running in the background aren’t actually doing anything — they’re just remaining in memory and using no CPU or other resources. When you access them again, they’ll quickly open, as they’re waiting in memory for you to return. If they were removed from memory, they would take longer to re-open as their data would have to be transferred from system storage back into RAM — in this way, a task killer can actually slow things down.
Android includes its own automatic task killer — if its memory fills up and it needs more memory for other reasons, it will automatically kill running apps, removing them from memory. You don’t have to install any software to take advantage of this.


End a Running App — The Easy Way

If you would like to manually close an app and remove it from memory, this is extremely easy on the latest versions of Android — Android 4.0 and up.
First, open the multitasking screen. On a Nexus 4 or Galaxy Nexus, simply press the dedicated multitasking button. On a phone without a multitasking button, like a Galaxy S4 or HTC One, you may have to long-press or double tap the home button to open this screen.
Next, swipe a recent app to the left or right of the screen and its thumbnail will disappear. In addition to removing this app from the list of recent apps, cleaning up the list, Android will also remove the app from memory.

Killing an app is generally not necessary. However, it can help if the app is misbehaving — killing and re-opening the app may cause it to work properly.

End a Running App — The Hard Way

You can also end running apps from Android’s settings screen. First, open the Settings screen and tap the Apps category.

Scroll down in the list, select an app, and tap the Force stop button to end the app’s running process and remove it from memory.


Managing Apps

From the app’s info screen where the Force Stop button is, you can also prevent the apps from showing notifications, view the amount of storage the app is using, clear its data or cache, prevent it from being a default application if it’s set as a default app, and view its permissions.

View Apps Running in the Background

From the Apps settings pane, you can also swipe over to the Running category to view apps that are running in the background. These apps aren’t just remaining in memory. They may start automatically at boot and stay running in the background so they can automatically do things. For example, chat apps like WhatsApp may remain running in the background so they can automatically receive messages.
If you would like to prevent these apps from running in the background, your best bet is to uninstall them from your phone — you can end their tasks, but they’ll just restart anyway.
Note that these apps may use very few resources, so you shouldn’t necessarily worry if they’re running in the background. If they’re running in the background and you don’t see them consuming much memory, battery, or network resources, they’re safe to leave alone.


Tap the Show Cached Processes option at the top-right corner of the screen to view apps that are cached in memory, but aren’t actually running in the background.
You can end such apps’ processes from here by tapping them and tapping Stop, but there should be no reason to do so.

View Memory Use

At the bottom of the Running apps screen, you’ll see a RAM meter. This shows you how much of your phone’s memory is being used and how much is free. Note that this can be misleading — your RAM may appear to be rather full, but it may just be full of cached applications. This will speed things up later — it’s good that your RAM is full, as Android is using your RAM as a cache to speed things up.

The Running apps screen also displays the memory used by running services and cached processes, so you can identify which apps are the most RAM-hungry.

View Battery Use

To view app-specific battery use, open the Settings screen and tap the Battery option. You’ll see a list of phone functions and apps, sorted by how much they’ve used your battery. This screen can help you see how apps are using your CPU and other resources. You’ll probably see apps you frequently use near the top of this list. If you see an app you don’t use here, it’s likely consuming resources in the background — you’ll want to uninstall it if you don’t use it.


The various task management features in Android should be more than enough for most people. The biggest feature available in third-party task manager apps is the task killer, but you shouldn’t need to automatically kill applications. Android will do that for you when it’s actually necessary.

Sunday, July 22, 2012

OptionMenu and ActionBar

,

ActionBar

The ActionBar is located at the top of the Activity that may display the Activity title, navigation modes, and other interactive items.
The following picture show the ActionBar of a typical Google Application with interactive items and a nagivation bar.

ActionBar Screenshot

OptionsMenu

The application can also open a menu which shows actions via a popup menu. This OptionsMenu is only available if the phone has a hardware "Options" button. Even if the hardware button is available, it is recommended to use the ActionBar, which is available for phones as of Android 4.0.
The following picture highlights the hardware button and the resulting menu as popup.

Old OptionsMenu

One of the reasons why the ActionBar is superior to the OptionsMenu, if that it is clearly visible, while the OptionsMenu is only shown on request and the user may not recognize that options are available.

Creating the menu

The OptionsMenu and the ActionBar is filled by the onCreateOptionsMenu() method of your Activity.
In the onCreateOptionsMenu() method you can create the menu entries. You can add menu entries via code or via the inflation of an existing XML resources.
The MenuInflator class allows to inflate menu entries defined in XML to the menu. MenuInflator can get accessed via the getMenuInflator() method in your Activity.
The onCreateOptionsMenu() method is only called once. If you want to influence the menu later you have to use the onPrepareOptionsMenu() method. onPrepareOptionsMenu() is not called for entries in the ActionBar for these entries you have to use the invalidateOptionsMenu() method.

Reacting to menu entry selection

If a menu entry is selected then the onOptionsItemSelected() method is called. As parameter you receive the menu entry which was selected so that you can react differently to different menu entries.

Using the home icon

The ActionBar also shows an icon of your application. You can also add an action to this icon. If you select this icon the onOptionsItemSelected() method will be called with the value android.R.id.home. The recommendation is to return to the main Activity in your program.

// If home icon is clicked return to main Activity
case android.R.id.home:
 Intent intent = new Intent(this, OverviewActivity.class);
 intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
 startActivity(intent);
 break; 

ActionBar tabs

It is also possible to add tabs to the ActionBar which can be used for navigation. Typically Fragments are used for this purpose. We demonstrate this in the Fragments chapter.

Custom Views in the ActionBar

You can also add a custom View to the ActionBar. The following code snippet demonstrates that.

@Override
public void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.main);
 
 ActionBar actionBar = getActionBar();
 // add the custom view to the action bar
 actionBar.setCustomView(R.layout.actionbar_view);
 actionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_CUSTOM
  | ActionBar.DISPLAY_SHOW_HOME);
} 

Contextual action mode

A contextual action mode activates a temporary ActionBar that overlays the application ActionBar for the duration of a particular sub-task.
The contextual action mode is typically activated by selecting an item or by long clicking on it.
To implemented this, call the startActionMode() method on a View or on your Activity. This method gets an ActionMode.Callback object which is responsible for the lifecycle of the contextual ActionBar.

Context menus

You can also assign a context menu to a View. A context menu is also activated if the user "long presses" the view.
If possible the contextual action mode should be preferred over a context menu.
A context menu for a view is registered via the registerForContextMenu(view) method. The onCreateContextMenu() method is called every time a context menu is activated as the context menu is discarded after its usage. The Android platform may also add options to your View, e.g. EditText provides context options to select text, etc.


===   Tutorial: ActionBar   ===

Project


Create a project called "de.vogella.android.socialapp" with the Activity called OverviewActivity.

Add a menu XML resource

Select your project, right click on it and select NewOtherAndroidAndroid XML File to create a new XML resource.
Select the Menu option, enter mainmenu.xml as filename and press the Finish button.

Creating a new XML resource for the menu

This will create a new mainmenu.xml file in the res/menu folder of your project. Open this file and select the Layout tab of the Android editor.
Press the Add button and select the Item entry. Maintain a entry similar to the following screenshot. Via the ifRoom attribute you define that the menu entry is displayed in the ActionBar if there is sufficient space available.

How to maintain the menu entries in an menu xml file

Add a similar entry to the menu with the ID attribute set to "@+id/menuitem2", and the Title attribute set to "Test". Also set the ifRoom flag.
The resulting XML will look like the following.

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >

    <item
        android:id="@+id/menuitem1"
        android:showAsAction="ifRoom"
        android:title="Prefs">
    </item>
    <item
        android:id="@+id/menuitem2"
        android:showAsAction="ifRoom"
        android:title="Test">
    </item>

</menu> 

Change your OverviewActivity class to the following.

package de.vogella.android.socialapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class OverviewActivity extends Activity {
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mainmenu, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  switch (item.getItemId()) {
  case R.id.menuitem1:
   Toast.makeText(this, "Menu Item 1 selected", Toast.LENGTH_SHORT)
     .show();
   break;
  case R.id.menuitem2:
   Toast.makeText(this, "Menu item 2 selected", Toast.LENGTH_SHORT)
     .show();
   break;

  default:
   break;
  }

  return true;
 }
} 

Run your application. As there is enough space in the ActionBar otherwise you may see the Overflow menu or you have to use the Option menu button on your phone. If you select one item, you should see a small info message.

Social App running

Tutorial: Using the contextual action mode

Add a EditText element your main.xml layout file.

<?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" >

    <EditText
        android:id="@+id/myView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:ems="10" >

        <requestFocus />
    </EditText>

</LinearLayout> 

Create a new menu XML resource with the file name "contextual.xml"

<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
    <item
        android:id="@+id/toast"
        android:title="Toast">
    </item>

</menu> 

Change your Activity to the following.

package de.vogella.android.socialapp;

import android.app.Activity;
import android.os.Bundle;
import android.view.ActionMode;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;

public class OverviewActivity extends Activity {
 protected Object mActionMode;
 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.main);
  // Define the contextual action mode
  View view = findViewById(R.id.myView);
  view.setOnLongClickListener(new View.OnLongClickListener() {
   // Called when the user long-clicks on someView
   public boolean onLongClick(View view) {
    if (mActionMode != null) {
     return false;
    }

    // Start the CAB using the ActionMode.Callback defined above
    mActionMode = OverviewActivity.this
      .startActionMode(mActionModeCallback);
    view.setSelected(true);
    return true;
   }
  });
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  MenuInflater inflater = getMenuInflater();
  inflater.inflate(R.menu.mainmenu, menu);
  return true;
 }

 @Override
 public boolean onOptionsItemSelected(MenuItem item) {
  Toast.makeText(this, "Just a test", Toast.LENGTH_SHORT).show();
  return true;
 }

 private ActionMode.Callback mActionModeCallback = new ActionMode.Callback() {

  // Called when the action mode is created; startActionMode() was called
  public boolean onCreateActionMode(ActionMode mode, Menu menu) {
   // Inflate a menu resource providing context menu items
   MenuInflater inflater = mode.getMenuInflater();
   // Assumes that you have "contexual.xml" menu resources
   inflater.inflate(R.menu.contextual, menu);
   return true;
  }

  // Called each time the action mode is shown. Always called after
  // onCreateActionMode, but
  // may be called multiple times if the mode is invalidated.
  public boolean onPrepareActionMode(ActionMode mode, Menu menu) {
   return false; // Return false if nothing is done
  }

  // Called when the user selects a contextual menu item
  public boolean onActionItemClicked(ActionMode mode, MenuItem item) {
   switch (item.getItemId()) {
   case R.id.toast:
    Toast.makeText(OverviewActivity.this, "Selected menu",
      Toast.LENGTH_LONG).show();
    mode.finish(); // Action picked, so close the CAB
    return true;
   default:
    return false;
   }
  }

  // Called when the user exits the action mode
  public void onDestroyActionMode(ActionMode mode) {
   mActionMode = null;
  }
 };

} 

If you run this example and long press the EditText widget, your contextual ActionBar is displayed.

Contextual ActionBar demonstrated

(vogella.com)

Your first Android project

,

Install the demo application

This application is also available on the Android Marketplace under Android Temperature converter .
Alternatively you can also scan the following barcode with your Android smartphone to install it via the Google Play application.

QR Code to install the Android Temperature converter

Create Project

Select FileNewOtherAndroidAndroid Project and create the Android project "de.vogella.android.temperature". Enter the following.

New Android Project Wizard


New Android Project Wizard - Android Target


New Android Project Wizard - Package Definition

Press "Finish". This should create the following directory structure.

Android Project Structure

Modifying resources

As described in the Android Development Tools (ADT) chapter, ADT provides specialized editors for resources files, e.g. layout files. These editors allow to switch between the XML representation of the file and a richer user interface via tabs on the bottom of the editor.
The following description uses the rich user interface to build layout files. For validation purposes, the resulting XML is also included in the description.

Create attributes

Android allows you to create static attributes, e.g. Strings or colors. These attributes can for example be used in your XML layout files or referred to via Java source code.
Select the file "res/values/string.xml" and press the Add button. Select "Color" and enter "myColor" as the name and "#3399CC" as the value.

Adding Android Attributes


Details for a String

Add the following "String" attributes. String attributes allow the developer to translate the application at a later point.


Table 1. String Attributes
Name Value
celsius to Celsius
fahrenheit to Fahrenheit
calc Calculate


Switch to the XML representation and validate that the values are correct.

<?xml version="1.0" encoding="utf-8"?>
<resources>
 <string name="hello">Hello World, Convert!</string>
 <string name="app_name">Temperature Converter</string>
 <color name="myColor">#3399CC</color>
 <string name="myClickHandler">myClickHandler</string>
 <string name="celsius">to Celsius</string>
 <string name="fahrenheit">to Fahrenheit</string>
 <string name="calc">Calculate</string>
</resources> 

Add Views

Select "res/layout/main.xml" and open the Android editor via a double-click. This editor allows you to create the layout via drag and drop or via the XML source code. You can switch between both representations via the tabs at the bottom of the editor. For changing the position and grouping elements you can use the Eclipse "Outline" view.
The following shows a screenshot of the "Palette" view from which you can drag and drop new user interface components into your layout. Please note that the "Palette" view changes frequently so your view might be a bit different.

Palette for the Android Layout Editor


You will now create your new layout.
Right-click on the existing text object “Hello World, Hello!” in the layout. Select "Delete" from the popup menu to remove the text object. Then, from the “Palette” view, select Text Fields and locate "Plain Text". Drag this onto the layout to create a text input field. All object types in the section "Text Fields” derive from the class "EditText", they just specify via an additional attribute which text type can be used.
Afterwards select the Palette section "Form Widgets" and drag a “RadioGroup” object onto the layout. The number of radio buttons added to the radio button group depends on your version of Eclipse. Make sure there are two radio buttons by deleting or adding radio buttons to the group.
From the Palette section Form Widgets, drag a Button object onto the layout.
The result should look like the following.


Current layout of main.xml

Switch to main.xml and verify that your XML looks like the following.

<?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" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:text="EditText" >
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="RadioButton" >
        </RadioButton>

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="RadioButton" >
        </RadioButton>
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="Button" >
    </Button>

</LinearLayout> 

Edit View properties

If you select a user interface component (an instance of View), you can change its properties via the Eclipse "Properties" view. Most of the properties can be changed via the right mouse menu. You can also edit properties of fields directly in XML. Changing properties in the XML file is much faster, if you know what you want to change. But the right mouse functionality is nice, if you are searching for a certain property.
Open your main.xml layout file . The EditText control shows currently a default text. We want to delete this initial text in the XML code. Switch to the XML tab called main.xml and delete the android:text="EditText" property from the EditText part. Switch back to the "Graphical Layout" tab and check that the text is removed.
Use the right mouse click on the first radio button to assign the "celsius" String attribute to its "text" property. Assign the "fahrenheit" string attribute to the second radio button.

Change the text property of the radio button


Selection of the right text from the pre-defined string values

From now on, I assume you are able to use the properties menu on user interface components. You can always either edit the XML file or modify the properties via right mouse click.
Set the property "Checked" to true for the first RadioButton.
Assign "calc" to the text property of your button and assign "myClickHandler" to the onClick property.
Set the "Input type" property to "numberSigned" and "numberDecimal" on your EditText.
All your user interface components are contained in a LinearLayout. We want to assign a background color to this LinearLayout. Right-click on an empty space in Graphical Layout mode, then select Other PropertiesAll by NameBackground. Select “Color” and then select "myColor" "in the list which is displayed.

New look of the layout after the changes

Switch to the main.xml tab and verify that the XML is correct.

<?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:background="@color/myColor"
    android:orientation="vertical" >

    <EditText
        android:id="@+id/editText1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:inputType="numberDecimal|numberSigned" >
    </EditText>

    <RadioGroup
        android:id="@+id/radioGroup1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content" >

        <RadioButton
            android:id="@+id/radio0"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:checked="true"
            android:text="@string/celsius" >
        </RadioButton>

        <RadioButton
            android:id="@+id/radio1"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/fahrenheit" >
        </RadioButton>
    </RadioGroup>

    <Button
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:onClick="myClickHandler"
        android:text="@string/calc" >
    </Button>

</LinearLayout> 

Change the Activity source code

During the generation of your new Android project you specified that an Activity called ConvertActivity should be created. The project wizard created the corresponding Java class.
Change your code in ConvertActivity.java to the following. Note that the myClickHandler will be called based on the OnClick property of your button.

package de.vogella.android.temperature;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

public class ConvertActivity extends Activity {
  private EditText text;

  @Override
  public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);
    text = (EditText) findViewById(R.id.editText1);

  }

  // This method is called at button click because we assigned the name to the
  // "On Click property" of the button
  public void myClickHandler(View view) {
    switch (view.getId()) {
    case R.id.button1:
      RadioButton celsiusButton = (RadioButton) findViewById(R.id.radio0);
      RadioButton fahrenheitButton = (RadioButton) findViewById(R.id.radio1);
      if (text.getText().length() == 0) {
        Toast.makeText(this, "Please enter a valid number",
            Toast.LENGTH_LONG).show();
        return;
      }

      float inputValue = Float.parseFloat(text.getText().toString());
      if (celsiusButton.isChecked()) {
        text.setText(String
            .valueOf(convertFahrenheitToCelsius(inputValue)));
        celsiusButton.setChecked(false);
        fahrenheitButton.setChecked(true);
      } else {
        text.setText(String
            .valueOf(convertCelsiusToFahrenheit(inputValue)));
        fahrenheitButton.setChecked(false);
        celsiusButton.setChecked(true);
      }
      break;
    }
  }

  // Converts to celsius
  private float convertFahrenheitToCelsius(float fahrenheit) {
    return ((fahrenheit - 32) * 5 / 9);
  }

  // Converts to fahrenheit
  private float convertCelsiusToFahrenheit(float celsius) {
    return ((celsius * 9) / 5) + 32;
  }
} 

Start Project

To start the Android Application, select your project, right click on it, and select Run-AsAndroid Application. If an emulator is not yet running, it will be started. Be patient, the emulator starts up very slowly.
You should get the following result.

The running application in the emulator

Type in a number, select your conversion and press the button. The result should be displayed and the other option should get selected.


Starting an installed application

After you run your application on the virtual device, you can start it again on the device. If you press the "Home" button you can select your application.

How to select your application from the Android home menu


Selecting the application from the application choicer
(vogella.com)

Saturday, July 21, 2012

Error handling and typical problems

,
Things are not always working as they should. This section gives an overview over typical problems and how to solve them.

Clean Project

Several users report that they get the following errors:
  1. Project ... is missing required source folder: 'gen'
  2. The project could not be built until build path errors are resolved.
  3. Unable to open class file R.java.

To solve any of these errors, go to the project menu and select ProjectClean.

Problems with Android Debug Bridge (adb)

The communication with the emulator or your Android device might have problems. This communication is handled by the Android Debug Bridge (adb).
Eclipse allows to reset the adb in case this causes problems. Select therefore the DDMS perspective via WindowOpen PerspectiveOtherDDMS
To restart the adb, select the "Reset adb" in the Device View.


LogCat

The LogCat view shows you the log messages of your Android device and helps you to analyze problems. For example Java exceptions in your program would be shown here. To open this view, select WindowShow ViewOtherAndroidLogCat.

Java7

If Android complains that you cannot use Java7 select your right-click on your project and select the Java Compiler entry. Ensure that Java 1.5 or Java 1.6 is used. You may have to select the Enable project specific settings checkbox.

Java compiler settings

Eclipse reports file from R.java as missing

Sometimes Eclipse complains that a file, e.g. R.layout.main cannot be found. Check in your source code that you did not import android.R. An android.R import will prevent Eclipse from finding your R file.

Android editor not opened

Android provides nice editors to edit Android resource files, unfortunately these editor are not always automatically used due to bugs in the ADT. If that happens, you can open this editor manually. Right-click on your menu file and select Open withAndroid Menu Editor.

Emulator does not start

If your emulator does not start, make sure that the android-sdk version is in a path without any spaces in the path name.

Timeout during deployment

If you face timeout issues during deployment you can increase the default timeout in the Eclipse preferences. Select WindowPreferencesAndroidDDMS and increase the ADB connection timeout (in ms) value.

Installation failed due to insufficient storage

Sometimes the emulator will refuse to install an application with the error message: INSTALL_FAILED_INSUFFICIENT_STORAGE.
An Android virtual device provides by default only 64M for the storaging Android applications. You can clean your installed application by re-starting the emulator and selecting the Wipe user data " flag.
Alternatively you can set the data partition size. If you press edit on the AVD, you can set the Ideal size of data partition property via the New button.

Setting the Ideal size of data partition for the ADV

Debug Certificate expired

If you get the error message Debug Certificate expired switch to the folder which contains the Android AVD, e.g. .android under Linux and delete the debug.keystore file. This file is only valid for a year and if not present, Eclipse will regenerate the password.

Error message for @Override

The @Override annotation was introduced in Java 1.6. If you receive an error message for @Override, change the Java compiler level to Java 1.6. To do this, right-click on the project, select PropertiesJava Compiler Compiler compliance level and select 1.6 in the drop-down box.

Missing Imports

Java requires that classes which are not part of the standard Java Language are either fully qualified or declared via imports.
If you see an error message with the XX cannot be resolved to a variable text, right-click in your Editor and select Source Organize Imports to important required packages.
 

Android Development Tutorials Copyright © 2011 -- Template created by O Pregador -- Powered by Blogger Templates