0

What i am doing:: I have a adapter for which i am adding view in each row dynamically

What i am trying to do:: I am trying to detect onclick for imageView which was dynamically created


MainActivity.java

public class MainActivity extends Activity {

    TableLayout table_layout = null;
    private List<Map<String,CharSequence>> mListLayoutData=new ArrayList<Map<String,CharSequence>>();
    private Map<String, CharSequence> objMap = new HashMap<String, CharSequence>();

    private HFGridView gridView;

    private int screenWidth=0;
    private int screenHeight=0;

    Configuration newConfig;
    int numOfColumns=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (HFGridView) findViewById(R.id.gridView);
        table_layout = (TableLayout)findViewById(R.id.tableLayout);     
        gridView.setNumColumns(1);

        //Get the Width and the height of the screeen
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        Configuration config = getResources().getConfiguration();
        if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
        {

            //Portrait Mode
            numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
            gridView.setColumnWidth(metrics.widthPixels);

        }
        else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            //Landscape Mode
            numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
            gridView.setColumnWidth(metrics.widthPixels);

        }

        View header1 = getHeader("Header");
        View footer1 = getFooter("Footer");

        gridView.addHeaderView(header1);
        gridView.addFooterView(footer1);

        objMap.put("Starter","HEADER");
        mListLayoutData.add(objMap);
        if(numOfColumns==3){
            objMap = new HashMap<String, CharSequence>();
            objMap.put("1","starter");
            objMap.put("2","starter");
            objMap.put("3","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("4","starter");
            objMap.put("5","starter");
            mListLayoutData.add(objMap);
        }else{
            objMap = new HashMap<String, CharSequence>();
            objMap.put("1","starter");
            objMap.put("2","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("3","starter");
            objMap.put("4","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("5","starter");
            mListLayoutData.add(objMap);

        }

        gridView.setListener(new HFGridView.HFGridViewListener() {
            @Override
            public void readyToDisposeItems() {
                gridView.setAdapter(adapter);
            }
        });
    }



    private BaseAdapter adapter = new BaseAdapter() {


        @Override
        public int getCount() {
            //return 1;
            return mListLayoutData.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            TableLayout table_layout;
            ImageView imageView = null;
            TableRow table_row = null;
            LinearLayout linearLayout=null;



            if (view == null) {
                //Create the tableLayout and add params to it
                table_layout=new TableLayout(MainActivity.this);
                table_layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                //table_layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            } else {
                table_layout=(TableLayout) view;
            }

            final Map<String, CharSequence> objMap= mListLayoutData.get(position);

            //Create the tableRow and add params to it
            table_row = new TableRow(MainActivity.this);
            table_row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            //linearLayout.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            // inner for loop
            for (Entry<String, CharSequence> entry : objMap.entrySet()) {
                String key = entry.getKey();
                CharSequence value = entry.getValue();


                if(value=="HEADER"){
                    TextView textView = new TextView(MainActivity.this);
                    textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
                    textView.setText(key);
                    textView.setTextSize(20);
                    textView.setTextColor(getResources().getColor(R.color.redColor));
                    textView.setPadding(5, 5, 5, 5);
                    table_row.addView(textView);
                }else{
                    //Create the Linear Layout and add params to it
                    linearLayout=new LinearLayout(MainActivity.this);
                    linearLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
                    linearLayout.setOrientation(LinearLayout.VERTICAL);

                    //Create the ImageView and add params to it
                    imageView = new ImageView(MainActivity.this);
                    imageView.setLayoutParams(new LinearLayout.LayoutParams(160, 100));
                    //imageView.setBackgroundDrawable(imageView.getResources().getDrawable(R.drawable.eight));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setImageResource(R.drawable.eleven);

                    //Create the TextView and add params to it
                    TextView textView = new TextView(MainActivity.this);
                    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                    textView.setText(key);
                    textView.setTextSize(12);
                    textView.setTextColor(getResources().getColor(R.color.redColor));

                    linearLayout.addView(imageView);
                    linearLayout.addView(textView);
                    table_row.addView(linearLayout);

                }   
            }

            table_layout.addView(table_row);
            return table_layout;
        }
    };

    private View getHeader(String text) {
        View header = getLayoutInflater().inflate(R.layout.header, null);
        TextView textView = (TextView) header.findViewById(R.id.headerTextView);
        textView.setText(text);

        return header;
    }

    private View getFooter(String text) {
        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        TextView textView = (TextView) footer.findViewById(R.id.footerTextView);
        textView.setText(text);
        return footer;
    }
}
Devrath
  • 42,072
  • 54
  • 195
  • 297
  • setClickListener on imageView in getView and handle that. – Shayan Pourvatan Jul 30 '14 at 06:30
  • You can use, setTag to that dynamic created imageview and in onclick listener you can get tag and you will get which imageview is clicked – Rohit Jul 30 '14 at 06:32
  • @shayan pourvatan .... That is not working for Dynamically generated views ..... OnClicklistener throws error on loading the class when i do in getview method! – Devrath Jul 30 '14 at 06:38
  • @Rohit ... Can you show a snippet as your answer ! – Devrath Jul 30 '14 at 06:38
  • 1
    http://stackoverflow.com/questions/5291726/what-is-the-main-purpose-of-settag-gettag-methods-of-view – Rohit Jul 30 '14 at 06:40
  • @Devrath Are You Sure? because i don't think so at all, I do that many many time, set click listener use set tag with position and getTag in onClick method then you get position of list item then do what ever you want – Shayan Pourvatan Jul 30 '14 at 06:41
  • 1
    Also look http://stackoverflow.com/questions/10890100/recognizing-a-button-from-a-dynamic-view – Rohit Jul 30 '14 at 06:43
  • @shayan pourvatan ... Oops ! .... Sorry my mistake .... What i needed to do is set the onClickListener berore adding the views ! ...Solved ! – Devrath Jul 30 '14 at 06:44

1 Answers1

0

Found Solution to my answer !

This Link and comment helped me in finding the solution, sharing it so it might be helpful to someone !


public class MainActivity extends Activity {

    TableLayout table_layout = null;
    private List<Map<String,CharSequence>> mListLayoutData=new ArrayList<Map<String,CharSequence>>();
    private Map<String, CharSequence> objMap = new HashMap<String, CharSequence>();

    private HFGridView gridView;

    private int screenWidth=0;
    private int screenHeight=0;

    Configuration newConfig;
    int numOfColumns=0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);

        gridView = (HFGridView) findViewById(R.id.gridView);
        table_layout = (TableLayout)findViewById(R.id.tableLayout);     
        gridView.setNumColumns(1);

        //Get the Width and the height of the screeen
        DisplayMetrics metrics = new DisplayMetrics();
        getWindowManager().getDefaultDisplay().getMetrics(metrics);

        Configuration config = getResources().getConfiguration();
        if (config.orientation == Configuration.ORIENTATION_PORTRAIT)
        {

            //Portrait Mode
            numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
            gridView.setColumnWidth(metrics.widthPixels);

        }
        else if(config.orientation == Configuration.ORIENTATION_LANDSCAPE)
        {
            //Landscape Mode
            numOfColumns=(int) Math.ceil(metrics.widthPixels/160);
            gridView.setColumnWidth(metrics.widthPixels);

        }

        View header1 = getHeader("Header");
        View footer1 = getFooter("Footer");

        gridView.addHeaderView(header1);
        gridView.addFooterView(footer1);

        objMap.put("Starter","HEADER");
        mListLayoutData.add(objMap);
        if(numOfColumns==3){
            objMap = new HashMap<String, CharSequence>();
            objMap.put("1","starter");
            objMap.put("2","starter");
            objMap.put("3","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("4","starter");
            objMap.put("5","starter");
            mListLayoutData.add(objMap);
        }else{
            objMap = new HashMap<String, CharSequence>();
            objMap.put("1","starter");
            objMap.put("2","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("3","starter");
            objMap.put("4","starter");
            mListLayoutData.add(objMap);
            objMap = new HashMap<String, CharSequence>();
            objMap.put("5","starter");
            mListLayoutData.add(objMap);

        }

        gridView.setListener(new HFGridView.HFGridViewListener() {
            @Override
            public void readyToDisposeItems() {
                gridView.setAdapter(adapter);
            }
        });
    }



    private BaseAdapter adapter = new BaseAdapter() {


        @Override
        public int getCount() {
            //return 1;
            return mListLayoutData.size();
        }

        @Override
        public Object getItem(int i) {
            return null;
        }

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

        @Override
        public View getView(int position, View view, ViewGroup viewGroup) {
            TableLayout table_layout;
            ImageView imageView = null;
            TableRow table_row = null;
            LinearLayout linearLayout=null;



            if (view == null) {
                //Create the tableLayout and add params to it
                table_layout=new TableLayout(MainActivity.this);
                table_layout.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
                //table_layout.setDescendantFocusability(ViewGroup.FOCUS_BLOCK_DESCENDANTS);
            } else {
                table_layout=(TableLayout) view;
            }

            final Map<String, CharSequence> objMap= mListLayoutData.get(position);

            //Create the tableRow and add params to it
            table_row = new TableRow(MainActivity.this);
            table_row.setLayoutParams(new TableLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            //linearLayout.setLayoutParams(new LinearLayout.LayoutParams(TableLayout.LayoutParams.FILL_PARENT,TableLayout.LayoutParams.WRAP_CONTENT));

            // inner for loop
            for (Entry<String, CharSequence> entry : objMap.entrySet()) {
                String key = entry.getKey();
                CharSequence value = entry.getValue();


                if(value=="HEADER"){
                    TextView textView = new TextView(MainActivity.this);
                    textView.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.FILL_PARENT,TableRow.LayoutParams.WRAP_CONTENT));
                    textView.setText(key);
                    textView.setTextSize(20);
                    textView.setTextColor(getResources().getColor(R.color.redColor));
                    textView.setPadding(5, 5, 5, 5);
                    table_row.addView(textView);
                }else{
                    //Create the Linear Layout and add params to it
                    linearLayout=new LinearLayout(MainActivity.this);
                    linearLayout.setLayoutParams(new TableRow.LayoutParams(TableRow.LayoutParams.WRAP_CONTENT,TableRow.LayoutParams.WRAP_CONTENT));
                    linearLayout.setOrientation(LinearLayout.VERTICAL);

                    //Create the ImageView and add params to it
                    imageView = new ImageView(MainActivity.this);
                    imageView.setLayoutParams(new LinearLayout.LayoutParams(160, 100));
                    //imageView.setBackgroundDrawable(imageView.getResources().getDrawable(R.drawable.eight));
                    imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
                    imageView.setImageResource(R.drawable.eleven);

                    //Create the TextView and add params to it
                    TextView textView = new TextView(MainActivity.this);
                    textView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT));
                    textView.setText(key);
                    textView.setTextSize(12);
                    textView.setTextColor(getResources().getColor(R.color.redColor));


                    imageView.setOnClickListener(new OnClickListener() {

                        @Override
                        public void onClick(View v) {
                            // TODO Auto-generated method stub
                            Toast.makeText(MainActivity.this, "Clicked !", Toast.LENGTH_LONG).show();
                        }
                    });

                    linearLayout.addView(imageView);
                    linearLayout.addView(textView);
                    table_row.addView(linearLayout);

                }   
            }





            table_layout.addView(table_row);
            return table_layout;
        }
    };

    private View getHeader(String text) {
        View header = getLayoutInflater().inflate(R.layout.header, null);
        TextView textView = (TextView) header.findViewById(R.id.headerTextView);
        textView.setText(text);

        return header;
    }

    private View getFooter(String text) {
        View footer = getLayoutInflater().inflate(R.layout.footer, null);
        TextView textView = (TextView) footer.findViewById(R.id.footerTextView);
        textView.setText(text);
        return footer;
    }
}
Community
  • 1
  • 1
Devrath
  • 42,072
  • 54
  • 195
  • 297