So I have a fragment with the following in the XML layout file:
<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"
tools:context="com.example.dwinnbrown.myapplicationv20.MainFragment">
<!-- TODO: Update blank fragment layout -->
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
</RelativeLayout>
I am now trying to load a url into the web view through the java file associated with the fragment. However I am running into an error "cannot resolve findViewByID(int)". Any ideas?
Here is the code I am trying to use for the fragment:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
/**
* A simple {#link Fragment} subclass.
*/
public class MainFragment extends Fragment {
public MainFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView view = (WebView) this.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
}
}
Do Something like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView view = (WebView) rootView.findViewById(R.id.webView);
view.getSettings().setJavaScriptEnabled(true);
view.loadUrl(url);
return rootView ;
}
Below is the xml for fragment:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".WebActivity">
<WebView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/webview">
</WebView>
</RelativeLayout>
And try this for fragment class
public class MainFragment extends Fragment {
WebView webView;
public MainFragment() {
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
webView=(WebView)getActivity().findViewById(R.id.webview);
WebSettings webSettings=webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webView.loadUrl("your_url");
webView.setWebViewClient(new Mybrowser());
if (savedInstanceState != null)
webView.restoreState(savedInstanceState);
else
webView.loadUrl("your_url");
}
private class Mybrowser extends WebViewClient {
#Override
public boolean shouldOverrideUrlLoading(WebView view, String url) {
view.loadUrl(url);
return true;
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_main, container, false);
String url = "http://winn-brown.co.uk/";
WebView wv = (WebView) view.findViewById(R.id.webView);
wv.setWebViewClient(new WebViewClient());
wv.getSettings().setJavaScriptEnabled(true);
wv.loadUrl(url);
}
Related
Hey all I am having a little bit of an issue with trying to call a fragment that has the videoview within it into my current fragment page.
When I click on the test button I set up it goes through the code without error but never shows the video fragment - I even gave it a background color of red so I could see it load into my current view.
My goal here is just to send a parameter of the video path to the video fragment videoview player and start playing it.
videoPlay.java
public class videoPlay extends Fragment {
public videoPlay(){
//constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout._fragvideoplay, container, false);
MediaController mc= new MediaController(getActivity());
VideoView view = (VideoView)rootView.findViewById(R.id.videoView);
String path = Environment.getExternalStorageDirectory().getAbsolutePath().toString() + "/sddir/Bubble Guppies.mp4";
view.setVideoURI(Uri.parse(path));
view.setMediaController(mc);
view.start();
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
}
}
_fragvideoplayer.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorPrimary">
<VideoView
android:id="#+id/videoView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1" />
</LinearLayout>
FragMovies.java:
public class FragMovies extends Fragment {
public FragMovies(){
//constructor
}
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout._fragmovies, container, false);
WebView view = (WebView) rootView.findViewById(R.id.webView);
view.setWebViewClient(new WebViewClient() {
#Override
public void onPageStarted(WebView view, String url, Bitmap favicon) {
super.onPageStarted(view, url, favicon);
container.findViewById(R.id.webView).setVisibility(View.GONE);
container.findViewById(R.id.pBar1).setVisibility(View.VISIBLE);
}
#Override
public void onPageFinished(WebView view, String url) {
super.onPageFinished(view, url);
container.findViewById(R.id.pBar1).setVisibility(View.GONE);
container.findViewById(R.id.webView).setVisibility(View.VISIBLE);
}
});
view.getSettings().setCacheMode(WebSettings.LOAD_CACHE_ELSE_NETWORK);
view.getSettings().setJavaScriptEnabled(true);
view.getSettings().setAllowContentAccess(true);
view.getSettings().setAllowFileAccess(true);
view.getSettings().setLoadsImagesAutomatically(true);
view.getSettings().setAllowFileAccess(true);
view.getSettings().setBuiltInZoomControls(false);
view.getSettings().setDomStorageEnabled(true);
view.getSettings().setAppCacheEnabled(true);
view.getSettings().setDisplayZoomControls(false);
view.getSettings().setJavaScriptCanOpenWindowsAutomatically(true);
view.getSettings().setSupportZoom(false);
view.setHorizontalScrollBarEnabled(false);
view.setVerticalScrollBarEnabled(false);
view.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
view.loadUrl("https://www.bing.com/");
return rootView ;
}
}
_fragmovies.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/mainLayout">
<ProgressBar
android:id="#+id/pBar1"
android:layout_width="93dp"
android:layout_height="match_parent"
android:layout_alignParentTop="false"
android:layout_centerHorizontal="true"
android:layout_marginStart="950px"
android:layout_marginLeft="950px" />
<WebView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/webView"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true" />
</LinearLayout>
Main_activity.java:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
PagerAdapter pagerAdapter;
Button button;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getViews();
//adapter setup
pagerAdapter = new com.example.telluridetainment.PagerAdapter(getSupportFragmentManager());
//attaching fragments to adapter
pagerAdapter.addFragment(new FragMovies(),"Movies");
viewPager.setOffscreenPageLimit(1);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
//setting icons
tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);
button = (Button) findViewById(R.id.button0);
button.setOnClickListener(new MyClass());
}
public class MyClass implements View.OnClickListener {
#Override
public void onClick(View v) {
videoPlay myfragment = new videoPlay();
//pass data
Bundle bundle = new Bundle();
bundle.putString("KEY","DATA");
myfragment.setArguments(bundle);
FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentManager.addToBackStack(null);
fragmentManager.replace(R.id.viewPager, myfragment).commit();
}
}
private void getViews() {
tabLayout = findViewById(R.id.mTabLayout);
viewPager = findViewById(R.id.viewPager);
}
}
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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:layout_width="2000px"
android:layout_height="1200px"
tools:context=".MainActivity">
<Button
android:id="#+id/button0"
android:layout_width="200dp"
android:layout_height="152dp"
android:text="Button"
android:textColor="#B71C1C" />
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/appbarLayout"
tools:ignore="SpeakableTextPresentCheck">
</android.support.v4.view.ViewPager>
</RelativeLayout>
The path to the movie is correct. And the movie is there in the "external" SD card path. Clicking around on the new fragment (videoPlay) yields no video menu (play, remind, pause...) so its not there.
Any help would be great!
I have developed an app which contains fragment View but I was exposed to a problem, the app is crashing. Can you help me in solving the problem?
Error I am getting is:
java.lang.NullPointerException: Attempt to invoke virtual method 'int android.view.View.getWidth()' on a null object reference
at com.abdeljalilkchih.palestine.Fragment.FragmentOne$1.run(FragmentOne.java:59)
04-28 13:45:51.898 23913-24013/com.abdeljalilkchih.palestine E/chromium: [ERROR:gl_context_virtual.cc(39)] Trying to make virtual context current without decoder.
Can you please tell me how can I solve this error?
FragmentOne.Java
package com.abdeljalilkchih.palestine.Fragment;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import com.abdeljalilkchih.palestine.R;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.MobileAds;
import com.google.android.gms.ads.AdView;
import yalantis.com.sidemenu.interfaces.ScreenShotable;
public class FragmentOne extends Fragment implements ScreenShotable {
private AdView MyAdView2;
private View Fragmentone_view;
private Bitmap bitmap;
public static FragmentOne newInstance() {
FragmentOne fragmentOne = new FragmentOne();
return fragmentOne;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one, container, false);
MobileAds.initialize(this.getContext(), "ca-app-pub-9961705383410701~3103812044");
MyAdView2 = rootView.findViewById(R.id.MyAdView2);
AdRequest adRequest = new AdRequest.Builder().build();
MyAdView2.loadAd(adRequest);
WebView webView;
webView=(WebView) rootView.findViewById(R.id.Web_View2);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
//ربط الفيو بالصفحات
webView.loadUrl("file:///android_asset/webfiles/index.html");
return rootView;
}
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.Fragmentone_view = view.findViewById(R.id.container);
}
#Override
public void takeScreenShot() {
Thread threads = new Thread() {
#Override
public void run() {
Bitmap bitmap = Bitmap.createBitmap(Fragmentone_view.getWidth(),
Fragmentone_view.getHeight(), Bitmap.Config.ARGB_8888);
Canvas canvas = new Canvas(bitmap);
Fragmentone_view.draw(canvas);
FragmentOne.this.bitmap = bitmap;
}
};
threads.start();
}
#Override
public Bitmap getBitmap() {
return bitmap;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
}
fragment_one.xml
<?xml version="1.0" encoding="utf-8"?>
<io.codetail.widget.RevealFrameLayout
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">
<FrameLayout
android:id="#+id/Container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:id="#+id/Main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical">
<!--
your code here
-->
<com.google.android.gms.ads.AdView xmlns:ads="http://schemas.android.com/apk/res-auto"
android:id="#+id/MyAdView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0"
ads:adSize="BANNER"
ads:adUnitId="#string/ad_unit_banner2"></com.google.android.gms.ads.AdView>
<WebView
android:id="#+id/Web_View2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0" />
</LinearLayout>
</FrameLayout>
</io.codetail.widget.RevealFrameLayout>
Looks like you have defined the variable Fragmentone_view locally to method onViewCreated()
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
this.Fragmentone_view = view.findViewById(R.id.container);
}
To utilize the variable in another method, you need to define it globally.
private View Fragmentone_view;
didnt initialize properly thats why you getting null pointer exception
try below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_one,container,false);
this.Fragmentone_view = rootView
MobileAds.initialize(this.getContext(), "ca-app-pub9961705383410701~3103812044");
MyAdView2 = rootView.findViewById(R.id.MyAdView2);
AdRequest adRequest = new AdRequest.Builder().build();
MyAdView2.loadAd(adRequest);
WebView webView;
webView=(WebView) rootView.findViewById(R.id.Web_View2);
webView.getSettings().setBuiltInZoomControls(true);
webView.getSettings().setJavaScriptEnabled(true);
webView.setWebViewClient(new WebViewClient());
//ربط الفيو بالصفحات
webView.loadUrl("file:///android_asset/webfiles/index.html");
return rootView;
}
Try this \
replace :
public class FragmentOne extends Fragment implements ScreenShotable {
...
//replace this line with >> private FrameLayout Fragmentone_view;
private View Fragmentone_view;
...
}
with :
private FrameLayout Fragmentone_view;
I've made an app and that includes fragments.My PDF viewer works perfectly fine but not in fragments.How can I fix that ? I have asset files and everything.Compiled git hub reference, XML just having trouble with fragments.Thank You
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
//pdfView = getView().findViewById(R.id.pdfView);
//pdfView.fromAsset("tenor-madness.pdf").load();
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_tab1, container, false);
}
XML FİLE
<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"
tools:context="liselimanyak.practice.Tag2">
<!-- TODO: Update blank fragment layout -->
<com.github.barteksc.pdfviewer.PDFView
android:id="#+id/pdfView"
android:layout_width="match_parent"
android:layout_centerInParent="true"
android:layout_height="match_parent"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="tab1"
android:layout_centerInParent="true"
android:textSize="30dp"/>
</RelativeLayout>
You can't call findViewById() in onCreate() ....do it in onCreateView (using something like following)...or alternatively in onViewCreated (using the view param)
View v = inflater.inflate(R.layout.fragment_tab1, container, false;
pdfView = v.findViewById(R.id.pdfView);
...or, if using onViewCreated
#Override
public void onViewCreated(View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
pdfView = view.findViewById(R.id.pdfView);
pdfView.fromAsset("tenor-madness.pdf").load();
}
I added the custom toolbar in xml and then added it as Actionbar in fragment.
But it's not showing anything from Title to Menus. The only actionbar is visible
Here is my xml code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<!-- Must be last for right layering display -->
<FrameLayout
android:id="#+id/toolbar_container"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar2"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="#color/colorPrimary" />
</FrameLayout>
</FrameLayout>
And here is my java code
public class searchView extends Fragment{
Toolbar search;
MaterialSearchView search_view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_view,container,false);
FrameLayout layout = (FrameLayout) inflater.inflate(R.layout.search_view, container, false);
search = (Toolbar)layout.findViewById(R.id.toolbar2);
((AppCompatActivity) getActivity()).setSupportActionBar(search);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
setHasOptionsMenu(true);
return v;
}
}
In these code I called the fragment in MainActivity On Bottombar click
else if (position==1){
searchView f = new searchView();
getSupportFragmentManager().beginTransaction().replace(R.id.framelayout,f).commit();
}
return true;
Toolbar search;
MaterialSearchView search_view;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.search_view,container,false);
setHasOptionsMenu(true);
search = (Toolbar)v.findViewById(R.id.toolbar2);
((AppCompatActivity) getActivity()).setSupportActionBar(search);
((AppCompatActivity) getActivity()).getSupportActionBar().setTitle("Contacts");
return v;
}
}
to show options menu override
#Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
//add your menu here;
}
I insert one fragment in activity_main.xml using java, i put fragment in linear layout in activity_main.
Fragment xml file has one textView.
How can i change TextView when app start from MainActivity using java, when app start and when it called onCreate?
I try few solution form stacoverflow but i can get this to work.
Emulator get crashed when he start to load app.
Here is activity_main.xml code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
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="com.example.android.fragment_promjenaviewprekojave.MainActivity">
<LinearLayout
android:id="#+id/linearLayoutFragment_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
</LinearLayout>
</LinearLayout>
Here is Fragment code:
package com.example.android.fragment_promjenaviewprekojave;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class BlankFragment extends Fragment {
public BlankFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
}
Here is xml file for that fragment who contain only 1 TextView
<LinearLayout 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"
tools:context="com.example.android.fragment_promjenaviewprekojave.BlankFragment">
<TextView
android:id="#+id/fragmentTextView_id"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
Here is the MainActivity.java from where I want to change fragment TextView
package com.example.android.fragment_promjenaviewprekojave;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
BlankFragment objA = new BlankFragment();
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.add(R.id.linearLayoutFragment_id, objA);
fragmentTransaction.commit();
TextView textView = (TextView) findViewById(R.id.fragmentTextView_id);
textView.setText("We add text to fragment TextView");
}
}
In your BlankFragment code, change
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_blank, container, false);
}
to :
//Declare a member to hold a reference to your TextView
private TextView myTextView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_blank, container, false);
myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
myTextView .setText("Whatever text you want");
return (v);
}
//Also declare a function to modify the text on your fragment
public void setCustomText(String str) {
if (myTextView != null) {
myTextView.setText(str);
}
}
Now back in your activity, you can call
objA.setCustomText("Hello World!");
Bare in mind that you will need to store a reference to objA in your activity, such as
private BlankFragment objA;
The onCreate will then be :
objA = new BlankFragment();
Lastly, this method works fine once the views are all in place and drawn.
If you're looking to change the text inside the fragment at creation, you will want to use a newInstance pattern on the fragment, where you can pass the text as an argument.
Something like this :
objA = BlankFragment.newInstance("Hello world");
Where newInstance is defined in your fragment as follow :
public static BlankFragment newInstance(String str) {
BlankFragment f = new BlankFragment();
Bundle args = new Bundle();
args.putString("TextTag", str);
f.setArguments(args);
return (f);
}
And then you can retrieve this value in the onCreate of your Fragment like this :
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View v = inflater.inflate(R.layout.fragment_blank, container, false);
myTextView = (TextView) v.findViewById(R.id.fragmentTextView_id);
String text = getArguments().getString("TextTag", "");
myTextView .setText(text);
return (v);
}
In your xml, you should have a fragment container. Assuming the ID of this is fragment_id:
For support library,
BlankFragment blankFrag = (BlankFragment)getSupportFragmentManager().
findFragmentById(R.id.fragment_id);
Or else for non-support library Fragment:
BlankFragment blankFrag = (BlankFragment)getFragmentManager().
findFragmentById(R.id.fragment_id);
You will also need a public method in the BlankFragment class with a signature like setTextViewText(String text) and then call it from MainActivity like blankFrag.setTextViewText("my text");