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.
Your switch statement is wrong
should be
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