Android Multiple View switching, without Activity's -



Android Multiple View switching, without Activity's -

i making game, , making levelselect class 3 different views, (jail, yard , sand), used have multiple intent's, if way, there no way shut downwards application, , doesn't homecoming quite well.

i tried using setcontentview() method, turn's out isn't able load class competent view, tried combining class, class can't initialize objects outside of current view in process. question is: how can utilize multiple views without launching new activity?

package com.muttgames.shadowpuzzle; import android.app.activity; import android.content.context; import android.os.bundle; import android.view.layoutinflater; import android.view.view; import android.view.view.onclicklistener; import android.widget.linearlayout; public class levelselect extends activity implements onclicklistener{ protected void oncreate(bundle savedinstancestate){ super.oncreate(savedinstancestate); setcontentview(r.layout.jail_level_view); //defining inflater layoutinflater inflater = (layoutinflater) getsystemservice(context.layout_inflater_service); //defining sandview layout relativelayout jailview = (relativelayout) findviewbyid(r.id.jail_level); view innerviewsjailview = inflater.inflate(r.id.yard_level, jailview, true); view jailmenu = innerviewsjailview.findviewbyid(r.id.jail2menu); jailmenu.setonclicklistener(this); //defining sandview layout relativelayout yardview = (relativelayout) findviewbyid(r.id.yard_level); view innerviewsyardview = inflater.inflate(r.id.yard_level, yardview, true); view yardmenu = innerviewsyardview.findviewbyid(r.id.yard2menu); yardmenu.setonclicklistener(this); //defining sandview layout relativelayout sandview = (relativelayout) findviewbyid(r.id.sand_level); view innerviewssandview = inflater.inflate(r.id.sand_level, sandview, true); view sandmenu = innerviewssandview.findviewbyid(r.id.sand2menu); sandmenu.setonclicklistener(this); } @override public void onclick(view v) { switch(v.getid()){ case r.id.jail2menu: finish(); break; case r.id.yard2menu: finish(); break; case r.id.sand2menu: finish(); break; } } }

i realize little class, little tryout's prevent myself having write immense parts of code 1 time again , again.

the error in part initialize yardmenu , sandmenu buttons, while not in r.layout.jail_level_view xml file.

this code stop levelselect activity whenever wanted homecoming main_activity.

how alter that, instead of shutting downwards activity, can alter activity_main xml file.

xml files(they pretty much same, minor adjustments in color etc...:

<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/jail_level" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/app_bg" android:orientation="vertical" > <textview android:id="@+id/view_title" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_centerhorizontal="true" android:layout_margintop="30dp" android:text="jail levels" android:textsize="30sp" /> <button android:id="@+id/level1" style="@drawable/jail_button" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/view_title" android:layout_marginleft="45dp" android:layout_margintop="30dp" android:background="@drawable/jail_button" android:text="1" /> <button android:id="@+id/level2" style="@drawable/jail_button" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/view_title" android:layout_marginleft="40dp" android:layout_margintop="30dp" android:layout_torightof="@id/level1" android:background="@drawable/jail_button" android:text="2" /> <button android:id="@+id/level3" style="@drawable/jail_button" android:layout_width="50dp" android:layout_height="50dp" android:layout_below="@id/view_title" android:layout_marginleft="40dp" android:layout_margintop="30dp" android:layout_torightof="@id/level2" android:background="@drawable/jail_button" android:text="3" /> <button style="@drawable/yard_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="30dp" android:layout_marginleft="225dp" android:background="@drawable/yard_button" android:text="next" /> <button android:id="@+id/jail2menu" style="@drawable/menu_button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentbottom="true" android:layout_marginbottom="30dp" android:layout_marginleft="122dp" android:background="@drawable/menu_button" android:text="menu" /> </relativelayout>

logcat:

02-14 12:27:58.309: e/androidruntime(8647): fatal exception: main 02-14 12:27:58.309: e/androidruntime(8647): java.lang.runtimeexception: unable start activity componentinfo{com.muttgames.shadowpuzzle/com.muttgames.shadowpuzzle.levelselect}: android.content.res.resources$notfoundexception: resource id #0x7f08000d type #0x12 not valid 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread.performlaunchactivity(activitythread.java:2180) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread.handlelaunchactivity(activitythread.java:2230) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread.access$600(activitythread.java:141) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread$h.handlemessage(activitythread.java:1234) 02-14 12:27:58.309: e/androidruntime(8647): @ android.os.handler.dispatchmessage(handler.java:99) 02-14 12:27:58.309: e/androidruntime(8647): @ android.os.looper.loop(looper.java:137) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread.main(activitythread.java:5039) 02-14 12:27:58.309: e/androidruntime(8647): @ java.lang.reflect.method.invokenative(native method) 02-14 12:27:58.309: e/androidruntime(8647): @ java.lang.reflect.method.invoke(method.java:511) 02-14 12:27:58.309: e/androidruntime(8647): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:793) 02-14 12:27:58.309: e/androidruntime(8647): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:560) 02-14 12:27:58.309: e/androidruntime(8647): @ dalvik.system.nativestart.main(native method) 02-14 12:27:58.309: e/androidruntime(8647): caused by: android.content.res.resources$notfoundexception: resource id #0x7f08000d type #0x12 not valid 02-14 12:27:58.309: e/androidruntime(8647): @ android.content.res.resources.loadxmlresourceparser(resources.java:2144) 02-14 12:27:58.309: e/androidruntime(8647): @ android.content.res.resources.getlayout(resources.java:853) 02-14 12:27:58.309: e/androidruntime(8647): @ android.view.layoutinflater.inflate(layoutinflater.java:394) 02-14 12:27:58.309: e/androidruntime(8647): @ com.muttgames.shadowpuzzle.levelselect.oncreate(levelselect.java:25) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activity.performcreate(activity.java:5104) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.instrumentation.callactivityoncreate(instrumentation.java:1080) 02-14 12:27:58.309: e/androidruntime(8647): @ android.app.activitythread.performlaunchactivity(activitythread.java:2144) 02-14 12:27:58.309: e/androidruntime(8647): ... 11 more

you need layout inflater , container. create illustration "sandmenu" linearlayout , in code:

sandmenu.removeallviews(); layoutinflater inflater = (layoutinflater) .getsystemservice(context.layout_inflater_service); view innerviewssandmenu = inflater.inflate(r.layout.yourlayout, sandmenu, true);

and later can access inner views of sandmenu in way:

innerviewssandmenu.findviewbyid(r.id.....

android multiple-views

Comments

Popular posts from this blog

web services - java.lang.NoClassDefFoundError: Could not initialize class net.sf.cglib.proxy.Enhancer -

Accessing MATLAB's unicode strings from C -

javascript - mongodb won't find my schema method in nested container -