0

Ans: Use viewFlipper.getCurrentView().getId()

Hi, I'm stuck in a position where I have to execute a set of statements based on the screen that is being displayed (Have to use IF condition). The layout folder contains only one screen (i.e., main.xml). But inside the main.xml there are three absolute layouts (01,02 & 03). I'm using flipper to swipe through these screens.

Earlier when I used to switch between two screens (main.xml and main2.xml), I had no problem. I was using two buttons to switch between the screens and I used a flag inside each button and used the same flag in my IF condition to execute the code. Now that I've implemented flipper, I'm unable to use flag. So I thought of using the LAYOUT ID in the IF condition. But I need to know how to check for the layout id in java coding. Can somebody help me out?

For Ex:

if(findViewById(R.id.AbsoluteLayout01)==true)
{
execute
}
else if(findViewById(R.id.AbsoluteLayout02)==true)
{
execute #2
}

(The above code throws the "Incompatible operand types View and boolean" error)

So I tried

if(findViewById()==v1) //v1 is the object for AbsoluteLayout01
{
execute
}
else if(findViewById()==v2) //v2 is the object for AbsoluteLayout02
{
execute #2
}

(The above code throws the "The method findViewById(int) in the type Activity is not applicable for the arguments ()" error)

emmarbe
  • 103
  • 3
  • 7

4 Answers4

3

Simplest way, you can just call ViewFlipper.getDisplayedChild() and just compare based on the index (0, 1, 2). If it's absolutely necessary to compare by id, you can also do:

ViewFlipper vf = //whatever assignment
switch(vf.getCurrentView().getId()) {
    case R.id.AbsoluteLayout1:
        //do work
}
Kevin Coppock
  • 133,643
  • 45
  • 263
  • 274
  • thanks! I was expecting such a reply. But I've still got some problem. Here's what I did: ViewFlipper viewFlipper=(ViewFlipper)findViewById(R.id.flipper); if(viewFlipper.getCurrentView().getId()==R.id.AbsoluteLayout01){statement1} similar to statement 2 and statement 3. It is working for the first two if and not for the third if(i mean, the third screen). And BTW, I used "Toast.makeText(getApplicationContext(), viewFlipper.getCurrentView().getId(), Toast.LENGTH_LONG).show();" inside onFling and I always get the "False" as toast. – emmarbe Feb 14 '11 at 23:58
  • Problem Details: Here's what I did: ViewFlipper viewFlipper=(ViewFlipper)findViewById(R.id.flipper); if(viewFlipper.getCurrentView().getId()==R.id.AbsoluteLayout01){statement1} similar to statement 2 and statement 3. It is working for the first two if and not for the third if(i mean, the third screen). And BTW, I used "Toast.makeText(getApplicationContext(), viewFlipper.getCurrentView().getId(), Toast.LENGTH_LONG).show();" inside onFling and I always get the "False" as toast. – emmarbe Feb 15 '11 at 00:09
  • thanks a lot! The code is working now. I had a small logical error in the statement 3. Now the respective codes for each screen is working. You've made my day! THE ANSWER FOR MY QUESTION IS "viewFlipper.getCurrentView().getId()" – emmarbe Feb 15 '11 at 00:16
  • Ah I see! I thought it might be something with indexes getting mixed up or something. Glad you got it working! – Kevin Coppock Feb 15 '11 at 00:20
2

Using == will not work between Views unless you overload the == operator....

Try .equals() instead.

if(findViewById(R.id.AbsoluteLayout01).equals(v1))

where v1 is a view.

f20k
  • 3,106
  • 3
  • 23
  • 32
  • 2
    "_unless you override the == operator..._" -- good luck trying to do that in Java. – Ted Hopp Feb 14 '11 at 23:11
  • @Ted Hopp. Very interesting, I did not realize that operator overloading was uncommon in Java (I have never tried it myself). Learning something new every day. However it really pains me to see people try doing == on everything without reading error messages... – f20k Feb 15 '11 at 00:46
0

In your first code block, you could try

if (findViewById(R.id.absoluteLayout01) != null) {
    ....
}

as findViewById either returns a view, if it finds it, or returns null.

As for how to find the layout id, have you just tried R.layout.(...)? That would work if they're all separate xml files as it returns the unique number for the whole file, rather than a viewgroup inside the file.

Steve Haley
  • 55,374
  • 17
  • 77
  • 85
  • @f20k - The code you provided might not throw error, but it has no use. As the given code, checks for AbsoluteLayout01 with that of the AbosluteLayout01's object v1. Both are same, so the statements will be executed all the time. @Steve H - I tried this one, but just like the above f20k's logic, even this return true all the time. @Lucas S. - Like I said, I need to get the id of the absolute layout which is being displayed by a flipper. BTW, Thanks everyone for helping me out. – emmarbe Feb 14 '11 at 22:38
0

Perhaps a listener attached to the flipper can tell you what you need to know?

Link - Touch Scroll on View Flipper in Android?

Community
  • 1
  • 1
R Hughes
  • 640
  • 9
  • 22