Friday, 1 January 2016

XML : How to show Sliding Menu Bar in every activity?

im new here so i am sorry if i didn't form my post easy to understand,

Im making a android app with a sliding menu, but i want it to be avaliable for all activities.

heres a example of my problem: i have 7 main options (Home, Translator, Tour, Settings etc) named fragment1 to fragment7 , but i also have a horizontalscrollview inside Tour with other activities, i want these 'other activities' to show the Sliding Menu aswell..

Heres my codes

AndroidManifest.xml

      <manifest xmlns:android="http://schemas.android.com/apk/res/android"      package="com.gnirt69.slidingmenuexample">        <application android:allowBackup="true" android:label="@string/app_name"          android:icon="@mipmap/ic_launcher" android:supportsRtl="true"          android:theme="@style/AppTheme">            <activity android:name=".MainActivity" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.LAUNCHER" />              </intent-filter>          </activity>            <activity android:name=".touring.Hotels" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.MAIN" />                  <category android:name="android.intent.category.TAB" />              </intent-filter>          </activity>            <activity android:name=".touring.Hotspots" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <category android:name="android.intent.category.TAB" />              </intent-filter>          </activity>            <activity android:name=".touring.Leisure" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <category android:name="android.intent.category.TAB" />              </intent-filter>          </activity>            <activity android:name=".touring.Nightlife" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <category android:name="android.intent.category.TAB" />              </intent-filter>          </activity>            <activity android:name=".touring.Resteraunts" android:label="Tour Guide with Translator">              <intent-filter>                  <action android:name="android.intent.action.VIEW" />                  <category android:name="android.intent.category.TAB" />              </intent-filter>          </activity>                  </application>      </manifest>    

ItemSlideMenu.java

      package com.gnirt69.slidingmenuexample.model;      public class ItemSlideMenu {        private int imgId;      private String title;        public ItemSlideMenu(int imgId, String title) {          this.imgId = imgId;          this.title = title;      }        public int getImgId() {          return imgId;      }        public void setImgId(int imgId) {          this.imgId = imgId;      }        public String getTitle() {          return title;      }        public void setTitle(String title) {          this.title = title;      }  }    

MainActivity.java

  import android.app.Fragment;  import android.app.FragmentManager;  import android.app.FragmentTransaction;  import android.content.Intent;  import android.os.Bundle;  import android.support.v4.widget.DrawerLayout;  import android.support.v7.app.ActionBarActivity;  import android.support.v7.app.ActionBarDrawerToggle;  import android.view.Menu;  import android.view.MenuItem;  import android.view.View;  import android.widget.AdapterView;  import android.widget.ListView;  import com.gnirt69.slidingmenuexample.adapter.SlidingMenuAdapter;  import com.gnirt69.slidingmenuexample.fragment.Fragment1;  import com.gnirt69.slidingmenuexample.fragment.Fragment2;  import com.gnirt69.slidingmenuexample.fragment.Fragment3;  import com.gnirt69.slidingmenuexample.fragment.Fragment4;  import com.gnirt69.slidingmenuexample.fragment.Fragment5;  import com.gnirt69.slidingmenuexample.fragment.Fragment6;  import com.gnirt69.slidingmenuexample.fragment.Fragment7;  import com.gnirt69.slidingmenuexample.touring.Hotels;  import com.gnirt69.slidingmenuexample.model.ItemSlideMenu;  import com.gnirt69.slidingmenuexample.touring.Hotspots;  import com.gnirt69.slidingmenuexample.touring.Leisure;  import com.gnirt69.slidingmenuexample.touring.Nightlife;  import com.gnirt69.slidingmenuexample.touring.Resteraunts;    import java.util.ArrayList;  import java.util.List;    /**   * Created by User on 10/18/2015.   */  public class MainActivity extends ActionBarActivity {        private List<ItemSlideMenu> listSliding;      private SlidingMenuAdapter adapter;      private ListView listViewSliding;      private DrawerLayout drawerLayout;      private ActionBarDrawerToggle actionBarDrawerToggle;          @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.main_activity);                //Init component          listViewSliding = (ListView) findViewById(R.id.lv_sliding_menu);          drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);          listSliding = new ArrayList<>();          //Add item for sliding list          listSliding.add(new ItemSlideMenu(R.drawable.homeeee, "Home"));          listSliding.add(new ItemSlideMenu(R.drawable.tourrrr, "Tour Guide"));          listSliding.add(new ItemSlideMenu(R.drawable.trrravel, "Translator"));          listSliding.add(new ItemSlideMenu(R.drawable.settings_black, "Settings"));          listSliding.add(new ItemSlideMenu(R.mipmap.ic_launcher, "Help"));          listSliding.add(new ItemSlideMenu(R.mipmap.ic_launcher, "About"));          listSliding.add(new ItemSlideMenu(R.mipmap.ic_launcher, "Exit App"));          adapter = new SlidingMenuAdapter(this, listSliding);          listViewSliding.setAdapter(adapter);                //Display icon to open/ close sliding list          getSupportActionBar().setDisplayHomeAsUpEnabled(true);            //Set title          setTitle(listSliding.get(0).getTitle());          //item selected          listViewSliding.setItemChecked(0, true);          //Close menu          drawerLayout.closeDrawer(listViewSliding);            //Display fragment 1 when start          replaceFragment(0);          //Hanlde on item click            listViewSliding.setOnItemClickListener(new AdapterView.OnItemClickListener() {              @Override              public void onItemClick(AdapterView<?> parent, View view, int position, long id) {                  //Set title                  setTitle(listSliding.get(position).getTitle());                  //item selected                  listViewSliding.setItemChecked(position, true);                  //Replace fragment                  replaceFragment(position);                  //Close menu                  drawerLayout.closeDrawer(listViewSliding);              }          });            actionBarDrawerToggle = new ActionBarDrawerToggle(this, drawerLayout, R.string.drawer_opened, R.string.drawer_closed){                @Override              public void onDrawerOpened(View drawerView) {                  super.onDrawerOpened(drawerView);                  invalidateOptionsMenu();              }                @Override              public void onDrawerClosed(View drawerView) {                  super.onDrawerClosed(drawerView);                  invalidateOptionsMenu();              }          };            drawerLayout.setDrawerListener(actionBarDrawerToggle);      }          @Override      public boolean onCreateOptionsMenu(Menu menu) {          getMenuInflater().inflate(R.menu.main_menu, menu);          return true;      }        @Override      public boolean onOptionsItemSelected(MenuItem item) {            if(actionBarDrawerToggle.onOptionsItemSelected(item)) {              return true;          }          return super.onOptionsItemSelected(item);      }        @Override      protected void onPostCreate(Bundle savedInstanceState) {          super.onPostCreate(savedInstanceState);          actionBarDrawerToggle.syncState();      }        //Create method replace fragment        private void replaceFragment(int pos) {          Fragment fragment = null;          switch (pos) {              case 0:                  fragment = new Fragment1();                  break;              case 1:                  fragment = new Fragment2();                  break;              case 2:                  fragment = new Fragment3();                  break;              case 3:                  fragment = new Fragment4();                  break;              case 4:                  fragment = new Fragment5();                  break;              case 5:                  fragment = new Fragment6();                  break;              case 6:                  fragment = new Fragment7();                  break;                  default:                  fragment = new Fragment1();                  break;          }                if(null!=fragment) {              FragmentManager fragmentManager = getFragmentManager();              FragmentTransaction transaction = fragmentManager.beginTransaction();              transaction.replace(R.id.main_content, fragment);              transaction.addToBackStack(null);              transaction.commit();          }          }        public void sendHotels(View view) {          Intent startNewActivity = new Intent(this, Hotels.class);          startActivity(startNewActivity);      }      public void sendHotspots(View view) {          Intent startNewActivity = new Intent(this, Hotspots.class);          startActivity(startNewActivity);      }      public void sendLeisure(View view) {          Intent startNewActivity = new Intent(this, Leisure.class);          startActivity(startNewActivity);      }      public void sendNightlife(View view) {          Intent startNewActivity = new Intent(this, Nightlife.class);          startActivity(startNewActivity);        }      public void sendResteraunt(View view) {          Intent startNewActivity = new Intent(this, Resteraunts.class);          startActivity(startNewActivity);      }    }    > main_activity.xml        <?xml version="1.0" encoding="utf-8"?>  <android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="match_parent"      android:layout_height="match_parent"      android:id="@+id/drawer_layout"      android:orientation="vertical">        <RelativeLayout          android:layout_width="match_parent"          android:layout_height="match_parent"          android:id="@+id/main_content"></RelativeLayout>        <ListView          android:layout_width="200dp"          android:layout_height="match_parent"          android:id="@+id/lv_sliding_menu"          android:background="#FFFFFF"          android:choiceMode="singleChoice"          android:layout_gravity="start"></ListView>  </android.support.v4.widget.DrawerLayout>    > Fragment1.xml        @Override      public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {          View rootView = inflater.inflate(R.layout.fragment1, container, false);          return rootView;      }    > fragment1.xml    <?xml version="1.0" encoding="utf-8"?>  <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:orientation="vertical" android:layout_width="match_parent"      android:layout_height="match_parent">        <TextView          android:layout_width="wrap_content"          android:layout_height="wrap_content"          android:text="Home"          android:textSize="30dp"          android:layout_gravity="center"/>  </LinearLayout>    

and here is my problem, i want the slidingmenu to show here also (a activity inside fragment1)

Hotels.java

         package com.gnirt69.slidingmenuexample.touring;    import android.content.Intent;  import android.os.Bundle;  import android.support.v4.app.FragmentActivity;  import android.view.View;    import com.gnirt69.slidingmenuexample.R;      /**   * Created by User on 12/14/2015.   */  public class Hotels extends FragmentActivity {              @Override      protected void onCreate(Bundle savedInstanceState) {          super.onCreate(savedInstanceState);          setContentView(R.layout.tourhotels);      }        public void sendHotels(View view) {          Intent startNewActivity = new Intent(this, Hotels.class);          startActivity(startNewActivity);      }      public void sendHotspots(View view) {          Intent startNewActivity = new Intent(this, Hotspots.class);          startActivity(startNewActivity);      }      public void sendLeisure(View view) {          Intent startNewActivity = new Intent(this, Leisure.class);          startActivity(startNewActivity);      }      public void sendNightlife(View view) {          Intent startNewActivity = new Intent(this, Nightlife.class);          startActivity(startNewActivity);        }      public void sendResteraunt(View view) {          Intent startNewActivity = new Intent(this, Resteraunts.class);          startActivity(startNewActivity);      }    }    

tourhotels.xml

       <LinearLayout          android:layout_height="fill_parent"          android:layout_width="fill_parent"          xmlns:android="http://schemas.android.com/apk/res/android" >          <HorizontalScrollView              android:layout_width="wrap_content"              android:layout_height="wrap_content">                <LinearLayout                  android:layout_width="wrap_content"                  android:layout_height="wrap_content"                  android:orientation="horizontal">                  <Button                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:text="@string/hotelss"                      android:id="@+id/Hotelio"                      android:onClick="sendHotels"/>                  <Button                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:text="@string/resteraunts"                      android:onClick="sendResteraunt"/>                  <Button                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:text="@string/hotspotss"                      android:onClick="sendHotspots"/>                  <Button                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:text="@string/leisure"                      android:onClick="sendLeisure"/>                  <Button                      android:layout_width="wrap_content"                      android:layout_height="wrap_content"                      android:text="@string/nightlife"                      android:onClick="sendNightlife"/>                </LinearLayout>          </HorizontalScrollView>      </LinearLayout>    

Could anyone help me? thanks !

ps. sorry again if the format of my post is wrong!

No comments:

Post a Comment