0

I am trying to create an android app. I have two buttons next and back in my android app. I want when i click on next button its open same activity with different background. Next time i again click next new background image. And on press on back button its show previous image. And if no previous image its shows menu on press. Similarly if background with last image its hide next button. I have no idea how to do this.

I have tried this:

@Override
    public void onCreate(Bundle savedInstanceState)
    {
        onCreate(savedInstanceState);
        back = (Button) findViewById(R.id.back);
        next = (Button) findViewById(R.id.next);
        back.setOnClickListener(this);
        next.setOnClickListener(this);
    }

    @Override
    public void onClick(View v) {
        if(v.getId()==R.id.back)
        {
            startActivty(new Intent(this,));
        }
        else if(v.getId()==R.id.next)
        {
            startActivity(newIntent(this,));
        }               
    }

Xml:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent" 
    android:background="@drawable/back">

    <Button
        android:id="@+id/back"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_marginBottom="25dp"
        android:background="@drawable/ques"
        android:text="Back" />

    <Button
        android:id="@+id/next"
        android:layout_width="100dp"
        android:layout_height="wrap_content"
        android:layout_alignBaseline="@+id/back2"
        android:layout_alignBottom="@+id/back2"
        android:layout_alignParentRight="true"
        android:background="@drawable/ques"
        android:text="Next" />

</RelativeLayout>

In layout as you can see i am using image back for background. I want when i click next new background image then next and so on.

But i dont know how to start same activity with differene backgroud.

John R
  • 2,078
  • 8
  • 35
  • 58

4 Answers4

1

Don't start new Activity just change the background:

Keep an array of background resources in your activity like:

int[] backgroundResId;

and one int variable to store current background index:

int currentIndex=0;

now inside your onCreate initialize this array with resource id's of all the backgrounds drawables:

backgroundResId=new int[]{R.drawable.a,R.drawable.b,R.drawable.c};
changeBackground()

create function changeBackground in activity:

private void changeBackground(){
     findViewById(R.id.root_layout).setBackgroundResource(backgroundResId[currentIndex]);
}

Now onClick of next button increase currentIndex:

currentIndex++;
if(current<=backgroundResId.length){
     changeBackground();
}else{
    // setVisibility of next button to invisible
}

onBackButton Click

currentIndex--;
if(current>=0){
     changeBackground();
     //// setVisibility of next button to visible
}else{
  //show menu
}
vipul mittal
  • 17,343
  • 3
  • 41
  • 44
  • In changeBackground() you are using id resource but i want to set background in layout. – John R Jan 11 '14 at 09:30
  • i want to use background using setContentView. – John R Jan 11 '14 at 09:36
  • @JohnR , If you want to use setContentView to change Background, you have to add as many layout files with different images. however that is not recommended – R9J Jan 11 '14 at 09:40
1

Make an images array and post your data to the next activity:

Intent intent = getIntent();
    intent.putExtra("background", imageIdInTheImageArray);
    startActivity(intent);

    //finish();

and in your onCreate function :

Bundle b = getIntent().getExtras();
    if (b != null) {
        int background = b.getInt("background");
        //set your background
    }
Saif Hamed
  • 1,084
  • 1
  • 12
  • 17
1

You can add an ImageView in your xml file.

<ImageView 
    android:id="@+id/imageView"
    android:layout_width="match_parent"
    android:layout_height="match_parent" />

you can change background using this

ImageView imageView = (ImageView) findViewById(R.id.imageView);
imageView.setBackgroundResource(resId);
R9J
  • 6,605
  • 4
  • 19
  • 25
0

Try this..

Global:

int[] backgrounds = new int[]{ images in drawable as int array };   
int count = 0;
Button back,next;
RelativeLayout img_backn_lay;

JAVA:

    setContentView(R.layout.activity_main);
    back = (Button) findViewById(R.id.back);
    next = (Button) findViewById(R.id.next);
    back.setOnClickListener(this);
    next.setOnClickListener(this);
    img_backn_lay = (RelativeLayout) findViewById(R.id.main_lay);
    img_backn_lay.setBackgroundResource(backgrounds[count]);
    count += 1; 

ClickListener:

@Override
public void onClick(View v) {
    // TODO Auto-generated method stub
    if(v.getId()==R.id.next)
    {
        if(backgrounds.length != count){
            img_backn_lay.setBackgroundResource(backgrounds[count]);
            count += 1; 
        }else{
            Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
        }
    }
    else if(v.getId()==R.id.back)
    {
        if(count != 0){
            img_backn_lay.setBackgroundResource(backgrounds[count]);
            count -= 1; 
        }else{
            Toast.makeText(MainActivity.this, "No images", Toast.LENGTH_LONG).show();
        }
    }    
}

XML:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/main_lay"
    android:layout_width="match_parent"
    android:layout_height="match_parent" >
Hariharan
  • 24,741
  • 6
  • 50
  • 54