Video from Youtube API does not play onClick - java

I'm not sure exactly why but for some reason my videos do not seem to play when clicked. Instead they queue in the player but do not play. Has anyone experienced this before or have any idea how the issue can be resolved?
Source:
public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);
fav_up_btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean favIsUp = fav_up_btn1
.getBackground()
.getConstantState()
.equals(getResources().getDrawable(
R.drawable.fav_up_btn1).getConstantState());
// set the background
fav_up_btn1
.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
: R.drawable.fav_up_btn1);
}
});
youTubeThumbnailView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO2_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
youTubeThumbnailView3.initialize(API_KEY, this);
youTubeThumbnailView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO3_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
youTubeThumbnailView4.initialize(API_KEY, this);
youTubeThumbnailView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO4_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
#Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader
.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader.setVideo(VIDEO_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
#Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
#Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
// true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}

Replace
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
With
youTubePlayer.loadVideo(VIDEO1_ID);

Related

I have created a wallpapers app using viewpager and urls of images but I am not able to set wallpapers I am placing my Urls in string

This is my mainactivity class and I am using my urls in the string. Anyone please tell me how to set wallpapers using multiple urls. I am using multiple urls to show in viewpager its working but I can't set wallpapers.
This is my main activity class.
I want to set wallpapers on floating button onclick listener
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
private String[] imageUrls = new String[]{
"https://i.jj.cc/MGTTwJ7Q/Ant-Man-474b00d1-4bdc-3ea6-88a3-1702c46f061c.jpg",
"https://i.jj.cc/SKvKN1dt/fdg-min.jpg",
"https://i.jj.cc/fLSR37gW/uhd-antman18-min.jpg",
"https://i.jj.cc/Gt0hvBZF/uhd-antman7-min.jpg",
"https://i.jj.cc/ryWpMJrQ/Antman-And-The-Wasp-d4a753af-1dd1-4df2-aeb6-d39c732fd16a.jpg",
"https://i.jj.cc/RVLVnZGt/uhd-antman21-min.jpg",
"https://i.jj.cc/t4RRdGmM/Ant-man-and-the-wasp-a294bb80-e6f9-41c1-bf46-07af64e3e348.jpg",
"https://i.jj.cc/y8f1vpMY/Antman-d8033f49-b33c-4cd4-b3ca-651417df20da.jpg",
"https://i.jj.cc/yNkV5XKh/Antman-Abstract-HD-05ef2c84-e5d3-41e9-afa1-1400c315bf06.jpg",
"https://i.jj.cc/8C91VJk2/IMG-0139.jpg",
"https://i.jj.cc/Znh4CGdj/antman-70390c1f-2d63-41ea-a487-e34668167e7e.jpg",
"https://i.jj.cc/vTLM907y/antman-2fde23ba-9eac-4f11-acf7-b872b9b71121.jpg",
"https://i.jj.cc/66dBDpkP/Antman-d353b93b-5c57-4363-ad92-4a974423d2b5.jpg",
"https://i.jj.cc/rFMqcLcw/Antman-df2e8adb-b0a0-42b6-ade7-38d041349ed1.jpg",
"https://i.jj.cc/YCSkGfvc/Antman-32cb0b83-f4b6-4a24-854a-913b593c0291.jpg",
"https://i.jj.cc/zGPNnbhy/razakbaap49.jpg",
"https://i.jj.cc/tgZjDqd2/antman05-uhd.jpg"
};
private int indexOfImage = 0;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
ViewPager viewPager = findViewById(R.id.view_pager);
ViewPagerAdapter adapter = new ViewPagerAdapter(this, imageUrls);
viewPager.setAdapter(adapter);
viewPager.setOnPageChangeListener(new MyPageChangeListener());
FloatingActionButton floatingActionButton = (FloatingActionButton)findViewById(R.id.fab);
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
}
});
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_camera) {
// Handle the camera action
} else if (id == R.id.nav_gallery) {
} else if (id == R.id.nav_slideshow) {
} else if (id == R.id.nav_manage) {
} else if (id == R.id.nav_share) {
} else if (id == R.id.nav_send) {
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
private class MyPageChangeListener implements ViewPager.OnPageChangeListener {
#Override
public void onPageScrolled(int i, float v, int i1) {
}
#Override
public void onPageSelected(int i) {
MainActivity.this.indexOfImage = i;
}
#Override
public void onPageScrollStateChanged(int i) {
}
}
}
This is my viewpager adapter class
public class ViewPagerAdapter extends PagerAdapter {
private Context context;
private String[] imageUrls;
ViewPagerAdapter(Context context, String[] imageUrls) {
this.context = context;
this.imageUrls = imageUrls;
}
#Override
public int getCount() {
return imageUrls.length;
}
#Override
public boolean isViewFromObject(#NonNull View view, #NonNull Object object) {
return view == object;
}
#NonNull
#Override
public Object instantiateItem(#NonNull ViewGroup container, int position) {
ImageView imageView = new ImageView(context);
Picasso.with(context)
.load(imageUrls[position])
.fit()
.centerCrop()
.error(R.drawable.ic_error_outline_black_24dp)
.into(imageView);
container.addView(imageView);
return imageView;
}
#Override
public void destroyItem(#NonNull ViewGroup container, int position, #NonNull Object object) {
container.removeView((View) object);
}
}
Add permission in manifest
<uses-permission android:name="android.permission.SET_WALLPAPER"/>
Add this AsyncTask class in your MainActivity
public class SetWallpaper extends AsyncTask<String, Void, Bitmap> {
ProgressDialog progressDialog = new ProgressDialog(MainActivity.this);
#Override
protected Bitmap doInBackground(String... params) {
Bitmap bitmap = null;
try {
bitmap = Picasso.get().load(params[0]).get();
} catch (IOException e) {
e.printStackTrace();
}
return result;
}
#Override
protected void onPostExecute (Bitmap result) {
super.onPostExecute(result);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(getBaseContext());
try {
wallpaperManager.setBitmap(result);
progressDialog.dismiss();
Toast.makeText(getApplicationContext(), "Wallpaper changed", Toast.LENGTH_SHORT).show();
} catch (IOException ex) {
ex.printStackTrace();
}
}
#Override
protected void onPreExecute () {
super.onPreExecute();
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setMessage("Loading image...");
progressDialog.setCancelable(false);
progressDialog.show();
}
}
Then try
floatingActionButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
SetWallpaper sw = new SetWallpaper();
sw.execute(imageUrls[indexOfImage])
}
});

RecycleView on Fragment using Custom Adapter with Retrofit

public class AgentAdapter extends RecyclerView.Adapter<AgentAdapter.CustomViewHolder> {
private List<Agent> agentList;
private Context mContext;
public AgentAdapter(Context context, List<Agent> agentList) {
this.agentList= agentList;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.agent_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
final Agent agent = agentList.get(i);
//Setting text view title
customViewHolder.textViewAgentName.setText(agent.getAgentName());
customViewHolder.textViewAgentPhone.setText(agent.getAgentPhone());
customViewHolder.textViewAgentRegion.setText(agent.getRegion());
customViewHolder.textViewAgentStatus.setText(agent.getAgentStatus());
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(agent);
}
};
customViewHolder.textViewAgentName.setOnClickListener(listener);
}
#Override
public int getItemCount() {
return (null != agentList ? agentList.size() : 0);
}
public void updateAnswers(List<Agent>agent ) {
agentList = agent;
notifyDataSetChanged();
}
class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView textViewAgentName;
protected TextView textViewAgentPhone;
protected TextView textViewAgentRegion;
protected TextView textViewAgentStatus;
public CustomViewHolder(View view) {
super(view);
this.textViewAgentName = (TextView) view.findViewById(R.id.textAgentName);
this.textViewAgentPhone= (TextView) view.findViewById(R.id.textAgentPhone);
this.textViewAgentRegion = (TextView) view.findViewById(R.id.textAgentRegion);
this.textViewAgentStatus = (TextView) view.findViewById(R.id.textAgentStatus);
}
}
private OnItemClickListener onItemClickListener;
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
That's my fragment and this is my Adapter
public class AgentAdapter extends RecyclerView.Adapter<AgentAdapter.CustomViewHolder> {
private List<Agent> agentList;
private Context mContext;
public AgentAdapter(Context context, List<Agent> agentList) {
this.agentList= agentList;
this.mContext = context;
}
#Override
public CustomViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.agent_row, null);
CustomViewHolder viewHolder = new CustomViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(CustomViewHolder customViewHolder, int i) {
final Agent agent = agentList.get(i);
//Setting text view title
customViewHolder.textViewAgentName.setText(agent.getAgentName());
customViewHolder.textViewAgentPhone.setText(agent.getAgentPhone());
customViewHolder.textViewAgentRegion.setText(agent.getRegion());
customViewHolder.textViewAgentStatus.setText(agent.getAgentStatus());
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
onItemClickListener.onItemClick(agent);
}
};
customViewHolder.textViewAgentName.setOnClickListener(listener);
}
#Override
public int getItemCount() {
return (null != agentList ? agentList.size() : 0);
}
public void updateAnswers(List<Agent>agent ) {
agentList = agent;
notifyDataSetChanged();
}
class CustomViewHolder extends RecyclerView.ViewHolder {
protected TextView textViewAgentName;
protected TextView textViewAgentPhone;
protected TextView textViewAgentRegion;
protected TextView textViewAgentStatus;
public CustomViewHolder(View view) {
super(view);
this.textViewAgentName = (TextView) view.findViewById(R.id.textAgentName);
this.textViewAgentPhone= (TextView) view.findViewById(R.id.textAgentPhone);
this.textViewAgentRegion = (TextView) view.findViewById(R.id.textAgentRegion);
this.textViewAgentStatus = (TextView) view.findViewById(R.id.textAgentStatus);
}
}
private OnItemClickListener onItemClickListener;
public OnItemClickListener getOnItemClickListener() {
return onItemClickListener;
}
public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
this.onItemClickListener = onItemClickListener;
}
and this is my MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//add this line to display menu1 when the activity is loaded
displaySelectedScreen(R.id.nav_menu2);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
displaySelectedScreen(item.getItemId());
return true;
}
private void displaySelectedScreen(int itemId) {
//creating fragment object
Fragment fragment = null;
//initializing the fragment object which is selected
switch (itemId) {
case R.id.nav_menu1:
fragment = new caseFragment();
break;
case R.id.nav_menu2:
fragment = new testFragment();
break;
}
//replacing the fragment
if (fragment != null) {
FragmentTransaction ft = getSupportFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment);
ft.commit();
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
}
when i tried to open my fragment, the app crashed and the logcat is blank.
previously i could show my json data from activity but now when i tried to use fragment instead, it crashed.
Im using java and android studio.
Anyone know why? Thx in advance.

How can display ListView if I click on NavigationDrawer [duplicate]

I want to build an android application with a Fragment which contains a List of Option and if I click on one of this option, I can see, another fragment with other option.
So I build this:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".activity.MainActivity"
tools:ignore="MergeRootFrame" >
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<LinearLayout
android:layout_width="480dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" >
<ListView
android:id="#+id/left_list"
android:layout_width="240dp"
android:layout_height="match_parent"
android:background="#6dc659"
android:foreground="#color/white">
</ListView>
<fragment
android:id="#+id/navigation_drawer"
android:name="it.eresult.decipher.fragment.NavigationDrawerFragment"
android:layout_width="240dp"
android:layout_height="match_parent"
android:layout_gravity="start"
tools:layout="#layout/fragment_navigation_drawer" />
</LinearLayout>
</android.support.v4.widget.DrawerLayout>
This is the MainActivity.java
public class MainActivity extends ActionBarActivity implements NavigationDrawerFragment.NavigationDrawerCallbacks{
private DrawerLayout drawer;
private ListView leftList;
private String[] leftListStrings;
private String[] rightListStrings;
private NavigationDrawerFragment mNavigationDrawerFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
leftList = (ListView) findViewById(R.id.left_list);
leftListStrings = getResources().getStringArray(R.array.principal_menu);
leftList.setAdapter(new ArrayAdapter<String>(this,android.R.layout.simple_list_item_1,leftListStrings));
mNavigationDrawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.navigation_drawer);
mNavigationDrawerFragment.setUp(
R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
leftList.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
arg1.setBackgroundColor(getResources().getColor(R.color.selected_menu_option));
if(arg2 == 0){
}
}
});
drawer.setDrawerListener(new DrawerLayout.DrawerListener() {
#Override
public void onDrawerStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerSlide(View arg0, float arg1) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerOpened(View arg0) {
// TODO Auto-generated method stub
}
#Override
public void onDrawerClosed(View arg0) {
// TODO Auto-generated method stub
//rightList.setVisibility(View.INVISIBLE);
}
});
}
#Override
public void onNavigationDrawerItemSelected(int position) {
//TODO
}
class MayAdapter extends ArrayAdapter<String>{
List<String> myList = null;
public MayAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
myList = objects;
}
public List<String> getMyList() {
return myList;
}
public void setMyList(List<String> myList) {
this.myList = myList;
}
}
public void restoreActionBar() {
ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle("omnia");
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
int id = item.getItemId();
return super.onOptionsItemSelected(item);
}
}
This is NavigationDrawerFragment.java
public class NavigationDrawerFragment extends Fragment {
private static final String STATE_SELECTED_POSITION = "selected_navigation_drawer_position";
private NavigationDrawerCallbacks mCallbacks;
private ActionBarDrawerToggle mDrawerToggle;
private DrawerLayout mDrawerLayout;
private ListView mDrawerListView;
private View mFragmentContainerView;
private int mCurrentSelectedPosition = 0;
Toolbar toolBar;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
mCurrentSelectedPosition = savedInstanceState.getInt(STATE_SELECTED_POSITION);
}
// Select the default item.
selectItem(mCurrentSelectedPosition);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
// the fragment has menu items to contribute
setHasOptionsMenu(true);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mDrawerListView = (ListView) inflater.inflate(
R.layout.fragment_navigation_drawer, container, false);
mDrawerListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView parent, View view, int position, long id) {
selectItem(position);
}
});
mDrawerListView.setAdapter(new ArrayAdapter(
getActionBar().getThemedContext(),
android.R.layout.simple_list_item_activated_1,
android.R.id.text1,
new String[]{
getString(R.string.social_history),
getString(R.string.encounter),
getString(R.string.problems),
}));
mDrawerListView.setItemChecked(mCurrentSelectedPosition, true);
return mDrawerListView;
}
private ActionBar getActionBar() {
return ((AppCompatActivity) getActivity()).getSupportActionBar();
}
public boolean isDrawerOpen() {
return mDrawerLayout != null && mDrawerLayout.isDrawerOpen(mFragmentContainerView);
}
public void setUp(int fragmentId, DrawerLayout drawerLayout) {
mFragmentContainerView = getActivity().findViewById(fragmentId);
mDrawerLayout = drawerLayout;
// set a custom shadow that overlays the main content when the drawer opens
//mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
// ActionBarDrawerToggle ties together the the proper interactions
// between the navigation drawer and the action bar app icon.
mDrawerToggle = new ActionBarDrawerToggle(
getActivity(),
mDrawerLayout,
null,
R.string.navigation_drawer_open,
R.string.navigation_drawer_close
) {
#Override
public void onDrawerClosed(View drawerView) {
super.onDrawerClosed(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu();
}
#Override
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
if (!isAdded()) {
return;
}
getActivity().supportInvalidateOptionsMenu();
}
};
mDrawerLayout.post(new Runnable() {
#Override
public void run() {
mDrawerToggle.syncState();
}
});
mDrawerLayout.setDrawerListener(mDrawerToggle);
mDrawerToggle.setDrawerIndicatorEnabled(true);
}
private void selectItem(int position) {
mCurrentSelectedPosition = position;
if (mDrawerListView != null) {
mDrawerListView.setItemChecked(position, true);
}
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(mFragmentContainerView);
}
if (mCallbacks != null) {
mCallbacks.onNavigationDrawerItemSelected(position);
}
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mCallbacks = (NavigationDrawerCallbacks) activity;
} catch (ClassCastException e) {
throw new ClassCastException("Activity must implement NavigationDrawerCallbacks.");
}
}
#Override
public void onDetach() {
super.onDetach();
mCallbacks = null;
}
#Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(STATE_SELECTED_POSITION, mCurrentSelectedPosition);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
public static interface NavigationDrawerCallbacks {
void onNavigationDrawerItemSelected(int position);
}
}
With this code, if I try to run my application, I can see the complete menu like this
Instead, I want to see only the left menu, and if I click on one items, see the right menu

multi tasking in android

i get the Choreographer error The application may be doing too much work on its main thread.
In my main activity each time i launch the app or get back to the main activity.
From what i understand there's to many work to be done on one core (because i'm not using threads), so i have to find a solution which probably evolves threads and AsyncTask.
I was trying to figure out what exactly cause the cpu put his effort but i couldn't figure it out by myself.
Here's the code of the main activity which includes a navigation drawer:
public class MainActivity extends ActionBarActivity
implements View.OnClickListener {
private Animation animAlpha;
private Animation animRotate;
private MediaPlayer background_music;
static int is_login = 0;
private boolean isOpen = false;
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mDrawerToggle;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
private ViewFlipper flipper;
ImageButton messages,weekly_day_times,shabat_times,events;
private ViewGroup mContainerView1,mContainerView2,mContainerView3,mContainerView4;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
animAlpha = AnimationUtils.loadAnimation(this, R.anim.anim_alpha);
animRotate = AnimationUtils.loadAnimation(this, R.anim.anim_rotate);
background_music = MediaPlayer.create(this, R.raw.yedid_nefesh);
flipper = (ViewFlipper) findViewById(R.id.viewFlipper);
startFlipper();
background_music.start();
messages = (ImageButton) findViewById(R.id.messages_button);
weekly_day_times = (ImageButton) findViewById(R.id.weekly_times_button);
shabat_times = (ImageButton) findViewById(R.id.sat_times_button);
events = (ImageButton) findViewById(R.id.events_button);
messages.setOnClickListener(this);
weekly_day_times.setOnClickListener(this);
shabat_times.setOnClickListener(this);
events.setOnClickListener(this);
mContainerView1 = (ViewGroup) findViewById(R.id.add_shabat_container);
mContainerView2 = (ViewGroup) findViewById(R.id.add_weekly_container);
mContainerView3 = (ViewGroup) findViewById(R.id.add_messages_container);
mContainerView4 = (ViewGroup) findViewById(R.id.add_events_container);
findViewById(R.id.b_shabat_add).setOnClickListener(this);
findViewById(R.id.b_weekly_add).setOnClickListener(this);
findViewById(R.id.b_messages_add).setOnClickListener(this);
findViewById(R.id.b_events_add).setOnClickListener(this);
IntializeNavDrawer();
}
#Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
setButtonsAnimation(messages);
setButtonsAnimation(weekly_day_times);
setButtonsAnimation(shabat_times);
setButtonsAnimation(events);
}
private void setButtonsAnimation(ImageButton button) {
int rand = (int)(Math.random()*700);
Animation hyperspaceJump = AnimationUtils.loadAnimation(this, R.anim.hyperspace_jump);
hyperspaceJump.setStartOffset(rand);
button.startAnimation(hyperspaceJump);
}
/*
Initialize all list views in navigation drawer.
Handles actions of open or close drawer.
*/
private void IntializeNavDrawer() {
mTitle = mDrawerTitle = getTitle();
mDrawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout,
null, R.string.drawer_open,
R.string.drawer_close) {
public void onDrawerClosed(View view) {
getSupportActionBar().setTitle(mTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
public void onDrawerOpened(View drawerView) {
getSupportActionBar().setTitle(mDrawerTitle);
invalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
};
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
/*
Initialize properties and start the background flipper
*/
private void startFlipper() {
flipper.setFlipInterval(5000);
flipper.setInAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_in));
flipper.setOutAnimation(AnimationUtils.loadAnimation(this,
android.R.anim.fade_out));
flipper.startFlipping();
}
#Override
protected void onResume() {
super.onResume();
flipper.startFlipping();
background_music.start();
}
#Override
public void setTitle(CharSequence title) {
mTitle = title;
getSupportActionBar().setTitle(mTitle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onClick(View v) {
Intent i;
switch(v.getId())
{
case R.id.messages_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Messages.class);
startActivity(i);
return;
}
case R.id.sat_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, ShabatPrays.class);
startActivity(i);
return;
}
case R.id.weekly_times_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, WeeklyPrays.class);
startActivity(i);
return;
}
case R.id.events_button: {
i = new Intent(com.example.neotavraham.MainActivity.this, Events.class);
startActivity(i);
return;
}
case R.id.b_shabat_add:
if(isOpen){
removeItem(mContainerView1);
isOpen = false;
}else {
addItem(mContainerView1,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_weekly_add:
if(isOpen){
removeItem(mContainerView2);
isOpen = false;
}else {
addItem(mContainerView2,R.layout.nav_drawer_l_changes_item_1);
isOpen =true;
}
return;
case R.id.b_messages_add:
if(isOpen){
removeItem(mContainerView3);
isOpen = false;
}else {
addItem(mContainerView3,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
case R.id.b_events_add:
if(isOpen){
removeItem(mContainerView4);
isOpen = false;
}else {
addItem(mContainerView4,R.layout.nav_drawer_l_changes_item_2);
isOpen =true;
}
return;
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
return super.onCreateOptionsMenu(menu);
}
#SuppressLint("NewApi")
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// The action bar home/up action should open or close the drawer.
// ActionBarDrawerToggle will take care of this.
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return false;
}
#Override
protected void onStop() {
super.onStop();
background_music.pause();
flipper.stopFlipping();
}
/**** Method for Setting the Height of the ListView dynamically.
**** Hack to fix the issue of not showing all the items of the ListView
**** when placed inside a ScrollView ****/
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null)
return;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
int totalHeight = 0;
View view = null;
for (int i = 0; i < listAdapter.getCount(); i++) {
view = listAdapter.getView(i, view, listView);
if (i == 0) {
view.setLayoutParams(new ViewGroup.LayoutParams(desiredWidth, AbsListView.LayoutParams.WRAP_CONTENT));
}
view.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
totalHeight += view.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}
private void addItem(ViewGroup mContainerView,int layout) {
// Instantiate a new "row" view.
final ViewGroup newView = (ViewGroup) LayoutInflater.from(this).inflate(
layout, mContainerView, false);
// Because mContainerView has android:animateLayoutChanges set to true,
// adding this view is automatically animated.
mContainerView.addView(newView, 2);
}
public void removeItem(ViewGroup mContainerView){
mContainerView.removeViewAt(2);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// Pass any configuration change to the drawer toggles
mDrawerToggle.onConfigurationChanged(newConfig);
}
}
Also here are puctures with and withougt the navigation bar:
I was hoping you will show me what cuase to that error and an example on how should i get over it.
thank you
Here is some information from the Android documentation site about multi-threading.
https://developer.android.com/training/multiple-threads/index.html
http://developer.android.com/guide/components/processes-and-threads.html
http://developer.android.com/guide/faq/commontasks.html#threading
You can look around the web for more tutorials.

The method setOnPageChangeListener(ViewPager.SimpleOnPageChangeListener) is undefined for the type Home.ImagePagerAdapter

This is my first experience building something with ViewPager and I need a bit of help. I'm attempting to implement a solution found here:
ViewPager.SimpleOnPageChangeListener Does Not Function
However when I attempt to do so - I end up with the following:
The method setOnPageChangeListener(ViewPager.SimpleOnPageChangeListener) is undefined for the type Home.ImagePagerAdapter line 173: setOnPageChangeListener(mPageChangeListener);}
Cannot reference a field before it is defined Home.java line 173: setOnPageChangeListener(mPageChangeListener);}
Constructor call must be the first statement in a constructor line 171: super();
SOURCE:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ActionBar actionBar = getActionBar();
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
drawerListViewItems = getResources().getStringArray(R.array.items);
drawerListView = (ListView) findViewById(R.id.left_drawer);
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
listView = (VideosListView) findViewById(R.id.videosListView);
listView.setOnVideoClickListener(this);
new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();
}
Handler responseHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
populateListWithVideos(msg);
};
};
private void populateListWithVideos(Message msg) {
Library lib = (Library) msg.getData().get(
GetYouTubeUserVideosTask.LIBRARY);
listView.setVideos(lib.getVideos());
}
#Override
protected void onStop() {
responseHandler = null;
super.onStop();
}
#Override
public void onVideoClicked(Video video) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(video.getUrl()));
startActivity(intent);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ImagePagerAdapter extends PagerAdapter {{
super();
setOnPageChangeListener(mPageChangeListener);}
private int[] mImages = new int[] { R.drawable.selstation_up_btn,
R.drawable.classical_up_btn, R.drawable.country_up_btn,
R.drawable.dance_up_btn, R.drawable.hiphop_up_btn };
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(final int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
mCurrentTabPosition = position;
}
};
protected void onTabChanged(final PagerAdapter adapter, final int oldPosition, final int newPosition) {
if (oldPosition>newPosition){
}
else{
String PLAYLIST = "idconex";
View vg = findViewById (R.layout.home);
vg.invalidate();
}
}
}
}
EDIT:
public class Home extends YouTubeBaseActivity implements
VideoClickListener {
private VideosListView listView;
private ActionBarDrawerToggle actionBarDrawerToggle;
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
private int mCurrentTabPosition = NO_CURRENT_POSITION;
private static final int NO_CURRENT_POSITION = -1;
private DrawerLayout drawerLayout;
private ListView drawerListView;
private String[] drawerListViewItems;
private ViewPager mPager;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
String TAG = "DEBUG THIS";
String PLAYLIST = "idconex";
private OnPageChangeListener mPageChangeListener;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ActionBar actionBar = getActionBar();
ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
drawerListViewItems = getResources().getStringArray(R.array.items);
drawerListView = (ListView) findViewById(R.id.left_drawer);
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
listView = (VideosListView) findViewById(R.id.videosListView);
listView.setOnVideoClickListener(this);
new GetYouTubeUserVideosTask(responseHandler, PLAYLIST).execute();
}
Handler responseHandler = new Handler() {
#Override
public void handleMessage(Message msg) {
populateListWithVideos(msg);
};
};
private void populateListWithVideos(Message msg) {
Library lib = (Library) msg.getData().get(
GetYouTubeUserVideosTask.LIBRARY);
listView.setVideos(lib.getVideos());
}
#Override
protected void onStop() {
responseHandler = null;
super.onStop();
}
#Override
public void onVideoClicked(Video video) {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setData(Uri.parse(video.getUrl()));
startActivity(intent);
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class ImagePagerAdapter extends PagerAdapter {
public ImagePagerAdapter()
{
super();
setOnPageChangeListener(mPageChangeListener);
}
private int[] mImages = new int[] { R.drawable.selstation_up_btn,
R.drawable.classical_up_btn, R.drawable.country_up_btn,
R.drawable.dance_up_btn, R.drawable.hiphop_up_btn };
#Override
public int getCount() {
return mImages.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((ImageView) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
Context context = Home.this;
ImageView imageView = new ImageView(context);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
private final ViewPager.SimpleOnPageChangeListener mPageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(final int position) {
onTabChanged(mPager.getAdapter(), mCurrentTabPosition, position);
mCurrentTabPosition = position;
}
};
protected void onTabChanged(final PagerAdapter adapter, final int oldPosition, final int newPosition) {
//Calc if swipe was left to right, or right to left
if (oldPosition>newPosition){
// left to right
}
else{
//right to left
String PLAYLIST = "idconex";
View vg = findViewById (R.layout.home);
vg.invalidate();
}
final ViewPager viewPager = (ViewPager) findViewById(R.id.view_pager);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
int oldPos = viewPager.getCurrentItem();
#Override
public void onPageScrolled(int position, float arg1, int arg2) {
if(position > oldPos) {
//Moving to the right
} else if(position < oldPos) {
//Moving to the Left
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0) {
// TODO Auto-generated method stub
}
});
}}}
The problem with your code (or at least current code) is you don't have a constructor. Instead, you just have a super call sitting out in the middle of nowhere. And you have two opening curly brackets({) after the class declaration which is strange but I think that's because you don't understand the constructor in Java.
Try changing it to look more like
private class ImagePagerAdapter extends PagerAdapter {
// this is your constructor
public ImagePagerAdapter()
{
super();
setOnPageChangeListener(mPageChangeListener);
}
This change will most likely take care of all 3 of those errors.
You should consider going through a good tutorial and the docs below.
ViewPager Docs
Also, it is important that you know what constructors are which you can learn about Here in the Java Docs

Categories

Resources