emmarbe
emmarbe

Reputation: 103

Android: How to get the id of a layout inside a flipper?

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)

Upvotes: 0

Views: 8545

Answers (4)

f20k
f20k

Reputation: 3106

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.

Upvotes: 2

Kevin Coppock
Kevin Coppock

Reputation: 134714

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
}

Upvotes: 3

R Hughes
R Hughes

Reputation: 640

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

Link - Touch Scroll on View Flipper in Android?

Upvotes: 0

Steve Haley
Steve Haley

Reputation: 55754

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.

Upvotes: 0

Related Questions