Android: Linking to URL in Navigation Drawer - java

I am using the Navigation Components library for my Navigation Drawer. I want one item in said drawer to link to an external Website. The offical documentation doesn't say anything about external links. How do I implement that?
I could create a temporary fragment and navigate to that first but this solution is not very elegant in my opinion.

The line
NavigationUI.setupWithNavController(navigationView, navController);
Calls setNavigationItemSelectedListener internally to connect destinations to menu items - this is how a new destination is launched when you click a menu item. Navigation supports <activity> destinations, allowing you to start an activity when clicking on a menu item. You'd add an activity destination to your graph that uses the same id as your menu item:
<activity
android:id="#+id/nav_login_activity"
app:action="android.intent.action.VIEW"
app:data="https://www.your_url_here.com"/>
Then the default setupWithNavController will call startActivity for you.

Related

I can't Connect navigation drawer items with there related activities and fregmant

I have made a navigation drawer in my app , but I have a problem that I can't open any activity related to any menu item in navigation drawer .
for example in this code I am trying to only display a toast message ,also there is no response when I click on the item from the menu.
I assume you are using menu items and I would suggest giving the same id for the navigation destination and its corresponding item.
if items id is nav_home then destionation id should also be nav_home in navigation.xml
// Passing each menu ID as a set of Ids because each
// menu should be considered as top level destinations.
appBarConfiguration = AppBarConfiguration(setOf(
R.id.nav_home, {*other navigations*}), binding.drawerLayout)
setupActionBarWithNavController(navController, appBarConfiguration)
binding.navView.setupWithNavController(navController)
when you set it like this it will automatically connect the destination to items and u dont have to set the onclick manually

Unable to navigate from app bar after setting focusable attribute

I am creating a very simple WebView app with a few useful links and I want the initial focus to be on the app bar with easy access to the menu. This works if I add focusable attributes to the layout in main.xml
android:focusable="true"
android:focusableInTouchMode="true"
However I'm unable to navigate away from the app bar to the WebView content below unless I add requestFocus to the menu actions:
case R.id.url:
webView.loadUrl("https://example.com");
webView.requestFocus();
Then I can navigate down to the WebView content below, but as soon as I return to the app bar navigation is lost again. How can I freely navigate away from the app bar?
So I added the following code to main.xml and it solves my issue
android:nextFocusDown="#+id/webview"

Android Studio getSupportFragment.beginTransaction().replace causing crashes when navigating to another fragment(Java)

I am trying to implement a side-navigation bar (hamburger menu). I want to be able to select a new fragment to view from this menu, and then click buttons on that fragment to direct to another fragment (not in the hamburger menu list).
I was following a guide (https://www.youtube.com/watch?v=bjYstsO1PgI), but it doesn't show the second step of redirecting to the other fragment.
I keep getting a crash whenever I click a button that redirects to the other fragment, if it has been loaded by the hamburger menu.
The line I use is to render from the hamburger menu is:
getSupportFragmentManager().beginTransaction().replace(R.id.fragment_container, new FirstFragment()).commit();
This is called by the following event listener:
NavigationView navigationView = findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
and I get this error whenever I press a button to go to the 'other' fragment.
Navigation action/destination com.example.testApp:id/action_FirstFragment_to_submitScreen cannot be found from the current destination Destination(com.example.testApp:id/submitScreen) label=fragment_submit_screen class=com.example.renamedTestApp.SubmitScreen
This error is not present when I use
<include layout="#layout/content_main"/>
to load the first fragment on app startup (the one from the hamburger menu).
Any tips? This question may be confusing so let me know if I should tidy up my explanation.
I believe the error may be in the fragment not being fully loaded perhaps? If that is the case, what would be a better line to use to switch on the fly.
provide some code
try this
FragmentManager fragmnt= getActivity().getSupportFragmentManager();
FragmentTransaction tr= fragmnt.beginTransaction();
tr.replace(R.id.YOUR,new Yourfragment());
//or tr.replace(R.id.content_frame, new Yourfragment());
tr.commit();

Webview URL items not Visble

I have a android studio Project with a Webview and a Nav Bar and with the Nav bar I can change between WebViews.
My Problem is that when I but something in my ShoppingCard and switch with the Nav bar to my Shopping Card the Items in there are not Visble how can I solve, this? the ShoppingCard URL ends with .php if this helps
Best Regards T.S
just created a tiny app with two tabs which change two webviews. TAB 1 webview show cart items, other one (TAB 2) show product listing , from product listing i can add product and cart page auto reloaded/refreshed using AndroidCartApp (a JavascriptInterface ) class, and i can see added items in carts, there is no such issue, in this app, other than tabs all is under webviews/htmlapp , maybe you are adding items to cart in other way,

Android tab bar with different fragments and different action bar icons

Problem Description and Question
I'm using tab activity with action bar. In the HOME tab I have GridView which contains some items, like it is shown in the image below (Hot Lines, Taxi, Restaurants etc)
Wen user click on the items in the grid view I want my application to take following actions:
Change icon of application to grid view item image on which I pressed.
Change the test near the icon
Add standard back button near icon, which will go back to grid view screen.
Change the tab fragment to the one which I specify.
Like it is shown in the image below:
As I never deal with this kind of problems can you please give me example or share some link with me of how I can do this? and can I do this at all?
This might help:
Android studio - is possible to add tabs pointing to fragments from designer?
It is not exactly what you want, but a good start. If you are willing to put a bit of work in it, you should be able to get what you want. If you have a basic Frame to work with and more specific problems with this matter I will gladly help you out ^^
John
The first link you can check is THIS.
And you should read more about ActionBar.
The last thing is it's better if you google it first and try to write a code and when you got stuck somewhere share your code with us and ask for help.
You have to use actionbarsherlock library for it.
use android.support.v4.app.FragmentTabHost and TabWidget in the xml as shown below :
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
style="#style/yourstyle"/>
<FrameLayout
android:id="#+id/realtabcontent"
style="#style/realtabFrameContentStyle" />
<TabWidget
android:id="#android:id/tabs"
style="#style/yourtabstyle" />
</android.support.v4.app.FragmentTabHost>
Use SherlockFragmentActivity for showing the tabs.
In the activities code use following code (preferably in a function) to set the ctionbar icon and text :
activity.getSupportActionBar().setDisplayShowCustomEnabled(true);
activity.getSupportActionBar().setCustomView(R.layout.your_action_barlayout);
((TextView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.action_bar_title))).setText("your title");
ImageView homeButton = ((ImageView) (activity.getSupportActionBar().getCustomView().findViewById(R.id.your_icon)));
homeButton.setVisibility(View.VISIBLE);
homeButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(context, YourHOmeActivity.class);
intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
activity.startActivity(intent);
activity.finish();
}
});
ActionBar mActionBar = activity.getSupportActionBar();
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.show();
then call this function with your icon and your text in your fragments onResume() method.

Categories

Resources