android - How to make a button go to another page? -
okay have section_home layout has button code.
<button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@color/transparent" android:text="apply" android:textcolor="@color/white" android:elevation="0dp" android:id="@+id/button" android:layout_aligntop="@+id/linearlayout" android:layout_alignparentleft="true" android:layout_alignparentstart="true" />
what want when click's "apply" button go other layout that's called section_apply
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/window_bg" android:cliptopadding="false" android:fitssystemwindows="true"> <listview android:id="@+id/launcherslist" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_alignparentstart="true" android:layout_alignparenttop="true" android:background="@color/light_bg" android:divider="@color/transparent" />
here java class home layout called homefragment.
public class homefragment extends fragment { button button; private static final string market_url = "https://play.google.com/store/apps/details?id="; private string playstoredevaccount, playstorelisting, apponepackage, apptwopackage, appthreepackage; @override public view oncreateview(layoutinflater inflater, final viewgroup container, bundle savedinstancestate) { viewgroup root = (viewgroup) inflater.inflate(r.layout.section_home, container, false); playstoredevaccount = getresources().getstring(r.string.play_store_dev_link); playstorelisting = getactivity().getpackagename(); apponepackage = getresources().getstring(r.string.app_one_package); apptwopackage = getresources().getstring(r.string.app_two_package); appthreepackage = getresources().getstring(r.string.app_three_package); actionbar toolbar = ((appcompatactivity) getactivity()).getsupportactionbar(); if (toolbar != null) { toolbar.settitle(r.string.app_name); } observablescrollview content = (observablescrollview) root.findviewbyid(r.id.homecontent); textview ratebtn = (textview) root.findviewbyid(r.id.rate_button); ratebtn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { intent rate = new intent(intent.action_view, uri.parse(market_url + playstorelisting)); startactivity(rate); } }); //floatingactionbutton fab = (floatingactionbutton) root.findviewbyid(r.id.apply_btn); //fab.setcolornormal(getresources().getcolor(r.color.accent)); //fab.setcolorpressed(getresources().getcolor(r.color.accent_pressed)); //fab.setcolorripple(getresources().getcolor(r.color.semitransparent_white)); //fab.show(true); //fab.attachtoscrollview(content); //fab.setonclicklistener(new view.onclicklistener() { // @override //public void onclick(view v) { // ((mainactivity) getactivity()).result.setselectionbyidentifier(3); //((mainactivity) getactivity()).switchfragment(3, getresources().getstring(r.string.section_three), "apply"); // } //}); return root; } private boolean appisinstalled(string packagename) { final packagemanager pm = getactivity().getpackagemanager(); boolean installed; try { pm.getpackageinfo(packagename, packagemanager.get_activities); installed = true; } catch (packagemanager.namenotfoundexception e) { installed = false; } return installed; } }
this java class apply layout called applyfragment
public class applyfragment extends fragment { private static final string market_url = "https://play.google.com/store/apps/details?id="; private string intentstring; private final list<launcher> launchers = new arraylist<>(); @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { viewgroup root = (viewgroup) inflater.inflate(r.layout.section_apply, container, false); // splits launcher arrays | delimiter {name}|{package} string[] launcherarray = getresources().getstringarray(r.array.launchers); (string launcher : launcherarray) launchers.add(new launcher(launcher.split("\\|"))); actionbar toolbar = ((appcompatactivity) getactivity()).getsupportactionbar(); if (toolbar != null) toolbar.settitle(r.string.section_three); listview launcherslist = (listview) root.findviewbyid(r.id.launcherslist); launchersadapter adapter = new launchersadapter(launchers); launcherslist.setadapter(adapter); launcherslist.setonitemclicklistener(new adapterview.onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view view, int position, long id) { if (launchers.get(position).name.equals("google launcher")) gnldialog(); else if (launcherisinstalled(launchers.get(position).packagename)) openlauncher(launchers.get(position).name); else openinplaystore(launchers.get(position)); } }); return root; } private boolean launcherisinstalled(string packagename) { final packagemanager pm = getactivity().getpackagemanager(); boolean installed; try { pm.getpackageinfo(packagename, packagemanager.get_activities); installed = true; } catch (packagemanager.namenotfoundexception e) { installed = false; } return installed; } private void openlauncher(string name) { final string classname = "com.jahirfiquitiva.paperboard" + ".launchers." + character.touppercase(name.charat(0)) + name.substring(1).tolowercase().replace(" ", "").replace("launcher", "") + "launcher"; class<?> cl = null; try { cl = class.forname(classname); } catch (classnotfoundexception e) { log.e("launcher class missing", "launcher class for: '" + name + "' missing!"); } if (cl != null) { constructor<?> constructor = null; try { constructor = cl.getconstructor(context.class); } catch (nosuchmethodexception e) { log.e("launcher class cons", "launcher class for: '" + name + "' missing constructor!"); } try { if (constructor != null) constructor.newinstance(getactivity()); } catch (exception e) { e.printstacktrace(); } } } private void openinplaystore(final launcher launcher) { intentstring = market_url + launcher.packagename; final string launchername = launcher.name; final string cmname = "cm theme engine"; string dialogcontent; if (launchername.equals(cmname)) { dialogcontent = launcher.name + getresources().getstring(r.string.cm_dialog_content); intentstring = "http://download.cyanogenmod.org/"; } else { dialogcontent = launcher.name + getresources().getstring(r.string.lni_content); intentstring = market_url + launcher.packagename; } new materialdialog.builder(getactivity()) .title(launcher.name + getresources().getstring(r.string.lni_title)) .content(dialogcontent) .positivetext(r.string.lni_yes) .negativetext(r.string.lni_no) .callback(new materialdialog.buttoncallback() { @override public void onpositive(materialdialog dialog) { intent intent = new intent(intent.action_view); intent.setdata(uri.parse(intentstring)); startactivity(intent); } }).show(); } public class launcher { public final string name; public final string packagename; public launcher(string[] values) { name = values[0]; packagename = values[1]; } } class launchersadapter extends arrayadapter<launcher> { final list<launcher> launchers; launchersadapter(list<launcher> launchers) { super(getactivity(), r.layout.item_launcher, r.id.launchername, launchers); this.launchers = launchers; } @override public view getview(int position, view convertview, viewgroup parent) { view item = convertview; launcherholder holder; if (item == null) { layoutinflater inflater = layoutinflater.from(getactivity()); item = inflater.inflate(r.layout.item_launcher, parent, false); holder = new launcherholder(item); item.settag(holder); } else { holder = (launcherholder) item.gettag(); } // turns launcher name "something pro" "l_something_pro" int iconresource = getactivity().getresources().getidentifier( "ic_" + launchers.get(position).name.tolowercase().replace(" ", "_"), "drawable", getactivity().getpackagename() ); holder.icon.setimageresource(iconresource); holder.launchername.settext(launchers.get(position).name); if (launcherisinstalled(launchers.get(position).packagename)) { holder.isinstalled.settext(r.string.installed); holder.isinstalled.settextcolor(getresources().getcolor(r.color.green)); } else { holder.isinstalled.settext(r.string.noninstalled); holder.isinstalled.settextcolor(getresources().getcolor(r.color.red)); } return item; } class launcherholder { final imageview icon; final textview launchername; final textview isinstalled; launcherholder(view v) { icon = (imageview) v.findviewbyid(r.id.launchericon); launchername = (textview) v.findviewbyid(r.id.launchername); isinstalled = (textview) v.findviewbyid(r.id.launcherinstalled); } } } private void gnldialog() { final string applink = market_url + getresources().getstring(r.string.extraapp); new materialdialog.builder(getactivity()) .title(r.string.gnl_title) .content(r.string.gnl_content) .positivetext(r.string.lni_yes) .negativetext(r.string.lni_no) .callback(new materialdialog.buttoncallback() { @override public void onpositive(materialdialog dialog) { super.onpositive(dialog); intent intent = new intent(intent.action_view); intent.setdata(uri.parse(applink)); startactivity(intent); } } ).show(); } }
basically want button in home layout take applyfragment/section_apply layout.
this looks paperboard template, correct? code should work without issue if place in homefragment , id of button "button".
textview btn = (textview) root.findviewbyid(r.id.button); btn.setonclicklistener(new view.onclicklistener() { @override public void onclick(view v) { ((mainactivity) getactivity()).result.setselectionbyidentifier(3); ((mainactivity) getactivity()).switchfragment(3, getresources().getstring(r.string.section_three), "apply"); } });
edit: changed work code.
Comments
Post a Comment