Pages Navigation Menu

Coding is much easier than you think

How do I run an Android emulator automatically from Eclipse?

Posted by on Apr 14, 2013 in Android | 0 comments

 

You might want to know how to run an Android emulator automatically from Eclipse. Here is what you need to do.

Once you have installed ADT, you need to define an ADT launch configuration, in order for your project to execute itself as an Android executable.
 
** UPDATE: Android Complete tutorial now available here.
 

run configurations2

Then, in Eclipse go to Window –>Preferences  –> Run/Debug tab –> Launching.

In the €œLaunch Operation subsection, select €œAlways launch the previously launched application€ as shown below.

preferences_2

 

That is all!!!

Advertisements
Read More

Relative layout in android

Posted by on Apr 14, 2013 in Android | 0 comments

 

 

Relative layout, in general is used to organize elements based on their relationships with one another, and with the parent container. The arrangement of the views can be done using the 3 ways,

  • Relative To Container
  • Relative To Other Elements
  • Alignment With Other Elements

Relative to Container

  • android:layout_alignParentBottom -€“ Places the bottom of the element on the bottom of the container
  • android:layout_alignParentLeft -€“ Places the left of the element on the left side of the container
  • android:layout_alignParentRight -€“ Places the right of the element on the right side of the container
  • android:layout_alignParentTop -€“ Places the element at the top of the container
  • android:layout_centerHorizontal -€“ Centers the element horizontally within its parent container
  • android:layout_centerInParent -€“ Centers the element both horizontally and vertically within its container
  • android:layout_centerVertical -€“ Centers the element vertically within its parent container

Relative to Other Elements

  • android:layout_above – Places the element above the specified element
  • android:layout_below -€“ Places the element below the specified element
  • android:layout_toLeftOf -€“ Places the element to the left of the specified element
  • android:layout_toRightOf –€“ Places the element to the right of the specified element

Alignment with Other Elements

  • android:layout_alignBaseline – Aligns baseline of the new element with the baseline of the specified element
  • android:layout_alignBottom -€“ Aligns the bottom of new element in with the bottom of the specified element
  • android:layout_alignLeft -€“ Aligns left edge of the new element with the left edge of the specified element
  • android:layout_alignRight -€“ Aligns right edge of the new element with the right edge of the specified element
  • android:layout_alignTop -€“ Places top of the new element in alignment with the top of the specified element
Read More

Navigation Types in new Android SDK Tools

Posted by on Apr 14, 2013 in Android | 0 comments

 
Android SDK Tools Revision 20 also provides various Navigation Types for Blank activity. To use the template of Navigation Type, minimum SDK version of at least 14 is required. The various navigation templates available are as follows
 
** UPDATE: Android Complete tutorial now available here.
 

Tabs

Mostly this type of navigation type is used for action bars.

When you want to provide navigation tabs in an activity, using the action bar’s tabs is a great option (instead of using TabWidget), because the system adapts the action bar tabs for different screen sizes—placing them in the main action bar when the screen is sufficiently wide, or in a separate bar (known as the “stacked action bar”) when the screen is too narrow

navigation_tab


Tabs + Swipe

Mostly this type of navigation type is used for action bars.

navigation_tab+swipe

Swipe Views + Title Strip

Mostly this type of navigation type is used for action bars.

navigation_swipes+tile

Dropdown

Mostly this type of navigation type when action bars are provided with drop down options.

As another mode of navigation (or filtering) within your activity, the action bar offers a built in drop-down list. For example, the drop-down list can offer different modes by which content in the activity is sorted.

navigation_dropdown


Let€™s see how a sample Swipe Views + Title strip looks like when you just create a project using this navigation type and run it as such without editing its activity or source file.

Src : Navigation.java

package com.simplecodestuffs.navigationtype;
import android.app.ActionBar;
import android.app.FragmentTransaction;
import android.content.Context;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

public class MainActivity extends FragmentActivity {

    /**
     * The {@link android.support.v4.view.PagerAdapter} that will provide fragments for each of the
     * sections. We use a {@link android.support.v4.app.FragmentPagerAdapter} derivative, which will
     * keep every loaded fragment in memory. If this becomes too memory intensive, it may be best
     * to switch to a {@link android.support.v4.app.FragmentStatePagerAdapter}.
     */
    SectionsPagerAdapter mSectionsPagerAdapter;

    /**
     * The {@link ViewPager} that will host the section contents.
     */
    ViewPager mViewPager;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        // Create the adapter that will return a fragment for each of the three primary sections
        // of the app.
        mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

        // Set up the ViewPager with the sections adapter.
        mViewPager = (ViewPager) findViewById(R.id.pager);
        mViewPager.setAdapter(mSectionsPagerAdapter);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }

    /**
     * A {@link FragmentPagerAdapter} that returns a fragment corresponding to one of the primary
     * sections of the app.
     */
    public class SectionsPagerAdapter extends FragmentPagerAdapter {

        public SectionsPagerAdapter(FragmentManager fm) {
            super(fm);
        }

        @Override
        public Fragment getItem(int i) {
            Fragment fragment = new DummySectionFragment();
            Bundle args = new Bundle();
            args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, i + 1);
            fragment.setArguments(args);
            return fragment;
        }

        @Override
        public int getCount() {
            return 3;
        }

        @Override
        public CharSequence getPageTitle(int position) {
            switch (position) {
                case 0: return getString(R.string.title_section1).toUpperCase();
                case 1: return getString(R.string.title_section2).toUpperCase();
                case 2: return getString(R.string.title_section3).toUpperCase();
            }
            return null;
        }
    }
    /**
     * A dummy fragment representing a section of the app, but that simply displays dummy text.
     */
    public static class DummySectionFragment extends Fragment {
        public DummySectionFragment() {
        }
        public static final String ARG_SECTION_NUMBER = "section_number";
        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            TextView textView = new TextView(getActivity());
            textView.setGravity(Gravity.CENTER);
            Bundle args = getArguments();
            textView.setText(Integer.toString(args.getInt(ARG_SECTION_NUMBER)));
            return textView;
        }
    }
}

Layout: activity_mainactivity.xml:

<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/pager"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".Navigation" >

    <!--
    This title strip will display the currently visible page title, as well as the page
    titles for adjacent pages.
    -->

    <android.support.v4.view.PagerTitleStrip
        android:id="@+id/pager_title_strip"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_gravity="top"
        android:background="#33b5e5"
        android:paddingBottom="4dp"
        android:paddingTop="4dp"
        android:textColor="#fff" />

</android.support.v4.view.ViewPager>

Output:

output

Read More

An internal error occurred during: “Launching New_configuration”

Posted by on Apr 14, 2013 in Android | 1 comment

 

When you create and try to run a built project, sometimes eclipse will throw the following error,

An internal error occurred during: “Launching New_configuration”. Path for project must have only one segment.

When you go to your run configurations, you can see a screen similar to this,

run configurations

Error log:

!ENTRY org.eclipse.core.jobs 4 2 2011-02-20 12:12:00.397

!MESSAGE An internal error occurred during: “Launching android”.

!STACK 0

java.lang.IllegalArgumentException: Path for project must have only one segment.

at org.eclipse.core.runtime.Assert.isLegal(Assert.java:63)

at org.eclipse.core.internal.resources.WorkspaceRoot

.getProject(WorkspaceRoot.java:181)

at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate

.getProject(Unknown Source)

at com.android.ide.eclipse.adt.internal.launch.LaunchConfigDelegate

.launch(Unknown Source)

at org.eclipse.debug.internal.core.LaunchConfiguration

.launch(LaunchConfiguration.java:853)

at org.eclipse.debug.internal.core.LaunchConfiguration

.launch(LaunchConfiguration.java:703)

at org.eclipse.debug.internal.ui.DebugUIPlugin

.buildAndLaunch(DebugUIPlugin.java:866)

at org.eclipse.debug.internal.ui.DebugUIPlugin$8

.run(DebugUIPlugin.java:1069)

at org.eclipse.core.internal.jobs.Worker.run(Worker.java:55)

Solution:

This is because eclipse sometimes fails to recognize your application/project and that is why it is asking for a new configuration (as shown in the above figure). This cryptic message usually tries to tell you “Please enter a name and project in your run configuration”

  • In such cases, just click on Browse and select your project and click on apply to run your android application.
  • Otherwise, you can rebuild your application and run it again from run configurations. If this error, persists even after the above procedure consider restarting your eclipse try it again, it will work fine.

 

Read More

Importing project gave Unable to resolve target ‘android-7’

Posted by on Apr 14, 2013 in Android | 0 comments

 

When you try to import an already existing android project in your eclipse, sometimes you tend to get the error €œUnable to resolve target ‘android-7€.  This is simple and nothing wrong in your source file or your activity file. The problem is with the SDK version you have specified.

Let us consider I am setting my minimum SDK for my application as 7, so my project properties will have the line target=android-7.

Problem:

Now if you are trying to import this project in another system which doesn’€™t have API 7 then eclipse will throw an error, Unable to resolve target ‘android-7’

Solution:

To do this, you have 2 options:

  • You can download API 7 for your application using SDK manager to run your application only on Android API 7. So you have to set your manifest file with the SDK version as follows.

<uses-sdk

android:minSdkVersion=”8″

android:targetSdkVersion=”8″ />

OR

  • You don’t have the API level 7 downloaded. You can right click the project and go properties->android and select another available API. As long as you don’t change the min sdk version in your manifest you are still targeting as many devices as before.

<uses-sdk

android:minSdkVersion=”8″

android:targetSdkVersion=”15″ />

In the latter case, your application will run on any targeted devices with API ranging above 8 and till API 15.

Read More

Difference between Wrap content and Fill parent/match parent

Posted by on Apr 14, 2013 in Android | 1 comment

 
Wrap content: As the name suggests, it refers to wrapping a widget€™s/view contents within the view. The wrap content can be best explained using button view. For eg: in case of button, Button name is €œwrap€ at first and now you change it to wrapcontent€, the size of the button increases to wrap its content.
 
** UPDATE: Android Complete tutorial now available here.
 
This is due to the reason that the attributes android:layout_width and android:layout_height is set to wrap_content as shown below.

                                wrapcontent_1

wrapcontent_2

Fill Parent/Match parent: As the name suggest, fills/matches the screen (size of parent layout). Parent refers to the area occupied by the layout in an activity.

Note: If the fill parent/match parent is used in your layout, the views in your activity get adjusted according to size of your mobile screen to match the parent. (since bigger screen phones will have larger screen area)

fill parent

 

Read More