I'm noob on android development, and im trying to do a simple app, for details about certain products and stuff.
i have the the following layout:
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+android:id/realtabcontent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
There are 2 tabs on that tabHost navigation, each one extends fragment like this
package co.com.smartmedia.vademecumtg;
import co.com.smartmedia.vademecumtg.R;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class LineasTab extends Fragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
return inflater.inflate(R.layout.tab2, container, false);
}
}
In the first tab of that navigation i have a listView, and each item of that list, must call another activity that will show details about it.
im calling a new activity by calling this method from an itemClickListener:
public void intentForMedView(int pos){
Log.w("debbuging", "start");
Intent intent = new Intent(getActivity().getApplicationContext(), MedViewActivity.class);
intent.putExtra(EXTRA_MEDTITLE, array_sort.get(pos).getNombre());
intent.putExtra(EXTRA_MEDCONTENT, array_sort.get(pos).getLinea());
Log.w("debbuging", "send intent"+intent);
startActivity(intent);
}
and the activity that i'm calling is the following:
package co.com.smartmedia.vademecumtg;
import co.com.smartmedia.vademecumtg.R;
import android.app.Activity;
import android.app.Fragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MedViewActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_med_view);
Intent intent = getIntent();
String title = intent.getStringExtra(MedicamentosTab.EXTRA_MEDTITLE);
setTitle(title);
TextView text = (TextView) findViewById(R.id.text);
String lineName = intent.getStringExtra(MedicamentosTab.EXTRA_MEDCONTENT);
text.setText(lineName);
}
}
My problem is:
When the detail activity is shown, my navigation Disappears (the tabHost navigation), and i don't want it....
some tips or ways to do it ?
thaks...
I got the answer now, The way to accomplish this architecture, is to use FragmentTransaction
http://developer.android.com/guide/components/fragments.html
Cause it isn't necessary another activity... just play with the fragments of the UI.
FragmentManager fragmentManager = getFragmentManager()
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
And take care with the android version cause android.support.v4.app.FragmentManager cannot cast to the android.app.FragmentManager
Hope this be usefull
Related
I am hardly trying to create a simple application with a top menu and a changeable view below (by pressing the buttons in the menu fragment we change the view of the fragment below).
So, I have 2 fragments inside the main view but when trying to run the application in the emulator I get an error like:
Cause by android.app (bla bla bla, piece of crap Eclipse doesn't even allow copying the errors):
Trying to instantiate a class com.example.android.topmenu that is not a fragment
So, these are my XML layouts:
main.xml
<?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:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<fragment
android:id="#+id/menuFragment"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:name="com.example.android.topmenu" >
</fragment>
<fragment
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.example.android.bottomcontent" >
</fragment>
</LinearLayout>
topmenu.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:orientation="horizontal" >
<Button
android:id="#+id/Button1"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
bottom_content.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#+string/content_text" />
</LinearLayout>
and these are the classes for the main activity and the fragments
main_activity
package com.example.android;
import com.example.android.R;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class OLife extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
// The activity is being created
super.onCreate(savedInstanceState);
// Set view
setContentView(R.layout.main);
}
#Override
protected void onDestroy() {
super.onDestroy();
// The activity is about to be destroyed.
super.onDestroy();
// Stop method tracing that the activity started during onCreate()
android.os.Debug.stopMethodTracing();
}
}
topmenu
package com.example.android;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMenu extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.topmenu, container, false);
return view;
}
}
bottomcontent
package com.example.android;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class OLifeMain extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.bottom_content, container, false);
return view;
}
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
}
}
You should be using FragmentActivity instead of Activity that is because you are using support Fragments and multiple Fragments in your activity main
Edit
You can now use the Appcompat support library and extends AppCompatActivity to support toolbar and fragment for lower api.
In my case it turned out, I was doing stuff in onCreate in the wrong order:
setContentView(R.layout.activity_qr_code_scan);
super.onCreate(savedInstanceState);
instead of
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_qr_code_scan);
As you are using fragments in your layout and I suggest you to extend your class from fragment or fragment activity.
I have to create an application in which I have to work with Fragment.
In the MainActivity, there is a webView. From the SecondActivity I
have starting using Fragment.
Here is the code of SecondActivity:
package com.dev.testapp;
import android.app.Activity;
import android.app.Fragment;
import android.app.FragmentManager;
import android.app.FragmentTransaction;
import android.os.Bundle;
import android.view.View;
public class Second extends Activity {
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
}
public void choosefragment(View view) {
Fragment fg;
if(view == findViewById(R.id.secondbtn2)) {
fg = new SecondFragment();
}
else
{
fg = new FirstFragment();
}
FragmentManager fm = getFragmentManager();
FragmentTransaction fragmentTransaction = fm.beginTransaction();
fragmentTransaction.replace(R.id.frag1, fg);
fragmentTransaction.commit();
}
}
Hhere is its second.xml file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#ffffff" >
<Button
android:id="#+id/secondbtn1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="first fragment"
android:onClick="chooseFragment" />
<Button
android:id="#+id/secondbtn2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="second fragment"
android:onClick="chooseFragment"/>
<fragment
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:name="com.dev.testapp.fragment1"
android:id="#+id/frag1"
/>
</LinearLayout>
After that I created two more classes for Fragments as it is required
code of FirstFragment:
package com.dev.testapp;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FirstFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment1,container, false);
}
}
And here is the SecondFragment:
package com.dev.testapp;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class SecondFragment extends Fragment{
public View onCreate(LayoutInflater inflater,
ViewGroup container , Bundle savedInstanceState) {
//Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment2,container, false);
}
}
When I am done with all this, my SecondActivity is showing an error that I don't know how to handle:
As noted in previous answers, your problem is incorrect Fragment library for FirstFragment.
Change line 5 of FirstFragment.java:
import android.support.v4.app.Fragment;
to:
import android.app.Fragment;
Your second fragment is of the type android.app.Fragment while your first fragment is android.support.v4.app.Fragment.
your activity use this import android.app.Fragment;
your fragment is import android.support.v4.app.Fragment;
So you either have both extending the Fragment in the app package or in the support library
I know its probably something simple I am missing here, but for some reason I can't seem to get my layout to display when I run my app using the emulator. Any feedback would be great. Thanks
my fragment
package com.pctoolman.planme.app;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
public class PlanMeMainFragment extends Fragment {
private Button mNewButton, mExistingButton;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.planme_main_fragment, container, false);
mNewButton = (Button)v.findViewById(R.id.new_event_button);
mNewButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent myIntent = new Intent(getActivity(), NewEventSetupActivity.class);
getActivity().startActivity(myIntent);
}
});
return v;
}
}
my layout file:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#ff3a0b">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="#string/plan_me"
android:textSize="36dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="2"
android:gravity="center|top"
android:textAlignment="center"
android:textColor="#2c58ff"
android:id="#+id/textView">
</TextView>
</LinearLayout>
<Button
android:id="#+id/new_event_button"
android:layout_height="wrap_content"
android:layout_width="wrap_content"
android:layout_gravity="bottom"
android:padding="5dp"
android:text="#string/new_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="160dp" />
<Button
android:id="#+id/existing_event"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"
android:text="#string/existing_event"
android:layout_centerHorizontal="true"
android:layout_marginTop="260dp"/>
</RelativeLayout>
here is the activity file
package com.pctoolman.planme.app;
import android.support.v4.app.Fragment;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
public class PlanMeMainActivity extends FragmentActivity {
#Override
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.planme_main_activity);
}
}
You should make a transatcion via FragmentManager to show your fragment in the activity. Suppose you have container (FrameLayout, for example) with id = fragment_container in your planme_main_activity.xml. If so, you should add the following after setContentView(R.layout.planme_main_activity);:
getSupportFragmentManager()
.beginTransaction()
.replace(R.id.fragment_container, new PlanMeMainFragment())
.commit();
But if you declared fragment directly in XML, please post your planme_main_activity.xml
Maybe it's because you haven't positioned the buttons properly. It's easy to move things around in a relative layout. You don't need to use gravity. You can use:
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_above="#+id/btn"
android:layout_below="#+id/btn"
I'm assuming you cut out the closing tag for the RL.
I am trying to allow my app to call functions from my fragment classes in the fragmentactivity. However I am having a lot of issues just finding the fragment. Right now I can find one fragment by id which I think is the fragment hosting the tabs. But when I get the child fragment manager I get returned a null. Can anyone help?
This is the fragment activity
package com.example.profileactivity;
import android.os.Bundle;
import android.app.Activity;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.widget.TabHost.TabSpec;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTabHost;
public class ProfileActivity extends FragmentActivity {
// Fragment TabHost as mTabHost
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this,getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("user").setIndicator("User",
getResources().getDrawable(R.drawable.ic_user_tab)),
UserProfileTab.class,null);
mTabHost.addTab(mTabHost.newTabSpec("payment").setIndicator("Payment",
getResources().getDrawable(R.drawable.ic_payment_tab)),
PaymentInfoTab.class,null);
}
public void buttonClick(View view){
FragmentManager fm = getSupportFragmentManager();
if(fm.findFragmentById(R.id.realtabcontent) == null){
Log.d("TABHOST", "didnt find fragment");
}
else{
Log.d("TABHOST", "found fragment");
//PaymentInfoTab paymentTab = (PaymentInfoTab)fm.findFragmentByTag("payment");
//paymentTab.boom();
if(fm.findFragmentById(R.id.realtabcontent).getChildFragmentManager().findFragmentByTag("payment") != null){
PaymentInfoTab paymentTab = (PaymentInfoTab)fm
.findFragmentById(R.id.realtabcontent)
.getChildFragmentManager()
.findFragmentByTag("payment");
paymentTab.boom();
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.profile, menu);
return true;
}
}
This is the xml for the fragmentactivity.
<android.support.v4.app.FragmentTabHost
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_weight="0"/>
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
Try using the tag on the activity's FragmentManager directly.
The complete reference of your example seems to be this. Follow this example with more details, you will see that the writer keeps track of tabs and their tag.
I had the similar issue with fetching fragments after tab change which always returned null. Posting Runnable at the end of TabHost message queue did the trick:
tabHost.post(new Runnable() {
#Override
public void run() {
Fragment fragment = (Fragment) getChildFragmentManager().findFragmentByTag(tag);
fragment.updateList(newList);
}
});
Using fragments for the first time. As fragment in itself an activity, going by the documentation, I think I've written it correctly in the manifest.xml.
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/example_fragment"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<fragment class="com.test.methods.ExampleFragment"
android:id="#+id/list"
android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
<FrameLayout android:id="#+id/details" android:layout_weight="1"
android:layout_width="0px"
android:layout_height="match_parent" />
</LinearLayout>
My fragment:
package com.test.methods;
import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import com.crumbin.main.R;
public class ExampleFragment extends ListFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.id.list,
Shakespeare.TITLES));
return inflater.inflate(R.layout.example_fragment, container, false);
}
}
My Activity:
package com.test.methods;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
public class DemoUserActivity extends FragmentActivity{
protected void OnCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
if (savedInstanceState == null)
{
ExampleFragment details = new ExampleFragment();
details.setArguments(getIntent().getExtras());
getSupportFragmentManager().beginTransaction().add(
android.R.id.content,details).commit();
}
}
}
My intent:
intent = new Intent().setClass(this, DemoUserActivity.class);
spec = tabHost.newTabSpec("list").setIndicator("FragmentTest",
res.getDrawable(R.drawable.icon_list_tab))
.setContent(intent);
tabHost.addTab(spec);
I get no output out of this. I tried debugging but it is not even reaching my activity file.
No Activity is different thing, and Fragment is different, you can use these to create your application moduler, and replace one with another with minimum efforts, but can not use as a replacement of other, if You want to use a fragment, you need to declare an activity, in that activity you need to add your fragment, individual fragment without Activity can not be used.