Girish Koundinya
Girish Koundinya

Reputation: 133

Using imageView as a button to navigate between layouts

I am trying to use image view as a button. So that when the user clicks on the image i want a differenot xml file to be displayed as layout.

I have changed as follows..

<ImageView
            android:id="@+id/telugu"
            android:layout_width="130dp"
            android:layout_height="25dp"
            android:layout_x="-34dp"
            android:layout_y="315dp"
            android:clickable="true"
            android:onClick="myClickHandler"

and myClickHandler is..

public void myClickHandler(View view) {
      switch (view.getId()) {
      case R.id.arts:
           setContentView(R.layout.arts);
     case R.id.music:
         setContentView(R.layout.music);

The problem is i am unable to go to the specific layout. It is going to the same layout no matter which image I select.

Upvotes: 0

Views: 2771

Answers (4)

user1213202
user1213202

Reputation: 1305

create Separate Classes for that .you can use Imageview OnClcikListener action like this.

ImageView iv=(ImageView)findViewById(R.id.imageView1); iv.setOnClickListener(new OnClickListener() {

        public void onClick(View v) {
            // TODO Auto-generated method stub
            Intent iv=new Intent();
            iv.setClass(getApplicationContext(), art.class);
            startActivity(iv);
        }
    });

Upvotes: 0

owen gerig
owen gerig

Reputation: 6172

You should create another class for that xml file. so you would have an arts.java arts.xml , music.java music.xml. then call and intent to go to that activity

    ImageView buttonLA = (ImageView) findViewById(R.id.buttonLouisiana);
    buttonLA.setOnClickListener(new View.OnClickListener() {
        public void onClick(View v) {
            Intent myIntent = new Intent(arts.this, music.class);
            startActivityForResult(myIntent, 0);
        }
    });

Upvotes: 2

FoamyGuy
FoamyGuy

Reputation: 46856

You aren't allowed to can call setContentView() more than once per activity, @dymmeh is right about your break statements being the problem.

I think you should still consider either use something like ViewSwitcher to change which layouts you are showing within one application. Or divide your different layouts into their own activity, this can have the added benefit of making your application "play nicely" with the back button.

Here is a nice view switcher tutorial if you choose to go that route.

Upvotes: 0

dymmeh
dymmeh

Reputation: 22306

Your switch statement is wrong

switch (view.getId()) {
      case R.id.arts:
           setContentView(R.layout.arts);
     case R.id.music:
         setContentView(R.layout.music);
}

should be

switch (view.getId()) {
      case R.id.arts:
           setContentView(R.layout.arts);
           break;
     case R.id.music:
         setContentView(R.layout.music);
         break;
}

You aren't putting breaks in your switch which is allowing your switch to drop down to the next statement. This means the setContentView method that you call will always be the bottom one in your switch. Add the "break;"s in there that I showed and you will be good to go

I'm not sure where people are getting the idea that you can't call setContentView more than once. This certainly is possible and I've just tested it myself to make sure.

The problem comes with managing data on the screen when simply calling setContentView. If you have a layout with 2 buttons on the screen and then switch to one with 3 buttons on the screen you have to somehow differentiate between those layouts in your code. In this case using a whole new activity would be the better option. Less logic code and overall easier to manage. However, I'm sure there are cases where calling setContentView more than once in the same activity is the suitable answer

Upvotes: 3

Related Questions