Set Default selection for GridView - android

Can anyone guide me how set particular element of grid view as selected ?
.setSelection(positionOfItem) is not working .
gridViewSize = (GridView) inflaterView.findViewById(R.id.grid_sizes);
gridViewSize.setAdapter(new PopupSizeAdapter(context,typeArr,1));
gridViewSize.setSelection(0);

it worked for me
mGridView.setSelection(pos);
mGridView.requestFocusFromTouch();
mGridView.setSelection(pos);

I came across the same issue today, following is my solution:
GridView.getViewTreeObserver.addOnGlobalLayoutListener(getLayoutListener(mGridView))
private static ViewTreeObserver.OnGlobalLayoutListener getLayoutListener(final GridView mGridView) {
ViewTreeObserver.OnGlobalLayoutListener listener = new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
View view = mGridView.getChildAt(0);
if(view != null) {
if(!view.isSelected()) {
view.setSelected(true);
} else {
// remove the listener after the first time.
if (Build.VERSION.SDK_INT < 16) {
mGridView.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mGridView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
}
}
};
return listener;
}
and also, if you are using android API >= 11, you can also add a View.OnLayoutChangeListener to GridView.

Try this:
grid.performItemClick(view, position, id);
Then my state drawable works correctly on GridView.

Related

Showcaseview library calling multiple times on Recyclerview's first item

I am using https://github.com/florent37/TutoShowcase this showcaseview library in my code.
It works fine in activity and fragment.But when I call in recyclerview item it shows multiple popups and gets blackout.
mRecyclerViewList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Logger.log("Call");
TextView textView = (TextView) mRecyclerViewList.getChildAt(0).findViewById(R.id.txt_add_tocart_btn);
Logger.log("Textview" + textView);
textView.setFocusableInTouchMode(true);
TutoShowcase.from((Activity) context).setContentView(R.layout.tuto_showcase_tuto_sample)
.setFitsSystemWindows(true).on(textView).addRoundRect(35).showOnce("1").show();
// unregister listener (this is important)
if (Build.VERSION.SDK_INT < 16) {
mRecyclerViewList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mRecyclerViewList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
How can I avoid multiple popup's?
Your question how to avoid Multiple popup's:
Just set a boolean value to avoid showing multiple times.
boolean isShown = false;
mRecyclerViewList.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Logger.log("Call");
if(!isShown){
TextView textView = (TextView) mRecyclerViewList.getChildAt(0).findViewById(R.id.txt_add_tocart_btn);
Logger.log("Textview" + textView);
textView.setFocusableInTouchMode(true);
TutoShowcase.from((Activity) context).setContentView(R.layout.tuto_showcase_tuto_sample)
.setFitsSystemWindows(true).on(textView).addRoundRect(35).showOnce("1").show();
isShown = true;
}
// unregister listener (this is important)
if (Build.VERSION.SDK_INT < 16) {
mRecyclerViewList.getViewTreeObserver().removeGlobalOnLayoutListener(this);
} else {
mRecyclerViewList.getViewTreeObserver().removeOnGlobalLayoutListener(this);
}
}
});
As described in documentation of OnGlobalLayoutListener:
to be invoked when the global layout state or the visibility of views within the view tree changes.
That's why you got many and many showcase views. Every time ViewTree changed, you generate showcase.
You don't need for ViewTreeObserver and GlobalLayoutListener.
Move
TutoShowcase.from
to onViewCreated for example.

How to disable BottomNavigationView click and Touch?

i want to implement behavior on certain condition the bottom view is unable to click, i want to make if bottom view item being click it does not navigate to that item but still stay at the current item
You can disable menu items if you want to disable bottom navigation view
private void enableBottomBar(boolean enable){
for (int i = 0; i < mBottomMenu.getMenu().size(); i++) {
mBottomMenu.getMenu().getItem(i).setEnabled(enable);
}
}
Kotlin style one-liner:
bottom_navigation.menu.forEach { it.isEnabled = false }
<android.support.design.widget.BottomNavigationView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="false"
android:contextClickable="false"/>
Try this code.it Disables the click.
Dynamicaly using Java pr Kotlin you can disable click.
bottomView.setEnabled(false);
bottomView.setFocusable(false);
bottomView.setFocusableInTouchMode(false);
bottomView.setClickable(false);
bottomView.setContextClickable(false);
bottomView.setOnClickListener(null);
setting onClick Listener to Null helps to Disable click events
bottomView.menu.forEach { it.isEnabled = false }
You can set the touch listeners of its subviews. Example using android-ktx:
bottomNav.children.forEach {
(it as? ViewGroup)?.children?.forEach {
it.setOnTouchListener { _, _ -> true } // or null to enable touch again
}
}
public class CustomBottomNavigationView extends BottomNavigationView {
...
#Override
public void setEnabled(boolean enabled) {
super.setEnabled(enabled);
ViewGroup menuView = (ViewGroup) getChildAt(0);
if (menuView != null) {
for (int i = 0; i < menuView.getChildCount(); i++) {
menuView.getChildAt(i).setEnabled(enabled);
}
}
}
}
You can do something like
bottomNavigation.menu.iterator().forEach { it.isEnabled = !error }

SwipeListView setSwipeMode none is disabling onclick as well as swipe

Hi am using a SwipeListView that has two buttons on its back view i'm trying to programatically set the SwipeListView so that it doesn't swipe by setting the swipe mode to none. the problem i am having is the front view is now not registering the click. Does any one know why???
Heres what i have tried so far
final SwipeListView messagesList = (SwipeListView)v.findViewById(R.id.list);
if(r_deleteMessages == false || r_markMessages == false) messagesList.setSwipeMode(SwipeListView.SWIPE_MODE_NONE);
if(messageData != null){
ViewGroup.LayoutParams params = messagesList.getLayoutParams();
int size;
size = messageData.size();
params.height = (SM_Global.pxFromDp(context, 80) * size) +3;
messagesList.setLayoutParams(params);
messagesList.requestLayout();
messagesList.setFocusable(false);
final MessagesAdapter messagesAdapter = new MessagesAdapter(context, R.layout.layout_message_item, messageData,messagesList,"profile");
messagesList.setAdapter(messagesAdapter);
Log.v("Auth","CAN READ MESSAGES | " + r_readMessages);
messagesList.setSwipeListViewListener(new BaseSwipeListViewListener() {
#Override
public void onClickFrontView(int position) {
super.onClickFrontView(position);
Log.v("Auth","CLICKED ");
}
});
You could add a Listener to the list view and override onChangeSwipeMode
if(r_deleteMessages == false || r_markMessages == false){
mList.setSwipeListViewListener(new BaseSwipeListViewListener() {
#Override
public int onChangeSwipeMode(int position) {
return SwipeListView.SWIPE_MODE_NONE;
}
});
}
This way you will still get touch events.
You may also need to do mList.setSwipeOpenOnLongPress(false); to disablr the long click
see: https://github.com/47deg/android-swipelistview/issues/9
I tried the suggested ways but didn't work for me. So I made changes in my List Adapter & handled the click event there. Now it is working fine.

Check if view element is added to layout or not programmatically

In my fragment class, I add a child view element programmatically to my layout conditionally :
LinearLayout child = (LinearLayout) inflater.inflate(R.layout.child_view, null);
LayoutParams params = new LayoutParams(LayoutParams.WRAP_CONTENT,100);
container.addView(child, params);
Since the above code will be run conditionally, so, at some point, I would like to check if the child view has added or not, how to make this checking programmatically?
If you creating view via inflater, you can check his parent
if(view.getParent() != null) {...}
I think you can simply use
findViewById(your_view_id)
method: If its result is null the view does not exists, otherwise the view is present
Sorry for late reply but you may try this alternative:
use container.getChildCount(); before adding and after adding a view. Like :
int x = container.getChildCount();
container.addView(child, params);
int y = container.getChildCount();
if(y > x)
Toast.makeText(context, "View Successfully Added!", Toas.LENGTH_SHORT).show();
Or if you have a view instance to find, you could:
if (container.indexOfChild(childView) == -1) {
// Add child to container.
}
With AndroidX you can use ViewGroup.contains(view: View): Boolean extension function.
I cannot write a comment so I write it here as a solution:
From API level 19 you can call isAttachedToWindow() which doesn't help a lot, but if you are aiming API 19 or higher, then this should work by the documentation.
maybe you can try this
child.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
child.getViewTreeObserver().removeOnGlobalLayoutListener(this);
// add to parent
}
});
or this one
child.addOnAttachStateChangeListener(new View.OnAttachStateChangeListener() {
#Override
public void onViewAttachedToWindow(View v) {
}
#Override
public void onViewDetachedFromWindow(View v) {
}
});

How to programmatically show next view in ViewPager?

I created a ViewPager and everything is working fine, however I want to have a previous next button outside of ViewPager which can be used to navigate inside ViewPager. How can I go to next Item on ViewPager without swiping manually?
As blessenm answered viewpager.setCurrentItem(int index) is the way to go.
A complete implementation just for completeness:
public void MoveNext(View view) {
//it doesn't matter if you're already in the last item
pager.setCurrentItem(pager.getCurrentItem() + 1);
}
public void MovePrevious(View view) {
//it doesn't matter if you're already in the first item
pager.setCurrentItem(pager.getCurrentItem() - 1);
}
Easiest way is:
nextButton.setOnClickListener { pager.arrowScroll(View.FOCUS_RIGHT) }
prevButton.setOnClickListener { pager.arrowScroll(View.FOCUS_LEFT) }
i fix it Better, ty Androiderson.
private void MoveNextTopSlideShow(View view)
{
if (_viewPager_TopImageSlide.CurrentItem == _viewPager_TopImageSlide.ChildCount)
{
if (_viewPager_TopImageSlide.ChildCount > 0)
{
_viewPager_TopImageSlide.SetCurrentItem(0,true);
}
}
else
{
//it doesn't matter if you're already in the last item
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.CurrentItem + 1, true);
}
}
private void MovePreviousTopSlideShow(View view)
{
if (_viewPager_TopImageSlide.CurrentItem == 0)
{
if (_viewPager_TopImageSlide.ChildCount > 0)
{
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.ChildCount-1, true);
}
}
else
{
//it doesn't matter if you're already in the first item
_viewPager_TopImageSlide.SetCurrentItem(_viewPager_TopImageSlide.CurrentItem - 1, true);
}
}

Categories

Resources