Missing Navigation Drawer hamburger icon in Activity - java

I built a Navigation Drawer with fragments and an Activity. All the fragments have the icon and drawer access is smooth as butter, but there is nothing in the Activity. The Activity is the default "Home Page", so access to the Navigation Drawer is critical. Typically not calling toggle.syncState(); is the solution, but it fails in this case.
MainActivity:
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.setDrawerIndicatorEnabled(true);
toggle.syncState();
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//Sets default fragment
Intent i = new Intent(MainActivity.this,GarageActivity.class);
startActivity(i);
navigationView.setCheckedItem(R.id.nav_garage);
}
//Name in Action bar
public void setActionBarTitle(String title) {
getSupportActionBar().setTitle(title);
}
#Override
public void onBackPressed() {
DrawerLayout drawer = findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
Activity in question:
public class GarageActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_garage);
getSupportActionBar().setTitle("My Garage");
}
}
Manifest:
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".GarageActivity">
<meta-data
android:name="android.support.parent_activity"
android:value=".MainActivity"/>
</activity>
</application>

First you need to enable actionbar's Home button. Then Assign Hamburger icon to Home button and write code to open the drawer in his listener. following are the steps:
Get hamburger/menu icon:
In the Project window, right-click the res folder and select New > Vector Asset.
Select Material icon as the asset type and then click the Icon button to open the Select Icon window.
Search for "menu" and select the menu icon (the icon is 3 horizontal lines).
Click OK, and then rename the file to "ic_menu" and click Next to import it.
Enable "Home" button in actionbar:
add this code in your onCreate method:-
ActionBar actionbar = getSupportActionBar();
actionbar.setDisplayHomeAsUpEnabled(true);
actionbar.setHomeAsUpIndicator(R.drawable.ic_menu);
Add code in onOptionsItemSelected method:
first create global variable of DrawerLayout so you can access it in other methods. add reference to that variable in onCreate and use it in onOptionsItemSelected to open the drawer. following is the code:
public class MainActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = findViewById(R.id.drawer_layout);
...
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case android.R.id.home:
mDrawerLayout.openDrawer(GravityCompat.START);
return true;
}
return super.onOptionsItemSelected(item);
}
}
source Create Navigation Drawer

Related

How to sync with time in custom calendar that I have made.? Like when I open my calendar it should show the current date and month

Main Activity ScreenI have made a custom calendar with textview, So I want the calendar to show current date and month when I open the calendar...
`My calendar should open directly in the current month and show the current date, just like google calendar.
<TextView
android:textColor="#color/Blue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:fontFamily="#font/caviardreams"
android:gravity="top|center"
android:text="1"
android:textSize="11sp"
android:textStyle="bold"
android:maxWidth="1dp"
android:maxHeight="1dp"/>
<TextView
android:textColor="#color/Blue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:fontFamily="#font/caviardreams"
android:gravity="top|center"
android:text="2"
android:textSize="11sp"
android:textStyle="bold"
android:maxWidth="1dp"
android:maxHeight="1dp"/>
<TextView
android:textColor="#color/Blue"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:fontFamily="#font/caviardreams"
android:gravity="top|center"
android:text="3"
android:textSize="11sp"
android:textStyle="bold"
android:maxWidth="1dp"
android:maxHeight="1dp"/>
`
I expect the current date to be highlighted in the current month.
This is the java code of MainActivity
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
ViewPager viewPager;
EditText txt;
Button btn;
String str;
AdView adView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
viewPager = findViewById(R.id.view_pager);
viewPager.setAdapter(new MyPagerAdapter(getSupportFragmentManager()));
//admob app id
MobileAds.initialize(this,"ca-app-pub-6376378742627184~5911477780");
adView = (AdView)findViewById(R.id.adView);
AdRequest adRequest = new AdRequest.Builder().addTestDevice(AdRequest.DEVICE_ID_EMULATOR).build();
adView.loadAd(adRequest);
}
public void date(View view) {
}
private class MyPagerAdapter extends FragmentStatePagerAdapter {
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int pos) {
switch(pos) {
case 0: return Frag1.newInstance("Frag1, Instance 1");
case 1: return Frag2.newInstance("Frag2, Instance 2");
case 2: return Frag3.newInstance("Frag3, Instance 3");
case 3: return Frag4.newInstance("Frag4, Instance 4");
case 4: return Frag5.newInstance("Frag5, Instance 5");
case 5: return Frag6.newInstance("Frag6, Instance 6");
case 6: return Frag7.newInstance("Frag7, Instance 7");
case 7: return Frag8.newInstance("Frag8, Instance 8");
case 8: return Frag9.newInstance("Frag9, Instance 9");
case 9: return Frag10.newInstance("Frag10, Instance 10");
case 10: return Frag11.newInstance("Frag11, Instance 11");
case 11: return Frag12.newInstance("Frag12, Instance 12");
default: return Frag1.newInstance("Frag1, Default");
}
}
#Override
public int getCount() {
return 12;
}
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
finish();
System.exit(0);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
finish();
System.exit(0);
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
ApplicationInfo app = getApplicationContext().getApplicationInfo();
String filePath = app.sourceDir;
int id = item.getItemId();
if (id == R.id.home)
{
// Handle the camera action
Intent a = new Intent(MainActivity.this,MainActivity.class);
startActivity(a);
}
else if (id == R.id.about)
{
Intent b = new Intent(MainActivity.this,About.class);
startActivity(b);
}
else if (id == R.id.nav_share)
{
Intent c = new Intent(Intent.ACTION_SEND);
c.setType("*/*");
String shareBody = "https://play.google.com/store/apps/details?id=com.jangphong.hem.karbicalender2";
String shareSub = "Download Karbi calendar 2019";
c.putExtra(Intent.EXTRA_SUBJECT,shareSub);
c.putExtra(Intent.EXTRA_TEXT,shareBody);
c.putExtra(Intent.EXTRA_STREAM,Uri.fromFile(new File(filePath)));
startActivity(Intent.createChooser(c,"Share using"));
}
else if (id == R.id.feedback)
{
Intent in = new Intent(Intent.ACTION_VIEW, Uri.parse("http://www.karbidigitalresources.tk"));
startActivity(in);
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
This is the Manifest file
<?xml version="1.0" encoding="utf-8"?>
<manifest
xmlns:android="http://schemas.android.com/apk/res/android"
package="com.jangphong.hem.karbicalender2">
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_karbi_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_karbi_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".MainActivity"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar" />
<activity android:name=".About" />
<activity
android:name=".SplashScreen"
android:noHistory="true"
android:theme="#style/AppTheme.Splash">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".edit_text" />
<activity android:name=".feb" />
<activity android:name=".march" />
<activity android:name=".april" />
<activity android:name=".may" />
<activity android:name=".june" />
<activity android:name=".july" />
<activity android:name=".august" />
<activity android:name=".sep" />
<activity android:name=".oct" />
<activity android:name=".nov" />
<activity android:name=".dec"></activity>
</application>

Opening a fragment via quick app shortcut

Hello, I do know that this is a duplicate question, but i was not able to find the solution from the other threads.
Here is my problem,
I have an app with bottom navigation bar with five fragment. I want to open the those fragment via the app shortcut.
Here what i have created,
shortcut.xml
<shortcuts xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="first"
android:enabled="true"
android:icon="#drawable/shortcut_1"
android:shortcutShortLabel="#string/shortcut_short_label_one"
android:shortcutLongLabel="#string/shortcut_long_label_one"
tools:targetApi="n_mr1">
<intent
android:action="com.xxxxxxx.xxxx.FIRST"
android:targetPackage="com.xxxxxxx.xxxx.test"
android:targetClass="com.xxxxxxx.xxxx.test.MainActivity" />
</shortcut>
<!-- Specify more shortcuts here. -->
<shortcut
android:shortcutId="second"
android:enabled="true"
android:icon="#drawable/shortcut_2"
android:shortcutShortLabel="#string/shortcut_short_label_two"
android:shortcutLongLabel="#string/shortcut_long_label_two"
tools:targetApi="n_mr1">
<intent
android:action="com.xxxxxxx.SECOND"
android:targetPackage="com.xxxxxxx.xxxx.test"
android:targetClass="com.xxxxxxx.xxxx.test.SecondFragment" />
</shortcut>
</shortcut>
Here is the onCreate() from the MainActivity
private static final String Second = "com.xxxxxxx.xxxx.SECOND";
//code vomited
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (Second.equals(getIntent().getAction())){
SecondFragment secondFragment = new SecondFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionTwo = getSupportFragmentManager().beginTransaction();
fragmentTransactionTwo.replace(R.id.content_main, secondFragment, "Fragment two");
fragmentTransactionTwo.commit();
}
BottomNavigationView navigation = findViewById(R.id.navigation);
BottomNavigationViewHelper.disableShiftMode(navigation); //disabling the shitty shifting mode
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
FirstFragment firstFragment = new FirstFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
fragmentTransactionOne.commit();
}
Here the first activty launches fine i.e MainActivity(). But Click on the second shortcut nothing happens.
Anyone explain me what am i doing wrong?
I see a couple of things that don't look right:
On shortcut.xml:
Both shortcuts should target the 'MainActivity' class, as this is the point of entry and the responsible for adding the fragments to the ui. So, you need to change the targetClass on the second shortcut and target the 'MainActivity' instead.
On the onCreate method:
When the action corresponds to the second shortcut, it sets the second fragment on the screen but the method goes on and sets the first fragment over it.
I guess it should be an if-else:
if (Second.equals(getIntent().getAction())) {
SecondFragment secondFragment = new SecondFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionTwo = getSupportFragmentManager().beginTransaction();
fragmentTransactionTwo.replace(R.id.content_main, secondFragment, "fragment two");
fragmentTransactionTwo.commit();
} else {
FirstFragment firstFragment = new FirstFragment();
android.support.v4.app.FragmentTransaction fragmentTransactionOne = getSupportFragmentManager().beginTransaction();
fragmentTransactionOne.replace(R.id.content_main, firstFragment, "Fragment one");
fragmentTransactionOne.commit();
}
i have same problem like yours, i found how to move to fragment. you should hit the action. put this in onCreate, maybe this will help you
if ("com.xxxxxxx.YourShortcutAction".equals(getIntent().getAction())) {
val fragment = YourFragment()
addFragment(fragment)
}
im using kotlin for android
After some time I found the answer to how to open the fragment via the shortcut Kotlin:
shortcut.xml
<shortcuts xmlns:android="http://schemas.android.com/apk/res/android">
<shortcut
android:shortcutId="mainShortcut"
android:enabled="true"
android:shortcutShortLabel="#string/somestring">
<intent
android:action="xxx.example.workapp.test"
android:targetPackage="xxx.example.workapp"
android:targetClass="xxx.example.workapp.MainActivity">
</intent>
</shortcut>
MainActivity
class MainActivity : AppCompatActivity() {
private lateinit var bottomNav : BottomNavigationView
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
loadFragment(HomeFragment())
if ("xxx.example.workapp.test" == intent.action) {
loadFragment(YourFragment())
}
bottomNav = findViewById(R.id.bottomNav)
bottomNav.setOnItemSelectedListener {
when (it.itemId) {
R.id.homeFragment -> {
loadFragment(HomeFragment())
true
}
R.id.SettingsFragment -> {
loadFragment(SettingsFragment())
true
}
else -> {
false
}
}
}
}
private fun loadFragment(fragment: Fragment){
val transaction = supportFragmentManager.beginTransaction()
transaction.replace(R.id.container,fragment)
transaction.addToBackStack(null)
transaction.commit()
}}
AndroidManifest.xml
...
<activity
android:name=".MainActivity"
android:configChanges="locale"
android:exported="true"
android:screenOrientation="portrait" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<meta-data
android:name="android.app.shortcuts"
android:resource="#xml/shortcuts" />
</activity>
...

Map Class's onCreate() and onMapReady() functions never ran

I've created a basic Android app that is intended to have a map with a hamburger drawer on-top of that. Each hamburger menu option will run object methods on the GoogleMap object.
The issue is that while the hamburger drawer is created and works, the map's fragment is placed in the correct place, the onCreate and subsequently onMapReady functions in MainActivity are not running meaning the GoogleMap object is never made. I added some print statements to these functions to make sure and they are never printed.
I've got two classes, HamburgerDrawer.java and MainActivity.java.
HamburgerDrawer.java:
package com.vanleusen.brighthelp;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.NavigationView;
import android.support.design.widget.Snackbar;
import android.support.v4.view.GravityCompat;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import com.google.android.gms.common.api.GoogleApiClient;
public class HamburgerDrawer extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener {
/**
* ATTENTION: This was auto-generated to implement the App Indexing API.
* See https://g.co/AppIndexing/AndroidStudio for more information.
*/
private GoogleApiClient client;
#Override
protected void onCreate(Bundle savedInstanceState) {
//testing
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_hamburger_drawer);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.setDrawerListener(toggle);
toggle.syncState();
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
/*FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
transaction.add(R.id.frame_container, new MyFragment());
transaction.commit();*/
//MainActivity map = new MainActivity();
}
#Override
public void onBackPressed() {
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
if (drawer.isDrawerOpen(GravityCompat.START)) {
drawer.closeDrawer(GravityCompat.START);
} else {
super.onBackPressed();
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.hamburger_drawer, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
#SuppressWarnings("StatementWithEmptyBody")
#Override
public boolean onNavigationItemSelected(MenuItem item) {
// Handle navigation view item clicks here.
int id = item.getItemId();
if (id == R.id.nav_roadmap) {
System.out.println("Roadmap");
//mapData.mapTypeInterface(1);
} else if (id == R.id.nav_satellite) {
System.out.println("Satellite");
//mapData.mapTypeInterface(2);
} else if (id == R.id.nav_hybrid) {
System.out.println("Hybrid");
//mapData.mapTypeInterface(3);
} else if (id == R.id.nav_terrain) {
System.out.println("Terrain");
//mapData.mapTypeInterface(4);
} else if (id == R.id.nav_problems) {
System.out.println("Report Problems");
} else if (id == R.id.nav_contact) {
System.out.println("Contact Us");
} else if (id == R.id.nav_about) {
System.out.println("About");
}
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
drawer.closeDrawer(GravityCompat.START);
return true;
}
}
MainActivity.java:
package com.vanleusen.brighthelp;
/**
* Created by Oscar on 22/10/2016.
*/
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import com.google.android.gms.maps.CameraUpdateFactory;
import com.google.android.gms.maps.GoogleMap;
import com.google.android.gms.maps.MapFragment;
import com.google.android.gms.maps.OnMapReadyCallback;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.MarkerOptions;
public class MainActivity extends Activity implements OnMapReadyCallback {
private GoogleMap mMap;
public static MapFragment mapFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.content_hamburger_drawer);
mapFragment = (MapFragment) getFragmentManager().findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
System.out.println("Ran onCreate method");
}
#Override
public void onMapReady(GoogleMap googleMap) {
System.out.println("Ran onMapReady method");
mMap = googleMap;
// Add a marker in Sydney and move the camera
LatLng sydney = new LatLng(-34, 151);
mMap.addMarker(new MarkerOptions().position(sydney).title("Marker in Sydney"));
mMap.moveCamera(CameraUpdateFactory.newLatLng(sydney));
}
}
I have multiple layout xml files to implement the hamburger menu, although the relevant one which contains the content for the drawer is here:
content_hamburger_drawer.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/content_hamburger_drawer"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="com.vanleusen.brighthelp.HamburgerDrawer"
tools:showIn="#layout/app_bar_hamburger_drawer">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<fragment
android:id="#+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
<view
android:id="#+id/myview"
android:layout_width="match_parent"
android:layout_height="fill_parent"
class="android.view.View" />
</FrameLayout>
</RelativeLayout>
And finally I've heard this may be connected to my AndroidManifest although I tried some corrections nothing seemed to work for me, I included this below:
AndroidManifest.xml:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.vanleusen.brighthelp">
<!--
The ACCESS_COARSE/FINE_LOCATION permissions are not required to use
Google Maps Android API v2, but you must specify either coarse or fine
location permissions for the 'MyLocation' functionality.
-->
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
<uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity
android:name=".HamburgerDrawer"
android:label="#string/app_name"
android:theme="#style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
launchMode ensures intents don't create multiple instances of it
<activity
android:name=".MainActivity"
android:label="Map"
android:theme="#style/AppTheme.NoActionBar"
android:launchMode="singleTask" >
</activity>
<!--
The API key for Google Maps-based APIs is defined as a string resource.
(See the file "res/values/google_maps_api.xml").
Note that the API key is linked to the encryption key used to sign the APK.
You need a different API key for each encryption key, including the release key that is used to
sign the APK for publishing.
You can define the keys for the debug and release targets in src/debug/ and src/release/.
-->
<meta-data
android:name="com.google.android.geo.API_KEY"
android:value="#string/google_maps_key" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
</manifest>
I'd highly appreciate it if someone could help me work out why my map's onCreate and onMapReady functions are not being executed.
I think you are not calling MainActivity class from anywhere that's why this problem is coming.Please call it from HamburgerDrawer class in onCreate method or any clicklistener.
Intent a=new Intent(getApplicationContext,MainActivity.class);
startActivity(a);

Select Item on Spinner

I want to ask for help.
I already created a list of activities in Android Studio.
Each Activity contains a specific item. Those items are different "Departments" in our University.
Example:
Activity 1 = College of Computer Studies;
Activity 2 = College of Teacher Education;
Activity 3 = College of Engineering.
My Spinner contains the departments.
My problem is, if I choose "College in Computer Science" in Spinner and click the "SEND" button, I want Activity 1 to be shown.
Would you help me with the code to do that?
**Spinner in activity_main.xml
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spin"
android:entries="#array/punpDepartments"
android:layout_gravity="center_vertical">
</Spinner>
**These are the items in strings.xml
<string-array name="punpDepartments">
<item>College of Computer Studies</item>
<item>College of Business Education</item>
<item>College of Criminal Justice Education</item>
<item>College of Marine Education</item>
<item>College of Nursing</item>
<item>College of Pharmacy</item>
<item>College of Education</item>
**My code in Intent at MainActivity.java
public class MainActivity extends ActionBarActivity {
private static Button button_send;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
OnClickButtonListener();
}
public void OnClickButtonListener(){
button_send = (Button) findViewById(R.id.button);
button_send.setOnClickListener(
new View.OnClickListener(){
#Override
public void onClick(View v){
Intent intent = new Intent("com.example.imelda.mythesis.ListActivity");
startActivity(intent);
}
}
);
}
**Extends in ListActivity.java
public class ListActivity extends ActionBarActivity {
private static Button button_next;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_list);
OnClickButtonListener();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_list, menu);
return true;
}
public void OnClickButtonListener() {
button_next = (Button) findViewById(R.id.button4);
button_next.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent("com.example.imelda.mythesis.SecondList");
startActivity(intent);
}
}
);
}
**I have also added this in AndroidManifest.xml enter code here
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.imelda.mythesis" >
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
<activity
android:name=".MainActivity"
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=".ListActivity"
android:label="#string/title_activity_list" >
<intent-filter>
<action android:name="com.example.imelda.mythesis.ListActivity" />
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
first get your spinner in the main class like this
Spinner spin =(Spinner)findViewById(R.id.spin);
now in your on click method do this
int position =spin.getSelectedItemPosition();
//change the activities with yours
switch (position){
case 0: intent.setClass(Activity1.class,MainActivity.this);
break;
case 1: intent.setClass(Activity2.class,MainActivity.this);
break;
case 2: intent.setClass(Activity3.class,MainActivity.this);
break;
case 3: intent.setClass(Activity4.class,MainActivity.this);
break;
case 4: intent.setClass(Activity5.class,MainActivity.this);
break;
}
startActivity(intent);

Android, Navigation Drawer - how to show IC_lancher

I have created in Eclipse a Android default project with navigation drawer.
But my App Icon is not showing on the Action bar. I want to use the min API 9.
I also using appcombat V7 for support library.
Main Activity :
public class MainActivity extends ActionBarActivity implements
NavigationDrawerFragment.NavigationDrawerCallbacks {
/**
* Fragment managing the behaviors, interactions and presentation of the
* navigation drawer.
*/
private NavigationDrawerFragment mNavigationDrawerFragment;
/**
* Used to store the last screen title. For use in
* {#link #restoreActionBar()}.
*/
private CharSequence mTitle;
ActionBar actionBar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Initiate
actionBar = getSupportActionBar();
mNavigationDrawerFragment = (NavigationDrawerFragment) getSupportFragmentManager()
.findFragmentById(R.id.navigation_drawer);
mTitle = getTitle();
// Set up the drawer.
mNavigationDrawerFragment.setUp(R.id.navigation_drawer,
(DrawerLayout) findViewById(R.id.drawer_layout));
}
#Override
public void onNavigationDrawerItemSelected(int position) {
// update the main content by replacing fragments
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager
.beginTransaction()
.replace(R.id.container,
Fragment1.newInstance(position + 1)).commit();
}
public void onSectionAttached(int number) {
switch (number) {
case 1:
mTitle = getString(R.string.title_section1);
break;
case 2:
mTitle = getString(R.string.title_section2);
break;
case 3:
mTitle = getString(R.string.title_section3);
break;
}
}
public void restoreActionBar() {
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
actionBar.setDisplayShowTitleEnabled(true);
actionBar.setTitle(mTitle);
// enable ActionBar app icon to behave as action to toggle nav drawer
actionBar.setDisplayHomeAsUpEnabled(true);
actionBar.setHomeButtonEnabled(true);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
if (!mNavigationDrawerFragment.isDrawerOpen()) {
// Only show items in the action bar relevant to this screen
// if the drawer is not showing. Otherwise, let the drawer
// decide what to show in the action bar.
getMenuInflater().inflate(R.menu.main, menu);
restoreActionBar();
return true;
}
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
Manifest:
<uses-sdk
android:minSdkVersion="8"
android:targetSdkVersion="21" />
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name=".SplashScreen"
android:label="#string/title_activity_splash_screen" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".MainActivity"
android:label="#string/app_name" >
</activity>
my Style :
<resources>
<!--
Base application theme, dependent on API level. This theme is replaced
by AppBaseTheme from res/values-vXX/styles.xml on newer devices.
-->
<style name="AppBaseTheme" parent="Theme.AppCompat.Light">
<!--
Theme customizations available in newer API levels can go in
res/values-vXX/styles.xml, while customizations related to
backward-compatibility can go here.
-->
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
</style>
</resources>
Thanks

Categories