Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding a view dynamically not showing view

I am using following code to inflate a view in my layout under its child LinearLayout:

LayoutInflater vi = (LayoutInflater)getApplicationContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = vi.inflate(R.layout.your_layout, null);
// fill in any details dynamically here
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
 textView.setText("your text");
// insert into main view
View insertPoint = findViewById(R.id.insert_point);
((ViewGroup) insertPoint).addView(v, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT));

But I can't see my view there. It shows up when I rotate my screen to landscape view. How to fix this?

EDIT:

Here is basically what I am doing. I have a TabActivity with two tabs. I use an application global which store a flag and TabHost of my TabActivity. I am also using a thread in the second tab activity which is monitoring the application global continuously to monitor flag. When I click a button in my first tab, I set the global to true. The thread in the second tab check its value. When it gets true, I run the async task and shift the current showing window to the second tab using my TabHost stired in ApplicationGlobal.

Code of second tab:

public class ShowDownloadsActivity extends Activity {
    private List<String> list;
    ApplicationGlobal ap;
    String tempUrl = "";
    LinearLayout lly;
    LayoutInflater inflater;
    Context con;
    static int k = 0;
    static int tmp = 0;
    static int size = 0;
    View insertPoint;
    View v;

    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.shwds);
        con = this;
        v = LayoutInflater.from(this).inflate(R.layout.downloadlistrow, null);
        insertPoint = (View) findViewById(R.id.layout_vids);
        inflater = (LayoutInflater) con.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        ap = (ApplicationGlobal) getApplication();
        lly = (LinearLayout) findViewById(R.id.layout_vids);

        Thread thread1 = new Thread() {
            public void run() {
                try {
                    boolean flg = ap.getFlag();
                    if (flg) {
                        tempUrl = ap.getUrl();
                        ap.setUrl("");
                        flg = false;
                        new downloadVideo().execute();
                    }
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        };
        thread1.run();
    }

    class downloadVideo extends AsyncTask<Hashtable<String, String>, String, String> {
        String resp = "";

        protected void onProgressUpdate(String... values) {

        }

        @Override
        protected void onPreExecute() {
            // fill in any details dynamically here 
            TextView textView = (TextView) v.findViewById(R.id.siz);
            textView.setText("your text");
            // insert into main view 
            addContentView(v, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
            super.onPreExecute();
        }

        @Override
        protected String doInBackground(Hashtable<String, String>... prmss) {
            try {
                final int return resp;
            }

            @Override
            protected void onPostExecute (String result){
                super.onPostExecute(result);
            }
        }
    }
}
like image 815
Pradeep Mittal Avatar asked Oct 26 '13 05:10

Pradeep Mittal


People also ask

Can we add or delete any view type in XML on runtime?

Yes, because you can not add/delete any View Type in XML on runtime. Dynamic layouts are developed using Java and can be used to create layouts that you would normally create using an XML file.


2 Answers

First Initialize your Layout to which you want to add subview.

For example

LinearLayout layout=(LinearLayout)findViewById(R.id.your_root_layout);

Then inflate your layout like below

LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
    View v= (View) mInflater.inflate(R.layout.your_layout, null);
TextView textView = (TextView) v.findViewById(R.id.a_text_view);
textView.setText("your text");
LinearLayout.LayoutParams p = new LinearLayout.LayoutParams(
                LinearLayout.LayoutParams.WRAP_CONTENT,
                LinearLayout.LayoutParams.WRAP_CONTENT);
layout.addView(textView, p);

Hope this will help you.

like image 93
Amit Gupta Avatar answered Oct 17 '22 18:10

Amit Gupta


Sometimes the issue is that you were expecting the LinearLayout to be vertical orientation, but if you don't set it to vertical, then the LinearLayout defaults to horizontal orientation, which if there is already an item with match parent width, then you won't see your additional items.

like image 25
jc12 Avatar answered Oct 17 '22 19:10

jc12