Android layout is widely used in the android application development.Android Preferences permits to save data for our application and stores as key values. The preference definition can also be done through an XML resource. The “PreferenceActivity” class extends the class Activity and it permits the simple handling of preferences. The above activity helps to load a preferences definition resource through the method addPreferencesFromResource(). Normally the PreferenceActivity is started from another activity through Intent.
Eg: – SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(this); Values access through the key of the preference setting.
Eg: –
String username = preferences.getString("username", "n/a");
We can create or change preferences by calling the edit() method. We call commit() after all the changes.
Eg: –
Editor edit = preferences.edit(); edit.putString("username", "new_value_for_user"); edit.commit();
We can create Android XML resource “preferences.xml” of type “PreferenceScreen”.
Open the file through right-mouse click and Open-with → Android XML Resource Editor. Press Add, add a “PreferenceCategory” and add two preferences “EditTextPreferences” to this category: “User” and “Password”.
We can also enter values for other properties of EditTextField, e.g. the inputMethod. Add the following attribute to the XML definition of your password field to make the input quoted with *.
Eg: –
android:inputType="textPassword"
Create the class MyPreferencesActivity which extends PreferenceActivity. This Activity will load the “preference.xml” file and will allow the user to change the values.
Eg: –
package com.android.socialapp; import android.os.Bundle; import android.preference.PreferenceActivity; public class MyPreferencesActivity extends PreferenceActivity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.preferences); } }
For the use of new preference activity and the preference values we adjust the “OverviewActivity”. The first button will display the current values of the preferences through a Toast and the second button will revert the maintained user name to demonstrate how you could change the preferences through code.
Eg: –
package com.android.socialapp; import android.app.Activity; import android.content.SharedPreferences; import android.content.SharedPreferences.Editor; import android.os.Bundle; import android.preference.PreferenceManager; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; import android.widget.Toast; public class OverviewActivity extends Activity { SharedPreferences preferences; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); Button button = (Button) findViewById(R.id.Button01); // Initialize preferences preferences = PreferenceManager.getDefaultSharedPreferences(this); button.setOnClickListener(new OnClickListener() { public void onClick(View v) { String username = preferences.getString("username", "n/a"); String password = preferences.getString("password", "n/a"); showPrefs(username, password); } }); Button buttonChangePreferences = (Button) findViewById(R.id.Button02); buttonChangePreferences.setOnClickListener(new OnClickListener() { public void onClick(View v) { updatePreferenceValue(); } }); } private void showPrefs(String username, String password) { Toast.makeText(OverviewActivity.this,"Input: " + username + " and password: "+ password, Toast.LENGTH_LONG).show(); } private void updatePreferenceValue() { Editor edit = preferences.edit(); String username = preferences.getString("username", "n/a"); // we will just revert the current user name and save again StringBuffer buffer = new StringBuffer(); for (int i = username.length() - 1; i >= 0; i--) { buffer.append(username.charAt(i)); } edit.putString("username", buffer.toString()); edit.commit(); // A toast is a view containing a quick little message for the // user. We give a little feedback Toast.makeText(OverviewActivity.this, "Reverted string sequence of user name.", Toast.LENGTH_LONG).show(); }
To open the new preference Activity we will use the onOptionsItemSelected() method. To show the current values of the preferences we define a button and use the class PreferenceManager to get the sharedPreferences.
Eg: –
@Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); inflater.inflate(R.menu.mainmenu, menu); return true; } // this method is called once the menu is selected. @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { // we have only one menu option case R.id.preferences: // Launch Preference activity Intent i = new Intent(OverviewActivity.this, MyPreferencesActivity.class); startActivity(i); // some feedback to the user Toast.makeText(OverviewActivity.this, "Enter your user credentials.", Toast.LENGTH_LONG).show(); break; } return true;}
Run our application the saved values should be displayed in small message windows (Toast) if you press your first button. If you press the second button the username should be reversed.
Layout Manager
Layout manger is a subclass of ViewGroup. Android allows different layout managers. They are
- LinearLayout
- FrameLayout
- RelativeLayout
- GridLayout
Layouts permits to define attributes. AbsoluteLayoutLayout is deprecated and TableLayout can be implemented more effectively via GridLayout.
LinearLayout
The LinearLayout place all its child elements into a single column or row depends on the attribute as android:rrientation. Horizontal and vertical values are the attribute values and horizontal is the default value.
Horizontal Linear Layout
FrameLayout or AbsoluteLayout
The Frame Layout is sort of a null layout specification and it allocate space on the screen for a single View to be drawn, and the View is always located at the upper left of the space. There is no way to place a different location for the View, and there can be only one View in the Layout. If more than one View is specify in the layout file, they are just drawn on top of each other, all pinned to the upper-left corner.
RelativeLayout
It permits to position the widget relative to each other and also permits for complex layouts.
Eg: – If we want to center a simple component, we just add one component to the RelativeLayout and set the android:layout_centerInParent attribute to true.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <ProgressBar android:id="@+id/progressBar1" style="?android:attr/progressBarStyleLarge" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> </RelativeLayout>
GridLayout
This layout permits to organize a view into a Grid and it separate its drawing are into rows, columns and cells. The default gridLayout is one column, one row and the position of a View depends on the order of the declaration of the Views.
ScrollView
It consists of one view that might be too big to fit on one screen and it displays the scroll bar to scroll the context.
Eg: –
Create an android project “com.android.scrollview” with the activity “ScrollView”. Create the following layout and class.
<?xml version="1.0" encoding="utf-8"?> <ScrollView xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:fillViewport="true" android:orientation="vertical" > <LinearLayout android:id="@+id/LinearLayout01" android:layout_width="fill_parent" android:layout_height="wrap_content" android:orientation="vertical" > <TextView android:id="@+id/TextView01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:paddingLeft="8dip" android:paddingRight="8dip" android:paddingTop="8dip" android:text="This is a header" android:textAppearance="?android:attr/textAppearanceLarge" > </TextView> <TextView android:id="@+id/TextView02" android:layout_width="wrap_content" android:layout_height="fill_parent" android:layout_weight="1.0" android:text="@+id/TextView02" > </TextView> <LinearLayout android:id="@+id/LinearLayout02" android:layout_width="wrap_content" android:layout_height="wrap_content" > <Button android:id="@+id/Button01" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="Submit" > </Button> <Button android:id="@+id/Button02" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_weight="1.0" android:text="Cancel" > </Button> </LinearLayout> </LinearLayout> </ScrollView>
package com.android.scrollview; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.widget.TextView; public class ScrollView extends Activity { /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); TextView view = (TextView) findViewById(R.id.TextView02); String s=""; for (int i=0; i < 100; i++) { s += "com "; } view.setText(s); } }
The attribute “android:fillViewport=”true”” ensures that the scrollview is set to the full screen even if the elements are smaller.