Android – Samsung Galaxy Tab 4 8.0 Help

Here are my notes on working with, debugging, and programming for the Android Galaxy Tab 4 8.0 inch.

Note 1. If you use a nightowl security system and Android operating system and you noticed that the night owl software recently stopped working like it no longer receives a video stream the problem can be resolved by downloading MyEyePro from the Google store for free and using it instead of nightowl light. The programs are almost identicle and are fully compatible.

NOTE 2. Programming in Android Studio using a galaxy 4 tablet as a testing platform.

First you’ll need to enable the developer mode on the device. Go to apps (at the bottom right hand corner of the screen, then to settings, then to general, then to about device, then to build number and tap that a number of times, eventually you’ll enable developer mode. Sneaky. Now there is a new section under the general settings called developer options. Here you can enable USB debugging which you should.

Next download Kies Version 3 and install and try it out. It is for transferring files between the tablet and the computer via USB. There is a new program that Samsung wants you to use that it will tell you about when you start Kies, but Kies 3 works fine from what I can tell and it appears that Kies contains the USB drivers that are necessary for USB debugging. http://www.samsung.com/us/kies/

Now plug in your tablet via USB to the computer, start Android Studio, click on Device Monitor and you’ll see the device. It will be listed as something like sm-t330nu etc. Click on that and click the camera icon to see if you can capture an image from the device. Check your device to give the computer permission to debug.

Now Android Studio and the device are connected, but you have to set up Android Studio to use your device for testing the program. Click the button which shows a robot that has a drop down menu and says “Select Run/Debug Configuration” in Android Studio. Then click edit configurations. Make sure you are under Android Application and click the plus button to create a new configuration. Name it something like “mytest” and under the general tab select the module “app”, the package should be default APK, the activity should be launch default, and the target device should be set to show chooser dialog without the check mark. Click okay and now change the button from whatever it was before to “mytest.” Remake the file, and click the “play” or “run” button. If nothing happens try exiting the program and re-entering the program and run again. The app you made should now be displayed on your device.

If you get a warning that says, “Warning:The project encoding (windows-1252) does not match the encoding specified in the Gradle build files (UTF-8).This can lead to serious bugs.” Go to File, Settings, Editor, File Encoding, and make sure the IDE Encoding and Project Encoding are both set to UTF-8. And change the Default encoding for properties files to UTF-8 as well. You can fix this problem permanently by going to file, other settings, default settings, editor, file encodings, and make sure all settings (there are three of them) there are set to UTF-8.

If you get a warning in the middle of the screen where the little phone picture appears that says, “Rendering Problems The following classes could not be found – android.support.v7.internal.widget.ActionBarOverlayLayout” you just need to set a theme for the app. Just above the picture of the phone is a word “APPTHEME”, click that and choose something like Holo Light and click okay.

The phone in the middle is setup to be similar to what you have to make the design process easier. If it is different than your phone or tablet where it says NEXUS 4 click that and choose another more similar setup. If none are sufficient choose add device definition, create virtual device, phone or tablet, new hardware profile set screen size and resolution, memory (which should be maybe 1 GB or you might get a warning about memory issues), other options you may want and click ok, finish, and leave all of that, now choose the tablet or phone you made instead of the NEXUS 4 and apply a theme.

Android Apps are designed with xml files named after the activity (the window you interact with) called something like “activity_mail.xml” and the program logic is stored in a java file named “MainActivity.java” or something. The xml is for design interface and management of string variables and resources, but the logic is stored in java files. Xml is a markup language similar to html and pretty easy to learn. Everything is withing <> and </> braces to tag everything. Things can be nested and everything is indexed by name. Once the basic widget has been declared with <></> properties defining widgets are set though code like android:textColor=”#000000″.

Note 3. XML Information

Android Programming Notes

Extensible Markup Language (XML) is a way to store information in a non-system-specific way.

Code example includes..

<Books>

<Book>

<Title>Animal Farm</Title>

<Author>George Orwell</Author>

</Book>

<Book>

<Title>Guilty</Title>

<Author>Ann Coulter</Author>

</Book>

<Books>

You can also use attributes instead of children for values.

<War year=”1860”>

<Name>War of Northern Aggression</Name>

<AltName1>Civil War</AltName1>

<AltName2>War Between the States</AltName2>

</War>

Each complete entry into the file is called an Element.

A DTD is a Document Type Definition for the user and computer to use for validation and instruction which describes what the each record must include and the root element. The DTD can be in the same file or separate. An example…

<?xml version=”1.0” encoding=”UTF-8?>

<!ELEMENT Movies (Movie*)>

<!ELEMENT Movie (Title, Price)>

<!ATTLIST Movie year CDATA #REQUIRED>

<!ELEMENT Title (#PCDATA)>

<!ELEMENT Price (#PCDATA)>

The first line tells us what version of xml is being used and what encoding format.

The next line declares an element called Movies which will serve as a root element containing child elements called Movie. The asterisk means that the child element can occur zero or multiple times. Other choices there include + and ? which means that one or more times and zero or one times respectively. Using a comma to separate elements means that one element must follow another and using | means one or the other can be used. #PCDATA means the data is a standard type of text data. Not used here, but also available is ANY and EMPTY meaning any child elements are allow and no child elements of any type are allowed.

Attributes need an ATTLIST tag which provides the name of each attribute, the type, and the default value, so that the syntax appears as <!ATTLIST element-name-it-can-be-associated-with attribute-name type-of-attribute default-value> An example is:

<!ATTLIST Book publisheddate CDATA #REQUIRED>

Element can be CDATA with the attribute value of any character string, string1 | string2 with the type of one of the listed strings, NMTOKEN which is a name token (a string made up of letters and numbers), NKTOKENS (two or more strings like the NKTOKEN, but separated by whitespace, ID (a name token that MUST be unique to the document), IDREF (an ID that must have been used somewhere else in the document), IDREFS (the same as IDREF but separated by whitespace).

Attribute defaults can be: #REQUIRED (and are so required), #IMPLIED (and are optional), value (optional and used if the attribute is omitted), and #FIXED value (optional, but if included it must be this value and if omitted this value is used by default). For example…

<!ATTLIST Book published-date CDATA #REQUIRED>

That means that the attribute is associated with the element Book, is named published-date and can be any kind of textual data, but it is required.

Another good example is…

<!ATTLIST Movie genre (Action|SciFi|Classic|Drama) Action>

There the attribute is for the element movie, the attribute is named genre, it can be either Action, SciFi, Classic, or Drama, and by default it is Action.

Note 4.

The AndroidManifest.xml file contains a list of all activities and it runs the ic launcher main activity. If you have trouble compiling and it says something about ic_launcher or mipmaps or drawable the problem is that code you are using is using code like “@drawable/ic_launcher” but Android Studio uses “@mipmap/ic_launcher” by default for its icon file. You’ll have to change the source code to reflect that, but simply changing it in the manifest file is not good enough, it will just change it back, make sure you change it throughout your program before trying to recompile.

Note 5.

To declare an integer it’s just int a = 1; as it is in java normally, but strings are handled differently.

To create a non-variable (final) string statically use private static final String MyMessage = “This is my message.”; below the MainActivity class’s opening brace before the first method.

Generally strings must be in their own resource file in the root of the particular application’s folder, then the app folder, src, main, res, values, strings.xml so put your string variables in there. The file structure is <resources> </resources> with the strings between the two, and the syntax is <string name=”my_string”>This is my string.</string>

Then you could access the contents of that string from within your program using a syntax similar to android:text=”@string/my_string”

To create a string array go into the string resource file as you did before as use this syntax…

<string-array name="my_array">
        <item>@string/customer_Bob/</item>
        <item>@string/customer_Sam</item>
        <item>@string/customer_Jack</item>
    </string-array>
You can also grab the info inside of an xml string resource using code like this… String myString = getResources().getString(R.string.my_name);
To change background colors… android:background=”#009900″
Note 6.
General Programming Notes for Android:
Activities are windows. Views are widgets or “visual components” although some are actually invisible, viewgroups group them together, layouts arrange views and viewgroups, linear and relative layouts are common. In the res/layout folder there are activity files like the main activity. They are .xml files.
You can edit the main activity file to change the starting window. The layout within this file is a view, which is widget of sorts, and within the layout view are child views like textboxes (called textviews), dialogs you can interact with and picture boxes etc. Examples of what you can do here is change the relative layout to linear, delete the standard textview (which is called a child view because it resides within the layoutview) used to make the helloworld message, and add a property to start the layout in horizontal mode instead of vertical with the code android:orientation = “horizontal”
To comment out lines, single or multiple <!– with –> at the end or use ctrl + / or ctrl + shift + / in android studio for easy comments.
The standard hello world line block is in its own view and looks like this…
  <TextView android:text=”@string/hello_world” android:layout_width=”wrap_content”
android:layout_height=”wrap_content” />
Wrap content means the view should only be as large as the content it contains. The opposite is wrap_parent which makes the view as large as the parent view which is the layout, but there is also match_parent.
Hint is the string to use when the content is empty. It can be anything.
The confusing section is “@string/hello_world”. This style is used because android doesn’t like directly hardcoded strings to be used. “Hello World!” would work, but it is ill advised because of multilingual support standards. Instead of directly coding strings like that android encourages the use of a file where all the strings are stored and so they are to be referenced by their name instead of by their content. The file is app/res/values/strings.xml and looks like this…

<resources>
    <string name=”app_name”>TestApplication</string>
    <string name=”hello_world”>Hello world!</string>
    <string name=”action_settings”>Settings</string>
</resources>

The app name and action settings are standard and hello_world is what is referenced in our standard hello world type program and so its standard as well. The string name is hello_world with the underscore and that is how it is referenced. Between the <> is the content of the string without quotes, which is a change from standard java and follows xml syntax instead.

Views can be assigned id attributes (properties) which can be used to reference them. For example android:id=”my_thing” would assign the name of my_thing to the view you created so you could manipulate the view from another portion of the program later such as changing the text of a text view or something. In particular you need this if you need to reference the view from the activity code, but you don’t if you work within one layout.

The @ symbol is used whenever you use a reference to an element (object) within an xml resource file. For example android:text=”@string/hello_world” refers to the hello_world element which is labeled as a string and found within the resource files. Within the file it appears as <string name=”hello_world”>Hello world!</string> Because of this labeling technique you could have a string and a picture both with the same name in the same resource file.

Note 7.

Colors: Within the strings resource file you can define colors so they can be referenced by name instead of by their hex code. I like to emulate green monitors so this is useful.

<color name=”old_school”>#00FF00</color>
<color name=”old_school_soft”>#99FF99</color>
<color name=”old_school_accurate”>#33FF66</color>

(jwp on nekochan recommends 33FF66 to emulate the classic green monitors.)

Now you could test out one of the colors with android:textColor=”@color/old_school” />

Note 8. Buttons

To create a button use the designer and add this line to the buttonview android:onClick=”doSomething” where doSomething or whatever will be the name of the method you will use to add functionality to the button. The method will be defined within the main activity’s code, not the activity’s xml file which is only used for the design of the interface. The method must be public, have a void return value, and have a View (the view that was clicked) as its only parameter.

The method will be placed within the braces of the mainactivities class and will appear as…

public void doSomething(View view) {
// Do something in response to button
}

The (View view) section means that the view which called the method will pass its own views values into the method making a copy of them into a variable called view which can be referenced locally within the method as needed. In other words you are making a copy of the view that called the method within the method so you can use any properties of the view as part of your method’s code.

The method will need an “intent” which is the calling of an abstract classes method which begins some sort of operation. In this case it will be used to launch another activity, but it can also be used to start services or bind things. The line will look like Intent intent = new Intent(this, LaunchNewActivity.class); of course you’ll need to write that class later. To use the intent class however you’ll need to import it using the import line of import android.content.Intent; where all your imports are or just click the warning that pops up (or press Alt+Enter) to do it automatically.

Note 9. Simple Popup Messages and first program.

Start your Android Studio, setup a basic program, set up the virtual phone, and in the project browser (to the far left) click 1: Project to see the project files, then go to app, java, the first .com file and MainActivity, and double click it. That is the source code for the program.

Make a new method (placing this code before the last brace mark)…

public void showMessage(){
Toast mytoast = Toast.makeText(MainActivity.this,”Hi”,Toast.LENGTH_LONG); mytoast.show();

//alternative line Toast.makeText(getApplicationContext(), “Hi”, //Toast.LENGTH_LONG).show();
}

That creates a method to display a message, now we need to call it when the program runs on its creation.

Go up to where it says…

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

And just before the ending brace add the code

showMessage();

Now run your program and it will show a message immediately. The code

Toast mytoast = Toast.makeText(MainActivity.this,”Hi”,Toast.LENGTH_LONG);

means use the Toast data type to create an instance called mytoast using the definition constructed from the method Toast.makeText(); and the parameters given to that method are MainActivity.this (which is the activity (window or display or whatever) in which the message will appear) and Hi is the message that will appear and then we use the Toast.LENGTH_LONG method to help us define the duration of how long the message will appear. Pretty simple.

Note 10.

Resources are stored in the res folder including layout, values, drawable. The ADT plug-in interprets these to give you access through the R variable object. So to get access to a TextView attributes for instance, from code you use, TextView localTextView = (TextView) findViewByID(R.id.interfaceTextView);

That makes a copy of the textview found in the interface which was labeled with the ID interfaceTextView. You are using it as a reference to create a local copy called myTextView. The copy is now local and can be used as an object in the code to manipulate the original textview in the interface. This is how objects are used. They are handles or controllers for attributes within other classes. TextView is a class whose parameters are defined within the interface design as found in the XML file. Without any modification it will be created as originally defined and have no other functionality, but by creating a local instance of it you can manipulate the created object with the copy and give it extra functionality and set its attributes remotely.

Leave a Reply