Diana
Diana

Reputation: 1437

Android Studio - Button opens new activity

I want to make an onclick listener so that when I touch the start button on the home page it will lead me to disclaimer. I have read many answers but I still have some problems with it.

I have declared my new activity in the manifest:

 <activity
            android:name=".Disclaimer"
            android:label="@string/title_activity_disclaimer" >
        </activity>

This is how my start button looks like in the page:

<Button
    android:layout_width="100dp"
    android:layout_height="50dp"
    android:text="START"
    android:id="@+id/button"
    android:layout_centerVertical="true"
    android:layout_centerHorizontal="true" />

And this is my main activity:

public class Home extends ActionBarActivity {

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

        Button btn = (Button)findViewById(R.id.open_activity_button);

        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(new Intent(Home.this, Disclaimer.class));
            }
        });
    }


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

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

One of the problems is that it cannot resolve symbol open_activity_button. Any ideas how I can resolve that?

Upvotes: 2

Views: 538

Answers (2)

DroidAks
DroidAks

Reputation: 327

In your code make the changes

  1. Your first & second activity is not extending Activity Class(Instead of ActionBarActivity)
  2. Also override OnCreate() activity of second activity.
  3. Also include the button in first activity by taking the ID. ex.In first Activity, private Button btnname;(In activity) now in onCreate(),btnname=(Button)findViewById(R.id.buttonid); and then on click of btnname give the code to move for second activity.

Refer this for an example Moving from one activity to another Activity in Android

Upvotes: 0

Ravi K Thapliyal
Ravi K Thapliyal

Reputation: 51711

You're <Button>'s andorid:id attribute should be

<Button
    ...
    android:id="@+id/open_activity_button"

That's why R.id.open_activity_button isn't generated and the compiler complains about not being able to find that. Alternatively, you can change your onCreate() code to

Button btn = (Button) findViewById(R.id.button);

Upvotes: 2

Related Questions