ImageView event when in view - android

I have a layout like so.
HorizontalScrollView
LinearLayout (Horizontal)
ImageView (A number of times)
Is there any event/way I can tell which ImageView is visible? I'd like an event if possible so I can store the index of the current Image that's visible.
Something like
class MyViewClass
private int index;
....
imageView.onEventViewListener(...
index = currentImageIndex;
);
EDIT: My current code loading the ImageViews
OnClickListener click = new OnClickListener() {
#Override
public void onClick(View v) {
Object i = v.getTag();
smoothScrollTo((width*((Integer) i+1)), 0);
}
};
for(int i = 0;i < mImages.size();i++) {
Gallery.Image image = mImages.get(i);
ImageView iv = new ImageView(getContext());
ViewGroup.LayoutParams par = new ViewGroup.LayoutParams(width, ViewGroup.LayoutParams.WRAP_CONTENT);
iv.setLayoutParams(par);
iv.setTag(i);
iv.setOnClickListener(click);
Picasso.with(getContext())
.load("http://www.example.com/files/galleries/" + image.getGalleryId() + "/" + image.getSrc() + "_full.jpg")
.into(iv);
layout.addView(iv);
}

You can use viewpager; it will provide all the events that's required by you

You can try this widget:HorizontalListView.
Its attribute mLeftViewAdapterIndex means first Visiable item's index.
Here is the source code in github.
https://github.com/MeetMe/Android-HorizontalListView/blob/master/AndroidHorizontalListView/src/com/meetme/android/horizontallistview/HorizontalListView.java

Related

Set buttons ids with Custom Adapter

Which will be the better way to create a Vertical Lineal layout with four or more buttons in each row
The problems I have face are the following:
Setting the id of each button manually will result in a lot of repetitive code, more resources usage and you will have to change everyone to add a feature or change something (I think using an adapter will be the most efficient way, but...)
From what I know using a CustomAdapter don't help you set a unique ID to the buttons
Can you use an adapter to set a different id for each button dipending of the row?
Example:
second button of third row: r3b2
fifth button of first row: r1b5
Thanks.
You can create the buttons programmatically in our Activity class like this
LinearLayout layout = (LinearLayout) view.findViewById(R.id.layout); // the layout in which u want to display the buttons
layout.setOrientation(LinearLayout.VERTICAL);
int count = 1;
for (int i = 0; i < 5; i++) { // i = row count
LinearLayout row = new LinearLayout(getActivity());
row.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
for (int j = 0; j < 7; j++) { // j = column count (create 7 buttons in each row)
int id = count;
final Button btnTag = new Button(getActivity());
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
params.weight = 1;
params.gravity = Gravity.CENTER;
btnTag.setLayoutParams(params);
btnTag.setBackgroundColor(Color.parseColor("#ffffff"));
String dateSuffix = count + getDayNumberSuffix(count);
btnTag.setTag(dateSuffix);
btnTag.setId(id);
btnTag.setText(count + "");
btnTag.setTextSize(12.0f);
btnTag.setOnClickListener(getOnClickDoSomething(btnTag, count));
row.addView(btnTag);
count++;
}
layout.addView(row);
}
View.OnClickListener getOnClickDoSomething(final Button btnTag, final int count) {
return new View.OnClickListener() {
public void onClick(View v) {
// here you can do what u want to do on button click
}
}

Change property of multiple image button

My activity has 9 image buttons, I would like to change certain properties of multiple image buttons like disable all the image buttons in a certain situation & then enable them back. I think may be i can use loop something like this....
// int [] ids = { R.id.imgBtn1, R.id.imgBtn2......, R.id.imgBtn9 };
// for (int i=0; i<=ids.length; i++){
// ids[i].setEnabled(true);
// }
Thank you all
define one list like follow:
List<ImageView> images = new ArrayList<>();
ImageView iv1 = (ImageView)findViewById(R.id.image1);
ImageView iv2 = (ImageView)findViewById(R.id.image2);
ImageView iv3 = (ImageView)findViewById(R.id.image3);
ImageView iv4 = (ImageView)findViewById(R.id.image4);
ImageView iv5 = (ImageView)findViewById(R.id.image5);
ImageView iv6 = (ImageView)findViewById(R.id.image6);
ImageView iv7 = (ImageView)findViewById(R.id.image7);
//.....
and put all in one list,
images.add(iv1);
images.add(iv2);
// add other view
then work with this list and do what you want like:
for (ImageView iv : images)
{
// your code
}
// Add views from array of ids
ArrayList<View> views = new ArrayList<>();
int [] ids = { R.id.imgBtn1, R.id.imgBtn2, R.id.imgBtn9 };
for (int i=0; i<ids.length; ++i){
views.add(findViewById(ids[i]));
}
// Loop each view and enable it
for (View view : views) {
view.setEnabled(true);
}

i want to redraw on a view Android programming

I am creating an app that displays the graphs of input given by a user. The graph is drawn on a linear layout using some library..i want the linear layout to redraw the new function requested by the user everytime the user clicks the draw button..I have tried using layout.invalidate() but this is not working in my app.please help ..below is code snipet :
bb.setOnClickListener(new OnClickListener()
{
public void onClick(View v)
{
String text = ee.getText().toString(); // getting the user expression input
LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
layout.setVisibility(View.INVISIBLE);
Expression data = Expression.createExpression(text) ;
if(text == ""){
Toast.makeText(getApplicationContext(), "please enter a valid equation", Toast.LENGTH_LONG).show();
layout.setVisibility(View.INVISIBLE);
}
else
{
draw(data) ;
layout.setVisibility(View.VISIBLE);
layout.invalidate();
}
}
});
public void draw(Expression x)
{
final GraphView graphing = new LineGraphView(this, "sketch");
int num = 350;
GraphViewData[] array = new GraphViewData[num];
double w=0;
for (int i=0; i<num; i++) {
w += 0.2;
array[i] = new GraphViewData(i, x.evaluate(w,0,0)); }
// add data
graphing.addSeries(new GraphViewSeries(array));
// set view port, start=2, size=40
graphing.setViewPort(0, 120);
graphing.getGraphViewStyle().setNumHorizontalLabels(2);
graphing.setScrollable(true);
// optional - activate scaling / zooming
graphing.setScalable(true);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
layout.addView(graphing);
layout.invalidate();
}
First:
graphing.setViewPort(0, 120);
is it correct? To me it looks like you set view width to 0.
Next, you sure you don't want to discard old results by removing old views from layout?
Last, layout auto-invalidated when you add or remove child views.

Textview image in android

In my application i have to show an image in my text view. Also When i click 1st time 1 image should draw and clicking next time another image should be drawn.Is it possible to draw an image in textview?Please help me..
You can use
setCompoundDrawables(Drawable left, Drawable top, Drawable right, Drawable bottom)
for the textView
You can set a background image using the android:background attribute
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/image" />
Or programatically with the setBackgroundDrawable()or setBackgroundResource() methods:
textView.setBackgroundResource(R.drawable.image);
You can take an array of Drawables, an an index for that:
TextView tv;
Drawable[] drbl = new Drawable[4];
int drblIndex = 0;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
drbl[0] = getResources().getDrawable(R.drawable.ic_launcher);
drbl[1] = getResources().getDrawable(R.drawable.img2);
drbl[2] = getResources().getDrawable(R.drawable.img3);
drbl[3] = getResources().getDrawable(R.drawable.right_arrow);
tv = (TextView) findViewById(R.id.textView1);
tv.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
tv.setBackgroundDrawable(drbl[drblIndex++]);
if(drblIndex == drbl.length)
drblIndex = 0;
}
});
}
Then you can set values of that array as I have done.
Then onClick you can move to next Index and can set new Drawable-Image to TextView.
When Index reaches to last value, set it to zero, simply.
Set OnClickListener on the TextView and in its onClick() method, use the following methods to set the images :
textview.setBackgroundDrawable();
or
textview.setBackgroundResource();
try this code, textview with image.
CharSequence dogstate = null ;
Html.fromHtml("" + " "
+ "your text" + "", featuregetter, null);
dogatstatus.append(dogstate);
private ImageGetter featuregetter = new ImageGetter() {
public Drawable getDrawable(String source) {
Log.i(" get drawable mathod ", "");
Bitmap B = BitmapFactory.decodeResource(getResources(),
R.drawable.bone);
BitmapDrawable BD = new BitmapDrawable(B);
BD.setBounds(0, 0, B.getWidth(), B.getHeight());
return BD;
}
};

android- Placing two programatically created imageView next to each other in a RelativeLayout

I have created two imageViews promatically as shown below:
public void createImageViews(Integer count){
ImageView[] imageViewArray = new ImageView[count];
for (int i = 0; i < count; i++ ) {
imageViewArray[i] = new ImageView(getBaseContext());
imageViewArray[i].setId(i); // unique property for every imageView
if(i==0){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
imageViewArray[i].setLayoutParams(params);
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i]);
Log.v("first", "first"+i);
}
else if(i < 3){
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
imageViewArray[i].setBackgroundResource(imagesForIv[i]);
_UIRLParent.addView(imageViewArray[i],params);
Log.v("second", "second"+i);
}
}
I just need to place the second imageView toRightOf first imageView. Can someone help me. This is eating away a lot of my time.
try https://stackoverflow.com/a/5191159/1436931
you are using wrong index values.
at line
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i].getId());
you aligning current image RIGHT of current image. :)
you need to track the index of last imageView id i.e left Image view
you need something like this
params.addRule(RelativeLayout.RIGHT_OF,imageViewArray[i-1].getId());

Categories

Resources