How to use a single webView for multiple buttons in fragments - android

I am developing my android application (Java) in android studio i am new to this.
there are two fragments A & B,
fragment A contains multiple buttons (different buttons with different links) and
fragment b contain webview.
I tried but the application is crashing when click the button.
Fragment A code
String[] urls=new String[6];
Bundle bundle;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_external_links, container, false);
LinearLayout fb_link = (LinearLayout) view.findViewById(R.id.fb_link_icon);
fb_link.setOnClickListener(this);
urls[0] ="https://facebook.com";
LinearLayout google_link = (LinearLayout) view.findViewById(R.id.google_link_icon);
google_link.setOnClickListener(this);
urls[1] ="https://google.com";
LinearLayout hotmail_link = (LinearLayout) view.findViewById(R.id.hotmail_link_icon);
hotmail_link.setOnClickListener(this);
urls[2] ="https://hotmail.com";
LinearLayout yahoo_link = (LinearLayout) view.findViewById(R.id.yahoo_link_icon);
yahoo_link.setOnClickListener(this);
// urls[3] ="https://yahoo.com";
return view;
}
#Override
public void onClick(View v) {
Fragment fragment = null;
switch (v.getId()){
case R.id.fb_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[0]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
case R.id.google_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[1]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
case R.id.hotmail_link_icon:
fragment = new WebViewFragment();
bundle.putString("links" , urls[2]);
fragment.setArguments(bundle);
replaceFragment(fragment);
break;
}
}
public void replaceFragment(Fragment Fragment){
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_layout, Fragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
fragment B code
WebView webview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_web_view, container, false);
webview.findViewById(R.id.webview_frame);
Bundle bundle = this.getArguments();
String website = bundle.getString("links");
String webSite = getActivity().getIntent().getExtras().getString("links");
webview.setWebViewClient(new WebViewClient());
webview.loadUrl(website);
WebSettings websettings = webview.getSettings();
websettings.setJavaScriptEnabled(true);
return view;
}
}

Related

a custom title for the fragment

Good afternoon, please tell me how you can set a custom title for the fragment in Android Studio? I use this code: public class FirstFragment extends Fragment implements View.OnClickListener {
public FirstFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
Button aboutBtn = (Button) rootView.findViewById(R.id.aboutButton);
aboutBtn.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View view) {
Fragment fragment = null;
switch (view.getId()) {
case R.id.aboutButton:
fragment = new AboutFragment();
replaceFragment(fragment);
break;
....
}
}
public void replaceFragment(Fragment someFragment) {
FragmentTransaction transaction = getFragmentManager().beginTransaction();
transaction.replace(R.id.frame_container, someFragment);
transaction.addToBackStack(null);
transaction.commit();
}
}
When I tried to use title="Name1"; this is not works. then if use getActivity().setTitle("Name1"); it works, but when press the back button the title remains unchanged
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_home, container, false);
// Add this line here to change title
getActivity().setTitle("your title");
Button aboutBtn = (Button) rootView.findViewById(R.id.aboutButton);
aboutBtn.setOnClickListener(this);
return rootView;
}

how to send a String data from Main-activity to fragment?

I have a web-view in a fragment. I want to load the webview by selecting a url from the main activity. For example my webview is loading the url http://google.com/ ,which is from (url) my main activity. In other words, I have a string like below in my main activity , I want to use it to load my web-view which is in the fragment , how can I do that?
Please help me to solve this problem
String url="http://google.com/";
Use this to send data to fragment from activity
Bundle bundle=new Bundle();
bundle.putString("url", "http://google.com/");
//set Fragmentclass Arguments
Fragmentclass fragobj=new Fragmentclass();
fragobj.setArguments(bundle);
And then recieve in fragment's onCreateView method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String strtext=getArguments().getString("url");
return inflater.inflate(R.layout.fragment, container, false);
}
You can do that by two ways.
1) By making the setter method for url.
public class WebViewFragment extends Fragment {
private String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
return rootView;
}
public void setUrl(String url){
this.url = url;
}
}
Call that methord before adding fragment:
WebViewFragment fragment = new WebViewFragment();
fragment.setUrl(url);
getSupportFragmentManager().beginTransaction()
.replace(R.id.container,fragment).commit();
2) You can use that bu intent bundle
Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
Fragment fragment = new Fragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
Parse it in fragment:
public class WebViewFragment extends Fragment {
private String url;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup
container, #Nullable Bundle savedInstanceState) {
url = getArguments().getString("url");
return rootView;
}
}
try this:
Fragment homeFragment = new LotteryDetailFragment();
FragmentTransaction homeTransaction = getFragmentManager().beginTransaction();
String str = "you string";
Bundle bundle = new Bundle();
bundle.putString("param_str", str);
homeFragment.setArguments(bundle);
homeTransaction.addToBackStack("HomeFragment");
homeTransaction.replace(R.id.frame_container, homeFragment, "HomeFragment");
homeTransaction.commit();
in on onCreateView of destination fragment
String str=getArguments().getString("param_str");
Try this :
1 - From Activity :
Bundle bundle = new Bundle();
bundle.putString("url", "www.google.com");
Fragment fragment = new YourFragment();
fragment.setArguments(bundle);
getSupportFragmentManager().beginTransaction().replace(R.id.container, fragment).commit();
2 - In Fragment :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View layoutView = inflater.inflate(R.layout.your_layout, container, false);
this.activity = getActivity();
String url = getArguments().getString("url");
return layoutView;
}
Try
Send data from Activity
Bundle data = new Bundle();
data.putString("url", "http://www.google.com");
MyFragment frg = new MyFragment();
frg.setArguments(data);
Receive data in Fragment
String url = getArguments().getString("url");

WebView return null?

In my program, there are many fragments.
In the ActionBar, I have a SearchView, and result of the search is displayed in fragment0.
When I go to fragment2 (that does not have Webview), I must go back fragment0 to show results.
However, when I go back to fragment0 and call the Webview, it is null.
What's problem and solution?
Here is MainActivity (where search query is excuted):
Fragment fragment0 = new SearchScrActivity();
FragmentTransaction ft = getSupportFragmentManager()
.beginTransaction();
ft.replace(R.id.content_frame, fragment0);
ft.commit();
WebView _webview = (WebView) findViewById(R.id.webViewResult);
Here is SearchScrActivity.java
public class SearchScrActivity extends SherlockFragment {
private WebView _webView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_search_screen, container,
false);
_webView = (WebView) rootView.findViewById(R.id.webViewResult);
_webView.setBackgroundColor(0x00000000);
_webView.setLayerType(WebView.LAYER_TYPE_SOFTWARE, null);
String meaning;
meaning = "";
_webView.loadDataWithBaseURL(null, meaning, "text/html",
"utf-8", null);
return rootView;
}
}

Use single Fragment for multiple UI tasks?

I am using the Fragment master detail architecture
All Fragment Classes have the same logic
Is it possible to "detach" the Fragment from the layout and use just one Fragment class
So this code:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
if("001".equalsIgnoreCase(id)){
arguments.putString(Fragment001.ARG_ITEM_ID, id);
Fragment001 fragment = new Fragment001();
fragment.setArguments(arguments); fragManager.replace(R.id.item_detail_container, fragment);
}
else if("002".equalsIgnoreCase(id)){
arguments.putString(Fragment002.ARG_ITEM_ID, id);
Fragment002 fragment = new Fragment002();
fragment.setArguments(arguments);
fragManager.replace(R.id.item_detail_container, fragment);
}
fragManager.commit();
Will turn into Something like:
FragmentTransaction fragManager = getSupportFragmentManager().beginTransaction();
GenericFragment fragment = new GenericFragment();
fragment.setUiId(id)
fragManager.replace(R.id.item_detail_container, fragment);
fragManager.commit();
List item
yes its possible, you can check and select which layout to use in onCreateView...
public static final String ARG_ITEM_ID1 = "fragment001";
public static final String ARG_ITEM_ID2 = "fragment002";
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = null;
String id = "fragment001";
if(ARG_ITEM_ID1.equalsIgnoreCase(id)){
rootView = inflater.inflate(R.layout.fragment_1_layout, container, false);
}
else {
rootView = inflater.inflate(R.layout.fragment_2_layout, container, false);
}
return rootView;
}

How to Do Transaction of framents from one to another

I want to transact from one fragment to another... below given is my snippet...
I want transaction on button click...
public class Main extends Fragment {
// final View rootView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.main, container, false);
Button camera=(Button)rootView.findViewById(R.id.button1);
camera.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.layout.main, new CameraActivity());
ft.commit();
}
});
return rootView;
}
so please Assist me in solving this...
[1]<--->[2]<--->[3]<--->[4]<--->[5]
where []=fragments...
[1]- has buttons a,b,c,d,e
How to move from 1 to 3 OnClick of c ...
Here is the Camera Activity
public class CameraActivity extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.camera, container, false);
return rootView;
}
}
first of all this cant be activity
ft.replace(R.layout.main, new CameraActivity());
it has to be fragment
put something like this in your onclick listener in FirstFragment:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
SecondFragment fragment = new SecondFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
than in your SecondFragment onclick listener:
Bundle bundle = new Bundle();
bundle.putString("key", value); //if you want to pass parameter to second fragment
ThirdFragment fragment = new ThirdFragment();
fragment.setArguments(bundle);
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.replace(R.id.content_frame, fragment).addToBackStack(null);
ft.commit();
and so on ...
method addToBackStack(null) is providing you back as simple as that. You could also pass some tag in that method if you need it, but I think from question you want be needing it.
Also if you need to start Activity than dont use this method, use Intent instead.
hope it helps

Categories

Resources