I have an activity that has 2 buttons (Previous, Next) and a text view in between the two. Below that is a viewpager.
I would like to change the textview to the title of whatever page they are on.
Here is the class where it all happens:
public class MyWorkout extends BaseActivity {
NonSwipeableViewPager vp;
TextView txtViewTitle;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.my_workout);
mToolBar = activateToolbar();
setUpNavigationDrawer();
getSupportActionBar().setTitle("My Workout");
Intent intent = getIntent();
String workouttitle = intent.getStringExtra("workout");
switch(workouttitle) {
case "w29" : {
getSupportActionBar().setTitle("My Workout - Workout 29");
break;
}
case "w30" : {
getSupportActionBar().setTitle("My Workout - Workout 30");
break;
}
}
Button btnPrev = (Button) findViewById(R.id.btnPrevious);
Button btnNext = (Button) findViewById(R.id.btnNext);
vp = (NonSwipeableViewPager) findViewById(R.id.view_pager);
txtViewTitle = (TextView) findViewById(R.id.txtViewTitle);
this.addPages(vp);
//THIS IS WHAT I HAVE TRIED
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MoveNext();
}
});
btnPrev.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
MovePrevious();
}
});
}
public void MoveNext() {
//it doesn't matter if you're already in the last item
vp.setCurrentItem(vp.getCurrentItem() + 1);
}
public void MovePrevious() {
//it doesn't matter if you're already in the first item
vp.setCurrentItem(vp.getCurrentItem() - 1);
}
private void addPages(NonSwipeableViewPager pager) {
MyFragPagerAdapter adapter = new MyFragPagerAdapter(getSupportFragmentManager());
Intent intent = getIntent();
String workout = intent.getStringExtra("workout");
Bundle args = new Bundle();
args.putString("workout", workout);
Week1Mon week1MonFragment = new Week1Mon();
week1MonFragment.setArguments(args);
adapter.addPage(week1MonFragment);
Week1Wed week1WedFragment = new Week1Wed();
week1WedFragment.setArguments(args);
adapter.addPage(week1WedFragment);
Week1Fri week1Fri = new Week1Fri();
week1Fri.setArguments(args);
adapter.addPage(week1Fri);
Week2Mon week2MonFragment = new Week2Mon();
week2MonFragment.setArguments(args);
adapter.addPage(week2MonFragment);
Week2Wed week2WedFragment = new Week2Wed();
week2WedFragment.setArguments(args);
adapter.addPage(week2WedFragment);
Week2Fri week2Fri = new Week2Fri();
week2Fri.setArguments(args);
adapter.addPage(week2Fri);
pager.setAdapter(adapter);
}
}
The part I have tried:
if (vp.getCurrentItem() == 0) {
txtViewTitle.setText("Week One, Monday");
}
if (vp.getCurrentItem() == 1) {
txtViewTitle.setText("Week One, Wednesday");
}
This sets the textview to Week One, Monday. Unfortunately it doesn't change when the user hits the Next button. I'm also hoping theres a better way, as I don't want to "assume" that the current item is that day of the week, because every week is different. I also need to figure out how to set the current page when the app is opened, because it saves the position is shared pref and I have to load it in the class above, and somehow set the page to a current page (preferably by the title)
Here is MyFragPagerAdapter:
public class MyFragPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> pages = new ArrayList<>();
public MyFragPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
return pages.get(position);
}
#Override
public int getCount() {
return pages.size();
}
public void addPage(Fragment f) {
pages.add(f);
}
#Override
public CharSequence getPageTitle(int position) {
return pages.get(position).toString();
}
}
Thanks for any help, let me know if you need to see more code!
You can use,
viewPager.addOnPageChangeListener(new OnPageChangeListener() {
public void onPageScrollStateChanged(int state) {}
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
public void onPageSelected(int position) {
// Check if this is the page you want.
}});
Related
I have and application in which I am using the Single activity and different fragments let say on activity start I call fragment A , and then after taking inputs I switch to fragment B and then Fragment C .
For Some reasons I have changed the Overflow Icon successfully from styles. But now The only problem is that for some reasons I want to show the overflow icons on Fragment B but not on Fragment A and C . for this I am doing this
public static void setOverflowButtonColor(final Activity activity, final int i) {
final String overflowDescription = activity.getString(R.string.abc_action_menu_overflow_description);
final ViewGroup decorView = (ViewGroup) activity.getWindow().getDecorView();
final ViewTreeObserver viewTreeObserver = decorView.getViewTreeObserver();
viewTreeObserver.addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
TintImageView overflow = null;
final ArrayList<View> outViews = new ArrayList<View>();
decorView.findViewsWithText(outViews, overflowDescription,
View.FIND_VIEWS_WITH_CONTENT_DESCRIPTION);
if (outViews.isEmpty()) {
return;
}
overflow = (TintImageView) outViews.get(0);
//overflow.setColorFilter(Color.CYAN);
overflow.setImageResource(R.drawable.my_overflow_image);
if (i == 1 && overflow!=null) {
overflow.setEnabled(false);
overflow.setVisibility(View.GONE);
} else if (overflow != null) {
overflow.setEnabled(true);
overflow.setVisibility(View.VISIBLE);
}
overflow.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(activity, "Overflow", Toast.LENGTH_SHORT).show();
}
});
removeOnGlobalLayoutListener(decorView, this);
}
});
}
public static void removeOnGlobalLayoutListener(View v, ViewTreeObserver.OnGlobalLayoutListener listener) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN) {
v.getViewTreeObserver().removeGlobalOnLayoutListener(listener);
}
else {
v.getViewTreeObserver().removeOnGlobalLayoutListener(listener);
}
}
So from Fragment A I am sending 1 in parameter so to hide the Icon but from Activity B I am sending 0 in parameter to re visible it , but it is not getting call.
Let me tell you this function working when it is called from Fragment A , I mean it is calling one time but not 2nd time or so on .
please tell me how to do this , if you know any other best method
Define an interface like below.
public interface FragmentHost{
public void onFragmentChange(int currentFragment);
}
Activity A should implement this interface.
class A extends Activity implents FragmentHost {
public static final int FRAGMENT_B = 0;
public static final int FRAGMENT_C = 1;
#Override
public void onFragmentChange(int currentFragment) {
if (currentFragment == FRAGMENT_A) {
// enable or disable button
} else if(currentFragment == FRAGMENT_B) {
// enable or disable button
}
}
}
And in each fragment . OnResume function call the onFragmentChange() method and pass the fragment id.
class B extends Fragment {
#Override
public void onResume() {
((FragmentHost) getParentActivity()).onFragmentChange(A.FRAGMENT_B);
}
}
I have an activity that manages instances of some sequential fragments and a ViewPager whose adapter is of type FragmentStatePagerAdapter. This fragments are sequentials because I want to create a sort of wizard that people must follow. All works fine, but I want to disable swipe when user wants to go to the next page (fragment) of the wizard if he has not completed all the fields in the current page.
package edu.polimi.dima.home121;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.ActionBarActivity;
import edu.polimi.dima.home121.model.Place;
/**
* Created by leo on 13/01/2015.
*/
public class NewPlaceActivity extends ActionBarActivity
implements NewPlaceSummaryFragment.OnFragmentInteractionListener,
NewPlaceWizardFirstStepFragment.OnFragmentInteractionListener,
NewPlaceWizardSecondStepFragment.OnFragmentInteractionListener,
NewPlaceWizardThirdStepFragment.OnFragmentInteractionListener,
NewPlaceWizardFourthStepFragment.OnFragmentInteractionListener {
private final String TAG = getClass().getSimpleName();
private static Place place;
private static final int NUM_PAGES = 7;
private ViewPager mPager;
private PagerAdapter mPagerAdapter;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
place = new Place();
setContentView(R.layout.activity_new_place);
mPager = (ViewPager) findViewById(R.id.pager);
mPagerAdapter = new ScreenSlidePagerAdapter(getSupportFragmentManager(), mPager, place);
mPager.setAdapter(mPagerAdapter);
/*if (savedInstanceState == null) {
getSupportFragmentManager().beginTransaction()
.add(R.id.newPlaceContainer, NewPlaceSummaryFragment.newInstance(place))
.commit();
}*/
}
#Override
public void onBackPressed() {
if (mPager.getCurrentItem() == 0) {
// If the user is currently looking at the first step, allow the system to handle the
// Back button. This calls finish() on this activity and pops the back stack.
super.onBackPressed();
} else {
// Otherwise, select the previous step.
mPager.setCurrentItem(mPager.getCurrentItem() - 1);
}
}
#Override
public void onFragmentInteraction(Uri uri) {
}
private class ScreenSlidePagerAdapter extends FragmentStatePagerAdapter {
private Place place;
private ViewPager mPager;
public ScreenSlidePagerAdapter(FragmentManager fm, ViewPager mPager, Place place) {
super(fm);
this.place = place;
this.mPager = mPager;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return new NewPlace0();
case 1:
NewPlaceWizardFirstStepFragment f1 = new NewPlaceWizardFirstStepFragment().newInstance(place);
f1.setPager(mPager);
return f1;
case 2:
NewPlaceWizardSecondStepFragment f2 = new NewPlaceWizardSecondStepFragment().newInstance(place);
f2.setPager(mPager);
return f2;
case 3:
NewPlaceWizardThirdStepFragment f3 = new NewPlaceWizardThirdStepFragment().newInstance(place);
f3.setPager(mPager);
return f3;
case 4:
NewPlaceWizardFourthStepFragment f4 = new NewPlaceWizardFourthStepFragment().newInstance(place);
f4.setPager(mPager);
return f4;
case 5:
NewPlace99 f5 = new NewPlace99();
return f5;
case 6:
NewPlaceSaving last = NewPlaceSaving.newInstance(place);
return last;
default:
return null;
}
}
#Override
public int getCount() {
//TODO sistemare il conteggio delle pagine
return NUM_PAGES;
}
}
}
For example the first fragment I have is:
public class NewPlaceWizardFirstStepFragment extends Fragment implements View.OnClickListener,
DialogInterface.OnClickListener {
private final String TAG = this.getClass().getSimpleName();
private static Place place;
private OnFragmentInteractionListener mListener;
private static View view;
private ViewPager pager;
public NewPlaceWizardFirstStepFragment() {
}
private TextView lblAvailableFrom;
private Button btnSingleRoom;
private Button btnDoubleRoom;
private Button btnStudioFlat;
private Button btnApartment;
private Button btnChangeDate;
private Button btnNext;
private EditText tfStreet;
private EditText tfCivic;
private EditText tfStair;
private EditText tfFloor;
private EditText tfCity;
private EditText tfPostalCode;
public static NewPlaceWizardFirstStepFragment newInstance(Place place) {
NewPlaceWizardFirstStepFragment fragment = new NewPlaceWizardFirstStepFragment();
fragment.place = place;
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_new_place_wizard_first_step, container, false);
setupUI();
return view;
}
public void setupUI() {
btnNext = (Button) view.findViewById(R.id.btnNext);
btnNext.setOnClickListener(this);
btnSingleRoom = (Button) view.findViewById(R.id.btnSingleRoom);
btnSingleRoom.setOnClickListener(this);
btnDoubleRoom = (Button) view.findViewById(R.id.btnDoubleRoom);
btnDoubleRoom.setOnClickListener(this);
btnStudioFlat = (Button) view.findViewById(R.id.btnStudioFlat);
btnStudioFlat.setOnClickListener(this);
btnApartment = (Button) view.findViewById(R.id.btnApartment);
btnApartment.setOnClickListener(this);
btnChangeDate = (Button) view.findViewById(R.id.changeDate);
btnChangeDate.setOnClickListener(this);
lblAvailableFrom = (TextView) view.findViewById(R.id.lblAvailableFrom);
tfStreet = (EditText) view.findViewById(R.id.tfStreet);
tfCivic = (EditText) view.findViewById(R.id.tfCivic);
tfStair = (EditText) view.findViewById(R.id.tfStair);
tfFloor = (EditText) view.findViewById(R.id.tfFloor);
tfCity = (EditText) view.findViewById(R.id.tfCity);
tfPostalCode = (EditText) view.findViewById(R.id.tfPostalAddress);
}
AlertDialog availableFromDialog;
private boolean enoughDataToContinue() {
boolean ret = true;
ret &= tfStreet.getText().length() != 0;
ret &= tfCivic.getText().length() != 0;
ret &= tfCity.getText().length() != 0;
return ret;
}
private void collectUserData() {
Address a = new Address();
a.setStreet(String.valueOf(tfStreet.getText()));
a.setNumber(String.valueOf(tfCivic.getText()));
a.setFloor(String.valueOf(tfFloor.getText()));
a.setStair(String.valueOf(tfStair.getText()));
a.setCity(String.valueOf(tfCity.getText()));
a.setPostalCode(String.valueOf(tfPostalCode.getText()));
place.setApartment(new Apartment());
place.getApartment().setAddress(a);
MiscServices.setCoordinates(place.getApartment());
Log.d(TAG, "first step done:" + new ObjectMapper().convertValue(place, Map.class).toString());
}
//TODO aggionare il toast
private void notifyUserOfInputIssues() {
Toast.makeText(getActivity().getApplicationContext(), "Complete all the fields to continue",
Toast.LENGTH_LONG).show();
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.btnNext:
if (enoughDataToContinue()) {
collectUserData();
pager.setCurrentItem(pager.getCurrentItem() + 1, true);
}
else
notifyUserOfInputIssues();
break;
case R.id.changeDate:
AvailableFromDialogBuilder availableFromDialogBuilder = new AvailableFromDialogBuilder(getActivity());
if (place.getAvailableFrom() != null)
availableFromDialogBuilder.setValue(place.getAvailableFrom());
availableFromDialogBuilder.setPositiveButton(R.string.my_continue, this);
availableFromDialog = availableFromDialogBuilder.show();
break;
case R.id.btnSingleRoom:
Log.v(TAG, "SingleRoom pressed");
clearSelection();
btnSingleRoom.setSelected(true);
place.setAccomodationType(Place.SINGLE_ROOM);
break;
case R.id.btnDoubleRoom:
Log.v(TAG, "DoubleRoom pressed");
clearSelection();
btnDoubleRoom.setSelected(true);
place.setAccomodationType(Place.DOUBLE_ROOM);
break;
case R.id.btnApartment:
Log.v(TAG, "Apartment pressed");
clearSelection();
btnApartment.setSelected(true);
place.setAccomodationType(Place.STANDARD_RENT);
break;
case R.id.btnStudioFlat:
Log.v(TAG, "StudioFlat pressed");
clearSelection();
btnStudioFlat.setSelected(true);
place.setAccomodationType(Place.STUDIO_FLAT);
break;
default:
Log.e(TAG, getResources().getString(R.string.error_ID) + v.getId());
}
}
public void clearSelection() {
btnApartment.setSelected(false);
btnSingleRoom.setSelected(false);
btnDoubleRoom.setSelected(false);
btnStudioFlat.setSelected(false);
}
;
#Override
public void onClick(DialogInterface dialog, int which) {
if (dialog == availableFromDialog) {
DatePicker datePicker = (DatePicker) availableFromDialog.findViewById(R.id.dpAvailableFrom);
Calendar cal = Calendar.getInstance();
cal.set(Calendar.DAY_OF_MONTH, datePicker.getDayOfMonth());
cal.set(Calendar.MONTH, datePicker.getMonth());
cal.set(Calendar.YEAR, datePicker.getYear());
place.setAvailableFrom(cal.getTime());
lblAvailableFrom.setText(datePicker.getDayOfMonth() + " - " + cal.getDisplayName(Calendar.MONTH, Calendar.SHORT, getResources().getConfiguration().locale) + " - " + datePicker.getYear());
} else Log.e(TAG, getResources().getString(R.string.error_dialog));
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
try {
mListener = (OnFragmentInteractionListener) activity;
} catch (ClassCastException e) {
throw new ClassCastException(activity.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
public void setPager(ViewPager pager) {
this.pager = pager;
}
public ViewPager getPager() {
return pager;
}
public interface OnFragmentInteractionListener {
// TODO: valutare se serve
public void onFragmentInteraction(Uri uri);
}
}
How can I disable the swipe to the next fragment (right to left) even if the backward swipe (left to right) must be always enabled?
First, a little advice; For each of the following lines, there is no need to new since the method is static:
NewPlaceWizardFirstStepFragment f1 = NewPlaceWizardFirstStepFragment.newInstance(place);
That said, I'd probably just add some logic to your pager adapter to limit the count by the number of completed pages. i.e. If you're on step 4, count is 4. Upon completing step 4, increment the count to allow the user to swipe to the next fragment.
See this stack overflow post for an example:
ViewPager disable swiping to a certain direction
The best way to block page swipe is to control if edit text is filled inside onPageScrolled in PageViewActivity.java (my MainActivity)
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
Log.i("test", "onPageScrolled - position = " + position);
currentPage = position;
txt1 = (EditText) findViewById(R.id.name_input);
if(position == 0) {
if(txt1.getText().toString().trim().length() == 0) {
pager.setCurrentItem(0);
} else if(txt1.getText().toString().trim().length() > 0) {
// DO NOTHING
}
}
}
I'm having a bit of trouble having the ViewPager displaying and running properly with a single fragment class.
So a bit of background. The Activity itself is supposed to allow users to answer questions in a survey. Each question consists of a question title, an input box, and a submit button. I've made a layout and a corresponding fragment (QuestionFragment) class to hold all of this.
My idea is to have the ViewPager hold a bunch of QuestionFragment's and the user will be able to swipe to the questions they would like to answer or edit. Basically each page will be using a QuestionFragment, but each will contain a unique question.
However my implementation doesn't look like its working out. The first page in the activity will have the UI set up (page 1) but the rest of the pages will have the default layout xml applied.
Note: at the moment I'm trying to set up the UI with the index number. You can ignore the Question[] array and any reference to it as the UI doesnt use it at this time. However I do try to set the typeface, which only works on the first page. You can take a look at the screenshots at the bottom, "How many moons does Saturn have" is the default xml in the layout. I've also noticed that the textview displays question 9, instead of 1.
Here is the fragment
public class SurveyTakerFragment extends Fragment {
private Question question;
private int index;
private TextView tv_question_title;
private EditText et_sms_response;
private Button btn_submit_response;
private SharedPreferences sharedPrefs;
private Typeface typeface;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment_question, container, false);
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
Activity activity = getActivity();
tv_question_title = (TextView) activity.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) activity.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) activity.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}
public void setQuestion(Question question, int index){
this.question = question;
this.index = index;
}
public void setSharedPrefs(SharedPreferences sharedPrefs){
this.sharedPrefs = sharedPrefs;
}
public void setTypeface(Typeface typeface){
this.typeface = typeface;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
Here is the adapter:
public class SurveyTakerAdapter extends FragmentPagerAdapter{
private Question[] questions;
private SharedPreferences sharedPrefs;
private Typeface typeface;
public SurveyTakerAdapter(FragmentManager fm, Question[] questions,
SharedPreferences sharedPrefs, Typeface typeface) {
super(fm);
this.questions = questions;
this.sharedPrefs = sharedPrefs;
this.typeface = typeface;
}
#Override
public Fragment getItem(int index) {
SurveyTakerFragment surveyTakerFragment = new SurveyTakerFragment();
surveyTakerFragment.setQuestion(questions[index], index);
surveyTakerFragment.setSharedPrefs(sharedPrefs);
surveyTakerFragment.setTypeface(typeface);
return surveyTakerFragment;
}
#Override
public int getCount() {
return questions.length;
}
#Override
public CharSequence getPageTitle(int position) {
return "Question: " + (position + 1);
}
}
Pager Activity
public class SurveyTaker extends FragmentActivity{
private final String APP_DATA = "appData";
private SurveyTakerAdapter surveyTakerAdapter;
private ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_survey);
Question[] questions = new Question[10];
FragmentManager fragmentManager = getSupportFragmentManager();
SharedPreferences sharedPrefs = getSharedPreferences(APP_DATA, MODE_PRIVATE);
Typeface robot = Typeface.createFromAsset(getAssets(), "Roboto-Thin.ttf");
surveyTakerAdapter = new SurveyTakerAdapter(fragmentManager, questions, sharedPrefs, robot);
viewPager = (ViewPager) findViewById(R.id.vp_survey_taker);
viewPager.setOffscreenPageLimit(10);
viewPager.setAdapter(surveyTakerAdapter);
viewPager.setOnPageChangeListener(new OnPageChangeListener() {
#Override
public void onPageSelected(int index) {
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
});
}
}
I'm assuming this is the case because I'm using a single fragment to populate all the pages in the viewpager, but I don't know how else to go about this. Is my design bad? Or is there something im missing?
Any help would be greatly appreciated!
Thanks!
It looks like the R.id.tv_question_title, R.id.tv_question_title and R.id.tv_question_title views are part of your R.layout.fragment_question layout. You should be referencing those views through your SurveyTakerFragment instance using getView(), not through the Activity via getActivity(). You are also duplicating effort in onActivityCreated() and setup().
Replace your onActivityCreated() and setup() implementations with this:
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setup();
}
void setup(){
View view = getView();
tv_question_title = (TextView) view.findViewById(R.id.tv_question_title);
et_sms_response = (EditText) view.findViewById(R.id.et_sms_response);
btn_submit_response = (Button) view.findViewById(R.id.btn_submit_response);
tv_question_title.setTypeface(typeface);
tv_question_title.setText("Question: " + index);
//TODO: Set question title.
//TODO: Pre-fill previous answer if any.
btn_submit_response.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
String response = et_sms_response.getText().toString();
//TODO: Submit response.
}
});
}
I am using Sliding menu from library jfeinstein, i have two sliding menu 'menuLeft' 'menuRight' in my activity one from left side and one from right,i have toggle buttons for respective sliding menu,how ever if menuLeft is open and if i slide from right to left in order to close menuLeft ,menuRiht also gets opened,what can be the solution to avoid this misbehaviour
here's my activity which contains SlidingMenu's
public class ChatListActivity extends SherlockActivity {
private SlidingMenu menuLeft;
private SlidingMenu menuRight;
private Button btnSliderLeftToggle;
private Button btnSliderRightToggle;
private ListView lvSliderLeft;
private ListView lvSliderRight;
private int width;
private int height;
private DBContacts db;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
getWindow().requestFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.chatlist_layout);
db = new DBContacts(this);
WindowManager wmanager = (WindowManager) getSystemService(WINDOW_SERVICE);
Display display = wmanager.getDefaultDisplay();
width = display.getWidth();
height = display.getHeight();
menuLeft = new SlidingMenu(this);
menuRight = new SlidingMenu(this);
initLeftSlider();
initRightSlider();
btnSliderLeftToggle = (Button) findViewById(R.id.mnuSlidingleftToggle);
btnSliderLeftToggle.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
ChatListActivity.this.menuLeft.toggle();
}
});
btnSliderRightToggle = (Button) findViewById(R.id.mnuSlidingRightToggle);
btnSliderRightToggle.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
ChatListActivity.this.menuRight.toggle();
}
});
menuLeft.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderLeft = (ListView) findViewById(R.id.lvSlidingmenuLeft);
MySLidingMenuLeftAdapter adapter = new MySLidingMenuLeftAdapter(ChatListActivity.this,
R.layout.crow_listview_lvslidingleft_chatlist);
lvSliderLeft.setAdapter(adapter);
}
});
menuRight.setOnOpenedListener(new OnOpenedListener()
{
#Override
public void onOpened()
{
lvSliderRight = (ListView) findViewById(R.id.lvSlidingmenuRight);
String column[] = new String[] { DBContacts.USERNAME};
int[] viewId = { R.id.txtContactName};
Cursor dataBaseCursor = db.getAllContacts();
MySLidingMenuRightAdapter customContactListAdapter = new MySLidingMenuRightAdapter(
ChatListActivity.this, R.layout.crow_lvslidingmenu_right_chatlist, dataBaseCursor, column,
viewId, 0);
lvSliderRight.setAdapter(customContactListAdapter);
}
});
}
private void initRightSlider()
{
menuRight.setMode(SlidingMenu.RIGHT);
menuRight.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuRight.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuRight.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuRight.setMenu(R.layout.sliding_menu_chatlist_right);
menuRight.setFadeDegree(0.35f);
}
private void initLeftSlider()
{
menuLeft.setMode(SlidingMenu.LEFT);
menuLeft.setBehindOffsetRes(R.dimen.slidingmenu_offset);
menuLeft.setTouchModeAbove(SlidingMenu.TOUCHMODE_FULLSCREEN);
menuLeft.attachToActivity(this, SlidingMenu.SLIDING_CONTENT);
menuLeft.setMenu(R.layout.sliding_menu_chatlist_left);
menuLeft.setFadeDegree(0.35f);
}
#Override
public void onBackPressed()
{
super.onBackPressed();
System.exit(0);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
menu.add("Refresh");
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
new SendNativeContacts(this).execute();
return true;
}
}
I have never faced this issue when i used both Left and Right SlidingMenu but for your problem you can have look at SlidingMenu Issues and you can try this solution for your problem. It may help you.
EDIT :
try this
You will need a patch which calls onOpened and onClosed methods for right menu it may have been included in latest code. Write logs to check method calls.
slidingMenuLeft.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuRight.setSlidingEnabled(false);
}
});
slidingMenuLeft.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuRight.setSlidingEnabled(true);
}
});
slidingMenuRight.setOnOpenedListener(new OnOpenedListener() {
#Override
public void onOpened(int pos) {
slidingMenuLeft.setSlidingEnabled(false);
}
});
slidingMenuRight.setOnClosedListener(new OnClosedListener() {
#Override
public void onClosed() {
slidingMenuLeft.setSlidingEnabled(true);
}
});
I am trying to make a nice horizontal viewgroup with the ViewFlow class.
I want to combine it with buttons. For example, when I click button 2 I go to view 2 of the viewgroup. Is that possible? I currently got this:
public class HousingActivity extends BaseActivity implements OnClickListener {
ViewFlow viewFlow;
ListView listView;
HousingViewflowAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.housing_activity);
init();
}
private void init() {
viewFlow = (ViewFlow) findViewById(R.id.viewflow);
setOnClickListner(this, this, new int[] { R.id.housing_button1,
R.id.housing_button2, R.id.housing_button3,
R.id.housing_button4 });
adapter = new HousingViewflowAdapter(this);
viewFlow.setAdapter(adapter);
viewFlow.setOnViewSwitchListener(new ViewSwitchListener() {
#Override
public void onSwitched(View view, int position) {
}
});
listView = (ListView) findViewById(R.id.housing_view1_listview);
ArrayList<Community> communityList = new ArrayList<Community>();
// Some dummy data
for (int i = 0; i < 10; i++) {
communityList.add(new Community("Place " + new Random().nextInt(),
new Random().nextInt(), 000000000, ""));
}
listView.setAdapter(new MainListViewAdapter(this, 0, communityList));
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
// viewFlow.onConfigurationChanged(newConfig);
}
#Override
public void onClick(View v) {
super.onClick(v);
switch (v.getId()) {
case R.id.housing_button1:
break;
case R.id.housing_button2:
break;
case R.id.housing_button3:
break;
case R.id.housing_button4:
break;
}
}
}
Thanks in advance.
You can look into:
viewFlow.setSelection(int position)
Ah ! I found it !
viewFlow.setAdapter(adapter,number);
The number being the page you want to show