My problem is that I have a home screen, and I would like to dynamically create a view under it with the click of a button, and then show the main view from the screen, displaying the view below it. I did this, but I feel that there must be a better way. The way I did this is very limited in that you can't just pop up again and again basically.
My main XML file is as follows:
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:id="@+id/subpage" android:layout_width="fill_parent" android:layout_height="fill_parent" > </RelativeLayout> <RelativeLayout android:id="@+id/homescreen" android:layout_width="fill_parent" android:layout_height="fill_parent" > <RelativeLayout android:layout_width="fill_parent" android:layout_height="fill_parent" android:background="@drawable/homebg" > </RelativeLayout> </RelativeLayout> </RelativeLayout>
I deleted unnecessary things. It is important. Note that the first child of the main layout is a relative layout with an id page. Since I use java to inflate another layout in the subpage layout, when the button is clicked, I animate the desktop layout from the screen. It seems that I didn’t have to declare a subpage in advance. To my question, is there a way to dynamically declare a new child layout under an existing layout?
==================================================== =======================
Edit: part 2 of the question
I am trying to use addView, and the application crashes. This is the code I use to try to add a view and inflate my xml. In the code below, subview is a ViewGroup, because as I understand it, you can only inflate in ViewGroups, not in regular views. Also, "activity" is defined at the top of the class as "private Activity activity = this". Any ideas that could cause a crash?
btnHelp.setOnClickListener(new OnClickListener() { public void onClick(View v) { subView = (ViewGroup)new View(getApplicationContext()); mainScreen.addView(subView,1); LayoutInflater inflater = activity.getLayoutInflater(); inflater.inflate(R.layout.help, subView); } });
==================================================== =======================
Edit: part 3 of the question
So one more problem. Everything works great, like bloating and crawling. However, the bloated view has a button in it. I am trying to assign a listener to this button, but it does not work. I do this by adding a listener to the button after the layout of the blower is called up in the btnHelp that I was working on. Here is the code:
btnHelp.setOnClickListener(new OnClickListener() { public void onClick(View v) { LayoutInflater inflater = activity.getLayoutInflater(); mainScreen.addView(inflater.inflate(R.layout.help, null),0); homeScrn.startAnimation(slideLeftOut); btnBackHome = (ImageView)findViewById(R.id.backMenuBtn); btnBackHome.setOnClickListener(goHome); } });
goHome is the handler that I defined below:
private OnClickListener goHome = new OnClickListener(){ public void onClick(View v) { Log.d("ClickEvent: ","btnBackHome Clicked"); homeScrn.startAnimation(slideRightIn); } };
When I click on the button referenced by btnBackHome, it does nothing. I'm just not sure if this is because the listener is not actually assigned, something keeps the button from clicking on it or something else.