-3

I am trying to make an information app that has multiple navigable pages in Android Studio. I will probably have around 40 or so pages of information. I was wondering whether I should manually create an activity for each and every page (with headers, images, and info) or to use one activity and programmatically add textviews and imageviews in accordance with a String.

I decided to first try using only one activity, and it's working alright, but to add new features like different fonts to emphasize words or titles or to add formulas would require a lot more complicated code.

For example I would have this in my string.xml file.

<string name="page1">
~Introduction~\n
Hi guys!\n
`image`
</string>

And then I would have a method called updatePage() that checks for my markers like ~ for titles and ` for images.

private void updatePage(int pPageNum){
        String dayString = "";
        pageNum = pPageNum;

        mScrollView.fullScroll(ScrollView.FOCUS_UP);

        mEditor.putInt("page", pageNum);
        mEditor.apply();

        //todo add substrings to look for specific image and formula markers
        try{
            dayString = dayJSON.getString(pPageNum+"");
            pageInfoLL.removeAllViews();

            int i = 0;
            int t = 0;
            int nonTextStartIndex = 0;
            int nonTextEndIndex = 0;
            while(i != -1 || t == 3){
                if(dayString.contains("`")){
                    nonTextStartIndex = dayString.indexOf("`");
                    String textSubstring = dayString.substring(0, nonTextStartIndex);

                    if(!textSubstring.equals("")){
                        addTextViewToLayout(pageInfoLL, textSubstring, 12);
                        dayString = dayString.substring(nonTextStartIndex + 1);
                    }

                    nonTextEndIndex = dayString.indexOf("`");
                    String imageName = dayString.substring(0, nonTextEndIndex);
                    Log.e("IMAGERESOURCENAME", imageName);
                    addImageToLayout(pageInfoLL, imageName);
                    dayString = dayString.substring(nonTextEndIndex+1);
                    Log.e("After Image String", dayString);

                    if(dayString.equals("")){
                        Log.e("String Finished", "STRING FINISHED");
                        i = -1;
                    }

                    t++;

                }else{
                    addTextViewToLayout(pageInfoLL, dayString, 12);
                    i = -1;
                }
            }
        }catch (JSONException je){
            je.printStackTrace();
            makeToast("JSON Error, Page Did Not Update Successfully.");
        }catch (NullPointerException ne){
            ne.printStackTrace();
            makeToast("NULL Error, Page Did Not Update Successfully.");
        }
    }

As you can see though, it removes a lot of the flexibility. I also wanted to add formulas using jqMath, but jqMath uses lots of characters that overlap with other uses. The logistics just becomes a lot more complicated. Thus, I want to know just what are the advantages to doing it the wya Im doing it right now. If it's only a smaller app file size then maybe I'll just manually make each page. Thanks for the help.

Iyatsu
  • 37
  • 1
  • 1
  • 7
  • This might help, [layout programmatically vs XML](https://stackoverflow.com/questions/35568058/android-layouts-performance-programmatic-vs-xml). – Tenten Ponce Jun 17 '19 at 02:36

1 Answers1

1

If you have to add many views in one activity, it may be efficient to create custom view class. For example, create custom view class like this.

Layout:

 <TextView
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:textSize="18sp"
        />

    <View
        android:layout_width="26dp"
        android:layout_height="26dp"
        android:layout_centerVertical="true"
        android:layout_marginLeft="16dp"
        android:layout_marginRight="16dp"
        />

    <ImageView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginRight="16dp"
        android:layout_centerVertical="true"
        android:visibility="gone"
        />

Class:

public class CustomView extends View {
    public CustomView(Context context, AttributeSet attrs) {
        super(context, attrs);
        //inflate above layout to this view
        LayoutInflater inflater = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        inflater.inflate(R.layout.view_color_options, this, true);
        //initialize textView and ImageView with each view attributes.
        TextView title = (TextView) getChildAt(0);
        title.setText(titleText);
        ....
    }
}

After create this class, you can create new custom view instance when you need to add this view to main activity layout.

//create new attribute as you want.
....

CustomView customView = new CustomView(context, attribute);
customView.setId(Integer.parseInt("5"));
customView.setLayoutParams(new LinearLayout.LayoutParams(
       LinearLayout.LayoutParams.FILL_PARENT,
       LinearLayout.LayoutParams.WRAP_CONTENT));

linearLayout.addView(customView);
...

Then you can add many views in your main layout easily.

double-beep
  • 5,031
  • 17
  • 33
  • 41
Wencheng Li
  • 126
  • 6
  • But the arrangement of my information page changes from page to page, so i might need 2 images for page 1, but like 3 textviews and 1 image and 1 formula for page 2 - and as you can see I already am adding views programmatically based on a string - when the updatePage encounters ` it will add an image and add textviews for the other parts of the string – Iyatsu Jun 17 '19 at 04:48
  • Yeah, In the CustomView class you can hide or show some views according to attribute parameters. For ex, suppose you make a custom view xml file with 3 text views and 5 image views. and suppose you need to add a custom view with 2 text views and 1 image view. In this case you can hide 1 text view and 4 image views. Then you can add fit view in main Layout.... P.S. Current your code is hard coding. – Wencheng Li Jun 17 '19 at 08:01
  • Welcome to Stack Overflow! Please don't post answers on obviously off topic/bad questions! [See: **Should one advise on off topic questions?**](//meta.stackoverflow.com/q/276572) – double-beep Jun 18 '19 at 05:45
  • 1
    I see. Thanks for your advise. – Wencheng Li Jun 18 '19 at 05:49
  • Oh, I get it - thanks Li - I'll try that, and what do you mean by hardcoding? How can I stop hard coding - thanks – Iyatsu Jun 18 '19 at 16:36