using ListView in swipe pages - android

Hi i have a swipe pages example which contain 3 page and i want to add a List View to this example.
But when i try to run that eclipse say "Your content must have a ListView whose id attribute is 'android.R.id.list' ". i do not know whats wrong.
My Main Activity Layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/customviewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Page1
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/blue"
android:orientation="vertical" >
<Button
android:id="#+id/buttonBlue"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/label_blue"
android:onClick="#string/listenerBlueButton" />
</LinearLayout>
Page2
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/yellow"
android:orientation="vertical" >
<Button
android:id="#+id/buttonYellow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/label_yellow"
android:onClick="#string/listenerYellowButton" />
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</ListView>
</LinearLayout>
Page 3
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/red"
android:orientation="vertical" >
<Button
android:id="#+id/buttonRed"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/label_red"
android:onClick="#string/listenerRedButton" />
</LinearLayout>
My Pager Adapter
package ro.ovidiuconeac.horizontalviewswiping;
public class CustomPagerAdapter extends PagerAdapter {
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position) {
case 0: {
resId = R.layout.page_1;
break;
}
case 1: {
resId = R.layout.page_2;
break;
}
case 2: {
resId = R.layout.page_3;
break;
}
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
#Override
public int getCount() {
return 3;
}
}
Main Activity
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create and set adapter
CustomPagerAdapter adapter = new CustomPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.customviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(1);
String[] ex=new String[10];
ex[0]="1";
ex[1]="2";
ex[2]="3";
ex[3]="1";
ex[4]="2";
ex[5]="3";
ex[6]="1";
ex[7]="2";
ex[8]="3";
ex[9]="3";
setContentView(R.layout.page_2);
ListView myList=(ListView)findViewById(android.R.id.list);
ListAdapter la=new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, ex);
myList.setAdapter(la);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
/**
* Click button on blue page
*/
public void onClickBlueButton(View v) {
Toast.makeText(getApplicationContext(), "Blue screen", Toast.LENGTH_SHORT).show();
}
/**
* Click button on yellow page
*/
public void onClickYellowButton(View v) {
Toast.makeText(getApplicationContext(), "Yellow screen", Toast.LENGTH_SHORT).show();
}
/**
* Click button on red page
*/
public void onClickRedButton(View v) {
Toast.makeText(getApplicationContext(), "Red screen", Toast.LENGTH_SHORT).show();
}
}

This is because you're extending ListActivity. ListActivity looks for a ListView with a specific identifier from the android package.
Have a look at this line of your ListActivity
setContentView(R.layout.activity_main);
Your ListActivity is inflating the layout for your main activity, instead of the layout for your page 2(which contains your listview).
You need to change that line in the onCreate of your ListActivity to inflate the correct layout. Let's say your ListActivity layout is called R.layout.list_activy. That line then becomes
setContentView(R.layout.list_activity);
Instead of what you currently have, I'm not sure what the name of your layout file containing the listview is called you didn't mention it(page?).
EDIT: I just remembered that ListActivity may not need a call to setContentView() at all, since you pass in the parent layout to your adapter in setAdapter() anyways. Try removing that line altogether and just call setAdapter. Both of these approaches should work though

You have ListView in your XML but you are not passing your adapter to it. Create a ListView and pass your adapter to this list view.
Try this
ListView myList=(ListView)findViewById(android.R.id.list);
myList.setAdapter(la);

Related

Android start activities in a loop

I have a SQLite Database with 3 records for example:
1 - Name1 - Do wash
2 - Name2 - Do this
3 - Name3 - Do that
Now I want to start one activity after another (for each record) when user presses a button. The Activity is always the same with other content.
So what is the best way to do this?
My thoughts are following:
- Make a for loop and use StartActivityForResult(...)
- Pass data throug all activities using intent
- Create a temporary table and read data in each activity
- Change only the content of the layout, but don't know how i could do this
So any suggestions on this?
I think for showing same type of view on multiple content you should use fragment with viewpager.
I am just suggesting code to you for viewpager and fragment -
layout for content (pages.xml)-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:id="#+id/rel" >
<TextView
android:id="#+id/pageContent"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
</RelativeLayout>
Main activity content(activity_main.xml) -
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<RelativeLayout
android:id="#+id/relativeTextview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/header"
android:padding="5dp" >
<android.support.v4.view.ViewPager
android:id="#+id/reviewpager"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
</RelativeLayout>
Pager adapter for setting on viewpager(ViewPagerAdapter.java)-
public class ViewPagerAdapter extends PagerAdapter {
int size;
Activity act;
View layout;
TextView pagecontent;
Button click;
public ViewPagerAdapter(MainActivity mainActivity, int noofsize) {
// TODO Auto-generated constructor stub
size = noofsize;
act = mainActivity;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return size;
}
#Override
public Object instantiateItem(View container, int position) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) act
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
layout = inflater.inflate(R.layout.pages, null);
pagecontent = (TextView) layout.findViewById(R.id.pageContent);
int pagenumberTxt=position + 1;
pagecontent.setText("Now your in Page No " +pagenumberTxt );
((ViewPager) container).addView(layout, 0);
return layout;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
// }
}
Now in main activity((MainActivity.java) -
public class MainActivity extends Activity {
int noofsize = 6;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPagerAdapter adapter = new ViewPagerAdapter(MainActivity.this,
noofsize);
ViewPager myPager = (ViewPager) findViewById(R.id.reviewpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
and do not forget to mention activity in manufest.xml.
In pages.xml you can add the button on on click of this button you can change the view for next and previous(mPager.setCurrentItem(position)) using listener.
Using activities for this will take a toll on RAM. Use fragments instead.
Answering your original question, as you have cited, make use of the onActivityResult() and as for your concern in this:
-Change only the content of the layout, but don't know how i could do this
Make use of the passed data from intent and handle accordingly.

How to use ViewPager, or something else, to just scroll through text?

Everything I have seen is overkill. I am simply looking for a way to have some part of my app scroll through several text snippets when swiping left/right. Imagine an image gallery that you scroll through, but just a line of text for each instead of an image. Thanks!
You can have a look at my other posts for more detailed answer. The explanation is very simple in my other posts.
How to properly use fragments with ViewPager?
Multiple ViewPagers in on Activity
How to swipe xml layouts using ViewPager
These are few examples you can find helpful. Hope this helps.. :)
If still having some issues with those answers, take a look at this simple example:
Just follow simple steps here:
Step 1: Create layout for your view pager:(activity_main.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
android:background="#333333">
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</RelativeLayout>
Step 2: Create another layout to hold your textview items: (items.xml)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/text_view1"
android:layout_width="fill_parent"
android:layout_height="40dp"
android:background="#94999F"
android:gravity="left|center_vertical"
android:paddingLeft="10dp"
android:text="Hello"
android:textColor="#FFFFFF"
android:textSize="17sp"
android:textStyle="bold"
android:visibility="visible" />
</LinearLayout>
Step 3: In you Main Activity create a class for your adapter or you can create a separate class for your adapter. This is just an adapter inside the same class. Here's the full code:
public class MainActivity extends Activity {
ViewPager mViewPager;
String[] items = { "one", "two", "three", "four", "five" };
ArrayList<String> mArrayList = new ArrayList<String>();
Context context;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
/*for (String s : items) {
mArrayList.add(s);
}*/
for(int i = 0;i<100;i++){
mArrayList.add("Item " + i);
}
mViewPager = (ViewPager) findViewById(R.id.pager);
MyAdapter mAdapter = new MyAdapter(context, mArrayList);
mViewPager.setAdapter(mAdapter);
}
#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;
}
/*Here's the adapter*/
private class MyAdapter extends PagerAdapter {
ArrayList<String> someList = new ArrayList<String>();
Context context;
public MyAdapter(Context context, ArrayList<String> newsomeList) {
super();
this.context = context;
this.someList = newsomeList;
}
#SuppressLint("NewApi")
#Override
public void finishUpdate(ViewGroup container) {
// TODO Auto-generated method stub
super.finishUpdate(container);
}
#Override
public int getCount() {
return someList.size();
}
#Override
public boolean isViewFromObject(View collection, Object object) {
return collection == ((View) object);
}
#Override
public Object instantiateItem(View collection, int position) {
// Inflating layout
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// Setting view you want to display as a row element
View view = inflater.inflate(R.layout.items, null);
TextView itemText = (TextView) view.findViewById(R.id.text_view1);
// Getting reference for text view and inflate the view for Answers
try {
itemText.setText(someList.get(position));
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View collection, int position, Object view) {
((ViewPager) collection).removeView((View) view);
}
}
}
And that's you need .. :)
Hope this will help you.. Its a very simple example for what you need.. Good Luck .. :)
take a look at this: Using ViewPager for Screen Slides
You can use HorizontalScrollView. An example is here. Instead of Buttons, you can use TextView to serve you puroise.
Or else you can use ViewPager also. Already others have given you tutorial just follow that. But I think, to serve your purpose HorizontalScrollView is needed.

How to implement click listener for a button in android Page Viewer?

I want to create a simple Page Control in android.... I want to move from one page to another page scrolling horizontally, like the home screen in android device.
I have multiple layout in xml like main.xml, layout_first.xml, layout_second.xml and layout_third.xml
Now I have a simple button in my layout_first.xml, I want to implement a click listener for the button like
button.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
}
});
No I don't know where to put the above code
Here is my main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Main Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<android.support.v4.view.ViewPager
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/myfivepanelpager"/>
</LinearLayout>
Here is my layout_first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="First Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
Here is my layout_second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Second Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my layout_third.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myTextView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Third Layout"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Here is my java code
public class MainActivity extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.myfivepanelpager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
}
#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;
}
private class MyPagerAdapter extends PagerAdapter
{
#Override
public int getCount()
{
// TODO Auto-generated method stub
return 3;
}
public Object instantiateItem(View collection, int position)
{
LayoutInflater inflater = (LayoutInflater) collection.getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
int resId = 0;
switch (position)
{
case 0:
resId = R.layout.layout_first;
break;
case 1:
resId = R.layout.layout_second;
break;
case 2:
resId = R.layout.layout_third;
break;
}
View view = inflater.inflate(resId, null);
((ViewPager) collection).addView(view, 0);
return view;
}
#Override
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
#Override
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
#Override
public Parcelable saveState() {
return null;
}
}
}
After View view = inflater.inflate(resId, null); add the following:
if(position == 0){
view.findViewById(R.id.button).setOnClickListener(new OnClickListener() {
public void onClick(View v){
}
});
}

How to get the exact moment when a fragment is no longer visible to the user?

I'm trying to do a wizard to save a sheet with simple informations.
I spent hours looking for solutions, but all of them didn't work.
I have an activity whose layout has just a ViewPager. This activity has two fragments (A and B).
When the first one is complete, the user clicks on the "Next Button" on action bar and then the second one should appear. At this point, when the user finish the process, he clicks on "Save Button" and the sheet should be saved.
I've tried to do this in several ways, but none of them have worked:
For now, I'm trying to know when the fragment A turns invisible (i.e., when the fragment B is occupating all the screen), then I will be able to send its data using an interface (like explained here). The problem is that I don't know which method to use...
I've already tried all the lifecycle methods but none of them has worked.
Does anybody have any suggestion?
You can use ViewPager.onPageScrolled() to detect when the user scrolls the ViewPager.
Check the ViewPager Ref Docs
I've modified Reference Docs example to demonstrate storing information from the first fragment into a variable in the parent activity when the user scrolls. I've included all the java and xml files, so you should be able to just cut and paste into a new project to get it running.
MainActivity.java
public class MainActivity extends FragmentActivity {
private static final int NUM_ITEMS = 2;
private MyAdapter mAdapter;
private ViewPager mPager;
private boolean mStoredCheckBoxState;
private TextView mStoredCheckBoxStateNotification;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStoredCheckBoxStateNotification = (TextView)findViewById(R.id.storedCheckState);
mAdapter = new MyAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
mPager.setAdapter(mAdapter);
mPager.setOnPageChangeListener(new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int arg0) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
FragmentA frag = (FragmentA)mAdapter.getItem(0);
// Store the checked state value from Fragment A
mStoredCheckBoxState = frag.getCheckBoxState();
// Update the UI with the stored CheckBox state value
if (mStoredCheckBoxState == true) {
mStoredCheckBoxStateNotification.setText("Checked");
} else {
mStoredCheckBoxStateNotification.setText("Not Checked");
}
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
});
}
public static class MyAdapter extends FragmentPagerAdapter {
private FragmentA mFragA = new FragmentA();
private FragmentB mFragB = new FragmentB();
public MyAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return mFragA;
case 1:
return mFragB;
}
return null;
}
#Override
public int getCount() {
return NUM_ITEMS;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stored Checkbox State : " />
<TextView
android:id="#+id/storedCheckState"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Not Checked" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1">
</android.support.v4.view.ViewPager>
</LinearLayout>
FragmentA.java
public class FragmentA extends Fragment {
private CheckBox mCheckBox;
private boolean mCheckBoxState;
// Getters
public boolean getCheckBoxState() {
return mCheckBoxState;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_a, container, false);
mCheckBox = (CheckBox)view.findViewById(R.id.checkBox1);
mCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
mCheckBoxState = isChecked;
}
});
return view;
}
}
fragment_a.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment A" />
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="CheckBox" />
</LinearLayout>
FragmentB.java
public class FragmentB extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_b, container, false);
return view;
}
}
fragment_b.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment B" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</LinearLayout>
Ok, I solved this problem overriding the method [setUserVisibleHint(boolean)][1] and checking the parameter! ;)

Android view pager with page indicator

I need to get page indicator in the view pager file with images. Here is my code.
public class IndicatorActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
MyPagerAdapter adapter = new MyPagerAdapter();
ViewPager myPager = (ViewPager) findViewById(R.id.pager);
myPager.setAdapter(adapter);
myPager.setCurrentItem(0);
TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat);
indicator.setViewPager( myPager );
}
}
In this code, i got an error in TitlePageIndicator indicator = (TitlePageIndicator)findViewById(R.id.indicat); as TitlePageIndicator cannot be resolved to a type. What is this error. How can I resolve it?
here is my xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<com.viewpagerindicator.TitlePageIndicator
android:id="#+id/indicat"
android:layout_height="wrap_content"
android:layout_width="fill_parent" />
</LinearLayout>
What code do I need to write in the TitlePageIndicator?
I want to do this without using fragments.
Me also created a class such as:
class MyPagerAdapter extends PagerAdapter {
private static Integer[] titles = new Integer[]
{
R.drawable.jk,R.drawable.lm,R.drawable.no
};
public int getCount() {
return 3;
}
public Object instantiateItem(View collection, int position) {
LayoutInflater inflater = (LayoutInflater) collection.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view;
ImageView iv;
view = inflater.inflate(R.layout.first, null);
((ViewPager) collection).addView(view, 0);
switch (position) {
case 0:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
case 1:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
case 2:
iv = (ImageView)view.findViewById(R.id.imageView1);
iv.setImageResource(titles[position]);
break;
}
return view;
}
public void destroyItem(View arg0, int arg1, Object arg2) {
((ViewPager) arg0).removeView((View) arg2);
}
public boolean isViewFromObject(View arg0, Object arg1) {
return arg0 == ((View) arg1);
}
}
Did I want to do anything more than this class?
Thanks in advance for help
UPDATE: 22/03/2017
main fragment layout:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RadioGroup
android:id="#+id/page_group"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="#dimen/margin_help_container"
android:orientation="horizontal">
<RadioButton
android:id="#+id/page1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:checked="true" />
<RadioButton
android:id="#+id/page2"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<RadioButton
android:id="#+id/page3"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RadioGroup>
</FrameLayout>
set up view and event on your fragment like this:
mViewPaper = (ViewPager) view.findViewById(R.id.viewpager);
mViewPaper.setAdapter(adapder);
mPageGroup = (RadioGroup) view.findViewById(R.id.page_group);
mPageGroup.setOnCheckedChangeListener(this);
mViewPaper.addOnPageChangeListener(this);
*************************************************
*************************************************
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
// when current page change -> update radio button state
int radioButtonId = mPageGroup.getChildAt(position).getId();
mPageGroup.check(radioButtonId);
}
#Override
public void onPageScrollStateChanged(int state) {
}
#Override
public void onCheckedChanged(RadioGroup radioGroup, int checkedId) {
// when checked radio button -> update current page
RadioButton checkedRadioButton = (RadioButton)radioGroup.findViewById(checkedId);
// get index of checked radio button
int index = radioGroup.indexOfChild(checkedRadioButton);
// update current page
mViewPaper.setCurrentItem(index), true);
}
custom checkbox state: Custom checkbox image android
Viewpager tutorial: http://architects.dzone.com/articles/android-tutorial-using
Here are a few things you need to do:
1-Download the library if you haven't already done that.
2- Import into Eclipse.
3- Set you project to use the library:
Project-> Properties -> Android -> Scroll down to Library section, click Add... and select viewpagerindicator.
4- Now you should be able to import com.viewpagerindicator.TitlePageIndicator.
Now about implementing this without using fragments:
In the sample that comes with viewpagerindicatior, you can see that the library is being used with a ViewPager which has a FragmentPagerAdapter.
But in fact the library itself is Fragment independant. It just needs a ViewPager.
So just use a PagerAdapter instead of a FragmentPagerAdapter and you're good to go.
I know this has already been answered, but for anybody looking for a simple, no-frills implementation of a ViewPager indicator, I've implemented one that I've open sourced. For anyone finding Jake Wharton's version a bit complex for their needs, have a look at https://github.com/jarrodrobins/SimpleViewPagerIndicator.
I have also used the SimpleViewPagerIndicator from #JROD. It also crashes as described by #manuelJ.
According to his documentation:
SimpleViewPagerIndicator pageIndicator = (SimpleViewPagerIndicator) findViewById(R.id.page_indicator);
pageIndicator.setViewPager(pager);
Make sure you add this line as well:
pageIndicator.notifyDataSetChanged();
It crashes with an array out of bounds exception because the SimpleViewPagerIndicator is not getting instantiated properly and the items are empty. Calling the notifyDataSetChanged results in all the values being set properly or rather reset properly.
Just an improvement to the nice answer given by #vuhung3990.
I implemented the solution and works great but if I touch one radio button it will be selected and nothing happens.
I suggest to also change page when a radio button is tapped. To do this, simply add a listener to the radioGroup:
mPager = (ViewPager) findViewById(R.id.pager);
final RadioGroup radioGroup = (RadioGroup)findViewById(R.id.radiogroup);
radioGroup.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
#Override
public void onCheckedChanged(RadioGroup group, int checkedId) {
switch (checkedId) {
case R.id.radioButton :
mPager.setCurrentItem(0, true);
break;
case R.id.radioButton2 :
mPager.setCurrentItem(1, true);
break;
case R.id.radioButton3 :
mPager.setCurrentItem(2, true);
break;
}
}
});
you have to do following:
1-Download the full project from here https://github.com/JakeWharton/ViewPagerIndicator
ViewPager Indicator
2- Import into the Eclipse.
After importing if you want to make following type of screen then follow below steps -
change in
Sample circles Default
package com.viewpagerindicator.sample;
import android.os.Bundle;
import android.support.v4.view.ViewPager;
import com.viewpagerindicator.CirclePageIndicator;
public class SampleCirclesDefault extends BaseSampleActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.simple_circles);
mAdapter = new TestFragmentAdapter(getSupportFragmentManager());
mPager = (ViewPager)findViewById(R.id.pager);
// mPager.setAdapter(mAdapter);
ImageAdapter adapter = new ImageAdapter(SampleCirclesDefault.this);
mPager.setAdapter(adapter);
mIndicator = (CirclePageIndicator)findViewById(R.id.indicator);
mIndicator.setViewPager(mPager);
}
}
ImageAdapter
package com.viewpagerindicator.sample;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
public class ImageAdapter extends PagerAdapter {
private Context mContext;
private Integer[] mImageIds = { R.drawable.about1, R.drawable.about2,
R.drawable.about3, R.drawable.about4, R.drawable.about5,
R.drawable.about6, R.drawable.about7
};
public ImageAdapter(Context context) {
mContext = context;
}
public int getCount() {
return mImageIds.length;
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
#Override
public Object instantiateItem(ViewGroup container, final int position) {
LayoutInflater inflater = (LayoutInflater) container.getContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View convertView = inflater.inflate(R.layout.gallery_view, null);
ImageView view_image = (ImageView) convertView
.findViewById(R.id.view_image);
TextView description = (TextView) convertView
.findViewById(R.id.description);
view_image.setImageResource(mImageIds[position]);
view_image.setScaleType(ImageView.ScaleType.FIT_XY);
description.setText("The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level"
+ "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level"
+ "The natural habitat of the Niligiri tahr,Rajamala Rajamala is 2695 Mts above sea level");
((ViewPager) container).addView(convertView, 0);
return convertView;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == ((View) object);
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
((ViewPager) container).removeView((ViewGroup) object);
}
}
gallery_view.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/about_bg"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/about_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="1" >
<LinearLayout
android:id="#+id/about_layout1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".4"
android:orientation="vertical" >
<ImageView
android:id="#+id/view_image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/about1">
</ImageView>
</LinearLayout>
<LinearLayout
android:id="#+id/about_layout2"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight=".6"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="SIGNATURE LANDMARK OF MALAYSIA-SINGAPORE CAUSEWAY"
android:textColor="#000000"
android:gravity="center"
android:padding="18dp"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearance" />
<ScrollView
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:fillViewport="false"
android:orientation="vertical"
android:scrollbars="none"
android:layout_marginBottom="10dp"
android:padding="10dp" >
<TextView
android:id="#+id/description"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:textColor="#000000"
android:text="TextView" />
</ScrollView>
</LinearLayout>
</LinearLayout>
You Can create a Linear layout containing an array of TextView (mDots).
To represent the textView as Dots provide this HTML source in your code .
refer my code .
I got this information from Youtube Channel TVAC Studio .
here the code : `
addDotsIndicator(0);
viewPager.addOnPageChangeListener(viewListener);
}
public void addDotsIndicator(int position)
{
mDots = new TextView[5];
mDotLayout.removeAllViews();
for (int i = 0; i<mDots.length ; i++)
{
mDots[i]=new TextView(this);
mDots[i].setText(Html.fromHtml("•")); //HTML for dots
mDots[i].setTextSize(35);
mDots[i].setTextColor(getResources().getColor(R.color.colorAccent));
mDotLayout.addView(mDots[i]);
}
if(mDots.length>0)
{
mDots[position].setTextColor(getResources().getColor(R.color.orange));
}
}
ViewPager.OnPageChangeListener viewListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageScrolled(int position, float positionOffset, int
positionOffsetPixels) {
}
#Override
public void onPageSelected(int position) {
addDotsIndicator(position);
}
#Override
public void onPageScrollStateChanged(int state) {
}
};`

Categories

Resources