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
Related
I'm trying to get an AlertDialog to appear if my counter is above 10.
I have tried using the TextView variable peopleCount in the if statement but it does not work too. I know using TextView will not work but I need to know if there is a workaround.
private TextView peopleCount;
private ImageView plusOne;
private ImageView minusOne;
private ImageView reset;
private int counter;
private View.OnClickListener clickListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
switch(v.getId()) {
case R.id.ivPlusOne :
plusCounter();
break;
case R.id.ivMinusOne :
minusCounter();
break;
case R.id.ivReset :
initCounter();
break;
}
}
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_people);
peopleCount = (TextView)findViewById(R.id.tvPeopleCount);
plusOne = (ImageView)findViewById(R.id.ivPlusOne);
plusOne.setOnClickListener(clickListener);
minusOne = (ImageView)findViewById(R.id.ivMinusOne);
minusOne.setOnClickListener(clickListener);
reset = (ImageView)findViewById(R.id.ivReset);
reset.setOnClickListener(clickListener);
initCounter();
if( counter > 10) {
AlertDialog.Builder peopleAlert = new AlertDialog.Builder(PeopleActivity.this);
peopleAlert.setCancelable(false);
peopleAlert.setTitle("People Count High");
peopleAlert.setMessage("Please check and replenish inventory");
peopleAlert.setPositiveButton("OK", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialogPeople, int which) {
dialogPeople.cancel();
}
});
peopleAlert.show();
}
private void initCounter(){
counter = 0;
peopleCount.setText(counter + "");
}
private void plusCounter(){
counter++;
peopleCount.setText(counter + "");
}
private void minusCounter(){
counter--;
peopleCount.setText(counter + "");
}
I expected the AlertDialog to appear when counter reached 11 but nothing happens.
OnCreate only runs once, You need to move the if statement to a function and call it from your plusCounter() and minusCounter() functions.
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.
}});
How to move the first position of image to second position in ListView on Button click,so that last position of image will comes to first position.
my tried codes below;
public class MainActivity extends AppCompatActivity {
private int[] images = {R.drawable.img1,R.drawable.img2,R.drawable.img3,R.drawable.img4,R.drawable.img5};
ArrayList<ImageModel> imageModels;
#Override
protected void onCreate (Bundle savedInstanceState) {
super.onCreate (savedInstanceState);
setContentView (R.layout.activity_main);
imageModels = new ArrayList<ImageModel> ();
for (int i = 0; i < images.length; i++) {
ImageModel imgMode = new ImageModel ();
imgMode.setImageId (images[i]);
imageModels.add (imgMode);
Log.d ("chk",""+images[i]);
}
ListView lstView = (ListView)findViewById (R.id.lst_view);
lstView.setAdapter (new BaseAdapterr(MainActivity.this,imageModels));
Button btnClick = (Button)findViewById (R.id.btn_click);
btnClick.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick (View v) {
for (int i=0 ;i < imageModels.size ();i++){
}
}
});
}}
Try using getting the object index and then use setIndex or sth like that for changing index.
Tarik.
this is your btnClick Listener , try it :
Button btnClick = (Button)findViewById (R.id.btn_click);
btnClick.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick (View v) {
...
ImageView firstImage= listView.getChiledAt(0);// get the firstimage
ImageView secnodImage= listView.getChiledAt(1);// get the second image
listView.addView(firstImage,2);// we added the first image to the second index
listView.addView(secondImage,1);// we added the second image to the first index
myAdapter.notifyDataSetChanged();// to refresh the view
...
}
});
For a direct scroll:
lstView.setSelection(0);
For a smooth scroll:
lstView.smoothScrollToPosition(0);
so your code should be following
btnClick.setOnClickListener (new View.OnClickListener () {
#Override
public void onClick (View v) {
lstView.clearFocus();
lstView.post(new Runnable() {
#Override
public void run() {
//int index=0; set your position
lstView.setSelection(index);
}
});
}
});
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 have a listview in my activity.
first time I click listview item, it works fine.
when I press back button and comes back to this activity, I can't click on the item anymore.
this listview works fine with android2.
when i test with android 4, happens this problem.
I found this kind of problem here, and I've tried all 'descendentFocusabilty' thing,
but not solved yet.
I know i shouldn't post almost same question. but i couldn't find any answer works fine there.
please help me solve this problem.
here is my child1 activity
public class TabChild1 extends NavigationActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.tabchild1);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1);
adapter.add("red");
adapter.add("green");
adapter.add("blue");
ListView listView = (ListView) findViewById(R.id.listview);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
ListView listView = (ListView) parent;
String item = (String) listView.getItemAtPosition(position);
Intent intent = new Intent(TabChild1.this, TabChild2.class);
int iNum = position;
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_SINGLE_TOP);
goNextHistory("TabChild2", intent);
}
});
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
here is my NavigationActivity
public class NavigationActivity extends Activity {
public void goNextHistory(String id, Intent intent) {
NavigationGroupActivity parent = ((NavigationGroupActivity) getParent());
View view = parent.group.getLocalActivityManager()
.startActivity(id,intent)
.getDecorView();
parent.group.replaceView(view);
}
#Override
public void onBackPressed() {
NavigationGroupActivity parent = ((NavigationGroupActivity) getParent());
parent.back();
}
}
here is my NavigationGroupActivity
public class NavigationGroupActivity extends ActivityGroup {
ArrayList<View> history;
NavigationGroupActivity group;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
history = new ArrayList<View>();
group = this;
}
public void changeView(View v) {
history.remove(history.size() - 1);
history.add(v);
setContentView(v);
}
public void replaceView(View v) {
history.add(v);
setContentView(v);
}
public void back() {
if(history.size() > 1) {
history.remove(history.size() - 1);
setContentView(history.get(history.size() - 1));
} else {
finish();
}
}
#Override
public void onBackPressed() {
group.back();
return;
}
}
and my child1 layout file
<?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" >
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
i think You have to use finish() inside the onBackPressed() method of class TabChild1. and comment the line parent.back() inside the onBackPressed() method of class NavigationActivity.
I think this will work for you.