My goal is to programm an Counter-Application. There is a picture in the middle of the smartphone and under the picture is a countdown starting at 100.
Every time you click on the picture the picture should flash and the counter should decrease till you have clicked 100 times and reached 0.
I have a little bit of code, but every time I run the project on my VM the application crashes.
package com.syntaix.appleclicker;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
int counter = 100;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final TextView text = (TextView) findViewById(R.id.TextView01);
final ImageView image = (ImageView) findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Here is the LogCat Screenshot:
LogCat
You can find the views in fragment when its created . But your are trying to get the views before attaching the fragment to the activity. You need to move some code to fragment as below...
MainActivity.java
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
PlaceholderFragment.java
public static class PlaceholderFragment extends Fragment {
int counter = 100;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
final TextView text = (TextView) rootView.findViewById(R.id.TextView01);
final ImageView image = (ImageView) rootView.findViewById(R.id.appel);
image.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
counter--;
text.setText("" + counter);
}
});
return rootView;
}
}
Related
I'm relatively new to both Java and the android platform and I have come across a problem in my code that is just completely beyond me. I am trying to lock onto a users location and move the camera to that location using the google maps api v2 however this is without success even with constant reading of resources and attempts.
The method requestLocationUpdates(String, long, float, LocationListener) in the type >LocationManager is not applicable for the arguments (String, int, int, LocationListener) >MainActivity.java /Loca/src/com/afrostudio/loca line 91 Java Problem
Also a very recent error has randomly occured:
R cannot be resolved to a variable
Any help would be fantastic please :)
package com.afrostudio.loca;
import android.app.ActionBar;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.content.Context;
import android.graphics.Color;
import android.graphics.drawable.ColorDrawable;
import android.location.Location;
import android.location.LocationManager;
import android.os.Bundle;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import com.google.android.gms.location.LocationListener;
import com.google.android.gms.maps.CameraUpdate;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.MapFragment;
public class MainActivity extends Activity
implements NavigationDrawerFragment.NavigationDrawerCallbacks {
private GoogleMap map;
/**
* Fragment managing the behaviors, interactions and presentation of the navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mNavigationDrawerFragment = (NavigationDrawerFragment)
getFragmentManager().findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
public class googleMap extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
LocationManager locationManager = (LocationManager)getSystemService(Context.LOCATION_SERVICE);
map = ((MapFragment) getFragmentManager().findFragmentById(R.id.map)).getMap();
// Define a listener that responds to location updates
LocationListener locationListner = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
double latti = location.getLatitude();
double longi = location.getLongitude();
CameraUpdate center=
CameraUpdateFactory.newLatLng(new LatLng(latti,
longi));
CameraUpdate zoom=CameraUpdateFactory.zoomTo(15);
map.moveCamera(center);
map.animateCamera(zoom);
}
};
// Register the listener with the Location Manager to receive location updates
locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, locationListner);
}
#Override
public void onDestroyView()
{
super.onDestroyView();
Fragment fragment = (getFragmentManager().findFragmentById(R.id.map));
FragmentTransaction ft = getActivity().getFragmentManager().beginTransaction();
ft.remove(fragment);
ft.commit();
}
}
public class MyFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_profile, container, false);
}
}
public class MyFragment3 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_settings, container, false);
}
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
Fragment fragment = new googleMap();
FragmentManager fragmentManager = getFragmentManager();
switch(position) {
case 1:
fragment = new googleMap();
break;
case 2:
fragment = new MyFragment2();
break;
case 3:
fragment = new MyFragment3();
break;
}
fragmentManager.beginTransaction()
.replace(R.id.container, fragment)
.commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
ActionBar ab = getActionBar();
ColorDrawable colorDrawable = new ColorDrawable(Color.parseColor("#A4C639"));
ab.setBackgroundDrawable(colorDrawable);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
((MainActivity) activity).onSectionAttached(
getArguments().getInt(ARG_SECTION_NUMBER));
}
}
}
The R error thing has to do with your xml files (layouts) check them carefully. Try cleaning the project as well... As for the Location I sugest you to use the LocationClient, it is newer and performs better... https://developer.android.com/training/location/receive-location-updates.html
I'm completely new to Android.
I want just a button that does a task but the sample code on the internet does not work.
Even though eclipse does not give any errors i can't run this app on my device. Here's the code:
package com.example.myfirstapp;
import com.example.myfirstapp.R;
import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.annotation.SuppressLint;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
#SuppressLint("ValidFragment")
public class MainActivity extends ActionBarActivity {
static Button btn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment implements View.OnClickListener {
public PlaceholderFragment() {
}
public void onClick(View v){
Intent browserIntent = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.sicioldrart.com"));
startActivity(browserIntent);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
return rootView;
}
}
}
Thanks for the help
You need to have an inflated view before you can look for yout button.
move this:
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
below
View rootView = inflater.inflate(R.layout.fragment_main, container,
false);
and change getView() with rootView
btn=(Button) getView().findViewById(R.id.bottone);
btn.setOnClickListener(this);
these two lines will come after
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
and replace getView() with rootView
btn=(Button) rootView.findViewById(R.id.bottone);
btn.setOnClickListener(this);
I am creating some APP in ADT and I am following one tutorial but I am stuck at performing button to change my text to seomething else. The problem is that, in the tutorial, he uses older version of ADT and mine is newer so it doesn't work anymore. The code what there is is like this:
(package name etc)
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment())
.commit();
final TextView textOne = (TextView) findViewById(R.id.textik);
Button pushthis = (Button) findViewById(R.id.gombik);}
}
#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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
return rootView;
}
}
}
This is probably a better example to follow.
http://www.vogella.com/tutorials/AndroidFragments/article.html
https://developer.android.com/training/basics/fragments/creating.html
The main think I see wrong with your code is
final TextView textOne = (TextView) findViewById(R.id.textik);
Button pushthis = (Button) findViewById(R.id.gombik);
They should not be initalized and declared here. If your using fragments, you should move it to the fragments. Below is a simple example.
public static class PlaceholderFragment extends Fragment implements Button.OnClickListener{
TextView textOne;
Button pushthis;
public PlaceholderFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
textOne = (TextView) rootView.findViewById(R.id.textik);
pushthis = (Button) rootView.findViewById(R.id.gombik);
//set handlers and anything else
pushthis.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.gombik:
textOne.setText("HI");
break;
}
}
}
I am trying to get the selected values of first second and third screen to be used in the final fourth screen. How would I go about accessing the radio groups of the previous fragments?
I have tried to store the selected value in the onPause method of each fragment but it gets called when I slide from the first fragment to the third fragment and not from the first to the second.
I have tried to use a listener for the radiogroup but it never gets called.
I have tried to access the other three fragments from the final Tsiatista fragment but without any success.
package com.tsiatistonmyfriend;
import java.io.IOException;
import java.sql.SQLException;
import java.util.Locale;
import net.justanotherblog.swipeview.R;
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.view.ViewPager;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
public class MainActivity extends FragmentActivity {
SectionsPagerAdapter mSectionsPagerAdapter;
ViewPager mViewPager;
public static String funnySerious;
#Override
protected 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)
{
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
OnClickListener listener = new OnClickListener()
{
#Override
public void onClick(View v)
{
RadioButton rb = (RadioButton) v;
funnySerious = (String) rb.getText();
}
};
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm)
{
super(fm);
}
#Override
public Fragment getItem(int position)
{
Fragment fragment = new FunnySerious();
switch (position)
{
case 0:
return fragment = new FunnySerious();
case 1:
return fragment = new MaleFemale();
case 2:
return fragment = new DirtyClean();
case 3:
return fragment = new Tsiatisto();
default:
break;
}
return fragment;
}
#Override
public int getCount() {
// Show 4 total pages.
return 4;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
case 3:
return getString(R.string.title_section4).toUpperCase(l);
}
return null;
}
}
public static class FunnySerious extends Fragment
{
View v;
public FunnySerious() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.funny_serious, container, false);
v = rootView;
return rootView;
}
}
public static class MaleFemale extends Fragment
{
public MaleFemale() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.male_female, container, false);
return rootView;
}
}
public static class DirtyClean extends Fragment
{
public DirtyClean() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dirty_clean, container, false);
return rootView;
}
}
public static class Tsiatisto extends Fragment
{
public Tsiatisto() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
//FunnySerious textFragment = (FunnySerious) this.getActivity().getSupportFragmentManager().findFragmentById(R.id.pager);
View rootView = inflater.inflate(R.layout.tsiatisto, container, false);
DataBaseHelper myDbHelper = new DataBaseHelper(this.getActivity().getApplicationContext());
try
{
myDbHelper.createDataBase();
myDbHelper.openDataBase();
}catch(IOException ioe)
{
throw new Error("Unable to create database");
}
final SQLiteDatabase db = myDbHelper.getDB();
Cursor cursor;
cursor = db.query("Tsiatista ORDER BY RANDOM() LIMIT 1", new String[] { "*" }, null, null, null, null, null);
//cursor = db.rawQuery("SELECT Verse FROM Tsiatista WHERE ID = 1", null);
if(cursor.moveToFirst())
{
String htr;
htr = (String) cursor.getString(cursor.getColumnIndex("Verse"));
EditText et = (EditText) rootView.findViewById(R.id.tsiatistoTxt);
et.setText(htr);
cursor.close();
}
return rootView;
}
}
}
Two ways,
Pass selected values as key value boolean pair in form of bundle, using setArgument method when you switch between fragment. You can then access this passed bundle and use selected values from previous fragment to drive you logic further, you can then club this values with values from current fragment and and navigate it further.
Read about shared preferences, you can always save value persistently and access it from all over your application, so using this you can save value selected by user from first three fragment, retrieve it on fourth and complete your logic.
Hope it help.
I'm completely new to android app development and I'm currently working on a project for school.
From my understanding so far, you can use the navigation drawer to swap out UI fragments.
I made a nav drawer that's working properly so far based on the tutorial given here:
https://developer.android.com/training/implementing-navigation/nav-drawer.html
I'm trying to have it switch views for each item that's selected. I made a view pager fragment activity and I want the program to replace the view with my view pager upon selecting the 4th item in the nav drawer (index 3). I added a switch statement in my fragment class to determine which layout to load.
Here's the activity for the nav drawer:
package com.mkge.mkg;
import android.content.res.Configuration;
import android.os.Bundle;
import android.support.v4.app.ActionBarDrawerToggle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.widget.DrawerLayout;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class MainActivity extends FragmentActivity {
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mTitle;
private String[] mPageTitles;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTitle = getTitle();
mPageTitles = getResources().getStringArray(R.array.nav_draw_array);
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerList = (ListView) findViewById(R.id.left_drawer);
mDrawerList.setAdapter(new ArrayAdapter<String>(this, R.layout.drawer_list_item, mPageTitles));
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this,
mDrawerLayout,
R.drawable.ic_drawer,
R.string.drawer_open,
R.string.drawer_close
) {
public void onDrawerClosed(View view) {
getActionBar().setTitle(mTitle);
invalidateOptionsMenu();
}
public void onDrawerOpened(View drawerView) {
getActionBar().setTitle(R.string.drawer_open);
invalidateOptionsMenu();
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
if (savedInstanceState == null) {
selectItem(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) return true;
return super.onOptionsItemSelected(item);
}
private class DrawerItemClickListener implements ListView.OnItemClickListener {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
selectItem(position);
}
}
private void selectItem(int position) {
Fragment fragment = new PageFragment();
Bundle args = new Bundle();
args.putInt(PageFragment.ARG_PAGE_NUMBER, position);
fragment.setArguments(args);
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.content_frame, fragment).commit();
mDrawerList.setItemChecked(position, true);
setTitle(mPageTitles[position]);
mDrawerLayout.closeDrawer(mDrawerList);
}
public static class PageFragment extends Fragment {
public static final String ARG_PAGE_NUMBER = "page_number";
public PageFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
int i = getArguments().getInt(ARG_PAGE_NUMBER);
View rootView;
switch(i) {
case 0:
rootView = inflater.inflate(R.layout.page_home, container, false);
break;
case 1:
rootView = inflater.inflate(R.layout.page_services, container, false);
break;
case 2:
rootView = inflater.inflate(R.layout.page_packages, container, false);
break;
case 3:
View view = new MeetViewPager().getViewPager();
rootView = view;
break;
case 4:
rootView = inflater.inflate(R.layout.page_about, container, false);
break;
case 5:
rootView = inflater.inflate(R.layout.page_contact, container, false);
break;
case 6:
rootView = inflater.inflate(R.layout.page_extras, container, false);
break;
default:
rootView = null;
}
String page = getResources().getStringArray(R.array.nav_draw_array)[i];
getActivity().setTitle(page);
return rootView;
}
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
I made this fragment activity here to create the view pager. I tried taking the view pager object I made here and passing it to the switch statement to my nav drawer activity.
package com.mkge.mkg;
import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
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.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
#SuppressLint("ValidFragment")
public class MeetViewPager extends FragmentActivity {
public ViewPager mViewPager;
public FragmentPagerAdapter mViewPagerAdapter;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.page_meet);
List<Fragment> fragments = getFragments();
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPagerAdapter = new mPageAdapter(getSupportFragmentManager(), fragments);
mViewPager.setAdapter(mViewPagerAdapter);
}
public View getViewPager() {
return mViewPager;
}
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
Fragment fragment = new mFragment();
Bundle args = new Bundle();
args.putInt("position", 0);
fragment.setArguments(args);
fList.add(fragment);
fragment = new mFragment();
args = new Bundle();
args.putInt("position", 1);
fragment.setArguments(args);
fList.add(fragment);
return fList;
}
private class mPageAdapter extends FragmentPagerAdapter {
private List<Fragment> fragments;
public mPageAdapter(FragmentManager f, List<Fragment> fragments) {
super(f);
this.fragments = fragments;
}
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
#Override
public int getCount() {
return this.fragments.size();
}
}
#SuppressLint("ValidFragment")
public static class mFragment extends Fragment {
public mFragment() {
}
public static final mFragment newInstance(int i) {
mFragment f = new mFragment();
Bundle args = new Bundle(1);
args.putInt("position", i);
f.setArguments(args);
return f;
}
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v;
int i = getArguments().getInt("postion");
switch(i) {
case 0:
v = inflater.inflate(R.layout.page_meet_0, container, false);
break;
case 1:
v = inflater.inflate(R.layout.page_meet_1, container, false);
break;
default:
v = null;
}
return v;
}
}
}
Any help would be appreciated thanks.
mViewPager.setCurrentItem(0);
This should be all you need to make your ViewPager change to the first tab.
This would be called from inside your selectItem() method.