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>
...
Related
I decided to execute a Firebase Robo Test for my app and noticed that it always crashes after opening an external activity and then returning back to the app. I was able to duplicate the problem by enabling the "Don't keep activities" in the Developer options.
Crash: java.lang.ClassCastException: com.example.problemtesting.Fragments.Fragment_2 cannot be cast to androidx.navigation.fragment.NavHostFragment
It indicates that the problem is caused by this line of code: NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
How to duplicate the problem:
Enable "Don't keep activities"
Open the app and then the app drawer
Select "Fragment 2"
Press on the Share button
Press on the Messages button (or any other external app)
Return to the app (Crash)
The app works fine if "Don't keep activities" is disabled.
MainActivity
public class MainActivity extends AppCompatActivity implements NavigationView.OnNavigationItemSelectedListener {
DrawerLayout drawer;
NavigationView navigationView;
FragmentManager fragmentManager;
AppBarConfiguration mAppBarConfiguration;
NavController navController;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
fragmentManager = getSupportFragmentManager();
drawer = findViewById(R.id.drawer_layout);
navigationView = findViewById(R.id.nav_view);
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
mAppBarConfiguration = new AppBarConfiguration.Builder(
R.id.nav_fragment_1, R.id.nav_fragment_2)
.setOpenableLayout(drawer)
.build();
NavHostFragment navHostFragment = (NavHostFragment) getSupportFragmentManager().findFragmentById(R.id.nav_host_fragment);
if (navHostFragment != null)
navController = navHostFragment.getNavController();
else navController = Navigation.findNavController(this, R.id.nav_host_fragment);
NavigationUI.setupActionBarWithNavController(MainActivity.this, navController, mAppBarConfiguration);
NavigationUI.setupWithNavController(navigationView, navController);
setNavigationViewListener();
}
#Override
public boolean onSupportNavigateUp() {
return NavigationUI.navigateUp(navController, mAppBarConfiguration)
|| super.onSupportNavigateUp();
}
private void setNavigationViewListener() {
navigationView.setNavigationItemSelectedListener(MainActivity.this);
}
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.nav_fragment_1: {
fragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, new Fragment_1())
.commitNow();
MainActivity.this.setTitle("Fragment 1");
break;
}
case R.id.nav_fragment_2: {
fragmentManager.beginTransaction()
.replace(R.id.nav_host_fragment, new Fragment_2())
.commitNow();
MainActivity.this.setTitle("Fragment 2");
break;
}
}
drawer.closeDrawers();
return true;
}
}
Fragment 1
public class Fragment_1 extends Fragment{
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
container.removeAllViews();
View root = inflater.inflate(R.layout.fragment_1, container, false);
return root;
}
}
Fragment 2
public class Fragment_2 extends Fragment {
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
container.removeAllViews();
View root = inflater.inflate(R.layout.fragment_2, container, false);
Button share = root.findViewById(R.id.share);
share.setOnClickListener(view -> {
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.putExtra(Intent.EXTRA_SUBJECT, "Subject");
intent.putExtra(Intent.EXTRA_TEXT, "Message");
startActivity(Intent.createChooser(intent, "Title"));
});
return root;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start"
>
<androidx.fragment.app.FragmentContainerView
android:id="#+id/nav_host_fragment"
android:name="androidx.navigation.fragment.NavHostFragment"
android:tag="my_fragment"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"
app:defaultNavHost="true"
app:navGraph="#navigation/mobile_navigation"
/>
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_main"
app:menu="#menu/main_drawer"
/>
</androidx.drawerlayout.widget.DrawerLayout>
mobile_navigation.xml
<?xml version="1.0" encoding="utf-8"?>
<navigation 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"
app:startDestination="#+id/nav_fragment_1">
<fragment
android:id="#+id/nav_fragment_1"
android:name="com.example.problemtesting.Fragments.Fragment_1"
tools:layout="#layout/fragment_1"
/>
<fragment
android:id="#+id/nav_fragment_2"
android:name="com.example.problemtesting.Fragments.Fragment_2"
tools:layout="#layout/fragment_2"
/>
</navigation>
main_drawer.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:showIn="navigation_view">
<group android:checkableBehavior="single">
<item
android:id="#+id/nav_fragment_1"
android:title="Fragment 1" />
<item
android:id="#+id/nav_fragment_2"
android:title="Fragment 2" />
</group>
</menu>
AndroidManifest.xml
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.problemtesting">
<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="com.example.problemtesting.MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
It's a frustrating problem that I can't figure out. I have tried to remove the NavigationView.OnNavigationItemSelectedListener listener and it works fine, but I need that listener for other controls such as fragment switching animations and drawer layout transitions.
The problem is that your onNavigationItemSelected is doing a FragmentTransaction, replacing the entire NavHostFragment with a Fragment. This is always the wrong way to change fragments when using a NavHostFragment - you should always be navigating to a destination.
However, in your case, you are doing way more work than is actually necessary. Instead, you should:
Remove your OnNavigationItemSelectedListener code entirely. Navigation has already set one up for you when you call NavigationUI.setupWithNavController(navigationView, navController); That includes closing the drawer and using the correct cross fade animation as per the Material design spec.
As per the top app bar guide, you should not be manually setting the title of the acctivity, but instead add an android:label to each destination in your graph and the setupActionBarWithNavController call you've done will do that for you.
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
I'm using Fragments in my app. One of them has a listView, and when I do a setOnItemClickListener, I want to pass the item clicked to the other fragment...
Here's the class OngletCours where I do my Intent:
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
Intent a = new Intent(getActivity(), OngletNotes.class);
startActivity(a);
}
});
return rootView;
}
I'm getting the following error when I try to do an Intent to go to another Fragment:
E/AndroidRuntime: FATAL EXCEPTION: main
android.content.ActivityNotFoundException: Unable to find explicit activity class {com.example.dasilvadd.students/com.example.dasilvadd.students.OngletNotes}; have you declared this activity in your AndroidManifest.xml?
Here is my Manifest.xml
<?xml version="1.0" encoding="utf-8"?>
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="Student"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".PageAccueil">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".Inscription"
android:label="Student" />
<activity
android:name=".Onglets"
android:label="Student"
android:theme="#style/AppTheme.NoActionBar" />
<activity android:name=".Reglages" />
<activity android:name=".MotDePasse" />
<meta-data
android:name="com.google.android.gms.version"
android:value="#integer/google_play_services_version" />
</application>
I've already tried to add it by myself, but it doesnt recognize the class I created for the Second Fragment (OngletNotes).
Please tell me how to solve this. And thank you in advance !
If you start OngletNotes using startActivity, I assume it's an Activity. In that case you need to add it to the AndroidManifest.xml
<activity
android:name=".OngletNotes"
android:label="Notes" />
If OngletNotes is a Fragment, it should be put inside of the Activity. You can't launch standalone Fragment without an Activity.
You need to create an Activity (don't forget to put it in AndroidManifest.xml)
Put your Fragment inside of the Activity (in xml or programmatically)
Start the Activity using startActivity
# Update: OngletNotes is a fragment
Fragment example:
public static OngletNotes newInstance(/* input parameters if any */) {
OngletNotes fragment = new OngletNotes();
// put values which you want to pass to fragment
// Bundle args = new Bundle();
// fragment.setArguments(args);
return fragment;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_ongletNotes, container, false);
// if you have passed some data above
// Bundle args = getArguments();
return view;
}
============================================================
# Update:
First, your onCreate in MainActivity,
// create a new instance of OngletCours (example is shown above)
OngletCours OngletCoursFragment = OngletCours.newInstance();
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_holder, OngletCoursFragment )
.commit();
// and your OnItemClickListener
l1.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
OngletNotes targetFragment = OngletNotes.newInstance();
FragmentTransaction transaction = getActivity().getSupportFragmentManager().beginTransaction();
transaction.addToBackStack(null);
transaction.replace(R.id.fragment_holder, targetFragment)
.commit();
}
});
activity_main.xml: you shoud have a FrameLayout which is used as a container to switch fragments
<FrameLayout
android:id="#+id/fragment_holder"
android:layout_width="match_parent"
android:layout_height="match_parent" />
After readed your last comment I have edited my aswer:
Open the fragment:
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
((MainActivity)getActivity()).setCurrentItem (1, true);
}
});
In your MainActivity which contains ViewPager For example private ViewPager mViewPager; you need setCurrentItem method:
public void setCurrentItem (int item, boolean smoothScroll) {
mViewPager.setCurrentItem(item, smoothScroll);
}
I suppose that OngleNotes fragment is the second fragment. That's why I send a 1 as a parameter.
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);
I have trouble moving to another listview activity from current navigation drawer listview.
From MyActivity to OS. look at case 3 under public void selection().
MainActivity.java
public void SelectItem(int possition) {
Fragment fragment = null;
Bundle args = new Bundle();
switch (possition) {
case 0:
// Home
fragment = new FragmentOne();
args.putString(FragmentOne.ITEM_NAME, (String) getText(R.string.desc));
args.putInt(FragmentOne.IMAGE_RESOURCE_ID, dataList.get(possition).getImgResID());
break;
case 1:
fragment = new FragmentHome();
break;
case 2:
fragment = new FragmentThree();
break;
case 3:
**Intent menu = new Intent(this,OperatingSystem.class);
startActivity(menu);**
break;
case 4:
fragment = new fragmentFour();
break;
case 5:
// About
fragment = new FragmentFive();
break;
default:
break;
}
fragment.setArguments(args);
FragmentManager frgManager = getFragmentManager();
frgManager.beginTransaction().replace(R.id.content_frame, fragment)
.commit();
mDrawerList.setItemChecked(possition, true);
setTitle(dataList.get(possition).getItemName());
mDrawerLayout.closeDrawer(mDrawerList);
}
OperatingSystem.java
package com.example.faez.bodyalarm;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class OperatingSystem extends ListActivity {
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
String[] values = new String[] { "Android", "iPhone", "WindowsMobile",
"Blackberry", "WebOS", "Ubuntu", "Windows7", "Max OS X",
"Linux", "OS/2" };
// use your custom layout
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.listview, R.id.mainListView, values);
setListAdapter(adapter);
setContentView(R.layout.listview);
}
#Override
protected void onListItemClick(ListView l, View v, int position, long id) {
String item = (String) getListAdapter().getItem(position);
Toast.makeText(this, item + " selected", Toast.LENGTH_LONG).show();
}
}
AndroidManifest
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.faez.bodyalarm" >
<application
android:debuggable="true"
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity android:name=".MainSplashScreen">
</activity>
<activity
android:name=".MyActivity"
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=".OperatingSystem"></activity>
</application>
</manifest>
Really appreciate if you guys can help. no problem during build it just stop when debug on phone.
in case 3 you should add return; after startActivity(menu); because that after the switch case you have some more code that will crash if you continue since your "fragment" var will stay null.
Try checking your OperatingSystem.java. I think setContentView() is not neccessary in ListActivity.
Also for the ArrayAdapter, you may want to put a layout containing a TextView and its corresponding ID.
Example
ArrayAdapter<String> adapter = new ArrayAdapter<>(this,
R.layout.row_view, android.R.id.text1, values);
row_view.xml
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/text1" />