Menu

170817Ago - CHAPTER 2

170817Ago - CHAPTER 2

Well, we have the IDE running and a basic app skeleton.

Today I go to add a simple input/output interface to display information easily.

As I commented, an Android app is formed by Activities, this is the memory container for objects, and the basic interface to work with the device, screen, keyboard, file system, etc...

If you look at "MainActivity.java" you can see that this main class extends AppCompatActivity that is a concrete class inherited from Activity. But now is important to understand that this class at the begin of run show and define the screen:

public class MainActivity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

    // Example of a call to a native method
    TextView tv = (TextView) findViewById(R.id.sample_text);
    tv.setText(stringFromJNI());
}

In this method (onCreate) using setContentView we can to indicate the layout (or design) that appear in the screen, in this case R.layout.activity_main.

Well, but where is this design?... you must look in the resources folder:

res/layout/activity_main.xml

Here, in this xml file we can design the UI (User interface), in this case we have only a <TextView> tag.

Is important know that resources are identified in Android Studio with:

R.<group>.id

See that this TextView has a id attribute setted to "@+id/sample_text"...

@+id tell to Android Studio that must add this id to the resource database.

When this layout is charged, inside the Activity object will be created the visual objects and we can recovery them using the id:

TextView tv = (TextView) findViewById(R.id.sample_text);

The we can use this object to set some values, as:

tv.setText(stringFromJNI());

In this case we set a string that we obtain calling to our C++ module, because we are programmers and we love do the things more complex than necessary ;D

Now we go to add a class to get informatión rom the device and show it in our screen.

For that we can use the PackageManager class, this can be obtained from Context. This class is the global object repository for our Activity.

Activity can give us the contex calling to getApplicationContext. then we can call to getPackageManager, and this give us device information across methods:

- getSystemAvailableFeatures()
- hasSystemFeature(String name)

Here the class to get system information:

package homebrew.dhtejada.androidblog.mysuperapp;

import android.content.Context;
import android.content.pm.FeatureInfo;
import android.content.pm.PackageManager;
import android.widget.TextView;

import java.util.ArrayList;

public class MySystemInfo {
        Context context;
        ArrayList<String> report=null;

        MySystemInfo(Context context){ this.context=context; }

        MySystemInfo checkCapabilities(){
            PackageManager pm = context.getPackageManager();

            FeatureInfo[] features=pm.getSystemAvailableFeatures();
            for (FeatureInfo fi : features){
                    report.add(fi.name);
            }
            return this;
        }
        MySystemInfo printReport(TextView tv){
            if (report==null) return this;
            String r=report.toString();
            tv.setText(r);
            return this;
        }
}

And we can use this class from MainActivity to set the information in our TextView:

MySystemInfo si=new MySystemInfo(getApplicationContext())
                .checkCapabilities()
                .printReport(tv);

Well, but this return a lot of information and does not fit into our screen, our TextView hasn't scrollbars and for that we must to use a ScrollView object. We can do this in our layout file activity_main.xml:

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="homebrew.dhtejada.androidblog.mysuperapp.MainActivity">

    <ScrollView android:visibility="visible"
        android:layout_height="335dip"
        android:layout_width="match_parent"
        android:keepScreenOn="true"
        android:scrollbarStyle="insideInset"
        android:id="@+id/scrollView1"
        android:scrollbars="vertical">

        <TextView
                android:id="@+id/sample_text"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                android:text="Hello World!"
                app:layout_constraintBottom_toBottomOf="parent"
                app:layout_constraintLeft_toLeftOf="parent"
                app:layout_constraintRight_toRightOf="parent"
                app:layout_constraintTop_toTopOf="parent" />
    </ScrollView>
</android.support.constraint.ConstraintLayout>

We can run the app installing it in our phone connected via USB directly from the IDE, clicking RUN and selecting the conected device. To do this you must to activate in the phone in Developer Options: "Enable USB Debug".

Posted by David Hernandez Tejada 2017-08-24 Labels: ANDROID STUDIO ANDROID DEVELOPMENT PROGRAMMING

Log in to post a comment.

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.