Start activity inside tabhost in android - java

I have a tabbed activity as shown below. I need to call a MapsActivity inside first tab("Search") which extends FragmentActivity. Any help in resolving this would help.
public class TabbedActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_tabbed);
TabHost tab = (TabHost) findViewById(R.id.tabHost);
tab.setup();
TabHost.TabSpec spec1 = tab.newTabSpec("Search");
spec1.setIndicator("Search");
spec1.setContent(R.id.layout1);
tab.addTab(spec1);
TabHost.TabSpec spec2 = tab.newTabSpec("Settings");
spec2.setIndicator("Settings");
spec2.setContent(R.id.layout2);
tab.addTab(spec2);
}
}
Tried this solution, but shows error "MapsActivity is not an enclosing class"
tab.addTab(tab.newTabSpec("Search")
.setIndicator("Search")
.setContent(new TabHost.TabContentFactory() {
public View createTabContent(String tag) {
return new TextView(MapsActivity.this);
}
}));

Activities-in-tabs has been deprecated as a technique for nearly six years.
Do something else, such as:
FragmentTabHost, with fragments for your tabs, such as MapFragment
ViewPager, with fragments for pages (MapFragment, SupportMapFragment) and a tabbed indicator (TabLayout, PagerTabStrip, etc.)

Related

Android Studio calling fragment function to unhide framelayout

Hey all I am having the worst time trying to get my code top work.
What I am trying to do is call a function in the MainActivity.java from another fragment FragMovies.java that should then unhide the invisible frameLayout which is called videoFrame.
The MainActivity.java has a TabView, ViewPager and the videoFrame.
In my FragMovies.java I have the following code linked to a button:
Button mainButton = (Button) rootView.findViewById(R.id.button0);
mainButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
MainActivity MA = new MainActivity();
MA.VidStart("thelostegg");
}
});
The onClick event then calls the function VidStart that's on the MainActiviy.java.
The vidStart function looks like this that's inside the MainActivity.java as well as the onCreate function:
public class MainActivity extends AppCompatActivity {
TabLayout tabLayout;
ViewPager viewPager;
FrameLayout frameLayout;
PagerAdapter pagerAdapter;
private Object videoPlay;
#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");
pagerAdapter.addFragment(new FragShows(),"Shows");
viewPager.setOffscreenPageLimit(2);
viewPager.setAdapter(pagerAdapter);
tabLayout.setupWithViewPager(viewPager);
//setting icons
tabLayout.getTabAt(0).setIcon(R.drawable.ic_movie_white_24dp);
tabLayout.getTabAt(1).setIcon(R.drawable.ic_movie_white_24dp);
}
public void VidStart(String theMP4) {
videoPlay myfragment = new videoPlay();
FragmentTransaction fragmentManager = getSupportFragmentManager().beginTransaction();
fragmentManager.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
frameLayout = findViewById(R.id.videoFrame);
frameLayout.setVisibility(View.VISIBLE);
fragmentManager.replace(R.id.videoFrame, myfragment).commit();
}
Everything goes fine until it gets the this line:
frameLayout = findViewById(R.id.videoFrame);
and my app crashes...
If I comment out that part of the code then it crashes on the
fragmentManager.replace(R.id.videoFrame, myfragment).commit();
BUT only caling the function from the other fragment. If I put the function call in the MainActivity.java onCreate function then it loads up the video and I can hear my video playing but of course can not see it due to the videoFrame still not visible.
The MainActivity XML:
The fragMovies XML (that's inside the MainActivity viewPager):
The _fragvideoplay XML (Called from the VideoPlay.java):
Basically I am looking to fix the calling of the function from another fragment (class) and have it work like it does being called within it's own class.
I'm sure I'm going about this all wrong. Any help would be great!

How to correct add a listener to a Fragment

I got two Fragments (basic-setup) with a FragmentPagerAdapter for use in a TabLayout.
tabLayout.setupWithViewPager(myViewPagerWithAdapter);
My Adapter takes an array of Fragments and titles as argument (also the FragmentManger for the super call).
MyViewPagerAdapter adapter = new MyViewPagerAdapter(getSupportFragmentManager() ,fragments, titles);
My Fragments (indeed 2) got a WebView in their xml file. To use the WebViews outside the fragments (it's kinda odd to code the control of the WebViews inside fragments, because the MainActivity is mainly involved with my user interaction). To know when my WebViews in the fragments were inflated, I added a simple interface, called OnFragmentCreatedListener, which is triggered in public void onActivityCreated(Bundle savedInstanceState) of the fragments. I add the listener through following way:
private OnFragmentCreatedListener listener;
...
public void addOnFragmentCreatedListener(OnFragmentCreatedListener listener) {
this.listener = listener;
}
In my MainActivity I add this listener like this:
public class MainActivity extends AppCompatActivity implements OnFragmentCreatedListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
...
MyFragment fragment = MyFragment.newInstance();
...
fragment.addOnFragmentCreatedListener(this);
...
}
#Override
public void onFragmentCreatedListener() {
// Do my stuff
}
Now when the screen rotates and the activity is restarted, the listener in the fragment stays null (wasn't initialized). Now I don't know what to do, because I don't understand this behavior. The code should do the same like in the first protected void onCreate(Bundle savedInstanceState) of the MainActivity. It's adding the listener but it stays null (like wtf?).
Thank you for spending your time!

How to acess a external view from the MainActivity

This problem let my brain burst. I got two Fragments, which contain a WebView each. These two Fragments are then placed in a ViewPagerAdapter, an adapter for my ViewPager. This ViewPager is then setup with a TabLayout. In code:
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
WebViewFragment webViewFragment1 = WebViewFragment.newInstance(1);
WebViewFragment webViewFragment2 = WebViewFragment.newInstance(2);
WebViewFragment[] fragments = new WebViewFragment[]
{
webViewFragment1,
webViewFragment2
};
String[] fragmentTitles = new String[]
{
"WebView1",
"WebView2"
};
ViewPager viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager(), fragments, fragmentTitles);
viewPager.setAdapter(adapter);
TabLayout tabLayout = (TabLayout)findViewById(R.id.tabLayout);
tabLayout.setupWithViewPager(viewPager);
}
This construction let me view the fragments like in e.g WhatsApp(gesture-controlled).
Nonetheless, I want to control the contents of the WebViews in the WebViewFragments in the MainActivty. But I got no idea, how to access the WebViews externally. The two webviews have IDs:
webView1 //and
webView2
When I try them to access with help of the
findViewById(R.id.webView1)
method, I get null as response.
Now, how I can access these views from the MainActivity?
WebViewFragment
private WebView webView;
public onViewCreated(....) {
webView = view.findViewBy(R.id.webview);
....
}
public WebView getWebView() {
return webView;
}
Activity
WebViewFragment webViewFragment1 = WebViewFragment.newInstance(1);
WebView webView = webViewFragment1.getWebView();
webView.... //WebView Stuff
Please make sure that the 2 Fragments are inflated and not Null.

SetOnClickListener cause unable to start acitivity component

I currently making a application however, I'm faced unable to start the activity componentsInfo. I found a lots of solutions on the net, but none of them is works for me. I try to clean my project file and restart Eclipse, errors still occur. I try to debug with DDBS, and find a 'setOnClickListener' had something wrong. I try to edit it. But error still occur.
Note: Implements view.OnClickListener didn't works for me also.
public class MainActivity extends Activity{
private Button mBreakfast;
private Button mLunch;
private Button mDinner;
private Button mSnack;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (savedInstanceState == null) {
getFragmentManager().beginTransaction()
.add(R.id.container, new PlaceholderFragment()).commit();
}
mBreakfast = (Button) findViewById(R.id.btn_breakfast);
mLunch = (Button) findViewById(R.id.btn_lunch);
mDinner = (Button) findViewById(R.id.btn_dinner);
mSnack = (Button) findViewById(R.id.btn_snack);
mBreakfast.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(MainActivity.this,RestInfoFragment.class);
startActivity(intent);
}
});
}
}
And well it is probably a mistake to import!
if so:
import android.content.DialogInterface.OnClickListener;
change to:
import android.view.View.OnClickListener;
RestInfoFragment is a fragment, and you want to start it on click of the button?
Fragments aren't invoked like Activities using Intents.To start a fragment, use this code:
getFragmentManager().beginTransaction()
.replace(R.id.container, new RestInfoFragment).commit();
From your naming , I guess the RestInfoFragment is a Fragment, and you are using a button click in another activity to navigate to the RestInfoFragment, like an Activity.
You can't use Fragment as an activity. Either change your RestInforFragment to extend a FragmentActivity or inflate the fragment in a view.
If you extends FragmentActivity , you don't need any changes to the current MainActivity.
If you are using Fragment
getFragmentManager().beginTransaction()
.replace(R.id.container, new RestInfoFragment).commit();
If your RestInfoFragment is using the whole screen , I recommend the use of FragmentActivity.

Creating an android fragment that opens another fragment based on button activity

I'm trying to create an activity that adds a dynamic fragment at runtime. From that fragment I want to be able to open six other fragments on button click. [Going to use a case to implement this most likely]
Think of it as a windows 8 UI; with 6 buttons, each one opens a new fragment.
Unfortunately I have no idea how to go about this. I can't seem to get the button to pass data back to the main activity. I've also lost quite a bit of my code due to a git mishap. Here's what I recreated.
If you have any tips on coding style, syntax, java, OO- those are all welcome too. I'm coming from a C background. My end goal would be to create a replaceFragment(Frag) method for some easy syntactic sugar later on. Though I couldn't implement that with any success so far.
Another small question with fragments - I'm trying to add them dynamically at run-time - do I need to create all of them at run time? So each one needs a .add [Drink fragment, Menu fragment] or do I just need to do the .replace
SingleFragmentActivity.java
public abstract class SingleFragmentActivity extends FragmentActivity{
protected abstract Fragment createFragment();
FragmentManager fm = getSupportFragmentManager();
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE); //Lock screen orientation for app
Fragment frag = fm.findFragmentById(R.id.fragment_container);
fm.beginTransaction()
.add(R.id.fragment_container,frag)
.commit();
}
}
Customer_Activity.java
public class Customer_Activity extends SingleFragmentActivity {
public static Context appContext;
#Override
protected Fragment createFragment() {
return new CustomerSelectionFragment();
}
}
CustomerSelectionFragment
public class CustomerSelectionFragment extends Fragment implements OnClickListener{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.customer_selection_fragment, container, false);
//Buttons should be placed here?
Button btnDrink = (Button) v.findViewById(R.id.Drink);
btnDrink.setOnClickListener(this);
Button btnMenu = (Button) v.findViewById(R.id.Menu);
btnDrink.setOnClickListener(this);
return v;
}
//implement the onClick method here
public void onClick(View v) {
// Perform action on click
switch(v.getId()) {
case R.id.Drink:
//Not sure how to pass "Create Drink Fragment to activity?
break;
case R.id.Menu:
//Pass Create Menu fragment to activity?
break;
}
}
}
Totally ok with people editing my post for good-faith reasons [clarity, etc].
Any communication between fragments should be done via activity . Here is the link to developers site http://developer.android.com/training/basics/fragments/communicating.html , the tutorial is about communicating between fragments and pretty much explains everything.

Categories