Slide on gallery app - android

I'm currently working on a app that is a like a gallery app. The app need to load some images from the resources folder and when the user selects one, the app should load the selected image, and if the user slides the screen, the app should load the next image or the previous image.
The problem I have is that when I click the image the app always loads the first image. I try to use a intet with a int to solve it, but the program gives me an error.
I'm quite new in android programing, sorry if it is a simple question.
Here is the code of the app:
Main activity:
package com.example.erasor.pruebaslide;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Image1 (View view) {
Intent intent= new Intent(this, Slide.class);
startActivity(intent);
}
public void Image2 (View view) {
Intent intent = new Intent(this, Slide.class);
intent.putExtra("position", 2);
startActivity(intent);
}
}
Slide activity:
package com.example.erasor.pruebaslide;
import android.content.Context;
import android.content.Intent;
import android.support.v4.view.PagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.RelativeLayout;
public class Slide extends AppCompatActivity {
private CustomPagerAdapter mCustomPagerAdapter;
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_slide);
Intent intent = getIntent();
int position = intent.getIntExtra("position", 0);
mCustomPagerAdapter = new CustomPagerAdapter(this);
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mCustomPagerAdapter);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
int[] mResources = {
R.drawable.image1,
R.drawable.image2,
R.drawable.image3,
R.drawable.image4,
};
class CustomPagerAdapter extends PagerAdapter {
Context mContext;
LayoutInflater mLayoutInflater;
public CustomPagerAdapter(Context context) {
mContext = context;
mLayoutInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return mResources.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((RelativeLayout) object);
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
View itemView = mLayoutInflater.inflate(R.layout.fragment_slide, container, false);
ImageView imageView = (ImageView) itemView.findViewById(R.id.image);
imageView.setImageResource(mResources[position]);
container.addView(itemView);
return itemView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout) object);
}
}
}

I think you are missing the part where you tell the mViewPager which position to display, try this:
mViewPager.setAdapter(mCustomPagerAdapter);
mViewPager.setCurrentItem(position);

Related

Android: Change text in a PagerAdapter

I have a MainActivity which calls a fragment (ReadStory).
ReadStory has a ViewPager which uses a PagerAdapter.
The page has an image and a TextView at the bottom. (This example only shows the TextView)
All of this works well.
There's a requirement that when the user touches/clicks the TextView various words are highlighted in red. This also works well.
The problem I have is that the text (with red highlights) is displayed on the next screen not the current visible screen.
I have set a onClickListener on the TextView which sets the relevant parts to red and then sets the text on the TextView.
The ViewPager loads the current screen plus the previous and next screens. The TextView is 'attached' to the "nextScreen".
How do I change the TextView on the current screen?
I've searched for the answer for several days but I can't seem to find an answer. Any help would be appreciated.
My code - MainActivity
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button myButton;
ReadStory RS;
// *********************************************************************************************
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button);
myButton.setOnClickListener(textViewClickListener);
}
// *********************************************************************************************
private View.OnClickListener textViewClickListener = new View.OnClickListener() {
public void onClick(View v) {
int[] mStory_resources = {R.string.S1P0, R.string.S1P1, R.string.S1P2, R.string.S1P3, R.string.S1P4, R.string.S1P5, R.string.S1P6, R.string.S1P7,
R.string.S1P8, R.string.S1P9, R.string.S1P10, R.string.S1P11};
int mStoryWords = R.string.S1words;
RS = new ReadStory();
Bundle args = new Bundle();
args.putIntArray("Story", mStory_resources);
args.putInt("storyWords", mStoryWords);
RS.setArguments(args);
FragmentManager fm = getSupportFragmentManager();
FragmentTransaction ft = fm.beginTransaction();
ft.addToBackStack("readStory");
ft.replace(R.id.activity_main, RS);
ft.commit();
}
};
// *********************************************************************************************
}
ReadStory
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
public class ReadStory extends Fragment {
static String currentString = "";
static String newString = "";
private static final String story = "Story";
private static final String storyWords = "storyWords";
private int[] mStory_resources;
private int mStory_words;
ViewPager viewPager;
CustomSwipeAdapter adapter;
public ReadStory() {
// Required empty public constructor
}
// *********************************************************************************************
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mStory_resources = getArguments().getIntArray(story);
mStory_words = getArguments().getInt(storyWords);
}
}
// *********************************************************************************************
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
final RelativeLayout parent = (RelativeLayout) inflater.inflate(R.layout.read_story, container, false);
viewPager = (ViewPager) parent.findViewById(R.id.view_pager);
adapter = new CustomSwipeAdapter(getActivity());
adapter.story_resources = mStory_resources;
adapter.story_words = mStory_words;
viewPager.setAdapter(adapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
currentString = getString(mStory_resources[position]);
}
#Override
public void onPageScrollStateChanged(int state) {
}
});
return parent;
}
// *********************************************************************************************
}
PagerAdapter
import android.content.Context;
import android.os.Build;
import android.support.v4.view.PagerAdapter;
import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
class CustomSwipeAdapter extends PagerAdapter{
int[] story_resources;
int story_words;
private TextView textView;
private Context ctx;
CustomSwipeAdapter(Context ctx)
{
this.ctx = ctx;
}
// *********************************************************************************************
#Override
public int getCount() {
return story_resources.length;
}
// *********************************************************************************************
#Override
public boolean isViewFromObject(View view, Object object) {
return (view==object);
}
// *********************************************************************************************
#SuppressWarnings("deprecation") // Put here as setText is deprecated as of API 16 - Jelly_bean
#Override
public Object instantiateItem(ViewGroup container, int position) {
LayoutInflater layoutInflater;
layoutInflater = (LayoutInflater) ctx.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View item_view = layoutInflater.inflate(R.layout.swipe_layout,container,false);
textView = (TextView) item_view.findViewById(R.id.image_count);
textView.setText(story_resources[position]);
textView.setOnClickListener(textViewClickListener);
container.addView(item_view);
return item_view;
}
// *********************************************************************************************
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
container.removeView((RelativeLayout)object);
}
// *********************************************************************************************
#SuppressWarnings("deprecation") // Put here as fromHtml is deprecated as of API 24 - N
private View.OnClickListener textViewClickListener = new View.OnClickListener() {
public void onClick(View v) {
String patternString = "\\b(" + ctx.getString(story_words) + ")\\b";
Pattern pattern = Pattern.compile(patternString, Pattern.CASE_INSENSITIVE);
Matcher matcher = pattern.matcher(ReadStory.currentString);
StringBuffer bufStr = new StringBuffer();
while (matcher.find()) {
String rep = matcher.group();
matcher.appendReplacement(bufStr, "<font color='#EE0000'>" + rep + "</font>");
}
matcher.appendTail(bufStr);
ReadStory.newString = bufStr.toString();
int sdk = android.os.Build.VERSION.SDK_INT;
if(sdk >= Build.VERSION_CODES.N) {
textView.setText(Html.fromHtml(ReadStory.newString));
} else {
textView.setText(Html.fromHtml(ReadStory.newString));
}
Toast.makeText(ctx, "Clicked", Toast.LENGTH_LONG).show();
}
};
// *********************************************************************************************
}
Using
((TextView)v).setText(Html.fromHtml(ReadStory.newString));
instead of
textView.setText(Html.fromHtml(ReadStory.newString));
There is reference problem with textView object it consists reference of next item.

How use getDrawable() while using ViewPager

I was trying to use Drawable Animation in one of my Viewpager. But when I try to use getDrawable() method, it gives me null pointer exception. My ViewPager has 4 page. I think this is because my XML files are made in Viewpager and I cannot access them with this method. I already set a source for an ImageView in my Viewpager but still it doesn't work. Please have a look at my codes and help me.
My Main Activity:
import android.graphics.drawable.AnimationDrawable;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.WindowManager;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new CustomPagerAdapter(this));
ImageView myAnimation = (ImageView)findViewById(R.id.imageView2);
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}
}
My ViewPager Adapter:
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.graphics.drawable.Drawable;
import android.support.v4.view.PagerAdapter;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class CustomPagerAdapter extends PagerAdapter {
private Context mContext;
public CustomPagerAdapter(Context context) {
mContext = context;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
ModelObject modelObject = ModelObject.values()[position];
LayoutInflater inflater = LayoutInflater.from(mContext);
ViewGroup layout = (ViewGroup) inflater.inflate(modelObject.getLayoutResId(), collection, false);
collection.addView(layout);
return layout;
}
#Override
public void destroyItem(ViewGroup collection, int position, Object view) {
collection.removeView((View) view);
}
#Override
public int getCount() {
return ModelObject.values().length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public CharSequence getPageTitle(int position) {
ModelObject customPagerEnum = ModelObject.values()[position];
return mContext.getString(customPagerEnum.getTitleResId());
}
}
and my enum to add new Pages:
public enum ModelObject {
RED(R.string.wp1, R.layout.wp1),
BLUE(R.string.wp2, R.layout.wp2),
LOL(R.string.wp3, R.layout.wp3),
WP(R.string.wp5,R.layout.wp4),
GREEN(R.string.wp4, R.layout.wp4);
private int mTitleResId;
private int mLayoutResId;
ModelObject(int titleResId, int layoutResId) {
mTitleResId = titleResId;
mLayoutResId = layoutResId;
}
public int getTitleResId() {
return mTitleResId;
}
public int getLayoutResId() {
return mLayoutResId;
}
}
Please help me if you can.
sorry for bad English.
#Override
public void onPageSelected(int pos) {
//find your layout in with the position & then implement
ImageView myAnimation = (ImageView)findViewById(R.id.imageView2); // this imageview for particular viewpager page
final AnimationDrawable myAnimationDrawable = (AnimationDrawable)myAnimation.getDrawable();
myAnimationDrawable.start();
}

Cube Transition effect animation Not Working in Tabactivity

i am working on Cube Transition effect animation ,but when i am trying to add in Tab activity,then it looses cube transition effect,its works fine without tab, i am using ToxicBakery/ViewPagerTransforms library for this effect
my code is
package com.ToxicBakery.viewpager.transforms.example;
import android.annotation.SuppressLint;
import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.Window;
import android.widget.ImageButton;
import android.widget.ImageView;
import android.widget.TabHost;
import android.widget.Toast;
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
import java.util.ArrayList;
#SuppressLint("ResourceAsColor")
#SuppressWarnings("deprecation")
public class MainActivity extends TabActivity {
TabHost tabHost;
ImageButton ib;
private ViewPager mPager;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
tabHost = getTabHost();
setTabs();
}
private void setTabs() {
addTab("", R.drawable.home, HomeActivity.class);
addTab("", R.drawable.search, ListActivity.class);
addTab("", R.drawable.camerabg, HomeActivity.class);
addTab("", R.drawable.like, AlarmActivity.class);
addTab("", R.drawable.profile, SettingActivity.class);
ib = (ImageButton) findViewById(R.id.ibHome);
ib.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(MainActivity.this, "Hello", Toast.LENGTH_LONG)
.show();
}
});
}
private void addTab(String labelId, int drawableId, Class<?> c) {
Intent intent = new Intent(this, c);
TabHost.TabSpec spec = tabHost.newTabSpec("tab" + labelId);
View tabIndicator = LayoutInflater.from(this).inflate(
R.layout.tab_indicator, getTabWidget(), false);
ImageView icon = (ImageView) tabIndicator.findViewById(R.id.icon);
icon.setImageResource(drawableId);
spec.setIndicator(tabIndicator);
spec.setContent(intent);
tabHost.addTab(spec);
}
}
and cube transition activty is
package com.example.turnster;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.os.Bundle;
import android.support.v13.app.FragmentStatePagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.ImageView;
import android.widget.ListView;
import com.ToxicBakery.viewpager.transforms.CubeOutTransformer;
import java.util.ArrayList;
import java.util.HashMap;
import adapters.SampleAdapter;
public class HomeActivity extends Activity{
ListView Lv;
SampleAdapter adapter;
private PageAdapter mAdapter;
private ViewPager mPager;
private static final ArrayList<TransformerItem> TRANSFORM_CLASSES;
static {
TRANSFORM_CLASSES = new ArrayList<>();
TRANSFORM_CLASSES.add(new TransformerItem(CubeOutTransformer.class));
//TRANSFORM_CLASSES.add(new TransformerItem(CubeInTransformer.class));
}
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.list_home_lv);
mPager=(ViewPager)findViewById(R.id.container);
// Lv = (ListView)findViewById(R.id.listView);
// ArrayList<HashMap<String, String>> data = new ArrayList<HashMap<String, String>>();
// HashMap<String,String> temp4 = new HashMap<String,String>();
// temp4.put("name", "Korigad");
// temp4.put("heightt", "3000ft");
// temp4.put("heightt", "3000ft");
// temp4.put("heightt", "3000ft");
// temp4.put("heightt", "3000ft");
// data.add(temp4);
// adapter = new SampleAdapter(HomeActivity.this,
// android.R.layout.simple_list_item_1, data,
// getApplication());
// Lv.setAdapter(adapter);
mAdapter = new PageAdapter(getFragmentManager());
mPager.setAdapter(mAdapter);
}
public static class PlaceholderFragment extends Fragment {
private static final String EXTRA_POSITION = "EXTRA_POSITION";
private static final int[] COLORS = new int[] { R.drawable.imgone, R.drawable.imgtwo, R.drawable.imgthree, R.drawable.imgfour };
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
final int position = getArguments().getInt(EXTRA_POSITION);
final ImageView textViewPosition = (ImageView) inflater.inflate(R.layout.fragment_main, container, false);
textViewPosition.setBackgroundResource(COLORS[position - 1]);
return textViewPosition;
}
}
private static final class TransformerItem {
final String title;
final Class<? extends ViewPager.PageTransformer> clazz;
public TransformerItem(Class<? extends ViewPager.PageTransformer> clazz) {
this.clazz = clazz;
title = clazz.getSimpleName();
}
#Override
public String toString() {
return title;
}
}
private static final class PageAdapter extends FragmentStatePagerAdapter {
public PageAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public Fragment getItem(int position) {
final Bundle bundle = new Bundle();
bundle.putInt(PlaceholderFragment.EXTRA_POSITION, position + 1);
final PlaceholderFragment fragment = new PlaceholderFragment();
fragment.setArguments(bundle);
return fragment;
}
#Override
public int getCount() {
return 4;
}
}
}
You may use the provided setPageTransformer() class
MainActivity.java
...
viewPager = (ViewPager)findViewById(R.id.view);
viewPager.setAdapter(new PageAdapter(getSupportFragmentManager()));
viewPager.setPageTransformer(true,new CubeOutPageTransformer());
...
CubeOutPageTransformer.java
public class CubeOutPageTransformer implements ViewPager.PageTransformer {
#Override
public void transformPage(View view, float position) {
view.setPivotX(position < 0f ? view.getWidth() : 0f);
view.setPivotY(view.getHeight() * 0.5f);
view.setRotationY(90f * position);
}
}
in case you wanted to use other different animation refer here

viewpager implemented in gridview position

I am working on an application that have to have a GridView in the main activity containing images.
When an image is clicked, it has to open in another Activity in a bigger size. In this Activity, a ViewPager has to be implemented so that I can swipe right and left to change between the images.
The problem is that when i click on an image, the first image of the images appear firstly (argentina flag) and then the viewpager works .. how can i make it start by the image that i clicked ?
This is my main activity code :
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
public class MainActivityPager extends ActionBarActivity
{
static int[] FLAG;
ViewPager viewPager;
PagerAdapter adapter;
GridView gridview;
int count;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_activity_pager);
FLAG = new int[] { R.drawable.flag_of_argentina, R.drawable.flag_of_brazil,
R.drawable.flag_of_england, R.drawable.flag_of_france,
R.drawable.flag_of_germany, R.drawable.flag_of_italy, R.drawable.flag_of_spain,
R.drawable.flag_of_uruguay };
AdapterGrid adapter = new AdapterGrid(MainActivityPager.this, FLAG);
gridview = (GridView) findViewById(R.id.gridView1);
gridview.setAdapter(adapter);
gridview.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id)
{
Intent intent = new Intent(view.getContext(), ActivityImage.class);
intent.putExtra("position", position);
count = gridview.getCount();
intent.putExtra("count", count);
startActivity(intent);
}
});
}
#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);
}
}
This is my AdapterGrid class code :
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
public class AdapterGrid extends BaseAdapter
{
private int[] mFlag;
private Context mContext;
public AdapterGrid(Context context, int[] flag) {
//super(context, flag);
mContext = context;
mFlag = flag;
}
#Override
public int getCount() {
return mFlag.length;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
View grid;
LayoutInflater inflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null)
{
grid = new View(mContext);
grid = inflater.inflate(R.layout.grid_single, null);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
imageView.setImageResource(mFlag[position]);
} else {
grid = (View) convertView;
}
return grid;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return 0;
}
}
This is my ActivityImage code :
import android.content.Intent;
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.Menu;
public class ActivityImage extends FragmentActivity
{
private static int COUNT;
MyAdapter mAdapter;
ViewPager mPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment_pager);
Intent intent = getIntent();
COUNT = intent.getExtras().getInt("count");
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager) findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
}
public static class MyAdapter extends FragmentPagerAdapter {
public MyAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
#Override
public int getCount() {
return COUNT;
}
#Override
public Fragment getItem(int position) {
return FragmentImage.init(position);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_image, menu);
return true;
}
}
This is my FragmentImage class code :
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
public class FragmentImage extends Fragment {
int fragVal;
static FragmentImage init(int pos) {
FragmentImage frag = new FragmentImage();
Bundle args = new Bundle();
args.putInt("val", pos);
frag.setArguments(args);
return frag;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
fragVal = getArguments() != null ? getArguments().getInt("val") : 1;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.fragment_image, container,
false);
ImageView imageView = (ImageView) layoutView.findViewById(R.id.imageView1);
imageView.setImageResource(MainActivityPager.FLAG[fragVal]);
return layoutView;
}
}
Thanks.
You set the position intent.putExtra("position", position); but never use it.
get it and
set after
mPager.setAdapter(mAdapter);
mPager.setCurrentItem(CurrentPosition);

All sound not loaded in array

I have made app. Here i used viewpager two swipe 9 images & accordingly i have loaded 9 sounds but it only plays 8 sounds so when i swipe previous page the sound is not match according to the image. here is my code..
package com.android.learning_numbers;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.app.Activity;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;
public class Numbers extends Activity {
MediaPlayer mp;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_numbers);
ViewPager viewPager = (ViewPager)findViewById(R.id.view_pager);
ImagePagerAdapter adapter = new ImagePagerAdapter();
viewPager.setAdapter(adapter);
// viewPager.setOnPageChangeListener(change);
}
private class ImagePagerAdapter extends PagerAdapter {
private int[] mImages = new int[] {R.drawable.no1,R.drawable.no2,R.drawable.no3,R.drawable.no4,R.drawable.no5,R.drawable.no6,R.drawable.no7,R.drawable.no8,R.drawable.no9,};
private int[] mAudio = new int[]{R.raw.one,R.raw.two,R.raw.three,R.raw.four,R.raw.five,R.raw.six,R.raw.seven,R.raw.eight,R.raw.nine,};
#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 = Numbers.this;
ImageView imageView = new ImageView(context);
int padding =context.getResources().
getDimensionPixelSize(R.dimen.activity_vertical_margin);
imageView.setPadding(padding, padding, padding, padding);
imageView.setScaleType(ImageView.ScaleType.CENTER);
imageView.setImageResource(mImages[position]);
((ViewPager) container).addView(imageView, 0);
if(position>=1){
mp = MediaPlayer.create(Numbers.this, mAudio[position-1]);
mp.start();
}
// Log.v("Testing", "Ok");
// Toast.makeText(getApplicationContext(), "Page Changing", 5000).show();
return imageView;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ImageView) object);
}
}
}

Categories

Resources