Friday 15 June 2012

android - Replace activity content with static fragment -



android - Replace activity content with static fragment -

i have app using activity @ start. loading xml-file set content @ onstart-procedure. after content changed fragment allow user create input in edittext view. fragment loaded dynamically @ run time using fragmentmanager , transaction. on fragment there button go on. when user clicks button content loaded @ onclick-procedure of button. tried replace first fragment 1 called “listfrag.java” using layout file called “list_frag.xml”. in portrait mode new simple xml-layout views on it.

the problem starts when device in landscape mode. check “getresources().getconfiguration().orientation“. @ time alter start fragment “listfrag.java” version of layout “list_frag.xml”. hence set layout in new res folder called “layout-land”. layout defines static fragment left pane , frame-layout right pane. frame-layout serves container detail fragmen loaded if user clicked item in left pane.

i not know if possible alter content of activity fragment on using layout static fragment definition in it. tried nil works. may here has idea.

here source-code single files in project. shorten code removed import-statements: mainactivity:

package com.example.wbslideshow; public class mainactivity extends activity implements mainfrag.onstartfragbtnclicklistener { public static final string keyval = "startpath"; bundle mysavedinstancestate; mainfrag newmainfrag; @override public void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); mysavedinstancestate = savedinstancestate; //initialize preferences xml-file //if app running first time taken xml-file preferencemanager.setdefaultvalues(this, r.xml.preferences, false); //load mainfrag select path images , start-button newmainfrag = new mainfrag(); fragmenttransaction transaction = getfragmentmanager().begintransaction(); transaction.add(r.id.mycontainer, newmainfrag); transaction.addtobackstack(null); transaction.commit(); } //procedure interface of mainfrag class images in given path @override public void onstartfragbtnclicked(string root) { //call procedure "private boolean landscapemode()" check mode if (!landscapemode()) { listfrag newlistfrag = new listfrag(); //put value edittext field of mainfrag class arguments listfrag class bundle args = new bundle(); args.putstring(listfrag.frag_message_def_input, root); newlistfrag.setarguments(args); //change fragments dynamically fragmenttransaction transaction = getfragmentmanager().begintransaction(); transaction.replace(r.id.mycontainer, newlistfrag); transaction.addtobackstack(null); transaction.commit(); } else { //remove mainfrag fragmenttransaction transaction = getfragmentmanager().begintransaction(); transaction.remove(newmainfrag); transaction.commit(); //load static listfrag listfrag newlistfrag = new listfrag(); //put value edittext field of mainfrag class arguments listfrag class bundle args = new bundle(); args.putstring(listfrag.frag_message_def_input, root); //load imgfrag right pane framelayout imgfrag myimgfrag = new imgfrag(); if (myimgfrag != null) { transaction = getfragmentmanager().begintransaction(); transaction.add(r.id.myimgcontainer, myimgfrag); transaction.commit(); } } } private boolean landscapemode() { if (getresources().getconfiguration().orientation == configuration.orientation_portrait) { homecoming false; } else if (getresources().getconfiguration().orientation == configuration.orientation_landscape) { homecoming true; } else homecoming false; } }

mainfrag: bundle com.example.wbslideshow;

public class mainfrag extends fragment implements onclicklistener{ //, onsharedpreferencechangelistener { /* * constant representing value of android:key preferences.xml. * value found in android:defaultvalue */ public static final string keyval = "startpath"; public static final string jpgval = "pref-jpg"; public static final string pngval = "pref-png"; edittext myedittext; sharedpreferences sharedpref; //define interface communicate main activity when user clicked button private onstartfragbtnclicklistener mcallback; public interface onstartfragbtnclicklistener { public void onstartfragbtnclicked(string myinput); } public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate ) { view view = inflater.inflate(r.layout.main_frag, container, false); //instantiate start button , register onclicklistener button start = (button) view.findviewbyid(r.id.startbtn); start.setonclicklistener(this); //read current path pics sharedpreferences file activity myactivity = getactivity(); sharedpref = preferencemanager.getdefaultsharedpreferences(myactivity.getbasecontext()); //registerpreferencelistener(); myedittext = (edittext) view.findviewbyid(r.id.inputsearchpath); myedittext.settext(sharedpref.getstring(keyval, "/")); homecoming view; } @override public void onresume() { super.onresume(); //read current path pics sharedpreferences file activity myactivity = getactivity(); sharedpref = preferencemanager.getdefaultsharedpreferences(myactivity.getbasecontext()); myedittext.settext(sharedpref.getstring(keyval, "/")); } @override public void onattach (activity activity) { super.onattach(activity); seek {mcallback = (onstartfragbtnclicklistener) activity;} grab (classcastexception e) {throw new classcastexception(activity.tostring() + " must implement oncontrolbuttonclickedlistener");} } //change listfrag class if user clicked button @override public void onclick(view view) { activity myactivity = getactivity(); if (myactivity != null) { //get users input pass activity string root = ((edittext) myactivity.findviewbyid( r.id.inputsearchpath)).gettext().tostring(); //call interface method in activity go on mcallback.onstartfragbtnclicked(root); } } }

listfrag:

package com.example.wbslideshow; public class listfrag extends listfragment{ imageview image; //arrays files , folders private list<string> listitem = null; //textview object headline private textview tvmypath; //global variable taken start path 1 time //used compare when user clicked item private string g_startpath, g_myinput; /* define constant take passed input string start fragment */ public static string frag_message_def_input = "com.example.wbslideshow.call_listfragment"; @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate ) { view myview = inflater.inflate(r.layout.list_frag, container, false); tvmypath = (textview) myview.findviewbyid(r.id.startpath); homecoming myview; } @override public void onstart() { super.onstart(); activity myactivity = getactivity(); //get instance of image , create invisible image = (imageview) myactivity.findviewbyid(r.id.imageview1); image.setvisibility(view.invisible); bundle args = getarguments(); if (args != null) { g_startpath = args.getstring(frag_message_def_input); } //save input first time compare later new item click g_myinput = g_startpath; getdir(g_startpath); } private void getdir(string p_startpath) { //set headline tvmypath.settext("location: " + p_startpath); listitem = new arraylist<string>(); file f = new file(p_startpath); //file array get's folders , files input path file[] files = f.listfiles(); //startpath changed if user clicked new folder if(!g_myinput.equals(p_startpath)) { //alter g_myinput next comparing g_myinput = p_startpath; //put item create possible 1 directory listitem.add("../"); } arrays.sort(files, filecomparator); for(int i=0; < files.length; i++) { if(files[i].isfile()) { string filename = files[i].getname(); //get file extension int z = filename.lastindexof('.'); //read file extension string wbname = filename.substring(z+1); if (wbname.equalsignorecase("jpg")) {listitem.add(filename);} if (wbname.equalsignorecase("jpeg")) {listitem.add(filename);} } else {listitem.add(files[i].getname() + "/");} } arrayadapter<string> filelist = new arrayadapter<string>(getactivity(), r.layout.row, listitem); activity myactivity = getactivity(); listview mylist = (listview) myactivity.findviewbyid(android.r.id.list); mylist.setadapter(filelist); } //procedure sort arrays comparator<? super file> filecomparator = new comparator<file>(){ public int compare(file file1, file file2) { if(file1.isdirectory()){ if (file2.isdirectory()){ homecoming string.valueof (file1.getname().tolowercase(locale.getdefault())).compareto (file2.getname().tolowercase(locale.getdefault())); }else{ homecoming -1; } }else { if (file2.isdirectory()){ homecoming 1; }else{ homecoming string.valueof (file1.getname().tolowercase(locale.getdefault())).compareto (file2.getname().tolowercase(locale.getdefault())); } } } }; @override public void onlistitemclick(listview l, view v, int position, long id) { file file; //user clicked 1 path if (listitem.get(position) == "../") { file = new file(g_startpath); g_startpath = file.getparent(); } else //if user clicked image or //to new folder (>> getdir has called new path) // >>file has set path , position {file = new file(g_startpath + '/' + listitem.get(position));} bitmap mybitmap; //user clicked image >> image has shown in image view - nil else if (file.isfile()) { //if(file.canread()){ mybitmap = bitmapfactory.decodefile(file.getpath()); image.setimagebitmap(mybitmap); image.setvisibility(view.visible); //} }else { if (file.isdirectory()) { image.setvisibility(view.invisible); if (listitem.get(position) != "../"){g_startpath = file.getpath();} getdir(g_startpath); } } } }

imgfrag:

package com.example.wbslideshow; public class imgfrag extends fragment{ public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate){ view myimgview = inflater.inflate(r.layout.img_frag, container, false); homecoming myimgview; } }

the layouts activity_main.xml

<linearlayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="#da8306" android:orientation="vertical" android:id="@+id/mycontainer"> </linearlayout

main_frag.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/fragmain" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" > <textview android:id="@+id/startheader" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/startheader" /> <edittext android:id="@+id/inputsearchpath" android:layout_width="match_parent" android:layout_height="wrap_content" android:ems="10" android:hint="@string/startinputhint" > <requestfocus /> </edittext> <button android:id="@+id/startbtn" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/startbtn" /> </linearlayout>

layout/list_frag.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/listfrag" android:orientation="vertical" > <textview android:id="@+id/startpath" android:layout_width="match_parent" android:layout_height="wrap_content" android:background="#c27302" android:height="40dp" android:maxlines="1" android:scrollhorizontally="false" android:text="@string/list_header" android:textsize="16sp" android:textstyle="bold" /> <listview android:id="@android:id/list" android:layout_width="match_parent" android:layout_height="312dp" android:layout_gravity="fill" android:background="#b012eb" /> <textview android:id="@+id/empty" android:layout_width="match_parent" android:layout_height="wrap_content" /> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> <framelayout android:id="@+id/myimgcontainer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="fill_parent" /> </linearlayout>

layout-land/list_frag.xml:

<?xml version="1.0" encoding="utf-8"?> linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:baselinealigned="false" android:orientation="horizontal" > <!-- static fragment left pane --> <fragment android:name="com.example.wbslideshow.listfrag" android:id="@+id/listfrag" android:layout_width="0dp" android:layout_height="fill_parent" android:layout_weight="1" /> <framelayout android:id="@+id/myimgcontainer" android:layout_weight="2" android:layout_width="0dp" android:layout_height="fill_parent"> </framelayout> </linearlayout>

img_frag.xml:

<?xml version="1.0" encoding="utf-8"?> <linearlayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="horizontal" android:id="@+id/imgfrag"> <imageview android:id="@+id/imageview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:src="@drawable/abc_ab_bottom_solid_dark_holo" /> </linearlayout>

the code might not perfect not issue here. in portrait-mode app working. in landscape mode problem loading listfrag. here current logcat:

10-11 13:20:12.563: w/dalvikvm(5158): threadid=1: thread exiting uncaught exception (group=0x414539a8) 10-11 13:20:12.583: e/androidruntime(5158): fatal exception: main 10-11 13:20:12.583: e/androidruntime(5158): java.lang.illegalargumentexception: no view found id 0x7f060041 (com.example.wbslideshow:id/myimgcontainer) fragment imgfrag{417af140 #1 id=0x7f060041} 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:877) 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.fragmentmanagerimpl.movetostate(fragmentmanager.java:1057) 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.backstackrecord.run(backstackrecord.java:694) 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.fragmentmanagerimpl.execpendingactions(fragmentmanager.java:1435) 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.fragmentmanagerimpl$1.run(fragmentmanager.java:441) 10-11 13:20:12.583: e/androidruntime(5158): @ android.os.handler.handlecallback(handler.java:725) 10-11 13:20:12.583: e/androidruntime(5158): @ android.os.handler.dispatchmessage(handler.java:92) 10-11 13:20:12.583: e/androidruntime(5158): @ android.os.looper.loop(looper.java:153) 10-11 13:20:12.583: e/androidruntime(5158): @ android.app.activitythread.main(activitythread.java:5299) 10-11 13:20:12.583: e/androidruntime(5158): @ java.lang.reflect.method.invokenative(native method) 10-11 13:20:12.583: e/androidruntime(5158): @ java.lang.reflect.method.invoke(method.java:511) 10-11 13:20:12.583: e/androidruntime(5158): @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:833) 10-11 13:20:12.583: e/androidruntime(5158): @ com.android.internal.os.zygoteinit.main(zygoteinit.java:600) 10-11 13:20:12.583: e/androidruntime(5158): @ dalvik.system.nativestart.main(native method)

i can see there problem "no vie found ..." not know why. me seems layout file res/layout-land not loaded in landscape mode , hence oncreateview imgfrag gets problem. why?

andreas

id myimgcontainer available in layout layout-land/list_frag.xml, time click event called, main_frag shown on screen, instead of r.id.myimgcontainer in mainactivity user r.id.mycontainer itself.

android android-layout android-fragments

No comments:

Post a Comment