2

Hello I am very new to android programming. I would like to get some help regarding displaying a button under a string message under an activity which has been newly activated. Hope someone can help me. Here is the code which I have for the class MyActivity which activates when the button app tutorial is pressed. The xml for my main activity is also there. Also the displaymessage activity class and xml is also there.

package com.example.sadi_ace.myfirstapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;


public class MyActivity extends ActionBarActivity {

    public final static String EXTRA_MESSAGE = "com.example.sadi_ace.myfirstapp.MESSAGE";

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


        setContentView(R.layout.activity_my);

        }


    /** Called when the user clicks the Send button */
    public void TutorialText(View view) {
        Intent intent = new Intent(this, DisplayMessageActivity.class);
        String message = "This is the android tutorial, if you want to pass on the to the " +
                "functions please press SKIP!";
        intent.putExtra(EXTRA_MESSAGE, message);
        startActivity(intent);



    }





  //  @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_my, 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);
    }
}

XML for main activity

<RelativeLayout 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="#800080"
    android:weightSum="1">


    <Button
        android:layout_width="150dp"
        android:layout_height="wrap_content"
        android:text="@string/button_tutorial"
        android:background="#FFFFFF"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:onClick="TutorialText"
         />
</RelativeLayout>

DisplayMessageActivity class code

package com.example.sadi_ace.myfirstapp;

import android.content.Intent;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.Button;
import android.widget.TextView;


public class DisplayMessageActivity extends ActionBarActivity {




    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent intent = getIntent();
        String message = intent.getStringExtra(MyActivity.EXTRA_MESSAGE);

        // Create the text view
        TextView textView = new TextView(this);

        textView.setText(message);

        // Set the text view as the activity layout
        setContentView(textView);
    }


    //  @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_display_message, 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);
    }
}

XML for DisplayMessageActivity class

<RelativeLayout 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="#800080"
    android:weightSum="1">



</RelativeLayout>
Einar Sundgren
  • 4,325
  • 9
  • 40
  • 59

1 Answers1

0

Add button in your xml file activity_my like

   <Button
    android:id="@+id/btnskip"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"/>

After that use this button id in "onCreate " method and assign it to some button

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


    setContentView(R.layout.activity_my);
     skip=(Button)findViewById(R.id.btnskip);

  //add on click listener
    skip.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View arg0) {
            // TODO Auto-generated method stub
         Intent intent = new Intent(this, DisplayMessageActivity.class);
    String message = "This is the android tutorial, if you want to pass on the to the " +
            "functions please press SKIP!";
    intent.putExtra(EXTRA_MESSAGE, message);
    startActivity(intent);
    });
    }
Kunal
  • 432
  • 1
  • 4
  • 14
  • I am using android studio. I dont know if that would make a big difference. Its showing error on line skip.setOnClickListener(new View.OnClickListener and this, DisplayMessageActivity.class – user1792859 Mar 08 '15 at 14:30
  • use "MyActivity.this" instead of only "this" in Intent intent=new Intent(MyActivity.this,DisplayMessageActivity.class); – Kunal Mar 08 '15 at 17:10