Welcome to the Treehouse Community
Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.
Looking to learn something new?
Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.
Start your free trial
Andreas Robichaux
2,426 PointsBest way to have a menu go to different content views?
I am working on a custom app that I want to be a portfolio. As of now I have one mainActivity that has a bar going down the center divided into 8 buttons of things you could click on, like "web", "copywriting", "content creation", etc...
I just want to have it so what button you click takes you to the proper page but instead of making 8 different activities I want to try and code it like we learned to in the Interactive Story exercise with MVC...but I'm still kinda confused on a lot of it .
I was thinking if I pass a variable through off each button click with either a number "1" or a string, "web" then it would load the content for the page that is named the same. but not sure if this is the best way to think about it or do it.
I guess I would just make a content page with an array on it and title the array the names of the buttons? then somehow I would have to get the name of the button and pass it into an imageview for the header and pull in the content? Does this make any sense? I hope you can help! Thanks!
as far as passing a variable I found this on github:
//step 1:
import android.content.Intent;
//step 2: add to activity you want to sav variables from
Intent i = new Intent(getApplicationContext(), ActivityName.class);
i.putExtra("someVariable","variableValue");
startActivity(i);
//step 3: add to activity you want to pulll variables from
Bundle extras = getIntent().getExtras();
if (extras != null) {
String someVariable = extras.getString("someVariable");
}
here's what I have so far, two activities.
package com.dredaydesigns.dredaycreativeportfolio;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
public class MainActivity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button webButton = (Button) findViewById(R.id.webButton);
webButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String name = webButton.getText().toString();
Intent intent = new Intent(MainActivity.this, content_activity.class);
}
});
}
// private void startContent(String name) {
//Intent goToNext = new Intent(this, content_activity.class);
// goToNext.putExtra("name", name);
// startActivity(goToNext);
//}
//}
@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_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();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
and my content_activity which for now I had set up with the "web" header picture
package com.dredaydesigns.dredaycreativeportfolio;
import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.Menu;
import android.view.MenuItem;
public class content_activity extends ActionBarActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_content_activity);
@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_content_activity, 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);
}
}