How do I start a new activity from the main activity and display all items with the background?

advertisements

I am a beginner in Android programming and to learn it I am making a sample calculator example myself using the references on the web, now this is something I want to do. I followed the tutorial "Building you First App" as provided on official Google documentation and I was successful in sparking off a new activity on hitting a button using the Intents class, here is the image of the application at the start -:

but when I hit the enter button, even though I have set the android:background option for the new activity it doesn't set the background to that image, the only thing I get is a blank activity, here is the image -:

here is the code for my MainActivity that starts up when the application starts -

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/math_symbols" >

    <Button android:id="@+id/enter_button"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:text="@string/enter_button"
            android:onClick="enterCalculator"/>
</LinearLayout>

Here's the code for the second activity that starts when I hit the enter button and this is done using Intents -:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="@drawable/calculato_activity_1" >

     <RadioGroup android:id="@+id/operation_radio_group"
            android:layout_width="fill_parent"
            android:layout_height="wrap_content"
            android:layout_marginLeft="30dip"
            android:orientation="horizontal">

        <RadioButton android:id="@+id/sum"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="@string/sum">

        </RadioButton>

        <RadioButton android:id="@+id/difference"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="@string/difference">

        </RadioButton>

        <RadioButton android:id="@+id/multiply"
                 android:layout_width="wrap_content"
                 android:layout_height="wrap_content"
                 android:text="@string/multiply">

        </RadioButton>

    </RadioGroup>
</LinearLayout>

no background is displayed above and no views are added to the screen, also here is how I am using Intents in the MainActivity class -:

package com.example.mathcalculator;

import android.support.v7.app.ActionBarActivity;
import android.support.v7.app.ActionBar;
import android.support.v4.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.os.Build;

public class MainActivity extends ActionBarActivity {

    // this is the message used to enter the main calculator activity
    public static String ENTER_MESSAGE = "com.example.mathcalculator.MainActivity.Message";

    // this method is used to enter the actual calculator screen
    public void enterCalculator(View view){

        // we create an Intent to carry over the data from the button and enter the CalculatorActivity1
        Intent intent = new Intent(this, CalculatorActivity1.class);

        // put the key-value pair into the intent
        intent.putExtra(ENTER_MESSAGE, "ENTER");

        // start the new activity with the intent as the input which carries over the message bundle
        startActivity(intent);
    }

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

        if (savedInstanceState == null) {
            getSupportFragmentManager().beginTransaction()
                    .add(R.id.container, new PlaceholderFragment()).commit();
        }
    }

    @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, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(R.layout.fragment_main, container,
                    false);
            return rootView;
        }
    }

}

and here is the code for the CalculatorActivity1 class -:

package com.example.mathcalculator;

import android.support.v7.app.ActionBarActivity;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;

public class CalculatorActivity1 extends ActionBarActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {

        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.calculator_activity1, 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();
        if (id == R.id.action_settings) {
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    /**
     * A placeholder fragment containing a simple view.
     */
    public static class PlaceholderFragment extends Fragment {

        public PlaceholderFragment() {
        }

        @Override
        public View onCreateView(LayoutInflater inflater, ViewGroup container,
                Bundle savedInstanceState) {
            View rootView = inflater.inflate(
                    R.layout.fragment_calculator_activity1, container, false);
            return rootView;
        }
    }

}


You need to set a content view for your activity. It is already done in the sample app activity, so inside the oncreate method of your MainActivity, you can see: setContentView(R.layout.activity_main); It is setting the activity_main layout to the MainActivity content view. So you need to do this to the CalculatorActivity1 too, under the super.onCreate(savedInstanceState); You can find your second layout name in the res/layout folder.