1 Replies - 1089 Views - Last Post: 16 February 2017 - 10:30 PM

#1 mgrex   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 272
  • Joined: 25-March 10

Dynamically creating text view at run time with Relative Layout

Posted 16 February 2017 - 07:20 PM

I get the error message:
Unexpected cast to RelativeLayout: layout tag was TextView more... (Ctrl+F1)
Posted Image

The statement containing 'this', gives off an error message:
Posted Image

The following is the function containing the code to generate the TextViews based on number of entries in the Queue.

    static public void printQueue(CircularFifoQueue cgLog) {

        //final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        //final RelativeLayout log = (RelativeLayout) findViewById(R.id.log);

        TextView[] tv = new TextView[requiredLines];

        RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.log);

        for (int i = 0; i < 20; i++) {

            TextView dynaText = new TextView(this);

            dynaText.setText((i+1) + ": " + cgLog.get(i));
            dynaText.setTextSize(30);

            // Set the location of your textView.
            dynaText.setPadding(0, (i * 30), 0, 0);

            containerLayout.addView(dynaText);
        }
        /*for (int i = 0; i < cgLog.size(); i++) {
            System.out.println((i + 1) + ": " + cgLog.get(i));
        }*/
    }



This is the OnCreate method calling the function
Spoiler


This is the acitvity_main.xml

Spoiler


This is the image of the program at runtime, I'd like the queue's content to be displayed below the 'log' TextView object.

Posted Image

What the program looks like in VirtualBox Emulator
Posted Image

This post has been edited by mgrex: 16 February 2017 - 09:12 PM


Is This A Good Question/Topic? 0
  • +

Replies To: Dynamically creating text view at run time with Relative Layout

#2 mgrex   User is offline

  • D.I.C Regular

Reputation: 3
  • View blog
  • Posts: 272
  • Joined: 25-March 10

Re: Dynamically creating text view at run time with Relative Layout

Posted 16 February 2017 - 10:30 PM

I decided to take to two steps back. I made a multiline TextView object.

I got the following to work.
I would still like to know how to perform the alternative solution in my first post, i.e. how to create TextView object in static functions, while utilizing findViewById function.

If one may link to an example, or code it, it would be appreciated. This will greatly help my debugging skills in android, as I can output data using setText, from any function, to better pinpoint logical/runtime errors.

I've made huge gains from here!

        pounds.setOnKeyListener(new View.OnKeyListener() {
            public boolean onKey(View v, int keyCode, KeyEvent event) {
                // If the event is a key-down event on the "enter" button
                if ((event.getAction() == KeyEvent.ACTION_DOWN) &&
                        (keyCode == KeyEvent.KEYCODE_ENTER)) {
                    // Perform action on key press
                    try {

                        getTextFileData(); // Will be secluded to a 'Button Click Event', later.

                        result.setText(" Estimated Gallons: " + getCalculatedGallons(pounds.getText().toString()));

                        pounds.getText().clear();

                        outputLog.setText(printQueue(cgLog));

                        //printQueue(cgLog);
                    } catch (IOException e) {
                        result.setText("No txt @ \\Documents\\AppData\\");
                    } catch (StringIndexOutOfBoundsException ex) {
                        result.setText("Must contain subtraction (-)");
                    }
                    return true;
                }
                return false;
            }
        });
    }

    static public String printQueue(CircularFifoQueue cgLog) {

        //final RelativeLayout rl = (RelativeLayout) findViewById(R.id.rl);
        //final RelativeLayout log = (RelativeLayout) findViewById(R.id.log);

        //LayoutInflater inflater = (LayoutInflater) R.layout.activity_main.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        //final TextView layout = inflater.inflate(R.layout.activity_main, (TextView) findViewById(R.id.log));


        //TextView layout = rootLayout.inflate(rootLayout.getContext(), R.layout.custom_level_coast, rootLayout.findViewById(R.id.toast_layout_root));

        //tv = new TextView[requiredLines];
        //containerLayout = (RelativeLayout) findViewById(R.id.log);

        //TextView[] tv = new TextView[requiredLines];

        //RelativeLayout containerLayout = (RelativeLayout) findViewById(R.id.log);
                                       // (ViewGroup)getLayoutInflater().inflate(R.layout.image_preview,vg);

        /*for (int i = 0; i < requiredLines; i++) {

            TextView dynaText = new TextView(this);

            dynaText.setText((i+1) + ": " + cgLog.get(i));
            dynaText.setTextSize(30);

            // Set the location of your textView.
            dynaText.setPadding(0, (i * 30), 0, 0);

            containerLayout.addView(dynaText);
        }*/

        /*TextView logDisplay = (TextView) findViewById(R.id.display_log);
        for (int i = 0; i < cgLog.size(); i++) {
            textview.setText(builder.toString());
            //System.out.println((i + 1) + ": " + cgLog.get(i));
        }*/

        String combination = "";
        for (int i = 0; i < cgLog.size(); i++) {
            if (i < cgLog.size()-1) {
                combination += (cgLog.get(i) + "\n");
            } else {
                combination += cgLog.get(i);
            }
        }
        return combination;
    }



Fullcode
Spoiler


As one may see, the program involves 3 external classes.

This post has been edited by mgrex: 16 February 2017 - 10:44 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1