Android Open Source - SampleCustomView Main Activity






From Project

Back to project page SampleCustomView.

License

The source code is released under:

Apache License

If you think the Android project SampleCustomView listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package com.imran.samplecustomview;
//from   ww  w  . j  a v  a2 s. co m
import android.content.Intent;
import android.graphics.Color;
import android.graphics.Typeface;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.List;

public class MainActivity extends Activity {

    private static class Views{
        String name;
        String clazz;

        public Views(String name, String clazz){
            this.name = name;
            this.clazz = clazz;
        }
    }

    List<Views> mCustomViews;

    ListView mListView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        mCustomViews = new ArrayList<Views>();

        mCustomViews.add(new Views("Custom View 1", CustomViewActivity.class.getName()));

        mListView = (ListView) findViewById(R.id.chooser_listview);
        mListView.setAdapter(new ChooserAdapter());

        mListView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent;
                Views customView = mCustomViews.get((int)id);
                try {
                    intent = new Intent(getBaseContext(), Class.forName(customView.clazz));
                    startActivity(intent);
                } catch (ClassNotFoundException e) {
                    e.printStackTrace();
                }
            }
        });
    }


    @Override
    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;
    }


    private class ChooserAdapter extends BaseAdapter {

        @Override
        public int getCount() {
            return mCustomViews.size();
        }

        @Override
        public Object getItem(int position) {
            return mCustomViews.get(position);
        }

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

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

            TextView textView = new TextView(getBaseContext());
            textView.setTypeface(Typeface.createFromAsset(getAssets(), "fonts/Roboto-Thin.ttf"));
            textView.setTextSize(30);
            textView.setPadding(10, 10, 10, 10);
            textView.setTextColor(Color.BLACK);
            textView.setText(((Views)getItem(position)).name);
            return textView;
        }
    }
    
}




Java Source Code List

com.imran.samplecustomview.CustomViewActivity.java
com.imran.samplecustomview.CustomView.java
com.imran.samplecustomview.MainActivity.java