Sei sulla pagina 1di 5

When we create blank activity ..... what this Activity is?

You are probably wondering what an Activity is in Android, and what exactly its function is. An
Activity is a screen area that holds content and user interface (UI) designs that provide a frontend (display area) for your application to interface visually with your end-users.
The main activity is defined via XML mark-up and then inflated via Java code in your application
onCreate( ) method in your Main Activity class.

Creating User Interface Screen Layouts in Android Using


XML:
Every android application contains a default activity_main.xml . That is default screen layout
when this application is run on android device.
Now we will se inside the activity_main.xml file in depth:The first XML tag shown in the <RelativeLayout> tag, which provides a Layout Container that
will hold our user interface elements, in this case text, and eventually, imagery and buttons, for
example.

NOTE:-

match_parent value tells the Relative Layout container to expand to fill the entire
display screen.
wrap content value which essentially does the exact opposite of what the
match_parent setting does. Instead of expanding the tag to fill its container,
wrap_content shrink-wraps the container.
Property android:layout_below="@+id/element1" used to set an element
just below the element1 on the layout(screen) .

---------------------

Creation Of Menu Item:


1: In MainActivity.java override the method

public boolean onCreateOptionsMenu(Menu menu) {


// Inflate the menu; this adds items to the action bar if it is
//present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}

Note that: onCreateOptionsMenu(Menu menu) method just render the menu on screen but donot
respond if any menu item is selected .
2: in res/Menu/main.xml file add item like
<item
android:id="@+id/action_settings"
android:orderInCategory="100"
android:title="@string/action_settings"
app:showAsAction="never"/>
By adding many item in main.xml and just overwriding onCreateOptionsMenu(Menu
menu) method is sufficient to show the manu on screen. Like in image 1.1

Image 1.1
but remeber you can see the menu but it is not going to work now.

-----------------------

Ok, get it I can create Menu but how it works????


You are really greedy .... Okkkk, Do as i did :)
1: in src subfolder create an activity class for each menu Item. This activity class must be extends
an Activity class. Remember Activity class belongs to android.app package. So do not forget to
import class by statement
import android.app.Activity;

For example in our image 1.1 for menu Item 'Add A New Item' we added an Activity class named
NewPlanet.java
2: Inside the NewPlanet.java class override a method named
onCreate(Bundle savedInstanceState)

code Shown below:public class ConfigPlanet extends Activity {


@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_config);
}

3: Inside

------------------------

Difference Between android:layout_gravity and


android:gravity Properties:android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"

android:layout_gravity related to parent on the other hand android:gravity related to content in


parent.

For exapmle:
<TextView
android:id="@+id/textView1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:text="@string/text_namokar_hindi" />

in above android:gravity property is used to send text at the centre horizontallyof TextView Object
but android:layout_gravity will move the entire TextView object at the centre horizontally on
screen.

------------------------

Android Toast Class (android.widget.Toast) :Android Toast objects are used to send messages onto your Activity screen that appear for a
predefined period of time sending a message to the user.

Declaring a Toast object is done most simply by using the .makeToast() method off of a
Toast object.
The method requires : the current context
the message to be Toasted
the duration in the form of a constant.

The Toast call is coded as follows:


Toast myToast =
Toast.makeText(NewPlanet.this, "Mars Created", Toast.LENGTH_SHORT);
myToast.show();
---------------------------When we use .setOnTouchListener(new View.OnTouchListener() ...
and when we use .setOnClickListener(new View.OnClickListner()).....
in javba code???
Logic is simple if a element in activity.xml is Button kind of , then use .setOnCloickListener(new
View.OnClickListner())..... event listener.
On the other hand if a element have property android:clickable or android:longClickable or both

are used then we have to use setOnClickListener(new View.OnClickListner())..... event Listener.


-----------------

How to take input from User :


<EditText
android:id="@+id/tsxtSeconds"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:inputType="number"/>

there is an intresting property :


android:inputType this property is used to restrict the user to give input of
any type. Like above user can only gives an input of number type.

Potrebbero piacerti anche