Monday, August 10, 2015

Basic ListActivity With Layout


.

Basic ListActivity With Layout
Using Android Studio 1.4

1) Create New Project

Application Name: MyListActivityWithLayout1
Select API 14:Android 4.0 (IceCreamSandwich)
Follow the wizard to create New Empty Activity.
Select “Generate Layout File”

2) Edit res/layout/activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:tools="http://schemas.android.com/tools"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:paddingBottom="@dimen/activity_vertical_margin"
   android:paddingLeft="@dimen/activity_horizontal_margin"
   android:paddingRight="@dimen/activity_horizontal_margin"
   android:paddingTop="@dimen/activity_vertical_margin"
   tools:context=".MainActivity" >
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:id="@+id/mainText"
       android:text="My list" />
   <ListView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/mainText"
       android:id="@android:id/list"
       android:background="#aaaaaa" />
   <TextView
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_below="@+id/mainText"
       android:id="@android:id/empty"
       android:text="There is no data"
       android:textStyle="bold" />
</RelativeLayout>
OUTCOME.

3) Add Row Layout

3.1)res/layout/row_layout.xml

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
   android:layout_width="match_parent"
   android:layout_height="match_parent"
   android:orientation="vertical" >
   <TextView
       android:id="@+id/listText"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:padding="10dp"
       android:textSize="18sp"
       android:textStyle="bold"
       android:textColor="#ff00ff" />
</LinearLayout>
OUTCOME.

4) Add Action Bar

4.1) AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
   package="com.notarazi.mylistactivitywithlayout1" >
   <application
       android:allowBackup="true"
       android:icon="@mipmap/ic_launcher"
       android:label="@string/app_name"
       android:supportsRtl="true"
       android:theme="@style/AppTheme" >
       <activity android:name=".MainActivity"
           android:theme="@android:style/Theme.Holo.Light.DarkActionBar"
           android:label="ListView_Activity_Label">
           <intent-filter>
               <action android:name="android.intent.action.MAIN" />
               <category android:name="android.intent.category.LAUNCHER" />
           </intent-filter>
       </activity>
   </application>
</manifest>
OUTCOME.

5) Add Action Menu

5.1) Add Menu Resource

res/menu/menu_main.xml
<menu xmlns:android="http://schemas.android.com/apk/res/android"
   xmlns:app="http://schemas.android.com/apk/res-auto"
   xmlns:tools="http://schemas.android.com/tools" tools:context=".MainActivity">
   <item android:id="@+id/action_settings" android:title="settings"
       android:orderInCategory="100" app:showAsAction="never" />
</menu>

5.1) MainActivity.Java

package com.notarazi.mylistactivitywithlayout1;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       setListWithLayout();
   }
   private void setListWithLayout() {
       ArrayList listValues = new ArrayList<String>();
       listValues.add("Android");
       listValues.add("iOS");
       listValues.add("Symbian");
       listValues.add("Blackberry");
       listValues.add("Windows Phone");
       // initiate the listadapter
       ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.listText, listValues);
       // assign the list adapter
       setListAdapter(myAdapter);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
}
OUTCOME.

6) Add List Click Response

6.1) MainActivity.Java
package com.notarazi.mylistactivitywithlayout1;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import java.util.ArrayList;
public class MainActivity extends ListActivity {
   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);
       setContentView(R.layout.activity_main);
       setListWithLayout();
   }
   private void setListWithLayout() {
       ArrayList listValues = new ArrayList<String>();
       listValues.add("Android");
       listValues.add("iOS");
       listValues.add("Symbian");
       listValues.add("Blackberry");
       listValues.add("Windows Phone");
       // initiate the listadapter
       ArrayAdapter<String> myAdapter = new ArrayAdapter<String>(this, R.layout.row_layout, R.id.listText, listValues);
       // assign the list adapter
       setListAdapter(myAdapter);
   }
   @Override
   public boolean onCreateOptionsMenu(Menu menu) {
       // Inflate the menu; this adds items to the action bar if it is present.
       getMenuInflater().inflate(R.menu.menu_main, menu);
       return true;
   }
   @Override
   public boolean onOptionsItemSelected(MenuItem item) {
       // Handle action bar item clicks here. The action bar will
       // automatically handle clicks on the Home/Up button, so long
       // as you specify a parent activity in AndroidManifest.xml.
       int id = item.getItemId();
       //noinspection SimplifiableIfStatement
       if (id == R.id.action_settings) {
           return true;
       }
       return super.onOptionsItemSelected(item);
   }
   @Override
   protected void onListItemClick(ListView list, View view, int position, long id) {
       super.onListItemClick(list, view, position, id);
       String selectedItem = (String) getListView().getItemAtPosition(position);
       //String selectedItem = (String) getListAdapter().getItem(position);
       TextView text = (TextView) findViewById(R.id.mainText);
       text.setText("You clicked " + selectedItem + " at position " + position);
   }
}
OUTCOME.

DOWNLOAD

REFERENCES


.

No comments:

Post a Comment