I have several custom ImageViews which are added to a RelativeLayout dynamically.
public class EmptySoldierView extends ImageView {
.
.
#Override
public void onClick(View v) {
EmptySoldierView.this.getAnimation().cancel();
clickListener.onEmptySoldierClicked(player, regionID);
}
}
And I am trying to remove all of these custom views like this:
#Override
public void onEmptySoldierClicked(Player player, int areaID) {
player.addSoldier(areaID);
drawSoldier(player, areaID);
for (EmptySoldierView soldierView : emptySoldiers)
((ViewGroup) soldierView.getParent()).removeView(soldierView);
}
I know the correct way (documented) to remove views is using above approach. But it is not working in my case. I searched, read a lot of posts and struggled for three days before asking.
How to remove dynamically added custom views?
I figured it out. The problem is Animation. View is removed from layout however animation is still on image. animation.cancel() is not enough. I added setAnimation(null) before removing View.
Related
I'm trying to implement Floating Search View from this library: Floating Search View so everything working fine but when implementing this code
floatingSearchView.setOnSearchListener(new FloatingSearchView.OnSearchListener() {
#Override
public void onSuggestionClicked(final SearchSuggestion searchSuggestion) {
Log.d("ssssssss", "clicked");
mLastQuery = searchSuggestion.getBody();
}
#Override
public void onSearchAction(String query) {
}
});
it's not working please help me.
after 6 hours i found the answer my mistake was in xml design so i put floating search view first child in coordinator layout so i change it to the last child and it worked perfectly
I have a custom control inheriting from LinearLayout and I want to add a child control to it as soon as I know the size of this custom control.
What's the correct method to do this?
The below code is working fine:
yourView.getViewTreeObserver().addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
#Override
public boolean onPreDraw() {
//yourView.getWidth()
yourView.getViewTreeObserver().removeOnPreDrawListener(this);
return true;
}
});
Hope this will help.
you have to override onFinishInflate() method inside the custom layout. That is the callback called when all views has been inflated. Use this and addd your views.
This might help:
/**
* Great workaround to get view infos after it has been drawn
*/
mCustomView.post(new Runnable() {
#Override
public void run() {
//The code you want to run
}
});
I'm trying to do something like this:
When I go to this activity I have what is in black and some objects like EditText boxes.
Once I press the button I want those EditBoxes an other stuff that is up there to stay visible but unable to be edited (that's easy to do from code overriding onClick).
But at the same time I also want to load some layout down inside the same activity (from an xml) and change the button function to act over the objects of the new layout.
Could anyone give me an idea on how to do this two things staying in the same activity?
Update:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
editText1.setEnabled(false);
editText2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
}
});
}
On the first click I want the button to do that. But once it's pressed I want it to do another thing. How could I do that?
(that's easy to do from code overriding onClick).
Actually I would recommend enable or disable which is easier to trace by using
view.setEnabled(bool);
as for the other question I'd recommend adding the layout from the start with setting visibility to GONE and when needed set the visibility to VISIBLE
view.setVisibility(View.VISIBLE);
view.setVisibility(View.GONE);
Ok, I've realized it was a dumb question, just add a flag an edit it:
public void createButton(){
create_button.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!button_pressed) {
editText1.setEnabled(false);
edittext2.setEnabled(false);
hidden_layout.setVisibility(View.VISIBLE);
create_button.setText("New text");
button_pressed=true;
}
else{
create_button.setText("Second click");
create_button.setEnabled(false);
}
}
});
}
}
I'd like to go to (display) a specific item in my listview but without scrolling. I don't want any animation but I'd like to be instantaneously transported to the desired item.
I'm using a checkable listview : mylistview.setChoiceMode(1) .
I understood that mylistview.setSelection(position) is the solution, but when I use it, nothing happens (maybe because it's a checkable listview ?).
When I use mylistview.smoothScrollToPosition(position), it works well but I have obviously this scroll animation which I don't want.
What could I do ?
Thanks.
Try this out:
myListView.post(new Runnable()
{
#Override
public void run()
{
myListView.setSelection(pos);
View v = myListView.getChildAt(pos);
if (v != null)
{
v.requestFocus();
}
}
});
From this Google Developer list answer by Romain Guy Link
basically I want to encapsulate a simple component from code that I already have.
Basically it's a LinearLayout with buttons inside. These buttons will make changes to a ListView, and there is also some other small stuff that it will do.
Currently I have a XML layout with those, and I programmatically setup everything else: the buttons, the interaction between the list and the other small stuff.
Obviously I thought to myself, let's encapsulate this.
I started out trying to extend the LinearLayout and adding the buttons.
Already I have no idea how to inflate the buttons to add to the view
What method do I override to create this buttons just before the view gets created without messing with the measures and inflations, etc.
I've looked around but the custom components I see are either completely new components or components that simply add small functionality to the custom ones.
Is there some guidelines for doing this?
Good tutorials/examples?
Any help is appreciated. Thanks !
EDIT:
Okay, here is a little more specific stuff.
Basically I want to create a View that holds filter buttons for a ListView. This will be used in different places with different filters, so I need flexibility for the buttons.
Basically I'd like to do something like this:
CustomView view = new CustomView(activity);
view.addButton("Lala", new OnFilterClickListener {
onClick(ListView list, View v) {
// Do the filtering
}
});
mListView.addHeaderView(view);
I want the view to adapt it's weights for showing the buttons, show the user which filter is active, stuff like that.
But I still don't really know how to make those dynamically added buttons appear, where do I generate them, how to inflate them and stuff like that.
public class myLayout extends LinearLayout {
//...
public void addButton(String text, OnClickListener listener) {
Button newButton = new Button(mContext);
newButton.setText(text);
newButton.setOnClickListener(listener);
//Say we want the weights to be equal
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
addView(newButton, params);
}
//...
}
You can even do something to the view before dispatching the click like this:
public class myLayout extends LinearLayout {
//...
public void addButton(String text, final OnClickListener listener) {
Button newButton = new Button(mContext);
newButton.setText(text);
newButton.setOnClickListener(new OnClickListener(){
public void onClick(View v) {
//do whatever you want
//like change background of button or something
//finally
listener.onClick(v);
}
});
//Say we want the weights to be equal
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(0,LinearLayout.LayoutParams.Fill_PARENT, 1);
addView(newButton, params);
}
//...
}