------------ Wellcome bdinfo190.blogspot.com ------------ Wellcome bdinfo190.blogspot.com-------------

Android Studio for beginners, Part 2: Explore and code the app- নতুনদের জন্য অ্যান্ড্রয়েড স্টুডিও, পার্ট 2: অ্যাপ্লিকেশনটি অনুসন্ধান এবং কোড করুন

Android_Studio_icon.svghttpsbdinfo190.blogspot.com
Android_Studio_icon.svghttpsbdinfo190.blogspot.com
In Part one of this beginner's introduction to golem Studio, you discovered golem Studio in your development setting and need to understand the programme. Now, in Part 2, you may code your 1st app.

The animated mobile app consists of one activity, that presents Google's golem mechanism character and a button for enlivening the character. Clicking the button causes the character to bit by bit modification color from inexperienced to red to blue, then back to inexperienced. whereas the app is not particularly helpful, writing it'll assist you get snug with victimisation golem Studio. In Part 3, you may build associate degreed run the app victimisation an golem device somebody and a Kindle hearth pill.

[ Learn Java from starting ideas to advanced style patterns during this comprehensive 12-part course! ]
Note that this series has been updated for golem Studio three.2.1, this stable unharness as of this writing.

Android Studio's Project and editor windows
I introduced golem Studio's main window at the tip of half one. This window is split into many areas, as well as a Project window wherever you establish associate degree app's resource files, and numerous editor windows wherever you may write the code and specify resources for mobile apps in golem Studio. The Project window associate degreed an editor window area unit shown in Figure one.

figure1
Jeff Friesen
Figure 1. golem Studio's Project window associate degreed an editor window (click to enlarge)
The Project window highlights W2A, that is that the name of the app's W2A.java supply file (although the .java file extension is not shown). such as W2A is associate degree editor window, reached by double-clicking W2A within the Project window. The editor window reveals the file's current contents, during this case the skeletal Java ASCII text file for the app's main activity.

Each editor window is related to a tab. for instance, W2A's editor window is related to a W2A.java tab. A second tab known as main.xml (the default XML-based layout for the app's main activity) is additionally shown. you progress from one editor window to a different by clicking the window's tab.

download
Get the code
Download the ASCII text file for the golem example app: W2A.java. Created by Jeff Friesen for JavaWorld.
The golem example app
The example app (W2A.java) consists of a main activity that displays the golem mechanism character and a button. once the user presses the button, the mechanism animates through a series of colours. during this section, we'll explore the activity's ASCII text file and resources.

Explore and code the golem example app
The activity's ASCII text file is hold on within the file W2A.java, conferred in Listing one.

Listing 1. W2A.java

package ca.javajeff.w2a;

import golem.app.Activity;

import golem.graphics.drawable.AnimationDrawable;

import golem.os.Bundle;

import golem.view.View;

import golem.widget.Button;
import golem.widget.ImageView;

public category W2A extends Activity
read. (ImageView) findViewById(R.id.android);. This statement 1st calls read's View findViewById(int id) technique to search out the golem.widget.ImageView part declared in main.xml and known as golem. It instantiates ImageView and initializes it to the values declared within the main.xml file. The statement then saves this object's reference in native variable androidImage.

ImageView and AnimationDrawable
Next, the androidImage.setBackgroundResource(R.drawable.android_animate); statement invokes ImageView's transmitted (from View) void setBackgroundResource(int resID) technique, setting the view's background to the resource known by fossil oil. The R.drawable.android_animate argument identifies associate degree XML file named android_animate.xml (presented later), that stores info on the animation, and that is hold on in res's drawable directory. The setBackgroundResource() decision links the androidImage read to the sequence of pictures delineate by android_animate.xml, which can be drawn on this read. The initial image is drawn as a results of this technique decision.

ImageView lets associate degree app animate a sequence of drawables by line of work AnimationDrawable strategies. Before the app will do that, it should get ImageView's AnimationDrawable. The androidAnimation = (AnimationDrawable) androidImage.getBackground(); assignment statement that follows accomplishes this task by invoking ImageView's transmitted (from View) Drawable getBackground() technique. This technique returns the AnimationDrawable for the given ImageView, that is later on appointed to the androidAnimation field. The AnimationDrawable instance is employed to begin associate degreed stop an animation, a method i will describe shortly.

Finally, onCreate(Bundle) creates the Animate button. It invokes findByViewId(int) to get the button info from main.xml, then instantiates the golem.widget.Button category.

It then employs the read class's nested "1.0" encoding="utf-8"?>

   
   <Button android:id="@+id/animate"<br>
              android:layout_width="wrap_content"
              android:layout_height="wrap_content"
              android:text="@string/animate"/>

After the XML declaration, Listing a pair of declares a LinearLayout part that specifies a layout (a read cluster that arranges contained views on associate degree golem device's screen in some manner) for transcription contained widgets (including nested layouts) either horizontally or vertically across the screen.

The  tag specifies many attributes for dominant this linear layout. These attributes embody the following:

orientation identifies the linear layout as horizontal or vertical. Contained widgets area unit set out horizontally or vertically, and also the default orientation is horizontal. "horizontal" and "vertical" area unit the sole legal values that may be appointed to the present attribute.
layout_width identifies the breadth of the layout. Legal values embody "fill_parent" (to be as wide because the parent) and "wrap_content" (to be wide enough to surround content). (Note that fill_parent was renamed to match_parent in golem a pair of.2, however continues to be supported and wide used.)
layout_height identifies the peak of the layout. Legal values embody "fill_parent" (to be as tall because the parent) and "wrap_content" (to be tall enough to surround content).
gravity identifies however the layout is positioned relative to the screen. for instance, "center" specifies that the layout ought to be focused horizontally and vertically on the screen.
background identifies a background image, a gradient, or a solid color. For simplicity, I've hardcoded a hex color symbol to indicate a solid white background (#ffffff). (Colors would ordinarily be hold on in colours.xml and documented from this file.)
The LinearLayout part encapsulates ImageView and Button components. every of those components specifies associate degree id attribute, that identifies the part so it is documented from code. The resource symbol (special syntax that begins with @) appointed to the present attribute begins with the @+id prefix. for instance, @+id/android identifies the ImageView part as android; this part is documented from code by specifying R.id.android.

These components additionally specify layout_width and layout_height attributes for decisive however their content is set out. every attribute is appointed wrap_content so the part can seem at its natural size.

ImageView specifies a layout_marginBottom attribute to spot an area centrifuge between itself and also the button that follows vertically. The area is specific as ten dips, or density-independent pixels. These area unit virtual pixels that apps will use to specific layout dimensions/positions in an exceedingly screen density-independent approach.

Density-independent pixels
A density-independent element (dip) is admire one physical element on a 160-dpi screen, the baseline density assumed by golem. At runtime, golem transparently handles any scaling of the specified dip units, supported the particular density of the screen in use. Dip units area unit regenerate to screen pixels via the equation: pixels = dips * (density / 160). for instance, on a 240-dpi screen, one dip equals one.5 physical pixels. Google recommends victimisation dip units to outline your app's programme to make sure correct show of the programme on totally different device screens.

No comments

Powered by Blogger.