Wednesday, 7 January 2015

how to set the initial selected fragment?



I have created a fragment that contain 5 more fragments by using images.


However when I enter the default fragment, it did not select any initial fragment.


fragment_main.xml



<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://ift.tt/nIICcg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">

<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" >


<fragment
android:name="gymbuddyV3.gymbuddyv3.FragmentSearch"
android:id="@+id/fragment_place"
android:layout_width="match_parent"
android:layout_height="match_parent" />

</LinearLayout>

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:background="#000000" >

<ImageButton
android:id="@+id/btnSearch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:background="@android:color/transparent"
android:onClick="selectFrag"
android:src="@drawable/searchfill" />

<ImageButton
android:id="@+id/btnBrowse"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:background="@android:color/transparent"
android:onClick="selectFrag"
android:src="@drawable/browse" />

<ImageButton
android:id="@+id/btnCheckin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:background="@android:color/transparent"
android:onClick="selectFrag"
android:src="@drawable/checkin" />

<ImageButton
android:id="@+id/btnProfile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:background="@android:color/transparent"
android:onClick="selectFrag"
android:src="@drawable/selfs" />

<ImageButton
android:id="@+id/btnBuddy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="14dp"
android:background="@android:color/transparent"
android:onClick="selectFrag"
android:src="@drawable/friends" />
</LinearLayout>

</LinearLayout>


MainActivity.java



public class MainActivity extends Activity {
public static final String PREFS_NAME = "LoginPrefs";
ImageButton btnSearch, btnCheckin, btnProfile, btnBuddy, btnBrowse;

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

btnBrowse = (ImageButton) findViewById(R.id.btnBrowse);
btnSearch = (ImageButton) findViewById(R.id.btnSearch);
btnProfile = (ImageButton) findViewById(R.id.btnProfile);
btnCheckin = (ImageButton) findViewById(R.id.btnCheckin);
btnBuddy = (ImageButton) findViewById(R.id.btnBuddy);
}

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

@Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == R.id.itemLogout) {
SharedPreferences settings = getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editor editor = settings.edit();
// editor.remove("logged");
editor.clear();
editor.commit();
Intent i = new Intent(getApplicationContext(), LoginActivity.class);
startActivity(i);
finish();
} else if (item.getItemId() == R.id.itemLeaderboard) {

} else if (item.getItemId() == R.id.itemSettings) {

}
return super.onOptionsItemSelected(item);
}

public void selectFrag(View view) {
Fragment fr;
if (view == findViewById(R.id.btnBuddy)) {
fr = new FragmentBuddies();
btnSearch.setImageResource(R.drawable.search);
btnBrowse.setImageResource(R.drawable.browse);
btnCheckin.setImageResource(R.drawable.checkin);
btnProfile.setImageResource(R.drawable.selfs);
btnBuddy.setImageResource(R.drawable.friendsfill);

} else if (view == findViewById(R.id.btnProfile)) {
fr = new FragmentProfile();
btnSearch.setImageResource(R.drawable.search);
btnBrowse.setImageResource(R.drawable.browse);
btnCheckin.setImageResource(R.drawable.checkin);
btnProfile.setImageResource(R.drawable.selfsfill);
btnBuddy.setImageResource(R.drawable.friends);


} else if (view == findViewById(R.id.btnCheckin)) {
fr = new FragmentCheckin();
btnSearch.setImageResource(R.drawable.search);
btnBrowse.setImageResource(R.drawable.browse);
btnCheckin.setImageResource(R.drawable.checkinfill);
btnProfile.setImageResource(R.drawable.selfs);
btnBuddy.setImageResource(R.drawable.friends);

} else if (view == findViewById(R.id.btnBrowse)) {
fr = new FragmentBrowse();
btnSearch.setImageResource(R.drawable.search);
btnBrowse.setImageResource(R.drawable.browsefill);
btnCheckin.setImageResource(R.drawable.checkin);
btnProfile.setImageResource(R.drawable.selfs);
btnBuddy.setImageResource(R.drawable.friends);
} else {
fr = new FragmentSearch();
btnSearch.setImageResource(R.drawable.searchfill);
btnBrowse.setImageResource(R.drawable.browse);
btnCheckin.setImageResource(R.drawable.checkin);
btnProfile.setImageResource(R.drawable.selfs);
btnBuddy.setImageResource(R.drawable.friends);

}

FragmentManager fm = getFragmentManager();
getFragmentManager().popBackStack(null, FragmentManager.POP_BACK_STACK_INCLUSIVE);
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.fragment_place, fr, "selectedfragment").addToBackStack(null).commit();


}
}


One of the fragment.java class



public class FragmentSearch extends Fragment {
Spinner spnLocation, spnDate;
Button btnFind;

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {

View view = inflater.inflate(R.layout.fragment_search, container, false);
spnLocation = (Spinner)view.findViewById(R.id.spinnerLocation);
spnDate = (Spinner) view.findViewById(R.id.spinnerDate);
btnFind = (Button) view.findViewById(R.id.btnFind);
// Inflate the layout for this fragment
return view;
}
}

No comments:

Post a Comment