MenuItem intent - java

Good day, I am having a problem with creating an intent with a menuitem to navigate to another layout, my code is below.
I made a button that when clicked it should display the available options reading from the String.xml
This below is my code that has the layouts from the String.xml
/**********************************************************/
public class IndexActivity extends AppCompatActivity {
private String[] mNavigationDrawerItemTitles;
private DrawerLayout mDrawerLayout;
private ListView mDrawerList;
Toolbar toolbar;
private CharSequence mDrawerTitle;
private CharSequence mTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_index);
ImageView homeImageView = (ImageView) findViewById(R.id.homeImageView);
homeImageView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Initializing the popup menu and giving the reference as current context
PopupMenu popupMenu = new PopupMenu(IndexActivity.this, homeImageView);
// Inflating popup menu from popup_menu.xml file
popupMenu.getMenuInflater().inflate(R.menu.popup_menu, popupMenu.getMenu());
popupMenu.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
// Toast message on menu item clicked
Toast.makeText(IndexActivity.this, "You Clicked " + menuItem.getTitle(), Toast.LENGTH_SHORT).show();
// Intent x = new Intent(IndexActivity.this, menuItem.getClass());
return true;
}
});
// Showing the popup menu
popupMenu.show();
}
});
}
}

Related

set tabLayout title from json data android

i want to parse json data title into my tabLayout. i have two fragment for that, and now i want to get only title from my json data. currently i set manual tab title in MainActivity.java file. how i can parse json data into my tabLayout?
here is my MainActivity.java code:
public class MainActivity extends AppCompatActivity implements Home.OnFragmentInteractionListener,
Politics.OnFragmentInteractionListener {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
Toolbar toolbar;
Dialog Developer;
TextView txtclose;
TextView facebook;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xFF0F0E37);
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Home(),"Bangla");
viewPagerAdapter.addFragments(new Politics(),"Politics");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
Developer = new Dialog(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.about:
Developer.setContentView(R.layout.about);
txtclose =(TextView) Developer.findViewById(R.id.txtclose);
facebook = (TextView) Developer.findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView facebook = (TextView) findViewById(R.id.facebook);
Intent openFB = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://facebook.com/test/"));
startActivity(openFB);
}
});
txtclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Developer.dismiss();
}
});
Developer.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Developer.show();
break;
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
String shareTitile ="test";
String shareLink = "https://play.google.com/store/apps/details?id=test";
intent.putExtra(Intent.EXTRA_SUBJECT, shareTitile);
intent.putExtra(Intent.EXTRA_TEXT, shareLink);
startActivity(Intent.createChooser(intent, "Share test"));
break;
default:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}
here is my ViewPagerAdapter.java code:
public class ViewPagerAdapter extends FragmentPagerAdapter {
ArrayList<Fragment> fragments = (new ArrayList<>());
ArrayList<String> tabTitles = (new ArrayList<>());
public void addFragments(Fragment fragments, String titles){
this.fragments.add(fragments);
this.tabTitles.add(titles);
}
public ViewPagerAdapter(FragmentManager fm){
super(fm);
}
#Override
public Fragment getItem(int possition) {
return fragments.get(possition);
}
#Override
public int getCount() {
return fragments.size();
}
#Nullable
#Override
public CharSequence getPageTitle(int position) {
return tabTitles.get(position);
}
}
spouse, my json string is name. i want to parse that name into viewPagerAdapter.addFragments(new Home(),"Bangla"); and viewPagerAdapter.addFragments(new Politics(),"Politics");. here i want to replace Bangla to retrieve json data name and also replace Politics automatically from name. and i want to keep my 2 different fragments, because of - i will parse different data into both fragments.
i'm parsing josn from url as like https://website.com/api/details.php
here is my json response:
[
{"cid":"1","name":"Bangle"},
{"cid":"2","name":"Politics"},
{"cid":"3","name":"International"}
]
if you not understand my question, just let me know.
note: for better understanding, you can edit my code and post your answer!
thanks in advance.
public class MainActivity extends AppCompatActivity implements Home.OnFragmentInteractionListener,
Politics.OnFragmentInteractionListener {
TabLayout tabLayout;
ViewPager viewPager;
ViewPagerAdapter viewPagerAdapter;
Toolbar toolbar;
Dialog Developer;
TextView txtclose;
TextView facebook;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setTitleTextColor(0xFF0F0E37);
String jsonStr = ""; // this variable should contain the JSON response from url, assing json response to this variable.
JSONArray jsonarray = new JSONArray(jsonStr);
for (int i = 0; i < jsonarray.length(); i++) {
JSONObject jsonobject = jsonarray.getJSONObject(i);
String name = jsonobject.getString("name");
// If you have only two fragments then simply take a count variable other wise you can save this "name" in string array.
if(i==0){
viewPagerAdapter.addFragments(new Home(),name);
}else if(i == 1){
viewPagerAdapter.addFragments(new Politics(),name);
}
}
tabLayout = (TabLayout)findViewById(R.id.tabLayout);
viewPager = (ViewPager)findViewById(R.id.viewPager);
viewPagerAdapter = new ViewPagerAdapter(getSupportFragmentManager());
viewPagerAdapter.addFragments(new Home(),"Bangla");
viewPagerAdapter.addFragments(new Politics(),"Politics");
viewPager.setAdapter(viewPagerAdapter);
tabLayout.setupWithViewPager(viewPager);
Developer = new Dialog(this);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater menuInflater = getMenuInflater();
menuInflater.inflate(R.menu.home, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()){
case R.id.about:
Developer.setContentView(R.layout.about);
txtclose =(TextView) Developer.findViewById(R.id.txtclose);
facebook = (TextView) Developer.findViewById(R.id.facebook);
facebook.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
TextView facebook = (TextView) findViewById(R.id.facebook);
Intent openFB = new Intent(Intent.ACTION_VIEW,
Uri.parse("https://facebook.com/test/"));
startActivity(openFB);
}
});
txtclose.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Developer.dismiss();
}
});
Developer.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
Developer.show();
break;
case R.id.share:
Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType("text/plain");
intent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_WHEN_TASK_RESET);
String shareTitile ="test";
String shareLink = "https://play.google.com/store/apps/details?id=test";
intent.putExtra(Intent.EXTRA_SUBJECT, shareTitile);
intent.putExtra(Intent.EXTRA_TEXT, shareLink);
startActivity(Intent.createChooser(intent, "Share test"));
break;
default:
}
return super.onOptionsItemSelected(item);
}
#Override
public void onFragmentInteraction(Uri uri) {
}
}

How to changing menu item text?

When a user hit the "register" menu item it come to the register activity and after hit the button to return to the MainActivity with the menu item from "register" to "unregister" I don't know how would I go about changing the menu item text.
public class register extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_register);
Button register = findViewById(R.id.reg);
EditText name =findViewById(R.id.name);
EditText admin = findViewById(R.id.admin);
EditText email = findViewById(R.id.email);
register.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
startActivity(new Intent(register.this,MainActivity.class));
}
});
}
You can do it with onPrepareOptionsMenu:
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
MenuItem btn = menu.findItem(R.id.register_menu_item);
if (/* here check if user is registered or not */) {
btn.setTitle("Unregister");
} else {
btn.setTitle("Register");
}
return super.onPrepareOptionsMenu(menu);
}

Load local HTTP files in webview from drawer menu?

I am trying to create an android app witch loads local HTML pages in webview, I managed to make that part work, but now I have a drawer menu with some strings and when I click one of them I want to open a different HTML page, I am stuck on this part so I'm asking for some guidelines.
This is my code in MainActivity.java
public class MainActivity extends ActionBarActivity {
private WebView mWebView;
private ListView mDrawerList;
private DrawerLayout mDrawerLayout;
private ArrayAdapter<String> mAdapter;
private ActionBarDrawerToggle mDrawerToggle;
private String mActivityTitle;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mWebView = (WebView) findViewById(R.id.activity_main_webview);
mDrawerList = (ListView)findViewById(R.id.navList);mDrawerLayout = (DrawerLayout)findViewById(R.id.drawer_layout);
mActivityTitle = getTitle().toString();
addDrawerItems();
setupDrawer();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
WebSettings webSettings = mWebView.getSettings();
webSettings.setJavaScriptEnabled(true);
mWebView.setWebViewClient(new MyAppWebViewClient());
mWebView.loadUrl("file:///android_asset/www/index.html");
}
#Override
public void onBackPressed() {
if(mWebView.canGoBack()) {
mWebView.goBack();
} else {
super.onBackPressed();
}
}
private void addDrawerItems() {
String[] osArray = { "Android", "black" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
//load file to webView
mWebView.loadUrl("file:///android_asset/www/help.html");
//close drawer
mDrawerLayout.closeDrawers();
}
});
}
private void setupDrawer() {
mDrawerToggle = new ActionBarDrawerToggle(this, mDrawerLayout, R.string.drawer_open, R.string.drawer_close) {
/** Called when a drawer has settled in a completely open state. */
public void onDrawerOpened(View drawerView) {
super.onDrawerOpened(drawerView);
getSupportActionBar().setTitle("Navigation!");
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
/** Called when a drawer has settled in a completely closed state. */
public void onDrawerClosed(View view) {
super.onDrawerClosed(view);
getSupportActionBar().setTitle(mActivityTitle);
invalidateOptionsMenu(); // creates call to onPrepareOptionsMenu()
}
};
mDrawerToggle.setDrawerIndicatorEnabled(true);
mDrawerLayout.setDrawerListener(mDrawerToggle);
}
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#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_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) {
return true;
}
// Activate the navigation drawer toggle
if (mDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
See comments in code:
private void addDrawerItems() {
String[] osArray = { "Android", "black" };
//create also an array of paths to html files
final String[] pathsArray = { "file:///android_asset/www/help.html", "HERE_MUST_BE_ANOTHER_PATH_TO_ANOTHER_FILE" };
mAdapter = new ArrayAdapter<String>(this, android.R.layout.simple_list_item_1, osArray);
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(MainActivity.this, "Time for an upgrade!", Toast.LENGTH_SHORT).show();
//load file to webView from pathsArray
// getting current path from arg. position
mWebView.loadUrl(pathsArray[position]);
//close drawer
mDrawerLayout.closeDrawers();
}
});
}

How to use OnItemClickListener in Navigation Drawer?

I have a Navigation Drawer in my App.I have the items in the drawer but I don't know how I can open new Activity from each of items .
I have Tried some Methods & Commented It ...
Thanks for your Help ...
MainActivity
public class MainActivity extends ActionBarActivity
{
private ListView mDrawerList;
protected DrawerLayout mDrawer;
private CustomActionBarDrawerToggle mDrawerToggle;
private String[] menuItems;
String LOG_TAG = "Remote It";
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
supportRequestWindowFeature(WindowCompat.FEATURE_ACTION_BAR);
// getSupportActionBar().hide();
setContentView(R.layout.activity_main_drawer);
// enable ActionBar app icon to behave as action to toggle nav drawer
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
mDrawer = (DrawerLayout) findViewById(R.id.drawer_layout);
// set a custom shadow that overlays the main content when the drawer
// opens
mDrawer.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
_initMenu();
mDrawerToggle = new CustomActionBarDrawerToggle(this, mDrawer);
mDrawer.setDrawerListener(mDrawerToggle);
// if (savedInstanceState == null)
// {
// selectItem(0);
// }
}
private void _initMenu()
{
NsMenuAdapter mAdapter = new NsMenuAdapter(this);
// Add Header
mAdapter.addHeader(R.string.ns_menu_main_header);
// Add first block
menuItems = getResources().getStringArray(R.array.ns_menu_items);
String[] menuItemsIcon = getResources().getStringArray(R.array.ns_menu_items_icon);
int res = 0;
for (String item : menuItems)
{
int id_title = getResources().getIdentifier(item, "string", this.getPackageName());
int id_icon = getResources().getIdentifier(menuItemsIcon[res], "drawable", this.getPackageName());
NsMenuItemModel mItem = new NsMenuItemModel(id_title, id_icon);
// if (res==1) mItem.counter=12; //it is just an example...
// if (res==3) mItem.counter=3; //it is just an example...
mAdapter.addItem(mItem);
res++;
}
mAdapter.addHeader(R.string.ns_menu_main_header2);
mDrawerList = (ListView) findViewById(R.id.drawer);
if (mDrawerList != null)
mDrawerList.setAdapter(mAdapter);
mDrawerList.setOnItemClickListener(new DrawerItemClickListener());
}
#Override
protected void onPostCreate(Bundle savedInstanceState)
{
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
mDrawerToggle.syncState();
}
#Override
public void onConfigurationChanged(Configuration newConfig)
{
super.onConfigurationChanged(newConfig);
mDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu)
{
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.control_menu, menu);
return super.onCreateOptionsMenu(menu);
}
/* Called whenever we call invalidateOptionsMenu() */
#Override
public boolean onPrepareOptionsMenu(Menu menu)
{
// If the nav drawer is open, hide action items related to the content
// view
boolean drawerOpen = mDrawer.isDrawerOpen(mDrawerList);
menu.findItem(R.id.action_keyboard).setVisible(!drawerOpen);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item)
{
/*
* The action bar home/up should open or close the drawer.
* ActionBarDrawerToggle will take care of this.
*/
if (mDrawerToggle.onOptionsItemSelected(item))
{
return true;
}
// Handle your other action bar items...
return super.onOptionsItemSelected(item);
}
private class CustomActionBarDrawerToggle extends ActionBarDrawerToggle
{
public CustomActionBarDrawerToggle(Activity mActivity, DrawerLayout mDrawerLayout)
{
super(mActivity, mDrawerLayout, R.drawable.ic_drawer, R.string.ns_menu_open, R.string.ns_menu_close);
}
#Override
public void onDrawerClosed(View view)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_close));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
#Override
public void onDrawerOpened(View drawerView)
{
getSupportActionBar().setTitle(getString(R.string.ns_menu_open));
supportInvalidateOptionsMenu(); // creates call to
// onPrepareOptionsMenu()
}
}
//private void selectItem(int position)
//{
// switch (position)
// {
// case 1:
// Intent a = new Intent(MainActivity.this, ControlActivity.class);
// startActivity(a);
// break;
// case 2:
// Intent b = new Intent(MainActivity.this, Tutorial.class);
// startActivity(b);
// break;
// default:
// }
//}
private class DrawerItemClickListener implements ListView.OnItemClickListener
{
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id)
{
// Highlight the selected item, update the title, and close the
// drawer
// update selected item and title, then close the drawer
//mDrawerList.setItemChecked(position, true);
// String text = "menu click... should be implemented";
// Toast.makeText(MainActivity.this, text,
// Toast.LENGTH_LONG).show();
// // You should reset item counter
//mDrawer.closeDrawer(mDrawerList);
Intent intent = new Intent(MainActivity.this, Tutorial.class);
startActivity(intent);
}
}
}
NOTE
I need it to work with activities & not with Fragments .
If you want your NavigationDrawer in all your activities you can launch from the NavigationDrawer, the best way to handle that is not to launch new Activity, but to replace Fragment.
Because if you launch a new Activity each time you'll need to implement your NavigationDrawer in all your subactivities. (easily doable with heritage though).
There is a nice tutorial here to play with "replace" Fragments with the NavigationDrawer :
http://manishkpr.webheavens.com/android-navigation-drawer-example-using-fragments/

Navigation Drawer Does Not Use R.drawable.ic_drawer or swipe closed

I just added a navigation drawer to my app however it seems as if the R.drawable.ic_drawer and R.drawable.drawer_shadow aren't being used (it is simply showing a black background instead of the grey one in ic_drawer).
The other issue I'm having is the navigation drawer will open - but does not seem to close unless I hit the back button.
I used the following example:
http://hmkcode.com/android-creating-a-navigation-drawer/
but right now I'm attempting to figure out how to modify the following to get it to close when swiping it from right to left.
SCREENSHOT:
JAVA:
public class Home extends YouTubeBaseActivity implements
YouTubePlayer.OnInitializedListener,
YouTubeThumbnailView.OnInitializedListener {
public static final String API_KEY = "XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX";
public static final String VIDEO_ID = "o7VVHhK9zf0";
public static final String VIDEO1_ID = "xVHHJqntuXI";
public static final String VIDEO2_ID = "YWteQj_q3Ro";
public static final String VIDEO3_ID = "83ZgtqTw-mI";
public static final String VIDEO4_ID = "n5wMza29JwI";
private YouTubePlayer youTubePlayer;
private YouTubePlayerView youTubePlayerView;
private YouTubeThumbnailView youTubeThumbnailView1;
private YouTubeThumbnailView youTubeThumbnailView2;
private YouTubeThumbnailView youTubeThumbnailView3;
private YouTubeThumbnailView youTubeThumbnailView4;
private String[] drawerListViewItems;
private DrawerLayout drawerLayout;
private DrawerLayout mDrawerLayout;
private ListView drawerListView;
private ListView mDrawerList;
private ActionBarDrawerToggle actionBarDrawerToggle;
private ActionBarDrawerToggle mDrawerToggle;
private YouTubeThumbnailLoader youTubeThumbnailLoader;
ScrollView mainScrollView;
Button fav_up_btn1;
Button fav_dwn_btn1;
ArrayAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.home);
final ActionBar actionBar = getActionBar();
actionBar.setCustomView(R.layout.actionbar_custom_view_home);
actionBar.setDisplayShowTitleEnabled(false);
actionBar.setDisplayShowCustomEnabled(true);
// get list items from strings.xml
drawerListViewItems = getResources().getStringArray(R.array.items);
// get ListView defined in activity_main.xml
drawerListView = (ListView) findViewById(R.id.left_drawer);
// Set the adapter for the list view
drawerListView.setAdapter(new ArrayAdapter<String>(this,
R.layout.drawer_listview_item, drawerListViewItems));
drawerLayout = (DrawerLayout) findViewById(R.id.drawer_layout);
actionBarDrawerToggle = new ActionBarDrawerToggle(this, /* host Activity */
drawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.drawer_open, /* "open drawer" description */
R.string.drawer_close /* "close drawer" description */
);
drawerLayout.setDrawerListener(actionBarDrawerToggle);
getActionBar().setDisplayHomeAsUpEnabled(true);
drawerLayout.setDrawerShadow(R.drawable.drawer_shadow,
GravityCompat.START);
youTubePlayerView = (YouTubePlayerView) findViewById(R.id.youtubeplayerview);
youTubePlayerView.initialize(API_KEY, this);
mainScrollView = (ScrollView) findViewById(R.id.groupScrollView);
youTubeThumbnailView1 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview1);
youTubeThumbnailView1.initialize(API_KEY, this);
fav_up_btn1 = (Button) findViewById(R.id.fav_up_btn1);
fav_up_btn1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean favIsUp = fav_up_btn1
.getBackground()
.getConstantState()
.equals(getResources().getDrawable(
R.drawable.fav_up_btn1).getConstantState());
// set the background
fav_up_btn1
.setBackgroundResource(favIsUp ? R.drawable.fav_dwn_btn1
: R.drawable.fav_up_btn1);
}
});
youTubeThumbnailView1.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO1_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView2 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview2);
youTubeThumbnailView2.initialize(API_KEY, this);
youTubeThumbnailView2.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO2_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView3 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview3);
youTubeThumbnailView3.initialize(API_KEY, this);
youTubeThumbnailView3.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO3_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
youTubeThumbnailView4 = (YouTubeThumbnailView) findViewById(R.id.youtubethumbnailview4);
youTubeThumbnailView4.initialize(API_KEY, this);
youTubeThumbnailView4.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg1) {
if (youTubePlayer != null) {
youTubePlayer.cueVideo(VIDEO4_ID);
youTubePlayer.play();
mainScrollView.smoothScrollTo(0, 0);
}
}
});
}
#Override
public void onInitializationFailure(Provider provider,
YouTubeInitializationResult result) {
}
#Override
public void onInitializationSuccess(Provider provider,
YouTubePlayer player, boolean wasRestored) {
youTubePlayer = player;
if (!wasRestored) {
player.cueVideo(VIDEO_ID);
}
}
#Override
public void onInitializationFailure(YouTubeThumbnailView thumbnailView,
YouTubeInitializationResult error) {
}
#Override
public void onInitializationSuccess(YouTubeThumbnailView thumbnailView,
YouTubeThumbnailLoader thumbnailLoader) {
youTubeThumbnailLoader = thumbnailLoader;
thumbnailLoader
.setOnThumbnailLoadedListener(new ThumbnailLoadedListener());
youTubeThumbnailLoader.setVideo(VIDEO_ID);
}
private final class ThumbnailLoadedListener implements
YouTubeThumbnailLoader.OnThumbnailLoadedListener {
#Override
public void onThumbnailError(YouTubeThumbnailView arg0, ErrorReason arg1) {
}
#Override
public void onThumbnailLoaded(YouTubeThumbnailView arg0, String arg1) {
}
}
#Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
actionBarDrawerToggle.onConfigurationChanged(newConfig);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu items for use in the action bar
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.main, menu);
return super.onCreateOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// call ActionBarDrawerToggle.onOptionsItemSelected(), if it returns
// true
// then it has handled the app icon touch event
if (actionBarDrawerToggle.onOptionsItemSelected(item)) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
I use the below code:
mDrawerLayout.setDrawerShadow(R.drawable.drawer_shadow, GravityCompat.START);
// set up the drawer's list view with items and click listener
getActionBar().setDisplayHomeAsUpEnabled(true);
getActionBar().setHomeButtonEnabled(true);
mDrawerToggle = new ActionBarDrawerToggle(
this, /* host Activity */
mDrawerLayout, /* DrawerLayout object */
R.drawable.ic_drawer, /* nav drawer icon to replace 'Up' caret */
R.string.action_settings, /* "open drawer" description */
R.string.action_settings /* "close drawer" description */
)
See if it works.
Also check whether the ic_drawer image is present in drawable-hdpi, drawable-mdpi, drawable-xhdpi and drawable-xxhdpi folders in different sizes.
try this code
#Override
protected void onPostCreate(Bundle savedInstanceState) {
super.onPostCreate(savedInstanceState);
// Sync the toggle state after onRestoreInstanceState has occurred.
actbardrawertoggle.syncState();
}

Categories