Joe Hackerberg
Joe Hackerberg

Reputation: 467

How to create an activity with its corresponding XML file just tapping a button

I'm programming an app which one of its functionalities is that the users can tap on a plus button (typical (+)...) that has to create a new activity and a new XML file with a specific structure.

I'm beginner on Android and also in Stack Overflow, so due to that I'm unable to post images and make this question easier to understand.

I need you to summarize how to program this. I'm not telling you to codify my code, I just need to know if it's possible to do and ,more or less, the steps to get it.

Hope you can help me. Thanks!

Upvotes: 1

Views: 1604

Answers (2)

JoePhillips
JoePhillips

Reputation: 721

You can make the activity yourself and have a button open the activity.

You'd start by making a new android activity. With eclipse it's simply File >> New >> Other >> Android Activity and then just fill out the form and hit finish. Make sure your current project is open.

Draw the button in your xml file, make sure it has a unique Id to reference and your text is declared in your strings.xml file that should look like this.

<string name="strX">(x)</string>

then in your activity's xml file under your button make sure you have

android:text="@string/strX"

You can also reference this in the GUI in the properties window under text.

With the button code in your .java you could use OnClickListener and Intent and the code for the button would look something like this.

TextView buttonYourButton = (TextView) findViewById(R.id.ButtonYourButtonId);
Button pushYourButton = (Button) buttonPlay;

pushYourButton.setOnClickListener(new View.OnClickListener() {
  @Override public void onClick(View v) {
    Intent nameOfIntent = new Intent(NameOfCurrentClass.this, NameOfNewClass.class);
    startActivity(nameOfIntent);    
  }
});

R.id.ButtonYourButton is the Id you gave the button, and the .class is the name of the public class in that .java file. Like:

public class MainActivity { ...

Anyways, good luck I was where you were about a month ago. Don't forget to check out tutorials on Android development on youtube, there are about a million of them. Also you can search stackoverflow for questions that already have been asked.

Upvotes: 0

nstosic
nstosic

Reputation: 2614

Okay, so this is how you'd do it, if you're sure you want to create a new instance of the activity from that very same activity. First, make a reference to your "plus button". Assuming the android:id="@+id/plusButton, it'd be like this:

public class MainActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        //Initialize your layout and variables
        findViewById(R.id.plusButton).setOnClickListener(new View.onClickListener() {
            @Override
            public void onClick(View v) {
                Intent i = new Intent(getApplicationContext(), MainActivity.class);
                startActivity(i);
            }
        });
   }
}

This would launch a new instance of the same Activity (MainActivity) and give it focus.

Upvotes: 1

Related Questions