How to put image in Navigation Drawer

http://thedeveloperworldisyours.com/wp-content/uploads/putimagen.png

You can download it from here

When you put image in your navigation drawer you have to declare a layout, in this case relativeLayout.
In layout xml you declare a layout, now you put image inside layout, then you put listView.

<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/drawer_layout"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >

    <!-- main content -->

    <FrameLayout
        android:id="@+id/content_frame"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    </FrameLayout>

    <!-- Menu -->

    <RelativeLayout
        android:id="@+id/left_drawer"
        android:layout_width="240dp"
        android:layout_height="match_parent"
        android:layout_gravity="start"
        android:background="@drawable/degradee"
        android:orientation="vertical" >

        <ImageView
            android:id="@+id/image_view"
            android:layout_width="match_parent"
            android:layout_height="200dp"
            android:background="@drawable/theworldisyours" />

        <ListView
            android:id="@+id/list_view_drawer"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:layout_below="@id/image_view"
            android:choiceMode="singleChoice" />
    </RelativeLayout>

</android.support.v4.widget.DrawerLayout>

In your class you declare

package com.thedeveloperworldisyours.navigationdrawer;

import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.ActivityCompat;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.Toast;

import com.thedeveloperworldisyours.navigationdrawer.fragment.FirstFragment;
import com.thedeveloperworldisyours.navigationdrawer.fragment.SecondFragment;
import com.thedeveloperworldisyours.navigationdrawer.fragment.ThirdFragment;

public class MainActivity extends ActionBarActivity {

	private String[] mOptionMenu;
	private DrawerLayout mDrawerLayout;
	private RelativeLayout mDrawerRelativeLayout;
	private ListView mDrawerList;
	private ActionBarDrawerToggle mDrawerToggle;

	private CharSequence mTitleSection;
	private CharSequence mTitleApp;
	private Fragment mFragment = null;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);

		mOptionMenu = new String[] { getString(R.string.first_fragment),
				getString(R.string.second_fragment),
				getString(R.string.third_fragment) };
		mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
		mDrawerRelativeLayout = (RelativeLayout) findViewById(R.id.left_drawer);
		mDrawerList = (ListView) findViewById(R.id.list_view_drawer);
		mDrawerList.setAdapter(new ArrayAdapter<String>(getSupportActionBar()
				.getThemedContext(), android.R.layout.simple_list_item_1,
				mOptionMenu));
		initContentWithFirstFragment();

		mDrawerList.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> parent, View view,
					int position, long id) {

				switch (position) {
				case 0:
					mFragment = new FirstFragment();
					break;
				case 1:
					mFragment = new SecondFragment();
					break;
				case 2:
					mFragment = new ThirdFragment();
					break;
				}

				FragmentManager fragmentManager = getSupportFragmentManager();

				fragmentManager.beginTransaction()
						.replace(R.id.content_frame, mFragment).commit();

				mDrawerList.setItemChecked(position, true);

				mTitleSection = mOptionMenu[position];
				getSupportActionBar().setTitle(mTitleSection);

				mDrawerLayout.closeDrawer(mDrawerRelativeLayout);
			}
		});
		mDrawerList.setItemChecked(0, true);
		mTitleSection = getString(R.string.first_fragment);
		mTitleApp = getTitle();

		mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
				R.drawable.ic_drawer, R.string.drawer_open,
				R.string.drawer_close) {

			public void onDrawerClosed(View view) {
				getSupportActionBar().setTitle(mTitleSection);
				ActivityCompat.invalidateOptionsMenu(MainActivity.this);
			}

			public void onDrawerOpened(View drawerView) {
				getSupportActionBar().setTitle(R.string.app_name);
				ActivityCompat.invalidateOptionsMenu(MainActivity.this);
			}
		};

		mDrawerLayout.setDrawerListener(mDrawerToggle);

		getSupportActionBar().setDisplayHomeAsUpEnabled(true);
		getSupportActionBar().setHomeButtonEnabled(true);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {

		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {

		if (mDrawerToggle.onOptionsItemSelected(item)) {
			return true;
		}

		switch (item.getItemId()) {
		case R.id.action_settings:
			Toast.makeText(this, R.string.action_settings, Toast.LENGTH_SHORT)
					.show();
			;
			break;
		default:
			return super.onOptionsItemSelected(item);
		}

		return true;
	}

	@Override
	protected void onPostCreate(Bundle savedInstanceState) {
		super.onPostCreate(savedInstanceState);
		mDrawerToggle.syncState();
	}

	@Override
	public void onConfigurationChanged(Configuration newConfig) {
		super.onConfigurationChanged(newConfig);
		mDrawerToggle.onConfigurationChanged(newConfig);
	}

	public void initContentWithFirstFragment(){

		mTitleSection =getString(R.string.first_fragment);
		getSupportActionBar().setTitle(mTitleSection);
		mFragment = new FirstFragment();
		FragmentManager fragmentManager = getSupportFragmentManager();

		fragmentManager.beginTransaction()
				.replace(R.id.content_frame, mFragment).commit();
	}
}

You can download it from here

Multiple OnClickListener android

You can do the onClick() method one time.

Download code.

First step

implements OnClickListener

Then you have a error
add unimplemented method

 @Override
	public void onClick(View v) {
		// TODO Auto-generated method stub
        }

This is full code:

package com.thedeveloperworldisyours.multipleonclicklistener;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;

public class MainActivity extends ActionBarActivity implements OnClickListener {

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		Button first = (Button) findViewById(R.id.activity_main_button_first);
		Button second = (Button) findViewById(R.id.activity_main_button_second);
		Button third = (Button) findViewById(R.id.activity_main_button_third);
		first.setOnClickListener(this);
		second.setOnClickListener(this);
		third.setOnClickListener(this);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.main, menu);
		return true;
	}

	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		// Handle action bar item clicks here. The action bar will
		// automatically handle clicks on the Home/Up button, so long
		// as you specify a parent activity in AndroidManifest.xml.
		int id = item.getItemId();
		if (id == R.id.action_settings) {
			return true;
		}
		return super.onOptionsItemSelected(item);
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.activity_main_button_first:
			Toast.makeText(MainActivity.this, R.string.activity_main_first,
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.activity_main_button_second:
			Toast.makeText(MainActivity.this, R.string.activity_main_second,
					Toast.LENGTH_SHORT).show();
			break;
		case R.id.activity_main_button_third:
			Toast.makeText(MainActivity.this, R.string.activity_main_third,
					Toast.LENGTH_SHORT).show();
			break;
		default:
			break;
		}
	}
}

Multiple onClickListener

How to get coordinates of an address in android

public class MapActivity extends android.support.v4.app.FragmentActivity{

	private static final int MAP_ZOOM = 15;
	private String mAddress;
	private GoogleMap mMap;
	private double mLongitude;
	private double mlatitude;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_map_hotel_car);

		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			mAddress = extras.getString("Address");
		}
		mMap = ((SupportMapFragment) getSupportFragmentManager()
				.findFragmentById(R.id.map)).getMap();

		Log.v("ddd", mAddress);

		Geocoder geocoder = new Geocoder(this, Locale.getDefault());
                //To initialice list of address
		List<Address> addresses = null;
		try {
                        //To put the address in list
			addresses = geocoder.getFromLocationName(mAddress, 1);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
                //Get Latitude and longitude
		Address address = addresses.get(0);
		mLongitude = address.getLongitude();
		mlatitude = address.getLatitude();
		showMarker(mlatitude, mLongitude);
		centerMapOnMyLocation();

	}

	//show the poi in map
	private void showMarker(double lat, double lng)
	{
	    mMap.addMarker(new MarkerOptions()
	        .position(new LatLng(mlatitude, mLongitude))
	        .title("Pais: EspaƱa"));
	}
	//Center the point in map
	private void centerMapOnMyLocation() {
			mMap.moveCamera(CameraUpdateFactory.newLatLng(getMyLatLng()));
			mMap.animateCamera(CameraUpdateFactory.zoomTo(MAP_ZOOM));
	}
	private LatLng getMyLatLng() {
		return new LatLng(mlatitude, mLongitude);
	}
}

Go to google maps app with a address

The first step, you put the packaget for google maps

private String PACKAGET_MAPS="com.google.android.apps.maps";

The seconds step, you put query for to search the address.

private String HTTP_MAPS = "http://maps.google.com/maps?q=";

The third step, you put intent

if (Utils.isPackageInstalled(PACKAGET_MAPS, getApplicationContext())) {
StringBuilder stringBuilder = new StringBuilder();
//query of google maps
stringBuilder.append(HTTP_MAPS);
//String with your address
stringBuilder.append(separtorWithPlus("yourAddress"));
stringBuilder.append(",+");
//string of City
stringBuilder.append((separtorWithPlus("City")));
//intent with Uri which has query with address and city
intent = new Intent(android.content.Intent.ACTION_VIEW,
Uri.parse(stringBuilder.toString()));
startActivity(intent);
} else {
mShowMessage.showAlert(getResources().getString(
R.string.string_google_maps_uninstall));
}

Send data from activity to fragment in android

Update

Send data from activity to fragment in android

In this example We learn how to send two string from Activity to fragment.

Android recommends to use newInstance.

In the activity:

FirstFragment fragment = FirstFragment.newInstance("Param One","Param Two");

In the fragment:

    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }


From Activity you send data with intent as:

public class MainActivity extends AppCompatActivity {
 
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
    }
 
    public void goToFragment(View view) {
        FirstFragment fragment = FirstFragment.newInstance("Param One","Param Two");
 
        replaceFragment(fragment, "FirstFragment");
        view.setVisibility(View.GONE);
    }
 
 
 
    public void replaceFragment(Fragment fragment, String tag) {
 
        try {
 
            FragmentManager fragmentManager = getSupportFragmentManager();
            fragmentManager.beginTransaction().replace(R.id.content_main, fragment, tag).commit();
 
        } catch (Exception e) {
            Log.d(TAG, e.toString());
        }
 
    }
}

and in Fragment onCreateView method:

public class FirstFragment extends Fragment {
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
 
    private String mParam1;
    private String mParam2;
 
    public FirstFragment() {
        // Required empty public constructor
    }
 
    /**
     * Use this factory method to create a new instance of
     * this fragment using the provided parameters.
     *
     * @param param1 Parameter 1.
     * @param param2 Parameter 2.
     * @return A new instance of fragment FirstFragment.
     */
    public static FirstFragment newInstance(String param1, String param2) {
        FirstFragment fragment = new FirstFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, param1);
        args.putString(ARG_PARAM2, param2);
        fragment.setArguments(args);
        return fragment;
    }
 
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            mParam1 = getArguments().getString(ARG_PARAM1);
            mParam2 = getArguments().getString(ARG_PARAM2);
        }
    }
 
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_first, container, false);
        TextView textView1 = (TextView) view.findViewById(R.id.fragment_first_param_one);
        TextView textView2 = (TextView) view.findViewById(R.id.fragment_first_param_two);
 
        textView1.setText(mParam1);
        textView2.setText(mParam2);
 
        return view;
    }
 
    @Override
    public void onAttach(Context context) {
        super.onAttach(context);
    }
 
    @Override
    public void onDetach() {
        super.onDetach();
    }
 
}

fragment_first.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="com.example.javier.activitytofragment.FirstFragment">
 
    <TextView
        android:id="@+id/fragment_first_param_one"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:text="@string/hello_blank_fragment" />
 
    <TextView
        android:id="@+id/fragment_first_param_two"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_below="@+id/fragment_first_param_one"
        android:text="@string/hello_blank_fragment" />
 
</RelativeLayout>

contentent_main.xml(This content has the fragment)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/content_main"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">
 
</RelativeLayout>

content_activity.xml (This content has the activity)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:orientation="vertical" android:layout_width="match_parent"
    android:layout_height="match_parent">
 
    <Button
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_centerVertical="true"
        android:onClick="goToFragment" />
 
</RelativeLayout>

activity_main.xml(this layout has the contents)

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:paddingBottom="@dimen/activity_vertical_margin"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    tools:context="com.example.javier.activitytofragment.MainActivity">
 
    <include layout="@layout/content_activity" />
    <include layout="@layout/content_main" />
</RelativeLayout>