Pages

Tuesday, September 20, 2016

Android Example-ListActivity with Array Adapter


activity_mani.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content" >

    <ImageView
        android:id="@+id/icon"
        android:layout_width="100px"
        android:layout_height="100px"
        android:layout_marginLeft="4px"
        android:layout_marginRight="10px"
        android:layout_marginTop="4px"
        android:src="@mipmap/ic_launcher" >
    </ImageView>

    <TextView
        android:id="@+id/label"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="@+id/label"
        android:textSize="80px" >
    </TextView>

</LinearLayout>

MainActivity.java
package in.kantapp.example29;

import android.app.ListActivity;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;

public class MainActivity extends ListActivity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        String str[] = new String[] { "C Language", "C# Language", "Android Programming", "C++ Language" };
        MyAdapter adapter = new MyAdapter(this, str);
        setListAdapter(adapter);
    }


}

MyAdapter.java
package in.kantapp.example29;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class MyAdapter extends ArrayAdapter<String> {

    private final Context context;
    private final String[] var;

    public MyAdapter(Context context, String[] var) {
        super(context, R.layout.activity_main, var);
        // TODO Auto-generated constructor stub
        this.context = context;
        this.var = var;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent)
    {
        // TODO Auto-generated method stub
        LayoutInflater li = (LayoutInflater) context
                .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        View row = li.inflate(R.layout.activity_main, parent, false);
        TextView text = (TextView) row.findViewById(R.id.label);
        ImageView img = (ImageView) row.findViewById(R.id.icon);
        text.setText(var[position]);
        String s = var[position];
        if (s.startsWith("C Language")) {

            img.setImageResource(R.drawable.clogo);
        }
        else if(s.startsWith("C# Language")){
            img.setImageResource(R.drawable.csharp);
        }
        else if(s.startsWith("Android Programming")){
            img.setImageResource(R.drawable.android);
        }
        else{
            //c++
            img.setImageResource(R.drawable.cplus);
        }
        return row;
    }

}

AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
    package="in.kantapp.example29">

    <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>
    </application>

</manifest>

No comments:

Post a Comment