Alexandre Alexandre - 19 days ago 15
Android Question

My code seems to go twice in my switch case

I'm applying an example seen on the net to my code, to implement an introduction page (ViewPager with 4 pages).
For that I use the native code provided by Android Studio when creating a new activity.

I adapted the following code that write texte depending on the fragment we're on :

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
TextView textView = (TextView) rootView.findViewById(R.id.section_label);
textView.setText(getString(R.string.section_format, getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}


To the following code, with a switch, to display different thing (button color, texte, icon) depending on the fragment we're on:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getContext();

View rootView = inflater.inflate(R.layout.fragment_welcome, container, false);
TextView textViewTitle = (TextView) rootView.findViewById(R.id.txtViewWelcomeTitle);
TextView textViewDesc = (TextView) rootView.findViewById(R.id.txtViewWelcomeDesc);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageViewWelcome);

switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:

imageView.setImageResource(R.drawable.ic_menu_camera);
textViewTitle.setText(R.string.fragment_1_title);
textViewDesc.setText(R.string.fragment_1_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen1));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

/*btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// launch next page of the fragment.

}
});*/

break;

case 2:

imageView.setImageResource(R.drawable.ic_menu_gallery);
textViewTitle.setText(R.string.fragment_2_title);
textViewDesc.setText(R.string.fragment_2_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen2));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

break;

case 3:

imageView.setImageResource(R.drawable.ic_menu_manage);
textViewTitle.setText(R.string.fragment_3_title);
textViewDesc.setText(R.string.fragment_3_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen3));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

break;

case 4:

imageView.setImageResource(R.drawable.ic_menu_send);
textViewTitle.setText(R.string.fragment_4_title);
textViewDesc.setText(R.string.fragment_4_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen4));
btnNext.setText(R.string.start);
btnSkip.setVisibility(View.INVISIBLE);

break;
}
return rootView;
}


What I was hopping to see is 4 pages, with two button (Skip and Next) on 3 of them, and the Start button on the 4th.

What I see : The 2 button only on the 2 first pages, and the "start" button on the pages 3 and 4.

When I debug my app, it seems that my code go through the case 1, then through the case 2, and after that my app is displaying the case 1 page.
This means that, when I switch, my pages seems to be +1 of the position they should be (my page 1 is finally the 2, the 2 is the 3, ...).
The strange thing is that my texte and icons displayed are the right ones.

Hereunder is the complete WelcomActivity :

public class WelcomeActivity extends AppCompatActivity {

/**
* The {@link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {@link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {@link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;

/**
* The {@link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;

public static final String PREFS_NAME = "StartPref";
private PrefManager prefManager;
private static LinearLayout dotsLayout;
private static TextView[] dots;
private static Button btnSkip, btnNext;

@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

// Checking for first time launch - before calling setContentView()
/* prefManager = new PrefManager(this);
if (!prefManager.isFirstTimeLaunch()) {
goToHomePage();
finish();
}
*/
if (Build.VERSION.SDK_INT >= 21) {
getWindow().getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
}

setContentView(R.layout.activity_welcome);

Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());

// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);

dotsLayout = (LinearLayout) findViewById(R.id.layoutDots);
btnSkip = (Button) findViewById(R.id.btn_skip);
btnNext = (Button) findViewById(R.id.btn_next);

// making notification bar transparent
changeStatusBarColor();

btnSkip.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
goToHomePage(v);
}
});
}


@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_welcome, 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);
}

/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";

public PlaceholderFragment() {
}

/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Context context = getContext();

View rootView = inflater.inflate(R.layout.fragment_welcome, container, false);
TextView textViewTitle = (TextView) rootView.findViewById(R.id.txtViewWelcomeTitle);
TextView textViewDesc = (TextView) rootView.findViewById(R.id.txtViewWelcomeDesc);
ImageView imageView = (ImageView) rootView.findViewById(R.id.imageViewWelcome);

switch (getArguments().getInt(ARG_SECTION_NUMBER))
{
case 1:

imageView.setImageResource(R.drawable.ic_menu_camera);
textViewTitle.setText(R.string.fragment_1_title);
textViewDesc.setText(R.string.fragment_1_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen1));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

/*btnNext.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
// launch next page of the fragment.

}
});*/

break;

case 2:

imageView.setImageResource(R.drawable.ic_menu_gallery);
textViewTitle.setText(R.string.fragment_2_title);
textViewDesc.setText(R.string.fragment_2_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen2));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

break;

case 3:

imageView.setImageResource(R.drawable.ic_menu_manage);
textViewTitle.setText(R.string.fragment_3_title);
textViewDesc.setText(R.string.fragment_3_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen3));
btnNext.setText(R.string.next);
btnSkip.setVisibility(View.VISIBLE);

break;

case 4:

imageView.setImageResource(R.drawable.ic_menu_send);
textViewTitle.setText(R.string.fragment_4_title);
textViewDesc.setText(R.string.fragment_4_desc);

rootView.setBackgroundColor(ContextCompat.getColor(context, R.color.bg_screen4));
btnNext.setText(R.string.start);
btnSkip.setVisibility(View.INVISIBLE);

break;
}
return rootView;
}
}

/**
* A {@link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {

SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}

@Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a PlaceholderFragment (defined as a static inner class below).
return PlaceholderFragment.newInstance(position + 1);
}

@Override
public int getCount() {
// Show 4 total pages.
return 4;
}

@Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "SECTION 1";
case 1:
return "SECTION 2";
case 2:
return "SECTION 3";
case 3:
return "SECTION 4";
}
return null;
}
}

public void goToHomePage(View view) {
prefManager.setFirstTimeLaunch(false);
Intent intent = new Intent(this, HomePageActivity.class);
startActivity(intent);
}

/**
* Making notification bar transparent
*/
private void changeStatusBarColor() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT);
}
}

private void addBottomDots(int currentPage) {
dots = new TextView[mSectionsPagerAdapter.getCount()];

int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);

dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(this);
dots[i].setText(Html.fromHtml("&#8226;"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorsInactive[currentPage]);
dotsLayout.addView(dots[i]);
}

if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive[currentPage]);
}
}


And here are the fragment_welcome.xml, then the activity_welcome.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:background="@color/bg_screen1"
tools:context="com.example.avescera.remindme.WelcomeActivity$PlaceholderFragment" >

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center_horizontal"
android:orientation="vertical">

<ImageView
android:layout_width="@dimen/img_width_height"
android:layout_height="@dimen/img_width_height"
android:src="@drawable/ic_menu_manage"
android:id="@+id/imageViewWelcome" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="@android:color/white"
android:textSize="@dimen/slide_title"
android:textStyle="bold"
android:id="@+id/txtViewWelcomeTitle" />

<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:paddingLeft="@dimen/desc_padding"
android:paddingRight="@dimen/desc_padding"
android:textAlignment="center"
android:textColor="@android:color/white"
android:textSize="@dimen/slide_desc"
android:id="@+id/txtViewWelcomeDesc" />

</LinearLayout>




<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.example.avescera.remindme.WelcomeActivity">

<android.support.design.widget.AppBarLayout
android:id="@+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="@dimen/appbar_padding_top"
android:theme="@style/AppTheme.AppBarOverlay">

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="@style/AppTheme.PopupOverlay">

</android.support.v7.widget.Toolbar>

</android.support.design.widget.AppBarLayout>

<android.support.v4.view.ViewPager
android:id="@+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="@string/appbar_scrolling_view_behavior" />

<LinearLayout
android:id="@+id/layoutDots"
android:layout_width="match_parent"
android:layout_height="@dimen/dots_height"
android:layout_marginBottom="@dimen/dots_margin_bottom"
android:gravity="center"
android:orientation="horizontal"
android:layout_alignParentBottom="false"
android:layout_below="@+id/viewDots"
android:layout_gravity="bottom|center_horizontal"></LinearLayout>

<View
android:id="@+id/viewDots"
android:layout_width="match_parent"
android:layout_height="1dp"
android:alpha=".5"
android:background="@android:color/white"
android:layout_gravity="bottom|center" />

<Button
android:id="@+id/btn_next"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:background="@null"
android:text="@string/next"
android:textColor="@android:color/white"
android:layout_gravity="bottom|left" />

<Button
android:id="@+id/btn_skip"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:background="@null"
android:text="@string/skip"
android:textColor="@android:color/white"
android:layout_gravity="bottom|right" />

</android.support.design.widget.CoordinatorLayout>


If you need any furhter information, do not hesitate.

Alex

Answer

finally I find out how to answer to my issue (for those who would be interested in resolving this issue).

First of all, some more details. I used the built in activity tabbed-activity provided by android studio (2.2). This one allow to have fragment pager use, and it seems that the behavior (showing for example the first fragment, but in your code, you're already in the second one, did not correctly understood how this is working).

For me, I wanted to have 4 pages (my fragment pager with 4 parts) and 2 buttons at the bottom (attached to the activity xml and not to the fragment xml).

The solution is simlpe : add the fragment text update in the onCreateView() part and the button text update (and the onClickListener) in the OnCreate() part.

In the OnCreate() part, to know which page fragment is used, you'll have to use the ViewPager, with the addOnPageChangeListener on it. Then it's ok, you can add a switch in it to know which page, checking the value of the position (from 0 to the number of page - 1 you have).

Hope this helps.