1

I created a main.xml window on android in which I have taken a couple of button and now I created another Sound.xml window, so how can I open the window on the press of first window (main.xml) button.

On main java.

public class Main extends Activity {




@Override
protected void onCreate(Bundle savedInstanceState) {

   /** Called when the activity is first created. */

     super.onCreate(savedInstanceState);

     setContentView(R.layout.cyk_main);

     Button orderButton = (Button) findViewById(R.id.options_btn);
    orderButton.setOnClickListener(new OnClickListener() {

        @Override
        public void onClick(View v) {
            Intent intent = new Intent(Main.this,soundLayout.class);
            startActivity(intent);
        }
    });

on second window

public class soundLayout extends Activity {


@Override

  protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.soundLayout);
Button orderButton = (Button) findViewById(R.id.btn_finish_dialog);
orderButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
    finish();
}
});

}

<activity  
        android:name=".SoundOptionLayout
"android:theme="@android:style/Theme.Dialog" >
    </activity>
1999493
  • 96
  • 1
  • 1
  • 9
  • You can do this via an intent as you have done mostly. See the following link. Make sure you have added the second sctivity to the manifest. See the following link http://stackoverflow.com/questions/4186021/how-to-start-new-activity-on-button-click – PantsOffNow Aug 06 '13 at 14:51
  • yeah i have gone through your link .it help me to solve my problem. – 1999493 Aug 07 '13 at 11:07

1 Answers1

0

Main Window(activity)

package com.myproject.testprojectonintent;
import android.app.Activity; 
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;

public class MainActivity extends Activity implements OnClickListener{



/** the button that I've added in the xml*/
Button startBtn;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main_screen);

    /** Get the id of the button from the xml*/
    startBtn = (Button) findViewById(R.id.startBtn);
    startBtn.setOnClickListener( this);
}


@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    /** make a refernce to store the intent when the view has been clicked*/
    Intent intent;

    /** Make cases according to the number of buttons you have in screen
     * In this case, I've added one.*/
    switch(view.getId()){

    case R.id.startBtn :
        Log.i("btnClick","Start button id is"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
        intent = new Intent(MainActivity.this, ResultActivity.class);
        /** This is useful when you want to get the button that is clicked here on the 
         * destination activity*/
        intent.putExtra("button_click", "Start");
        /** Start the intent*/
        startActivity(intent);

        /*** Use this only when you want to finish your current activity while opening an another one.
         * if this is commented, then this activity will be running in the background.
        ***/
        this.finish();
        break;
    }

}

/*@Override
public boolean onCreateOptionsMenu(Menu menu) {
    // Inflate the menu; this adds items to the action bar if it is present.
    getMenuInflater().inflate(R.menu.main_screen, menu);
    return true;
}*/

}

Second Window(activity)

package com.myproject.testprojectonintent;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;

import android.widget.Button;
public class ResultActivity extends Activity implements OnClickListener{

Button resultBtn;

private String _showText;


@Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    setContentView(R.layout.result_screen);

    /** Fetched from the MAIN ACTIVITY */
    Bundle bundle = getIntent().getExtras();
    if(bundle!=null){
        _showText = bundle.getString("button_click");
    }
    Log.i("btnClick","button clicked is :"+_showText);


    resultBtn = (Button) findViewById(R.id.resultBtn);
    resultBtn.setOnClickListener(this);
}

@Override
public void onClick(View view) {
    // TODO Auto-generated method stub
    /** make a refernce to store the intent when the view has been clicked*/
    Intent intent;

    /** Make cases according to the number of buttons you have in screen
     * In this case, I've added one.*/
    switch(view.getId()){

    case R.id.resultBtn :
        Log.i("btnClick","Result button is clicked whose id is :"+view.getId());
        /** Intent should be fired from this activity to the other activity*/
        intent = new Intent(ResultActivity.this, MainActivity.class);

        /** Start the intent*/
        startActivity(intent);
        this.finish();

        break;
    }
}   
}

Mainfest

 <activity 
        android:name="com.myproject.testprojectonintent.ResultActivity"

        ></activity>

After working on this I am able to Solve my problem.

1999493
  • 96
  • 1
  • 1
  • 9