Support The Genics Blog at Patreon
Android Robot

Everyone knows this! Android is one of the best OS out there. Every day, many people enter the world of Android coding. And yes, it is also very easy. This is one of the major reasons for the success of Android. People try to get their hands into Android, by starting out with basic apps, understanding activities, and fragments. Then, they move on to something complex such as Lists, Arrays, different Views, Messaging, etc, expanding their knowledge of Android. Then they come upon the idea of making and publishing their own app. This is a general way, people tend to move into Android coding. And, it works well.

Many people come upon the idea of making a static content application which teaches others about specific theme or content, i.e., A content application. This is also a good idea. And it is also famous as people download new apps to learn a particular thing. And, of course, this article is for you if you wish to make a static content based Android application.

A general outline of what you would have thought

If you also thought of making such an app, then I can tell, what most novice or even some intermediate Android coders would think. Suppose, your application is about learning HTML. You know HTML very well and want to share your knowledge. Then you started an app and here comes the thing.

You might have thought to make a Home page(activity, if you know) followed by a splash screen. There you would link other activities to the Home activity with buttons. This implies that you are making different activities for each activity containing specific content that you want to show to the user! This means that to give information about more than 100 HTML tag(approx.), you will make these many activities(plus About page, credits, etc). This now makes sense that to handle that number of activities, we need to do something.

If we make 100+ activities an link them to the Home activity, the size of the app and load on the system would increase incredibly.

Support The Genics Blog at Patreon

The solution

Don’t worry! I am here to solve your problem. Just follow me.

The first and most important step, consider supporting us at Patreon, which helps us to share such great information with you!

Next, you can proceed to make your basic activities, including SplashScreen, HomePage, Contact, About and Credits. These are the basic requirements for almost every kind of app. This makes your app feel special and worthy,

Then, don’t start making activities like hell, just make another Activity named Content(.java of .kt– your choice, I know java and so, this article will teach the steps in java). And don’t make other activities by their single name.

Now, what we are going to do is the dynamic viewing of static content! Doesn’t that seem interesting, let dig into it.

What to do after that?

After that, include all the content just into the strings file. Since we want our content to be beautiful, let’s make it HTML(later, I will show you how to render that text into TextView):

<resource>
...
    <string name="tag_anchor"><![CDATA[
        <h1>Anchor tag</h1>
        <p>Anchor Tag is used to make hyperlinks. It links webpages.....</p>
        ...
    ]]></string>

    <string name="tag_heading"><![CDATA[
        <h1>Heading tags</h1>
        <p>Heading tags enable us to enlarge text and make headings.....</p>
        ...
    ]]></string>
...
</resource>

Be sure to wrap the HTML in <![CDATA[ tag to allow HTML usage in TextView.

Support The Genics Blog at Patreon

In the buttons of the Home activity, add the Intent as such:

tagAnchor.setOnClickListener(v->{//tagAnchor is identifier for showing info about anchor tag.
    Bundle b = new Bundle();
    b.putString("from", "tagAnchor");
    /*tagAnchor is the identifier that represents which button is pressed*/
    startActivity(new Intent(Home.this, Content.class).putExtras(b));
}

Also, see Three Ways to Open New Activity in Android.

Now I am going to give you the code of Content.java and the activity_content.xml file and I will explain whatever is going on.

Content.java

package com.example; //your package

import ...;

public class Content extends AppCompatActivity {

    @Override
    protected void onCreate(final Bundle savedInstanceState) {
        ...
        TextView content = findViewById(R.id.main_content);

        Intent in = getIntent();
        Bundle b = in.getExtras();
        String s = Objects.requireNonNull(b).getString("from");

        switch (Objects.requireNonNull(s)) {
            case "tagAnchor":

                //You can also dynamically change ToolBar text to set title of the page here.

                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    main_content.setText(Html.fromHtml(getString(R.string.tagAnchor), Html.FROM_HTML_MODE_COMPACT));
                }else{
                    main_content.setText(Html.fromHtml(getString(R.string.tagAnchor)));
                }
            break;

            case "tag_heading":
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
                    main_content.setText(Html.fromHtml(getString(R.string.tag_heading), Html.FROM_HTML_MODE_COMPACT));
                }else{
                    main_content.setText(Html.fromHtml(getString(R.string.tag_heading)));
                }
            break;
        }
        ...
    }
    
}

activity_content.xml

<LinearLayout...>

    <ToolBar...>

    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <LinearLayout
            android:layout_height="wrap_content"
            android:layout_width="match_parent"
            android:orientation="vertical"
            android:padding="10dp">

            <TextView
                android:id="@+id/main_content"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:textColor="#000000"
                android:linksClickable="true"
                android:textColorLink="@color/coloraccent"
                android:textSize="18dp"/>

        </linearLayout>
    </ScrollView>

</LinearLayout>
Support The Genics Blog at Patreon

Break-down of Code

Now, I am going to analyze the code. In the activity_content.xml, we define an element- TextView with id main_content and also, I initialize this in Content.java file. When the intent to required content is triggered, I assign a string(bundle object) with key-value pair. Every time I have the key named “from” which defines that a button has been clicked. It’s valued is the name of the button clicked.

In the Content.java file, I retrieve what is the value of the “from” key. On the basis of the value retrieved, I set the value of the TextView accordingly to show the content which is actually needed.

Conclusion

In this article, you saw how you can dynamically show static content in an activity. Instead of making different activities for each type of content, you want to show, you can make single activity and update it’s content as required.

I hope you find this article helpful and will share it to increase people’s efficiency in coding.