All we need to do is to use the method setMyLocationEnabled(boolean enabled) which enables the my-Location layer.
Like that :
private GoogleMap mMap;
mMap.setMyLocationEnabled(true);
private GoogleMap mMap;
mMap.setMyLocationEnabled(true);
public interface OnSomethingSelectedListener { public void passData(int position); }2. onAttach() : To be sure that Activity implements the Interface onAttach callback method of Fragment_A we instantiate an Instance of Ιnterface by casting the Activity passed into onAttach().
@Override public void onAttach(Activity activity) { super.onAttach(activity); // This makes sure that the container activity has implemented the callback interface. If not, it throws an exception try { listener = (OnSomethingSelectedListener) activity; } catch (ClassCastException e) { throw new ClassCastException(activity.toString() + " must implement OnHeadlineSelectedListener"); } }
public class MainActivity extends FragmentActivity implements OnSomethingSelectedListener
@Override public void passData(int position) { Log.d(TAG, "Give me the result " + position ); position = position +1; Log.d(TAG, "Give me the result " + position ); }
<button
android:id="@+id/button_id"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:text="send Integer">
</button>
5. And on Fragment's onCreateView() we add the listener for the event:
Button button = (Button) view.findViewById(R.id.button_id); button.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { Log.v(TAG, "onClick 1"); listener.passData(5); //pass an Integer } });We pass the value five to the method passData() Now when we click on the Button we open LogCat View and we see that value one has been added to the value of the parameter:
public class Fragment_A extends Fragment { @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { return inflater.inflate(R.layout.fragment_a_layout, container, false); } }and the fragment_a_layout as we said is a TextView :
<linearlayout android:layout_height="match_parent" android:layout_width="match_parent" android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"> <textview android:id="@+id/tv" android:layout_height="wrap_content" android:layout_width="fill_parent" android:text="Fragment A"> </linearlayout>Respectively we make up the class for the Fragment B and layout as fragment_a_layout. Now what's left is to bind the 2 fragments on Activity's layout.
<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=".MainLayoutActivity" > <fragment android:name="org.example.fragmentsapp.Fragment_A" android:id="@+id/id_frag_A" android:layout_width="100dp" android:layout_height="50dp" android:layout_marginBottom="20dp" /> <fragment android:id="@+id/id_frag_B" android:name="org.example.fragmentsapp.Fragment_B" android:layout_width="100dp" android:layout_height="50dp" android:layout_below="@id/id_frag_A" /> </RelativeLayout>
package org.example.fragmentsapp; import android.os.Bundle; import android.support.v4.app.FragmentActivity; public class MainLayoutActivity extends FragmentActivity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main_layout); } }
main
.[ {"id":"1","title":"Homeland"},{"id":"2","title":"Breaking Bad"}, {"id":"3","title":"Dexter"},{"id":"4","title":"Californication"} ]
public class TvShow() {Note that we need to Override the toString method for the Object TvShow so that every TextView in the list will display strings.
int id;
String title;
public TvShow( int id, String title) {
this.id = id;
this.title = title;
}
public int getID() {
return id;
}
public String getTitle() {
return title;
}
@Override
public String toString(){
String toReturn = "ID = "+id + " Title= " +title ;
return toReturn;
}
}
ArrayList<TvShow> alist = new ArrayList<TvShow>();
JSONArray jArray = new JSONArray(result); //result is the String response we got from our web service
for (int i = 0; i < len; i++) {JSONData jData = new JSONData(_id, _title);
JSONObject jObj = jArray.getJSONObject(i);
int _id = jObj.getInt("id");
String _title = jObj.getString("title");
} catch (Exception e) {
// TODO: handle exception
e.printStackTrace();
}
return alist;
ls.setAdapter(arrayAdapter);