Friday, August 4, 2017

Android application using list view.


/*******************************************************************************************
Program Name: Write an android application using list view.
Author:sujith
Date: 3/10/2016
*******************************************************************************************/


AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.sujithbaby.mylistprgm">
<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">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".List_items" />
<activity android:name=".ListDetails"></activity>
</application>

</manifest>






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:id="@+id/activity_main"
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="com.example.sujithbaby.mylistprgm.MainActivity">

<ListView
android:id="@+id/list_view_id"
android:layout_width="match_parent"
android:layout_height="wrap_content">

</ListView>
</RelativeLayout>


MainActivity.java
package com.example.sujithbaby.mylistprgm;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListAdapter;
import android.widget.ListView;

public class MainActivity extends AppCompatActivity {

ListView listView;
ListAdapter listAdapter;

String[] film_name={"godfather","shawshank redemtion","godfather 2","Forest gump","12 angry men"};
int[] images={R.drawable.m1,R.drawable.m2,R.drawable.m3,R.drawable.m4,R.drawable.m5};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView=(ListView)findViewById(R.id.list_view_id);
listAdapter= new List_items(MainActivity.this,film_name,images);
listView.setAdapter(listAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Intent intent= new Intent(MainActivity.this,ListDetails.class);
intent.putExtra("value",images[position]);
intent.putExtra("value1",film_name[position]);
startActivity(intent);
}
});
}
}


Activity_list_items.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:id="@+id/activity_list_items"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="5dp"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
android:background="#0E6DA7"
tools:context="com.example.sujithbaby.mylistprgm.List_items">
<ImageView
android:id="@+id/id_img"
android:src="@drawable/m1"
android:layout_width="100dp"
android:layout_height="100dp" />
<TextView
android:id="@+id/txt1"
android:textColor="#ffffff"
android:layout_toRightOf="@+id/id_img"
android:text="dummy text"
android:layout_width="match_parent"
android:layout_height="wrap_content" />

</RelativeLayout>



ListItems.java
package com.example.sujithbaby.mylistprgm;
import android.app.Activity;
import android.content.Context;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class List_items extends BaseAdapter {

Context context;
TextView film_name;
ImageView imageView;
int[] image_res;
String[] name_film;

public List_items(Context context,String[] name_film,int[] image_res) {
this.context = context;
this.name_film=name_film;
this.image_res=image_res;
}

@Override
public int getCount() {
return name_film.length;
}

@Override
public Object getItem(int position) {
return position;
}
@Override
public long getItemId(int position) {
return position;
}

@Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater li=(LayoutInflater)context.getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
convertView=li.inflate(R.layout.activity_list_items,null);
film_name=(TextView)convertView.findViewById(R.id.txt1);
imageView=(ImageView)convertView.findViewById(R.id.id_img);
film_name.setText(name_film[position]);
imageView.setImageResource(image_res[position]);
return convertView;
}


Activity_list_Details.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:id="@+id/activity_list_details"
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="com.example.sujithbaby.mylistprgm.ListDetails">
<ImageView
android:src="@drawable/m1"
android:id="@+id/id_details"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="@+id/txtDetails"
android:textSize="20dp"
android:textStyle="bold"
android:layout_below="@+id/id_details"
android:layout_width="match_parent"
android:layout_marginTop="15dp"
android:gravity="center"
android:layout_height="wrap_content" />
</RelativeLayout>




ListDetails.java
package com.example.sujithbaby.mylistprgm;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import android.widget.TextView;

public class ListDetails extends AppCompatActivity {
ImageView imageView;
TextView listDet;
int imag_link;
String img_name;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list_details);
imageView=(ImageView)findViewById(R.id.id_details);
listDet=(TextView)findViewById(R.id.txtDetails);
imag_link=getIntent().getIntExtra("value",0);
img_name=getIntent().getStringExtra("value1");
imageView.setImageResource(imag_link);
listDet.setText(img_name);
}
}

OUTPUT






























No comments:

Post a Comment