java - How to link two xml pages in Android app when a button is clicked? -
i making simple app in on home page, button placed. when button clicked, must display new page (xml in layout). how ..
my app contains in src directory, there 2 java files. 1. activity2 2. main_activity , 2 xml files in layout: 1. fragment_main.xml 2. activity.xml
i want when click "technical button", should display activity.xml page layout..
my codes:
main_activity.java
package com.example.vit;
import java.util.locale;
import android.util.log; import android.support.v7.app.actionbaractivity; import android.support.v7.app.actionbar; import android.support.v4.app.fragment; import android.support.v4.app.fragmentmanager; import android.support.v4.app.fragmenttransaction; import android.support.v4.app.fragmentpageradapter; import android.app.activity; import android.content.intent; import android.os.bundle; import android.support.v4.view.viewpager; import android.view.gravity; import android.view.layoutinflater; import android.view.menu; import android.view.menuitem; import android.view.view; import android.view.viewgroup; import android.widget.textview;
public class mainactivity extends actionbaractivity implements actionbar.tablistener {
/** * {@link android.support.v4.view.pageradapter} provide * fragments each of sections. use {@link fragmentpageradapter} * derivative, keep every loaded fragment in memory. if * becomes memory intensive, may best switch * {@link android.support.v4.app.fragmentstatepageradapter}. */ sectionspageradapter msectionspageradapter; /** * {@link viewpager} host section contents. */ viewpager mviewpager; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // set action bar. final actionbar actionbar = getsupportactionbar(); actionbar.setnavigationmode(actionbar.navigation_mode_tabs); // create adapter return fragment each of 3 // primary sections of activity. msectionspageradapter = new sectionspageradapter( getsupportfragmentmanager()); // set viewpager sections adapter. mviewpager = (viewpager) findviewbyid(r.id.pager); mviewpager.setadapter(msectionspageradapter); // when swiping between different sections, select corresponding // tab. can use actionbar.tab#select() if have // reference tab. mviewpager .setonpagechangelistener(new viewpager.simpleonpagechangelistener() { @override public void onpageselected(int position) { actionbar.setselectednavigationitem(position); } }); // each of sections in app, add tab action bar. (int = 0; < msectionspageradapter.getcount(); i++) { // create tab text corresponding page title defined // adapter. specify activity object, implements // tablistener interface, callback (listener) when // tab selected. actionbar.addtab(actionbar.newtab() .settext(msectionspageradapter.getpagetitle(i)) .settablistener(this)); } } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } @override public boolean onoptionsitemselected(menuitem item) { // handle action bar item clicks here. action bar // automatically handle clicks on home/up button, long // specify parent activity in androidmanifest.xml. int id = item.getitemid(); if (id == r.id.action_settings) { return true; } return super.onoptionsitemselected(item); } @override public void ontabselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { // when given tab selected, switch corresponding page in // viewpager. mviewpager.setcurrentitem(tab.getposition()); } @override public void ontabunselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } @override public void ontabreselected(actionbar.tab tab, fragmenttransaction fragmenttransaction) { } /** * {@link fragmentpageradapter} returns fragment corresponding * 1 of sections/tabs/pages. */ public class sectionspageradapter extends fragmentpageradapter { public sectionspageradapter(fragmentmanager fm) { super(fm); } @override public fragment getitem(int position) { // getitem called instantiate fragment given page. // return placeholderfragment (defined static inner class // below). return placeholderfragment.newinstance(position + 1); } @override public int getcount() { // show 3 total pages. return 3; } @override public charsequence getpagetitle(int position) { locale l = locale.getdefault(); switch (position) { case 0: return getstring(r.string.title_section1).touppercase(l); case 1: return getstring(r.string.title_section2).touppercase(l); case 2: return getstring(r.string.title_section3).touppercase(l); } return null; } } /** * placeholder fragment containing simple view. */ public static class placeholderfragment extends fragment { /** * fragment argument representing section number * fragment. */ private static final string arg_section_number = "section_number"; /** * returns new instance of fragment given section number. */ public static placeholderfragment newinstance(int sectionnumber) { placeholderfragment fragment = new placeholderfragment(); bundle args = new bundle(); args.putint(arg_section_number, sectionnumber); fragment.setarguments(args); return fragment; } public placeholderfragment() { } @override public view oncreateview(layoutinflater inflater, viewgroup container, bundle savedinstancestate) { view rootview = inflater.inflate(r.layout.fragment_main, container, false); return rootview; } } public void onclick(view v) { intent intent = new intent (v.getcontext(),activity.class); startactivityforresult (intent,0); }
}
2.activity2.java
package com.example.vit; import android.app.activity; public class activity2 extends activity public void oncreate(bundle, savedinstancestate) { { super.oncreate(savedinstancestate); setcontentview(r.layout.activity); } } 3. fragment_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:orientation="vertical" android:paddingbottom="@dimen/activity_vertical_margin" android:paddingleft="@dimen/activity_horizontal_margin" android:paddingright="@dimen/activity_horizontal_margin" android:paddingtop="@dimen/activity_vertical_margin" tools:context="com.example.vit.mainactivity$placeholderfragment" > <textview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <button android:id="@+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="31dp" android:onclick="dosomething" android:text="technical" /> <button android:id="@+id/button2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_margintop="50dp" android:text="non technical" /> </linearlayout>
activity.xml
<textview android:id="@+id/section_label" android:layout_width="wrap_content" android:layout_height="wrap_content" /> <textview android:id="@+id/textview1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="your request being processed" android:textappearance="?android:attr/textappearancelarge" />
intent intent = new intent(this, activitytwo.class); this.startactivity ( intent );
Comments
Post a Comment