Android: Landscape layouts not being used -
in application have 1 main activity sets tabbed viewpager 3 different fragments, each 1 it's own layout , class. i've created landscape versions of each layout file , placed them in res/layout-land. when run app , switch orientations, landscape layout isn't being used?
mainactivity:
package me.zaydbille.utilitywatch; import android.content.intent; import android.content.res.configuration; import android.support.v4.view.viewpager; import android.support.v7.app.actionbaractivity; import android.os.bundle; import android.support.v7.widget.toolbar; import android.view.menu; import android.view.menuitem; import java.util.arraylist; public class mainactivity extends actionbaractivity { // tab , viewpager variables toolbar toolbar; viewpager pager; viewpageradapter adapter; slidingtablayout tabs; charsequence titles[]={"coin flip", "counter", "choice"}; int numtabs = 3; @override protected void oncreate(bundle savedinstancestate) { super.oncreate(savedinstancestate); setcontentview(r.layout.activity_main); // creating toolbar , setting toolbar activity toolbar = (toolbar) findviewbyid(r.id.tool_bar); setsupportactionbar(toolbar); // creating viewpageradapter , passing fragment manager, titles fot tabs , number of tabs. adapter = new viewpageradapter(getsupportfragmentmanager(), titles, numtabs); // assigning viewpager view , setting adapter pager = (viewpager) findviewbyid(r.id.pager); pager.setadapter(adapter); // assiging sliding tab layout view tabs = (slidingtablayout) findviewbyid(r.id.tabs); tabs.setdistributeevenly(true); // make tabs fixed set true, makes tabs space evenly in available width // setting custom color scroll bar indicator of tab view tabs.setcustomtabcolorizer(new slidingtablayout.tabcolorizer() { @override public int getindicatorcolor(int position) { return getresources().getcolor(r.color.tabsscrollcolor); } }); // setting viewpager slidingtabslayout tabs.setviewpager(pager); } @override public boolean oncreateoptionsmenu(menu menu) { // inflate menu; adds items action bar if present. getmenuinflater().inflate(r.menu.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(); //noinspection simplifiableifstatement if (id == r.id.action_settings) { intent myintent = new intent(this, helpactivity.class); startactivity(myintent); return true; } return super.onoptionsitemselected(item); } // counter fragment helper methods public void addcount() { preferences.setintpref(this, preferences.getintpref(this) + 1); } public void clearcount() { preferences.setintpref(this, 0); } public int getcount() { return preferences.getintpref(this); } // choice fragment helper methods public void savelist(arraylist<string> list){ preferences.savelist(this, list); } public arraylist<string> getlist() { return preferences.getlist(this); } }
fragment 1:
package me.zaydbille.utilitywatch; import android.content.context; import android.content.res.configuration; import android.graphics.drawable.animationdrawable; import android.os.bundle; import android.support.annotation.nullable; import android.support.v4.app.fragment; import android.view.layoutinflater; import android.view.view; import android.view.viewgroup; import android.widget.button; import android.widget.imageview; import java.util.random; public class coinflip extends fragment { button flipbutton; random randomizer; imageview mspinningcoin; @override public view oncreateview(layoutinflater inflater, @nullable viewgroup container, @nullable bundle savedinstancestate) { view v = inflater.inflate(r.layout.coin_flip,container,false); flipbutton = (button) v.findviewbyid(r.id.flipbutton); mspinningcoin = (imageview) v.findviewbyid(r.id.coin_spinning); flipbutton.setonclicklistener(new button.onclicklistener() { public void onclick(view v) { randomizer = new random(); int number = randomizer.nextint(2); if (number == 0) { // heads ((animationdrawable) mspinningcoin.getbackground()).stop(); mspinningcoin.setbackgroundresource(r.drawable.coin_spin_heads); ((animationdrawable) mspinningcoin.getbackground()).start(); } else if (number == 1) { // tails ((animationdrawable) mspinningcoin.getbackground()).stop(); mspinningcoin.setbackgroundresource(r.drawable.coin_spin_tails); ((animationdrawable) mspinningcoin.getbackground()).start(); } } }); return v; } }
fragment 1's layout file:
<?xml version="1.0" encoding="utf-8"?> <relativelayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@color/colorprimarylight"> <linearlayout android:layout_width="wrap_content" android:layout_height="wrap_content" android:baselinealigned="false" android:orientation="vertical" android:layout_centervertical="true" android:layout_centerhorizontal="true" android:layout_alignparentend="false" android:layout_alignparentstart="false" android:layout_alignparentbottom="false" android:gravity="center_vertical|center_horizontal" android:layout_margintop="200dp"> <imageview android:id="@+id/coin_spinning" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@drawable/coin_spin_heads" android:contentdescription="@string/coindescription" /> <button style="?android:attr/buttonstylesmall" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="@string/flipbuttontext" android:id="@+id/flipbutton" android:layout_gravity="center_horizontal" /> </linearlayout> </relativelayout>
androidmanifest:
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="me.zaydbille.utilitywatch" > <application android:allowbackup="true" android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/apptheme" > <activity android:name=".mainactivity" android:configchanges="orientation|screenlayout|screensize" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.main" /> <category android:name="android.intent.category.launcher" /> </intent-filter> </activity> <activity android:name=".helpactivity" android:label="@string/title_activity_help" android:parentactivityname=".mainactivity" > <meta-data android:name="android.support.parent_activity" android:value="me.zaydbille.utilitywatch.mainactivity" /> </activity> </application> </manifest>
when use android:configchanges="orientation|screenlayout|screensize"
saying not want system of default behavior when these configuration changes happen. includes changing layouts. remove line or handle change yourself.
Comments
Post a Comment