Note that there are some explanatory texts on larger screens.

plurals
  1. POLaunch a activity with button click using a FragmentActivity
    primarykey
    data
    text
    <p>Hey guys I need help I'm making a launcher that needs to have a button that when clicked it opens the AllApps.class activity witch shows a list of all installed apps </p> <p>The other button needs to open a installed app that is on the device</p> <p>and the last button needs to open the default web browser and open google.com</p> <p>Here's my code!</p> <p>Home.java: </p> <pre><code>package com.dva.schooltoolshome; import java.util.Locale; import android.app.ActionBar; import android.app.FragmentTransaction; import android.content.Intent; import android.net.Uri; import android.os.Bundle; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; import android.support.v4.view.ViewPager; import android.view.LayoutInflater; import android.view.Menu; import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; import android.widget.ImageButton; import android.widget.TextView; public class Home extends FragmentActivity implements ActionBar.TabListener { SectionsPagerAdapter mSectionsPagerAdapter; ViewPager mViewPager; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_home); final ActionBar actionBar = getActionBar(); actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS); actionBar.setDisplayShowHomeEnabled(false); actionBar.setDisplayOptions(actionBar.getDisplayOptions() ^ ActionBar.DISPLAY_SHOW_TITLE); mSectionsPagerAdapter = new SectionsPagerAdapter( getSupportFragmentManager()); mViewPager = (ViewPager) findViewById(R.id.pager); mViewPager.setAdapter(mSectionsPagerAdapter); mViewPager .setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() { @Override public void onPageSelected(int position) { actionBar.setSelectedNavigationItem(position); } }); for (int i = 0; i &lt; mSectionsPagerAdapter.getCount(); i++) { if (mSectionsPagerAdapter.getPageTitle(i).toString() .equalsIgnoreCase("calc")) { actionBar.addTab(actionBar.newTab() .setIcon(R.drawable.calendar) .setTabListener(this)); } else if (mSectionsPagerAdapter.getPageTitle(i).toString() .equalsIgnoreCase("home")) { actionBar.addTab(actionBar.newTab().setIcon(R.drawable.home) .setTabListener(this)); } else if (mSectionsPagerAdapter.getPageTitle(i).toString() .equalsIgnoreCase("drive")) { actionBar.addTab(actionBar.newTab().setIcon(R.drawable.folder) .setTabListener(this)); } } mViewPager.setCurrentItem(1); } @Override public boolean onCreateOptionsMenu(Menu menu) { getMenuInflater().inflate(R.menu.home, menu); return true; } @Override public void onTabSelected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { mViewPager.setCurrentItem(tab.getPosition()); } @Override public boolean onOptionsItemSelected(MenuItem item) { switch (item.getItemId()) { case R.id.action_settings: startActivity(new Intent(android.provider.Settings.ACTION_APPLICATION_SETTINGS)); return true; default: return super.onOptionsItemSelected(item); } } @Override public void onTabUnselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } @Override public void onTabReselected(ActionBar.Tab tab, FragmentTransaction fragmentTransaction) { } public class SectionsPagerAdapter extends FragmentPagerAdapter { public SectionsPagerAdapter(FragmentManager fm) { super(fm); } @Override public Fragment getItem(int position) { Fragment fragment = null; switch (position) { case 0: fragment = new CalcFragment(); break; case 1: fragment = new HomeFragment(); break; case 2: fragment = new DriveFragment(); break; } return fragment; } @Override public int getCount() { return 3; } @Override public CharSequence getPageTitle(int position) { Locale l = Locale.getDefault(); switch (position) { case 0: return getString(R.string.title_section1).toUpperCase(l); case 1: return getString(R.string.title_section2).toUpperCase(l); case 2: return getString(R.string.title_section3).toUpperCase(l); } return null; } } public static class HomeFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; public HomeFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_homebar, container, false); return rootView; } } public static class CalcFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; public CalcFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.activity_calculator, container, false); return rootView; } } public static class DriveFragment extends Fragment { public static final String ARG_SECTION_NUMBER = "section_number"; public DriveFragment() { } @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { View rootView = inflater.inflate(R.layout.fragment_home_dummy, container, false); TextView dummyTextView = (TextView) rootView .findViewById(R.id.section_label); dummyTextView.setText("DRIVE"); return rootView; } } } </code></pre> <p>activity_homebar</p> <pre><code>&lt;RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:background="@drawable/bg" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context=".HomeBar" &gt; &lt;ImageButton android:id="@+id/apps" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_alignParentRight="true" android:background="@drawable/appdrawer" android:src="@drawable/appdrawer" /&gt; &lt;ImageButton android:id="@+id/wbrowser" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentLeft="true" android:layout_alignTop="@+id/apps" android:src="@drawable/browser" /&gt; &lt;/RelativeLayout&gt; </code></pre> <p>AllApps.java:</p> <pre><code>package com.dva.schooltoolshome; import android.app.LauncherActivity; import android.content.Intent; public class AllApps extends LauncherActivity { @Override protected Intent getTargetIntent () { // just a example, you should replace the method yourself Intent intent = new Intent(); intent.setAction(Intent.ACTION_MAIN); intent.addCategory(Intent.CATEGORY_LAUNCHER); intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); return intent; } } </code></pre> <p>I'm am still learning so please don't judge </p> <p>Thanks in advance</p> <p>Regards </p> <p>Rapsong11</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload