I have a LinearLayout with adding some images dynamically with their click listeners.
When I'm clicking on an ImageView I want to remove Onclick listener from all the ImageViews to prevent clicking on any ImageView again.
Can anyone suggest any permanent idea without boolean variable.
Set the onClickListener to null:
imageView.setOnClickListener(null);
You could give your LinearLayout an id, and then loop over all children and set the listener to null;
LinearLayout layout = findViewById(R.id.imagesLayout);
View v = null;
for(int i=0; i<layout.getChildCount(); i++) {
if(v instanceOf ImageView) //you dont have to do this when there are only imageViews
v.setOnClickListener(null)
}
You can do this like:
imageView.setOnClickListener(null);
or
imageView.setClickable(false);
Related
I'm adding multiple Views by code into Layout. I need each new View to be above previous one(top of the parent layout).
EDIT: To be more accurate I'll describe what the app module should does. User start with clean screen and one button at the bottom of the screen. The button adds a View at the top of the screen. Next clicks should add next views above previous ones to make the newest View be on the top of a container. The app saves state and on restart user see views in the same order.
Call the following method from Button's onClick Event.
private final int LAYOUT_TOP_INDEX = 0;
private void addViewOnTop(View view){
if(layout != null && view !=null)
layout.addView(view, LAYOUT_TOP_INDEX);
}
where 'layout' is your Layout (e.g., LinearLayout) to which the View is to be added.
Would really need more information from you to give a more accurate answer, but if you're saying what i think you are then you can just add these views to a LinearLayout with orientation set to vertical.
And assuming you're iterating through a list to dynamically add views, instead of incrementing from 0, increment down from the size of the list.
for(int i = size; i >= 0; i--){
linearLayout.add(new TextView(Context));
}
View positions inside ViewGroups are defined by the LayoutParams
How does this happen? Views pass their LayoutParams to their parent ViewGroups
//100% programatic approach with simple LayoutParams
LinearLayout myLinearLayout = new LinearLayout(this);
//if the **parent** of the new linear layout is a FrameLayout
FrameLayout.LayoutParams layoutParams =
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.MATCH_PARENT);
//or if you have the XML file you don't have to worry about this
//myLinearLayout = (LinearLayout)findViewById(R.id.my_simple_linear_layout);
//you could have a LinkedList<TextView>
LinkedList<TextView> textViewList = new LinkedList<>();
//assuming the order is the correct order to be displayed
Iterator<TextView> descendingIterator = textViewList.descendingIterator();
while(descendingIterator.hasNext())
{
//just add each TextView programatically to the ViewGroup
TextView tView = descendingIterator.next();
myLinearLayout.addView(tView);
}
Just like we defined LayoutParams for the LinearLayout we could also define LayoutParams for the TextView
IMPORTANT: when setting LayoutParams you need to be sure they fit the VIEWGROUP, that is the parent of the View being added
private TextView textViewFactory(String myText) {
TextView tView = new TextView(getBaseContext());
//controling the position relatively to the PARENT
//because you are adding the textview to a LINEAR LAYOUT
LinearLayout.LayoutParams paramsExample =
new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1.0f);
tView.setLayoutParams(paramsExample);
//configuring the insides of the textview
//you can also do all kinds of stuff programatically
tView.setGravity(Gravity.CENTER_HORIZONTAL);
tView.setPadding(10, 10, 10, 10);
tView.setTypeface(Typeface.DEFAULT_BOLD);// (null, Typeface.BOLD_ITALIC);
tView.setTypeface(Typeface.SANS_SERIF);
tView.setTypeface(null, Typeface.ITALIC);
tView.setTypeface(Typeface.defaultFromStyle(R.style.AppTheme));
tView.setId(R.id.aux_info);
tView.setText(myText);
//.........all kinds of stuff really
return tView;
}
If you mean adding a view programmatically so that the new one is added above the previous one, instead of below it, then I suggest this:
Maintain an ArrayList with the items you want to turn into views
Put them into a ListView
When you want to add a new view that must appear at the top of the list, insert it as the first element of your ArrayList and recreate the ListView from it.
I have the following scenario:
In a layout, I have imageviews aligned horizontally and vertically as shown in image.
First, which layout should be used for this purpose? RelativeLayout or FrameLayout? ListView inside the layout?
Also, instead of writing setOnClickListener for every imageview, how can I write just one click listener to get the clicked imageview?
A GridView is perfect for this. For more information on GridView, look here.
As for your onClickListener question: there is no easy way to do this, but what you can do is something like this:
Have an array containing every ImageView id like so:
public static final int[] IMAGE_VIEWS = {R.id.imageView1, R.id.imageView2, R.id.imageView3 /*etc*/}; //Make sure to list from the first imageview to the last image view in correct order
Define an onClickListener
private View.OnClickListener imageViewListener = new View.OnClickListener() {
#Override
public void onClick(View v) {
for (int i = 0; i < IMAGE_VIEWS.length; i++) {
if (v.getId() == IMAGE_VIEWS[i]) {
doSomethingWithImageViewClick(i); //if ImageView 4 was clicked, variable 'i' will be 3 (because arrays start at index = 0).
}
}
}
}
Set the onClickListeners for all your imageViews:
final ImageView view1 = (ImageView) view.findViewById(R.id.imageView1);
view1.setOnClickListener(imageViewListener);
//etc
Use 'GridView' in the layout and use 'setOnItemClickListener' to handle click events,
You can find more details in below link
GridView.
GridView can achieve the effect. I think you can look at this Grid View.
I have to create a view like this -
There will be three items shown with a show more option
On Click of show more the list will grow to 10 items which will be scrollable .
What could be the optimal way to implement this kind of view ?
Use HorizontalScrollView
On the first population of the scrollview detect the click on the 4th position and on the 4th item click reload the data and call adapter.notifyDataSetChanged()
fro this purpose you can Add 4 element at first time.and detect which Item is clicked by user if this is on 3rd position then you can add more element using this code snippets
HorizontalScrollView scrollView = (HorizontalScrollView) findViewById(R.id.scrollView1);
LinearLayout topLinearLayout = new LinearLayout(this);
// topLinearLayout.setLayoutParams(android.widget.LinearLayout.LayoutParams.FILL_PARENT,android.widget.LinearLayout.LayoutParams.FILL_PARENT);
topLinearLayout.setOrientation(LinearLayout.HORIZONTAL);
for (int i = 0; i < 15; i++){
final ImageView imageView = new ImageView (this);
imageView.setTag(i);
imageView.setImageResource(R.drawable.ic_launcher);
topLinearLayout.addView(imageView);
imageView.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
// TODO Auto-generated method stub
Log.e("Tag",""+imageView.getTag());
}
});
}
scrollView.addView(topLinearLayout);
hope so it will work for you.
enjoy your coding time:)
Use RecyclerView instead.
set the LinearLayoutManager's orientation HORIZONTAL, like this:
recyclerView.setLayoutManager(new LinearLayoutManager(context, HORIZONTAL, false);
Good luck.
I am trying to add a View to a RelativeLayout in my OnClickListener.
montrolButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// myParent is a relative layout
// newChild is an ImageView
myParent.addView(newChild);
requestLayout();
}
});
I have looked at the HierarchyViewer, I don' see my new child being added.
Can you please tell me if I miss anything?
I just tried the very same code and it works as it should. There could be an issue with your variable myParent which is not the element you expect it to be.
Also I did not have to call requestLayout() for the added view to appear on the screen.
Possibly try to just explicitly get another part of your view and add it there to see what is happening. Also just to try, you may do this:
RelativeLayout rv = (RelativeLayout) this.findViewById(R.id.right3);
ProgressBar iv = new ProgressBar(this);
rv.addView(iv);
to see if there is anything wrong with your image view instead of myParent
In any case it works if both elements are OK - there is nothing else to do in an activity.
Do you set the layout attributes of the new view (image view)?
I have added a bunch of images to a ViewFlipper and now I am performing an onClick event in the flipper. For this I would like to know the current child position so that I can perform some operations in an array. Is there anyway to find out the position of the current child.
Use this to get the current Child position:
flipper.getDisplayedChild();
And this to set child number to view:
flipper.setDisplayedChild(8);
In addflipperimages(ViewFlipper flipper) method you are adding bunch of images to ViewFlipper for that you are creating imageview, set tag to imageview, set imageview clickable true then write onclick method to imageview. go through the fallowing code it may works for you
here ids[] is an array of image ids
private void addFlipperImages(ViewFlipper flipper) {
int imageCount = ids.length;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.FILL_PARENT,
RelativeLayout.LayoutParams.FILL_PARENT);
for (int count = 0; count <imageCount; count++) {
ImageView imageView = new ImageView(this);
Bitmap imbm = BitmapFactory.decodeResource(this.getResources(),ids[count]);
imageView.setImageBitmap(imbm);
imageView.setLayoutParams(params);
imageView.setTag(count);
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
int id=(Integer) v.getTag();
Toast.makeText(ImageSliderVertical.this, id+"", Toast.LENGTH_LONG).show();
}
});
flipper.addView(imageView);
flipper.setTag(count);
}
}
Make use of indexOfChild().
Check this Post
How can I programmatically display a ViewFlipper's second child?
May this works for you.
I used this flipper.indexOfChild(flipper.getCurrentView())