How to get ViewFlipper's current child position - android

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())

Related

How can I avoid overlapping when addView to RelativeLayout?

There are some Textview,I need to add them in RelativeLayout dynamically. How can I add them without overlap with previous ? thx :D
public class CusRelativeLayout extends RelativeLayout {
private String TAG = "CusRelativeLayout";
...
public void add(String s){
final int childCount = getChildCount();
final TextView tv = new TextView(context);
tv.setText(s);
tv.setTextColor(getResources().getColor(R.color.black));
LayoutParams params = (LayoutParams) generateDefaultLayoutParams();
int[] xy = getRandomXY();
if(childCount == 0){
//no view
params.setMargins(xy[0],xy[1],0,0);
}else{
//what shoud I do???
}
addView(tv,params);
}
...
}
It looks like this:
result
when I add the first tag, I just need to get random X and Y,and new a textview.I use the method setMargins(x,y,0,0) to adding in the RelativeLayout.
But when I add the second tag , I can use getTop(),getLeft(),getHeight(),getiWidth() to getting the previous position and range.And use getRandomXY() to getting random X and Y for the second.But I can't get the range of the second to determine whether the second overlaps with the first
I tried to do this
final TextView tv = new TextView(context);
tv.setText(s);
tv.setTextColor(getResources().getColor(R.color.black));
tv.setLayoutParams(new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,ViewGroup.LayoutParams.WRAP_CONTENT));
tv.post(new Runnable() {
#Override
public void run() {
Log.e(TAG,"height:" + tv.getHeight());
Log.e(TAG,"width:" + tv.getWidth());
}
});
but the value is 0
If I just determine by X and Y , It will be this case.determine by X and Y
I just want to get the range of the new tag before adding to the RelativeLayout ,so I can determine whether it will overlap with the previous.
if I can determine all the tag position before add in Relativelayout?(I know the exact number of tag)
RelativeLayout is made to allow views to overlap each other. You could use a LinearLayout.
If you for some reason need to stick to RelativeLayout, then you have to place your new View with some layout parameters relative to previously added children
params.addRule(RelativeLayout.BELOW, R.id.below_id);
instead of R.id.below_id you could use your own ids that you have set on other children
Take a look here
Programatically add view one below other in relative layout

Get position of dynamic Image View

Outline
My application has a main Image View and below main view, it has dynamic horizontal imageview .
Code for dynamic image view
ImageView imageall = new ImageView(FullImage.this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(175, 175);
params.setMargins(10, 0, 0, 0);
imageall.setLayoutParams(params);
imageall.setBackgroundResource(R.drawable.imageview_border);
Picasso.with(context_tab1).load("http://example.com//upload/image/" + stringList.get(in)).into(imageall);
// Adds the view to the layout
layout.addView(imageall);
imageall.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
int position = (Integer) v.getTag();
image = "http://example.com/x/upload/image/" + stringList.get(position);
Picasso.with(context_tab1).load(image).into(fullimage);
}
});
}
I populate the dynamic imageview from array list. Everything works fine till this part.
Problem
Now, On Click of the dynmic imageview must get that image view position and change as the main imageview .
This is not working for me.
For better understanding please find the image attached.
Thanks.
No need of getting position of dynamic ImageView..
While creating the dynamic ImageView you can set Ids to them and also set the onClickListener on them. Override the Onclick method, check the Id with your Ids you gave to dynamic ImageView and replace the Main view's as per the ImageView clicked..
Happy coding.. :)

Android - Horizontal scroll with "Show more"

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.

How to setImageResource of a dynamically declared ImageButtons?

I have some dynamically declared ImageButtons, these ImageButtons don't have ids' and they are declared in a LinearLayout, I want to create a method to change Images resources of these ImageButtons when called, and as I don't have a predefined ids' for these ImageButtons I'm using the function getChildAt() with the LinearLayout, but getChildAt() doesn't provide setImageResource(), it just provide setBackgroundResource() and this function doesn't make a replacement to the old Image and also doesn't fit at the old Image as it provides a background not an Image Resource, what can I do with that ?
this is my method code :
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
Righter.getChildAt(i).setBackgroundResource(drawable); // here is where i change the background image
Righter.getChildAt(i).setClickable(true);
}
}
getChildAt() returns View. you need to typecast this View to ImageButton and call setImageResource() method...
ImageButton imageButton = (ImageButton) linearLayout.getChildAt(0);
imageButton.setImageResource(R.drawable.btnimage1);
try this
private void my_method(int drawable) {
int count = Righter.getChildCount(); // Righter is the LinearLayout
for (int i = 1; i < count; i++) {
((ImageButton)Righter.getChildAt(i)).setImageResource(R.drawable.btnimage1);
Righter.getChildAt(i).setClickable(true);
}
}
type cast you Righter.getChildAt(i) with ImageButton

OnClickListener of Child preventing the swipe of Android ViewPager

I am using a ViewPager with Dynamic Views as shown in this link. I am adding each page to the viewpager. The code to create each page and add it to the pager adapter is as follows
public View createPage( )
{
TableLayout table = new TableLayout(mycontext);
TableRow row = new TableRow(mycontext);
for(int i=0; i <= num_of_views; i++)
{
TableLayout catTable = new TableLayout(mycontext);
TableLayout.LayoutParams params = new TableLayout.LayoutParams();
params.height = LayoutParams.MATCH_PARENT;
TableRow catName = new TableRow(mycontext);
catName.setGravity(0x03);
catName.setLayoutParams(params);
TextView cat = new TextView(mycontext);
TableRow.LayoutParams RowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(85));
RowParams.weight = 1;
cat.setLayoutParams(RowParams);
cat.setPadding(35, 0, 5, 0);
cat.setGravity(0x13);
cat.setSingleLine(true);
cat.setHorizontallyScrolling(true);
cat.setFocusable(true);
cat.setFocusableInTouchMode(true);
cat.setLines(1);
cat.setText(data.getCats().get(i).getName());
cat.setTextColor(color);
cat.setSelected(true);
catName.addView(cat);
catTable.addView(catName);
for(int j=0; j < 5; j++)
{
TableRow trChild = new TableRow(mycontext);
trChild.setLayoutParams(params);
trChild.setGravity(0x11);
for (int k=0; k<4; k++)
{
TextView app = new TextView(mycontext);
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams(pixeltoDp(0),pixeltoDp(80));
margin = pixeltoDp(11);
tableRowParams.weight = 1;
app.setLayoutParams(tableRowParams);
app.setGravity(0x11);
app.setSingleLine(true);
app.setTextSize(pixeltoDp(12));
app.setTextColor(Color.BLACK);
app.setTypeface(verdana);
int pos = j+(k*5);
if((pos < mysize))
{
Drawable icon = getMyDrawable();
app.setCompoundDrawablesWithIntrinsicBounds(null, icon, null, null);
app.setText("some Name");
app.setTag(i+":"+pos);
app.setEllipsize(TextUtils.TruncateAt.END);
app.setOnClickListener(onMyClick);
}
trChild.addView(app);
}
catTable.addView(trChild);
}
TableRow.LayoutParams tableRowParams=
new TableRow.LayoutParams
(TableRow.LayoutParams.MATCH_PARENT,TableRow.LayoutParams.WRAP_CONTENT);
margin = pixeltoDp(5);
tableRowParams.setMargins(margin,0,margin, pixeltoDp(18));
catTable.setLayoutParams(tableRowParams);
}
row.setGravity(0x03);
table.addView(row);
return table;
}
The OnClickListener for the textview in the page is as follows
private View.OnClickListener onAppClick=new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
String gettext = (String)v.getTag();
String[] splittext = gettext.split(":");
Log.d("TAG","Click Event is Triggered");
}
};
The problem I am facing is that the viewpager slides when I swipe on the screen but if my finger starts the swipe from the text with the onclicklistener the page does not slide, also the click event is not called. If I click on the text view the click event is called. I am not able to figure why the viewpager in not flipping the pages if the swipe starts from the text view with click event.
To find out the reason for the bug I added a ontouchlistener to the viewpager and found that the touch event of the viewpager is called when the swipe happens on the part of the page where there is no text view and the touch event is not called at all if the swipe starts on top of the text view. If I remove the onclicklistener of the textview then the entire viewpager flips properly without any issue. So the problem is that the click event of the textview is stopping the touch events of the viewpager. Please tell me if I am doing something wrong in my create page code. The view which I create in this function is added to the viewpager through the pageadapter as shown in the tutorial I mentioned in the beginning.
Finally I found that there is a bug in the latest version of ViewPager where a TextView with a click event, singleLine set as true and gravity set as center is not passing the touch events to the ViewPager if the finger is moved starting on the top of the textview. If we make the singleLine as false or remove the Center gravity then the events are passed to the ViewPager then it can fling. We can't use both the center gravity and setsingleline(true) for the textview or a button with sinlgeline set as true. I have reported this as a bug here
See the 'apply single line' source here: https://github.com/android/platform_frameworks_base/blob/7bfb1e7f46d2ed3df6cc467bccec95b5999d9cc4/core/java/android/widget/TextView.java#L7877
It seems that there is somewhat of a solution: you can use setMaxLines(1) instead of setSingleLine()and it will work without impairing the system. However I can confirm your previous answer in which you say it's a bug

Categories

Resources