How to reuse my xml relative layout in android -
hi have m file relative layout , contains 9 image buttons. reuse layout multiple times changing names , id's. able gridview problem want use same customs design have using relative layout. can 1 how it.
<relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" > <imagebutton android:id="@+id/day0" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparentleft="true" android:layout_aligntop="@+id/day1" android:layout_marginleft="14dp" android:scaletype="fitxy" android:src="@drawable/day0" /> <imagebutton android:id="@+id/day3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day0" android:layout_below="@+id/day0" android:layout_margintop="54dp" android:src="@drawable/day3" /> <imagebutton android:id="@+id/day6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day3" android:layout_below="@+id/day3" android:layout_margintop="74dp" android:src="@drawable/day6" /> <imagebutton android:id="@+id/day4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/day6" android:layout_marginleft="25dp" android:layout_torightof="@+id/day3" android:src="@drawable/day4" /> <imagebutton android:id="@+id/day7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day4" android:layout_aligntop="@+id/day6" android:src="@drawable/day7" /> <imagebutton android:id="@+id/day1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day4" android:layout_alignparenttop="true" android:layout_margintop="39dp" android:scaletype="fitxy" android:src="@drawable/day1" /> <imagebutton android:id="@+id/day8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day5" android:layout_aligntop="@+id/day7" android:src="@drawable/day8" /> <imagebutton android:id="@+id/day2" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_aligntop="@+id/day1" android:layout_marginleft="28dp" android:layout_torightof="@+id/day1" android:scaletype="fitxy" android:src="@drawable/day2" /> <imagebutton android:id="@+id/day5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_above="@+id/day6" android:layout_alignleft="@+id/day2" android:src="@drawable/day5" /> <textview android:id="@+id/textview5" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/textview4" android:layout_alignbottom="@+id/textview4" android:layout_alignleft="@+id/day7" android:text="emotions" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview8" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbottom="@+id/textview7" android:layout_alignleft="@+id/ans6b" android:text="leadership" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/ans6b" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignbaseline="@+id/question6a" android:layout_alignbottom="@+id/question6a" android:layout_alignright="@+id/day1" android:text="gruppstart" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview4" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/day3" android:layout_toleftof="@+id/day4" android:text="adaption" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview7" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_below="@+id/day6" android:layout_toleftof="@+id/day4" android:text="behavior" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview9" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview3" android:layout_below="@+id/day8" android:text="satisfaction" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview6" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignright="@+id/day5" android:layout_below="@+id/day5" android:text="insight" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/textview3" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/day2" android:layout_below="@+id/day2" android:text="relationer" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/question6a" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignleft="@+id/textview7" android:layout_below="@+id/day0" android:layout_marginleft="15dp" android:text="goal" android:textappearance="?android:attr/textappearancemedium" /> <textview android:id="@+id/username" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignparenttop="true" android:layout_toleftof="@+id/textview3" android:text="medium text" android:textappearance="?android:attr/textappearancemedium" /> </relativelayout>
this xml file call xml file different activities changing names id , display image buttons. how can 1 please suggest me
import java.io.ioexception; import java.io.inputstream; import java.io.outputstream; import java.util.arraylist; import java.util.hashmap; import com.xylem.flygt.r; import android.os.asynctask; import android.os.bundle; import android.app.activity; import android.app.progressdialog; import android.util.log; import android.view.keyevent; import android.view.menu; import android.view.view; import android.view.viewgroup; import android.view.animation.accelerateinterpolator; import android.view.animation.animation; import android.view.animation.translateanimation; import android.widget.baseadapter; import android.widget.button; import android.widget.imagebutton; import android.widget.imageview; import android.widget.listadapter; import android.widget.tablelayout; import android.widget.textview; import android.widget.toast; import android.widget.viewflipper; import android.content.context; import android.content.intent; import android.graphics.bitmap; import android.graphics.bitmapfactory; import android.graphics.color; import android.widget.adapterview; import android.widget.adapterview.onitemclicklistener; import android.widget.gridview; public class menuactivity extends activity { static gridview gridview; bitmap bmp = null; public arraylist<bitmap> bitmaparray = new arraylist<bitmap>(); context context; imagebutton day0, day1, day2, day3, day4, day5, day6, day7, day8; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.grid_layout); day0 = (imagebutton) findviewbyid(r.id.day0); day1 = (imagebutton) findviewbyid(r.id.day1); day2 = (imagebutton) findviewbyid(r.id.day2); day3 = (imagebutton) findviewbyid(r.id.day3); day4 = (imagebutton) findviewbyid(r.id.day4); day5 = (imagebutton) findviewbyid(r.id.day5); day6 = (imagebutton) findviewbyid(r.id.day6); day7 = (imagebutton) findviewbyid(r.id.day7); day8 = (imagebutton) findviewbyid(r.id.day8); gridview = (gridview) findviewbyid(r.id.grid_view); gridview.setbackgroundcolor(color.parsecolor("#e3e4fa")); gridview.setonitemclicklistener(new onitemclicklistener() { @override public void onitemclick(adapterview<?> parent, view v, int position, long id) { system.out.println("clicked id " + position); if(position == 0) { system.out.println(" day 0 "); } if(position == 1) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "1"); system.out.println(" day 1 "); startactivity(i); } if(position == 2) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "2"); system.out.println(" day 2 "); startactivity(i); } if(position == 3) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "3"); system.out.println(" day 3 "); startactivity(i); } if(position == 4) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "4"); system.out.println(" day 4 "); startactivity(i); } if(position == 5) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "5"); system.out.println(" day 5 "); startactivity(i); } if(position == 6) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "6"); system.out.println(" day 6"); startactivity(i); } if(position == 7) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "7"); system.out.println(" day 7 "); startactivity(i); } if(position == 8) { intent = new intent(getapplicationcontext(), nextactivity.class); i.putextra("dayno", "8"); system.out.println(" day 8 "); startactivity(i); } } }); new checkday().execute(); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.main, menu); return true; } class checkday extends asynctask<string, string, string> { protected void onpreexecute() { super.onpreexecute(); } @override protected string doinbackground(string... arg0) { return null; } protected void onpostexecute(string file_url) { gridview.setadapter(new calenderadaptor(menuactivity.this)); } } }
i have written activity java creates image buttons in gridview. use want use in such way call customs layout in different activities because layout same ids , images change.
if understand right - have custom design layout buttons , want use same layout in other screen well.
you don't need rename anything, put layout in separate xml, let's commonbuttonlayout.xml. should contain this:
<?xml version="1.0" encoding="utf-8"?> <merge xmlns:android="http://schemas.android.com/apk/res/android" > .... layout ..... </merge>
now want use in other layout files. need include xml directly there following.
<?xml version="1.0" encoding="utf-8"?> <relativelayout ... ... > <include android:layout_centervertical="true" layout="@layout/commonbuttonlayout" /> </relativelayout>
Comments
Post a Comment