collapse expandablelistview when press back button - android

In my app I need to collapse ExpandableListView that already expanded when I press back button.
I can't find anything related online.
I have a large amount of data that should show in my expandable list view and I think it's so hard to collapse list by touching each Group manually.
Exp_List.setOnGroupCollapseListener(new OnGroupCollapseListener() {
#Override
public void onGroupCollapse(int groupPosition) {
Toast.makeText(getBaseContext(),Lessons_List.get(groupPosition)+ " is closed!", Toast.LENGTH_LONG).show();
}
});

Inside of onBackPressed, collapse all groups with a loop. If not groups collapsed, issue the default back press event:
#Override
public void onBackPressed() {
ExpandableListView elv;
boolean groupsCollapsed = false;
for (int i=0; i<elv.getCount(); ++i) {
if (elv.isGroupExpanded(i)) {
elv.collapseGroup(i);
groupsCollapsed = true;
}
}
// If no groups collapsed, call the default back button
if (!groupsCollapsed) {
super.onBackPressed();
}
}

Related

Closing the FAB sub menu on user touch

I am trying to implement an expandable fab, I implemented it and it works as expected i.e when the user taps on fab_main the submenu becomes visible and active, and when the user taps on the fab_main again the sub menu turn invisible and inactive.
What My problem is that I want the submenu to close (in case it is open) when the user taps on screen like for example the current activity
I have Tried using ontouch (which didn't work) and dispatchTouchEvent(which worked but I was not able to use the sub menu buttons
This is my code for how I implemented expandable FAB
fab_main.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
} else {
textView_img_edit.setVisibility(View.VISIBLE);
textview_bg_edit.setVisibility(View.VISIBLE);
fab2_bg_edit.startAnimation(fab_open);
fab1_img_edit.startAnimation(fab_open);
fab_main.startAnimation(fab_clock_wise);
fab2_bg_edit.setClickable(true);
fab1_img_edit.setClickable(true);
isOpen = true;
}
}
});
I used this code in both onTouch and dispatchTouchEvent
if (isOpen) {
textView_img_edit.setVisibility(View.INVISIBLE);
textview_bg_edit.setVisibility(View.INVISIBLE);
fab2_bg_edit.startAnimation(fab_close);
fab1_img_edit.startAnimation(fab_close);
fab_main.startAnimation(fab_anti_clock_wise);
fab2_bg_edit.setClickable(false);
fab1_img_edit.setClickable(false);
isOpen = false;
}
Try setting a onTouchListener on the base layout. You can check for MotionEvent.ACTION_UP and close your fab submenu (if it's open) there.

How to code a button with differents effects when pressed multiple times

I may be asking a basic question, but to be honest, I have no real developement or code knowledge. I've been requested to make a prototybe of some basic app, that is supposed mainly to be buttons on screens, activable or desactivable. I've written some kind of TL;DR in case my explanations are bad
I've been coding this on Android Studio 3.0, I (hardly) managed to place PNGs files on the screen, making it looking like a button.
Thing is, while some parts of the app are mainly constituted with independants togglable button. There a part where pressing a button must deselect the others. AND, if this button is pressed a second time open another activities.
Here's part of my code I'm using.
This one for independants buttons
indbutton1.setOnTouchListener(new View.OnTouchListener(){
// track if the image is selected or not
boolean isSelected = true;
public boolean onTouch(View v, MotionEvent event) {
if(isSelected) {
indbutton1.setImageResource(R.drawable.indbutton1slct);
} else {
indbutton1.setImageResource(R.drawable.indbutton1nosl);
}
// toggle the boolean
isSelected = !isSelected;
return false;
}
});
And this one for going into other activities
movements.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent gestesActivity = new Intent (getApplicationContext(), movements.class);
startActivity(movementsActivity);
finish();
}
});
TL;DR
How should I proceed to have a mix of pressing button, disabling others enabled, then, when pressed a second time, I go to another activity.
Thank you for any help :) -Pliskin
Here is how I would do that. Let's say you have 4 buttons.
// your class fields
boolean [] alreadyTouched = new boolean[4];
For each of the 4 buttons :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if(!alreadyTouched[0]){
setAlreadyTouched(0);
// one click actions here
}else{
// second click's action
}
}
});
Now you make a private method in your class :
private void setAlreadyTouched(int index){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i] = false;
if(index != -1)
alreadyTouched[index] = true;
}
And to reset your boolean array when the button looses the focus :
button0.setOnFocusChangeListener(new View.OnFocusChangeListener(){
#Override
public void onFocusChange(View view, boolean hasFocus){
if(!hasFocus)
setAlreadyTouched(-1);
}
});
You can do exactly the same thing but with an array of int if you want more than two clicks with some slight modifications. For example :
// your class fields
int[] alreadyTouched = new int[4];
Your privat method :
private void setAlreadyTouched(int index){
if(index == -1){
for (int i = 0; i< alreadyTouched.length; i++)
alreadyTouched[i]=0;
}else
alreadyTouched[index] = alreadyTouched[index] +1 ;
}
Then just add some if in your onClickListeners :
button0.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setAlreadyTouched(0);
switch(alreadyTouched[0]){
case 1:
// one click actions here
break:
case 2:
// second click's action
break:
// ... and so on
default:
// action for max number of clicks here.
}
}
});

Changes apply on the second tap

I have a RecyclerView and I am trying to make it with multiple selections so I can perform an action to all items selected.
I have to mention that in the fragment that holds one item I have a radio button(radioButton), some text, and a button(joinBtn).
What I want to do is that when I click the item, the radio button will become checked, and the button will become visible and enabled.
My class Group has a private boolean clicked = false by default.
In the ViewHolder I have the method bind :
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(group.isClicked())
radioButton.setChecked(true);
else
radioButton.setChecked(false);
}
And in the onClick method i have the actions I want to perform:
#Override
public void onClick(View view) {
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
My problem is the behaviour I get:
1st tap: radio button gets checked.
2nd tap: joinBtn appears.
3rd tap: radio button gets unchecked.
4th tap: joinBtn disappears.
How can I make the actions from 1st and 2nd tap to happen in one tap? The same for 3rd and 4th?
Try calling notifyItemChanged(int position) on the adapter instance after your onClick() logic. Pass the index of the item as the param.
The UI may not update by itself, but this function should refresh it.
Maintain a flag which will change on group click
boolean isChecked = false ;
public void bind(Group group) {
this.group = group;
mTitleTextView.setText(group.getName());
if(isChecked)
isChecked = false;
radioButton.setChecked(false);
else
isChecked = true;
radioButton.setChecked(true);
}
and then
#Override
public void onClick(View view) {
if (isChecked) {
joinBtn.setVisibility(View.GONE);
joinBtn.setEnabled(false);
group.setClicked(false);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setEnabled(true);
group.setClicked(true);
}
}
Apparently the problem was because of the radio button. The first thing I did was to use a ImageView instead of the radio button. In that ImageView I used (when needed) one of two pictures: selected or unselected.
In the bind method all I needed was: joinBtn.setVisibility(View.GONE); and radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
In the onClick method I used:
if (joinBtn.isShown()) {
joinBtn.setVisibility(View.GONE);
joinBtn.setClickable(false);
radioBtnImage.setImageResource(R.drawable.radio_btn_unselected);
}
else{
joinBtn.setVisibility(View.VISIBLE);
joinBtn.setClickable(true);
radioBtnImage.setImageResource(R.drawable.radio_btn_selected);
}
This did it for me!

Drag & Swipe in a RecyclerView List

I've been trying the ItemTouchHelper for Drag and Swipe actions in a RecyclerView List. I've used this guide and it's working perfectly, except that i can't make the swipe activate on LongClick. I've managed to do it for the Drag action:
textview.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
dragStartListener.onStartDrag(holder);
return false;
}
});
I don't know how to do it for the swipe, since it doesn't have an onStartSwipe() interface and any of my attempts to implement one, by mimicking the onStartDrag() interface, has failed.
How it can be made? Thank you for your time.
I've managed to implement the onStartSwipe() interface and it works by itself. However once i try to implement both drag and swipe on the same view, it acts strangely. More precisely, once i touch the view, for the entire hold down, only the drag or only the swipe animation would be activated.
Not only that, it is difficult to chose between the two actions since, it will get the first mirco movement you do to chose between either one or another.
I tried to this code for the above description:
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mDragStartListener.onStartDrag(holder);
return false;
}
});
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mSwipeStartListener.onStartSwipe(holder);
return true;
}
});
I also tried to use a framelayout like this:
tv.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mDragStartListener.onStartDrag(holder);
return true;
}
});
fl.setOnLongClickListener(new View.OnLongClickListener() {
#Override
public boolean onLongClick(View v) {
mSwipeStartListener.onStartSwipe(holder);
return true;
}
});
Here i drag the TextView and i swipe the FrameLayout. But since the text view is on top of the framelayout, it would only work dragging, and sometimes swipe if you touch on the corners of the item.
Any ideas on how to make both of drag and swipe work on the same item? I want to be able to either drag or swipe, once an element is LongClicked. I could use a separate ImageView for swiping and the TextView for dragging, but i'd rather not have to do that since it would ruin my design.
I am not sure why you are using draghelpers in recyclerview.
You can use ItemTouchHelper instead
It allows both drag and drop and swipe.
ItemTouchHelper
SimpleCallBack
Tutorial for implementation

How to close activity in android?

I have a menu button on the bottom and I have display menu icons from another activity like MenuTask.
But how to close that MenuTask activity if the user clicks again in the menu?
imgMenu.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
if (key == 0) {
// I want to show menu here
key = 1;
Intent intent = new Intent(MainActivity.this, MenuAction.class);
startActivityForResult(intent, 2);
setResult(0);
} else if (key == 1) {
// I want to Delete menu here
key = 0;
//onBackPressed();
finish();
}
}
});
Note: I think if i click second time , button could not fire. it means else part not execute.
Problem:
I have Clicked, right top menu button and open new activity menu action. but if i want to click again same menu button, i want to disappers that menu action. but menu button could not clicked.
How to make MenuButton Clickable? any idea? any other menthod? But when i click mobile backbutton menuaction layout disappeared.
Thanks in advance.
I thing this is not best way. You must use Fragment
If you want only kill activity MenuAction.this.finish();
#Override
public void onBackPressed() {
if (doubleBackToExitPressedOnce) {
super.onBackPressed();
return;
}
this.doubleBackToExitPressedOnce = true;
Toast.makeText(this, "Please click BACK again to exit", Toast.LENGTH_SHORT).show();
// handler to delay
new Handler().postDelayed(new Runnable() {
#Override
public void run() {
doubleBackToExitPressedOnce=false;
}
}, 2000);
}
you can go though this link Clicking the back button twice to exit an activity
same question

Categories

Resources