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 trialEleni Minadaki
3,687 PointsHow to make a navigator in an android app?
Hi everyone! I have a simple app based on interactiveStory Lessons with 50 articles, I need to give the user the ability to see and choose which they want with a simple menu or navigator, don't know. Does anyone knows how to make this or to tell me where should I search? Thanks for any help!
9 Answers
Eleni Minadaki
3,687 PointsHi Derek, thanks for your answer, I'll try it, can I give you feedback if needs?
Derek Markman
16,291 PointsYa sure
Eleni Minadaki
3,687 PointsHi again Derek, I create as you told me a new activity for the listView, But I can't call it from my main class. I have create already an intent in the main for another class. Below is my code , if you want to have a look:
MainActivity.class
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
meditCenterText.setText("
");
}}
LV.java //for the ListView public class LV extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState){ super.onCreate(savedInstanceState); setContentView(R.layout.activity_lv);
populateListView();
registerClickCallback();
}
private void populateListView() {
//Create listView items
String[] myItems = {"firstPost", "secondPost", "thirdPost", "fourthPost"};
//Build Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.da_item, myItems);
//Configure the listView
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.listView);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View viewClicked, int position, long id) {
TextView textView = (TextView) viewClicked;
String message = "You clicked # " + position
+ "which is string: " + textView.getText().toString();
Toast.makeText(LV.this,message,Toast.LENGTH_LONG).show();
}
});
}
}
Derek Markman
16,291 PointsIf I have some time I'll take a look at this after I get off of work tomorrow. If you know how, it would be helpful to push your project up to github.
Eleni Minadaki
3,687 PointsHi Derek, no problem if you have time it's ok. I don't know yet how to use github, I am learning these days. If you need tell me to post the other activities in the forum. Thanks anyway.
Derek Markman
16,291 PointsOk post your other code here, so I can re create your project when I get home today.
Eleni Minadaki
3,687 PointsHi Derek, I post my code below. Thank you! StringArrayAdapter.java import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.AdapterView; import android.widget.ArrayAdapter; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.TextView; import android.widget.Toast; import android.widget.Adapter;
import com.example.eleni.interactivestory.R;
/**
-
Created by Eleni on 4/3/2016. */ public class StringArrayAdapter extends Activity{
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main);
populateListView(); registerClickCallback();
}
private void populateListView() {
//Create listView items
String[] myItems = {"firstPost", "secondPost", "thirdPost", "fourthPost"};
//Build Adapter
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this, R.layout.activity_arrayAdapter, myItems);
//Configure the listView
ListView list = (ListView) findViewById(R.id.listView);
list.setAdapter(adapter);
}
private void registerClickCallback() {
ListView list = (ListView) findViewById(R.id.listView);
ListAdapter adapter;
list.setAdapter(adapter);{
}
});
}
StoryActivity.java
private Story mStory = new Story();
private ImageView mImageView;
private TextView mTextView;
private Page mCurrentPage;
private String mName;
private Button mPreviousButton;
private Button mNextButton;
private ScrollView mScrollView;
private Button mStartButton;
private View mlistView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_story2);
//Intent intent = getIntent();
//mName = intent.getStringExtra(getString(R.string.key_name));
if(mName == null){
mName = "";
}
Log.d(TAG, mName);
mImageView = (ImageView)findViewById(R.id.storyImageView);
mTextView = (TextView)findViewById(R.id.storyTextView);
mPreviousButton = (Button)findViewById(R.id.previousButton);
mNextButton = (Button)findViewById(R.id.nextButton);
mTextView.setMovementMethod(new ScrollingMovementMethod());
loadPage(0);
}
private void loadPage(int choice) {
mCurrentPage = mStory.getPage(choice);
Drawable drawable = getResources().getDrawable(mCurrentPage.getImageId());
mImageView.setImageDrawable(drawable);
String pageText = mCurrentPage.getText();
//add the name if placeholder included.
//pageText = String.format(pageText,mName);
mTextView.setText(pageText);
if(mCurrentPage.isSingle()){ mPreviousButton.setVisibility(View.VISIBLE); mPreviousButton.setOnClickListener(new OnClickListener() { @Override public void onClick(View v) { mPreviousButton.setVisibility(View.VISIBLE); loadPage(49); }}); mNextButton.setText("Start From The Beginning"); mNextButton.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { mNextButton.setVisibility(View.VISIBLE); loadPage(0); } });
}else{
mPreviousButton.setText(mCurrentPage.getChoices().getText1());
mNextButton.setText(mCurrentPage.getChoices().getText2());
mPreviousButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int previousPage = mCurrentPage.getChoices().getPreviousPage();
loadPage(previousPage);
}
});
mNextButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
int nextPage = mCurrentPage.getChoices().getNextPage();
loadPage(nextPage);
}
}); }}
}
public class MainActivity extends AppCompatActivity {
private TextView meditCenterText;
private Button mStartButton;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
meditCenterText = (TextView) findViewById(R.id.editCenterText);
mStartButton = (Button) findViewById(R.id.startButton);
meditCenterText.setMovementMethod(new ScrollingMovementMethod());
mStartButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
String TextView = meditCenterText.getText().toString();
startStory(TextView);
}
});
}
private void startStory(String TextView) {
Intent intent = new Intent(this, StoryActivity.class);
intent.putExtra("TextView", TextView);
startActivity(intent);
}
@Override
protected void onResume() {
super.onResume();
meditCenterText.setText("
");
}}
Story.java public class Story { private Page[] mPages; private int mPageNumber;
public Story() {
mPages = new Page[55];
mPageNumber = 0;
mPages[mPageNumber] = new Page(mPageNumber,
R.drawable.page0,
"firstText..................", new Choices(mPageNumber,"Previous", "Next")); //50texts....... ................ mPages[mPageNumber] = new Page(mPageNumber, R.drawable.page50, "50th text............");
}
public Page getPage(int pageNumber){
return mPages[pageNumber];
}
}
Eleni Minadaki
3,687 Pointspage.java public class Page {
private int mPageId;
private int mImageId;
private String mText;
private Choices mChoices;
private boolean mIsSingle = false;
public Page(int pageId, int imageId, String text, Choices choices) {
mPageId = pageId;
mImageId = imageId;
mText = text;
mChoices = choices;
}
public Page(int pageId, int imageId, String text) {
mPageId = pageId;
mImageId = imageId;
mText = text;
mChoices = null;
mIsSingle = true;
}
public int getPageId(){
return mPageId;
}
public void setPageId(int pageId){
mPageId = pageId;
}
public int getImageId() {
return mImageId;
}
public void setImageId(int imageId) {
mImageId = imageId;
}
public String getText() {
return mText;
}
public void setText(String text){
mText = text;
}
public Choices getChoices(){
return mChoices;
}
public void setChoices(Choices choices){
mChoices = choices;
}
public boolean isSingle() {
return mIsSingle;
}
public void setSingle(boolean isFinal) {
mIsSingle = isFinal;
}
}
Choices.java public class Choices { private String mText1; private String mText2;
private int mPreviousPage;
private int mNextPage;
public Choices(int currentPage, String text1, String text2) {
mPreviousPage = currentPage - 1;
mText1 = text1;
mNextPage = currentPage + 1;
mText2 = text2;
}
public String getText1() {
return mText1;
}
public void setText1(String text1) {
mText1 = text1;
}
public String getText2(){
return mText2;
}
public void setText2(String text2){
mText2 = text2;
}
public int getPreviousPage() {
return mPreviousPage;
}
public void setPreviousPage(int previousPage){
mPreviousPage = previousPage;
}
public int getNextPage() {
return mNextPage;
}
public void setNextPage(int nextPage) {
mNextPage = nextPage;
}
}
Eleni Minadaki
3,687 PointsWhat I try to do is: A list view which will displays the StringArray from the Story.class and the user can choose and see the text of his choice. (I didn't post the XML files because thought that there is no need, if I am wrong tell me to to post the files.)
Derek Markman
16,291 Pointsnah this should be ok. Please for the love of all that is holy learn github soon... what sdk version are you using?
Derek Markman
16,291 PointsIt's hard to tell from just looking at what you posted here. but to populate your list view from your array of Strings, you're going to need to implement a 'foreach' loop to loop through each of your array items and display it on your list adapter activity.
Eleni Minadaki
3,687 PointsYes you are right! I have to learn gitHub quickly, I have already take 2 lessons and keep going. Here is my build.gradle compileSdkVersion 23 buildToolsVersion "23.0.2"
defaultConfig {
minSdkVersion 15
targetSdkVersion 23
versionCode 1
versionName "1.0"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies { compile fileTree(dir: 'libs', include: ['*.jar']) testCompile 'junit:junit:4.12' compile 'com.android.support:appcompat-v7:23.1.1' compile 'com.android.support:design:23.1.1'
}
Derek Markman
16,291 PointsOK, I'll recreate your project and get back to you sometime this weekend possibly when I have more time.
Eleni Minadaki
3,687 PointsThanks a lot for your help Derek!
Derek Markman
16,291 PointsDo you have an email I can contact you on? I do not feel like using these forums to continue helping you with this.
Eleni Minadaki
3,687 Pointsyes of course. my mail is: helen.minadaki@gmail.com
Eleni Minadaki
3,687 PointsHere is my StringArrayAdapter xml file <?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical" android:layout_width="match_parent" android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/listView"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true">
</ListView>
</LinearLayout>
Derek Markman
16,291 PointsDerek Markman
16,291 PointsWell just thinking from the top of my head, you could create a new activity(intent), and use something like a listView to display all 50 articles, and then implement an onClickListener, so when a user clicks on an article from your list, it takes them to that specific article. Sorry if this isn't helpful, you didn't give a lot of detail.