Android Studio Change Fragment on Button Click - java

Basically I have two fragments with two classes for each and one main class and main activity. I want to set one of the fragment as the home screen and when I press a button it shows the other fragment and hides the previous one.
Problems:
My first problem is that the application is not even opening, its saying,"Unfortunately Application stopped working".
Next, when I click on my button both the fragments are merging and the last fragment is not disappearing.
My main class:
public class MainActivity extends ActionBarActivity {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
UpcomingProject Upcoming = new UpcomingProject();
fragmentTransaction.replace(android.R.id.content, Upcoming);
fragmentTransaction.commit();
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
#Override
public void onBackPressed() {
}
My Main Activity:
<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:layout_margin="10dp"
tools:context=".MainActivity">
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/upcomingproject"/>
<fragment
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/createproject"/>
</LinearLayout>
My Home page fragment xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
>
<Button
android:id="#+id/button_create"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:text="#string/button_create"
android:layout_margin="15sp"
android:background="#drawable/drawable_buttoncreate"
android:clickable="true" />
</LinearLayout>
My home page fragment java class:
public class UpcomingProject extends Fragment implements View.OnClickListener {
FragmentManager fragmentManager = getFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.upcomingproject, container, false);
Button CreateP = (Button)rootView.findViewById(R.id.button_create);
CreateP.setOnClickListener(this);
return rootView;
}
#Override
public void onClick(View v) {
CreateProject Create = new CreateProject();
fragmentTransaction.replace(android.R.id.content, Create);
fragmentTransaction.commit();
}
}
My second fragment, the one I want to switch too on the click of a button:
public class CreateProject extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView;
rootView = inflater.inflate(R.layout.createproject, container, false);
return rootView;
}
}
And the second fragment's xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="20dp"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:scrollbarAlwaysDrawVerticalTrack="true">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp">
<TextView android:id="#+id/name_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name_input"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_message"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_message"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/contact_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/contact_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/contact_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="number"
android:hint="#string/contact_description"/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp">
<TextView android:id="#+id/email_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/email_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/email_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="textEmailAddress"
/>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/category_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/category_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<RadioGroup
android:id="#+id/radioGroup"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
>
<RadioButton android:id="#+id/radio_individual"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_individual"
/>
<RadioButton android:id="#+id/radio_NPO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NPO"
/>
<RadioButton android:id="#+id/radio_NGO"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:hint="#string/radio_NGO"
/>
</RadioGroup>
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/title_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/title_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:inputType="text" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingTop="10dp"
android:layout_marginBottom="10dp"
>
<TextView android:id="#+id/description_input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/description_input"
android:layout_gravity="top"
android:textSize="20sp"/>
<EditText android:id="#+id/edit_description"
android:layout_weight="1"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:hint="#string/edit_description" />
</LinearLayout>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:paddingTop="10dp"
android:gravity="center"
android:layout_marginBottom="10dp">
<Button android:id="#+id/button_register"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_register"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
<Button android:id="#+id/button_reset"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/button_reset"
android:layout_margin="10sp"
android:background="#drawable/drawable_buttoncreate"/>
</LinearLayout>
</LinearLayout>
</ScrollView>

Try to use getSupportFragmentManager() in your Main Activity to replace first fragment. And getChildFragmentManager() when you replace second fragment

This is a very late reply, but I'll give it in case anyone else hits the same issue.
The reason for this problem was that you have explicitly defined the fragment in your xml file. If you want to swap the fragment it's best to define it as a FrameLayout and then swap it in your onCreate method using e.g.
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
FragmentTransaction fts = getSupportFragmentManager().beginTransaction();
fts.add(R.id.content, UpcomingProject.newInstance());
fts.commit();
setContentView(R.layout.activity_main);
}
This is all explained very well and in much more detail in the link in hrskrs's comment above (https://github.com/codepath/android_guides/wiki/Creating-and-Using-Fragments)

Related

No view found for ID for fragment HomeFragment

i have this problem for a while now seem i cant solve it
HomeFragment.java
public class HomeFragment extends Fragment {
/*- 01 Class Variables -------------------------------------------------------------- */
private View mainView;
private Cursor listCursor;
// Action buttons on toolbar
private MenuItem menuItemAddFood;
// Holding variables
private String currentDateYear = "";
private String currentDateMonth = "";
private String currentDateDay = "";
private String currentFoodId;
private String currentFoodName;
private String currentFdId;
private boolean lockPortionSizeByPcs;
private boolean lockPortionSizeByGram;
/*- 02 Fragment Variables ----------------------------------------------------------- */
private static final String ARG_PARAM1 = "param1";
private static final String ARG_PARAM2 = "param2";
// TODO: Rename and change types of parameters
private String mParam1;
private String mParam2;
private OnFragmentInteractionListener mListener;
/*- 03 Constructur ------------------------------------------------------------------ */
public HomeFragment() {
// Required empty public constructor
}
/*- 04 Creating Fragment ------------------------------------------------------------- */
// TODO: Rename and change types and number of parameters
public static HomeFragment newInstance(String param1, String param2) {
HomeFragment fragment = new HomeFragment();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
//OnCREATEEEEEE
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
//---------Set Title----------
((MainActivity)getActivity()).getSupportActionBar().setTitle("Home");
}
/*- 05 on Activity Created ---------------------------------------------------------- */
// Run methods when started
// Set toolbar menu items
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
/* Set title */
((MainActivity)getActivity()).getSupportActionBar().setTitle("Home");
// getDataFromDbAndDisplay
initializeHome();
// Create menu
setHasOptionsMenu(true);
} // onActivityCreated
/*- 06 On create view ---------------------------------------------------------------- */
// Sets main View variable to the view, so we can change views in fragment
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
mainView = inflater.inflate(R.layout.fragment_home, container, false);
return mainView;
}
/*- 07 set main view ----------------------------------------------------------------- */
// Changing view method in fragmetn
private void setMainView(int id){
LayoutInflater inflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
mainView = inflater.inflate(id, null);
ViewGroup rootView = (ViewGroup) getView();
rootView.removeAllViews();
rootView.addView(mainView);
}
/*- 08 on Create Options Menu -------------------------------------------------------- */
// Creating action icon on toolbar
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
// Inflate menu
MenuInflater menuInflater = ((MainActivity)getActivity()).getMenuInflater();
inflater.inflate(R.menu.menu_home, menu);
// ((MainActivity)getActivity()).getMenuInflater().inflate(R.menu.menu_home, menu);
// Assign menu items to variables
menuItemAddFood = menu.findItem(R.id.menu_action_add_food);
}
/*- 09 on Options Item Selected ------------------------------------------------------ */
// Action icon clicked on
// Menu
#Override
public boolean onOptionsItemSelected(MenuItem menuItem) {
int id = menuItem.getItemId();
if (id == R.id.menu_action_add_food) {
AddFoodToDiarySelectedMealNumber();
}
return super.onOptionsItemSelected(menuItem);
}
MainActivty
public class MainActivity extends AppCompatActivity
implements NavigationView.OnNavigationItemSelectedListener,
AddFoodToDiaryFragment.OnFragmentInteractionListener,
CategoriesFragment.OnFragmentInteractionListener,
FoodFragment.OnFragmentInteractionListener,
GoalFragment.OnFragmentInteractionListener,
HomeFragment.OnFragmentInteractionListener,
ProfileFragment.OnFragmentInteractionListener{
private AppBarConfiguration mAppBarConfiguration;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/* Toolbar */
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
/* Inialize fragmet */
Fragment fragment = null;
Class fragmentClass = null;
fragmentClass = HomeFragment.class;
try {
fragment = (Fragment) fragmentClass.newInstance();
} catch (Exception e) {
e.printStackTrace();
}
FragmentManager fragmentManager = getSupportFragmentManager();
fragmentManager.beginTransaction().replace(R.id.flContent, fragment).commit();
/* Navigation */
DrawerLayout drawer = (DrawerLayout) findViewById(R.id.drawer_layout);
ActionBarDrawerToggle toggle = new ActionBarDrawerToggle(
this, drawer, toolbar, R.string.navigation_drawer_open, R.string.navigation_drawer_close);
drawer.addDrawerListener(toggle);
toggle.syncState();
// Navigation items
NavigationView navigationView = (NavigationView) findViewById(R.id.nav_view);
navigationView.setNavigationItemSelectedListener(this);
//stetho---
Stetho.initializeWithDefaults(this);
new OkHttpClient.Builder().addNetworkInterceptor(new StethoInterceptor()).build();
/* Database */
DBAdapter db = new DBAdapter(this);
db.open();
/* Setup for food */
// Count rows in food
int numberRows = db.count("food");
if(numberRows < 1){
// Run setup
Toast.makeText(this, "Loading setup...", Toast.LENGTH_LONG).show();
DBSetupInsert setupInsert = new DBSetupInsert(this);
setupInsert.insertAllCategories();
setupInsert.insertAllFood();
Toast.makeText(this, "Setup completed!", Toast.LENGTH_LONG).show();
}
//check if there users in user table
//count rows in user table
numberRows = db.count("users");
Intent intent;
if(numberRows <1 ){
//sign Up
Toast.makeText(this, " you only few steps away from Signing up ...!", Toast.LENGTH_LONG).show();
intent = new Intent(MainActivity.this, SignUp.class);
startActivity(intent);
}else{
intent = new Intent(MainActivity.this, EditProfileGoal.class);
startActivity(intent);
}
db.close();
}
home_fragment.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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="fill_parent"
android:layout_height="fill_parent"
android:fitsSystemWindows="true">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="18dp"
android:layout_marginRight="18dp"
android:layout_marginBottom="18dp"
android:layout_marginLeft="18dp">
<TextView
android:id="#+id/text_home"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<!-- Breakfast -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineBreakfast"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddBreakfast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineBreakfast"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/break_fast"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergyBreakfast"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutBreakfastItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //Breakfast -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:id="#+id/textView"
android:background="#000"></TextView>
<!-- Lunch -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineLunch"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddLunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineLunch"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/lunch"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergyLunch"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutLunchItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //Lunch -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- Before training -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineBeforeTraining"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddBeforeTraining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineBeforeTraining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/before_training"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergyBeforeTraining"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutBeforeTrainingItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //BeforeT raining -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- After training -->
<TableLayout
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineAfterTraining"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddAfterTraining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineAfterTraining"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/after_training"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergyAfterTraining"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutAfterTrainingItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //After Training -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- Dinner -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineDinner"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddDinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineDinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/dinnner"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergyDinner"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutDinnerItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //Dinner -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- Snacks -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineSnacks"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddSnacks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineSnacks"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginBottom="10dp"
android:text="#string/snacks"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergySnacks"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutSnacksItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //Snacks -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- Supper -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineSupper"
android:layout_width="fill_parent"
android:background="#A9A9A9 "
android:layout_height="wrap_content">
<ImageView
android:id="#+id/imageViewAddSupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/ic_menu_add" />
<TextView
android:id="#+id/textViewHeadlineSupper"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="80"
android:layout_marginTop="10dp"
android:text="#string/supper"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewEnergySupper"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="10"
android:text="#string/energy"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<TableLayout
android:id="#+id/tableLayoutSupperItems"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</TableLayout>
<!-- //Supper -->
<TextView
android:layout_width="match_parent"
android:layout_height="3dp"
android:background="#000"></TextView>
<!-- todays reuslt -->
<!-- total -->
<TableLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TableRow
android:id="#+id/tableRowHeadlineTotal"
android:layout_width="fill_parent"
android:background=" #D3D3D3 "
android:layout_height="wrap_content">
<TextView
android:id="#+id/textViewHeadlineGoalWithActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="#string/goal_with_activity"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewFood"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/food_calories"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewSum"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/remaining"
android:textSize="18sp" />
</TableRow>
<!---->
<TableRow
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/headcell"
>
<TextView
android:id="#+id/textViewBodyGoalWithActivity"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="10dp"
android:text="#string/goal_with_activity"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewBodyFood"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/food_calories"
android:textSize="18sp" />
<TextView
android:id="#+id/textViewBodyRemain"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1"
android:text="#string/remaining"
android:textSize="18sp" />
</TableRow>
</TableLayout>
<!-- end todays reuslt -->
</LinearLayout>
</ScrollView>
main_activity.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.drawerlayout.widget.DrawerLayout 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:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:openDrawer="start">
<include
layout="#layout/app_bar_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.google.android.material.navigation.NavigationView
android:id="#+id/nav_view"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_gravity="start"
android:fitsSystemWindows="true"
app:headerLayout="#layout/nav_header_fragment"
app:menu="#menu/activity_main_drawer" />
</androidx.drawerlayout.widget.DrawerLayout>
the error
E/AndroidRuntime: FATAL EXCEPTION: main
Process: com.example.appdiet33, PID: 6829
java.lang.RuntimeException: Unable to start activity ComponentInfo{com.example.appdiet33/com.example.appdiet33.MainActivity}: java.lang.IllegalArgumentException: No view found for id 0x7f09009d (com.example.appdiet33:id/flContent) for fragment HomeFragment{35b333b} (1260b40b-35da-49bd-9060-13848371c9d8) id=0x7f09009d}
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2665)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2726)
at android.app.ActivityThread.-wrap12(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1477)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:154)
at android.app.ActivityThread.main(ActivityThread.java:6119)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:886)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:776)
Caused by: java.lang.IllegalArgumentException: No view found for id 0x7f09009d (com.example.appdiet33:id/flContent) for fragment HomeFragment{35b333b} (1260b40b-35da-49bd-9060-13848371c9d8) id=0x7f09009d}
at androidx.fragment.app.FragmentStateManager.createView(FragmentStateManager.java:305)
i have tried to use getChildFragmentManager() instead of getSupportFragmentManager() like similiar question asked
in HomeFragment
question asked but it didn't work for me
any suggestion please?
Edit:
i have solved turned out i have misspelled the id inside app_bar_main.xml and fixed it

setOnItemClickListener() not working on ExpandableHeightGridView in Fragment

Hello good morning in my view i cant handled the setOnItemClickListener
I am trying to create an application which dynamically lists installed applications in a gridview.I am able to display the applications in a gridview form but not able to make these items clickable. Nothing happens when i click these items. The code inside the setOnItemClickListener does not get triggered on click event.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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=".Tab2profile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
//my scrollview
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/MainRLyout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/girislayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/usernameTv"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:textSize="#dimen/text_size_Orta"
android:textColor="#color/cardview_dark_background"
android:layout_height="wrap_content"
tools:ignore="PrivateResource" />
<Button
android:id="#+id/Login_LogOut_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/logout_rounded"
android:drawableRight="#drawable/logout_icon"
android:padding="0dp"
android:text="#string/Logout_Text_str"
android:textColor="#android:color/white"
android:textSize="12sp"
tools:ignore="RelativeOverlap,RtlHardcoded" />
</RelativeLayout>
//in here i cant handled the item click funktşon
<syr13.comapp4.Custum_Adapters.ExpandableHeightGridView
android:id="#+id/Your_Apps_Li"
android:numColumns="3"
android:layout_below="#id/Your_Apps_Tv"
android:horizontalSpacing="6dp"
android:layout_margin="5dp"
android:verticalSpacing="6dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
</syr13.comapp4.Custum_Adapters.ExpandableHeightGridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</FrameLayout>
I think you can Change your Code
from
button.setOnItemClickListener(this);
To
button.setOnClickListener(this);
for example :-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
//Do Something
break;
}
}
}

Dynamic TextView not updating in fragment

I'm trying to set a TextView that's in a fragment but the text won't update in the view. The text is 1 of 2 pages in a ViewPager. The ViewPager is inside another fragment that has a button which when clicked uses an interface to the MainActivity that calls the changeText() that's inside the fragment with my text.
Fragment Inside ViewPager:
public class PlaceholderFragment extends Fragment {
public TextView textFV;
public PlaceholderFragment() {}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.tab_pull, container, false);
textFV = rootView.findViewById(R.id.textFV);
return rootView;
}
#Override
public void onViewCreated(#NonNull View view, Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
public void changeText() {
textFV.setText("Test"); - Won't update view.
System.out.println(textFV.getText()); - Shows correct text.
}
}
Relevant part of Fragment that contains ViewPager:
OnHeadlineSelectedListener callback;
public void setOnHeadlineSelectedListener(OnHeadlineSelectedListener callback) {
this.callback = callback;
}
public interface OnHeadlineSelectedListener {
void onArticleSelected();
}
//...
#Override
public void onClick (View v) {
callback.onArticleSelected();
}
Relevant MainActivity Part:
public void onArticleSelected() {
PlaceholderFragment articleFrag = (PlaceholderFragment) getSupportFragmentManager().findFragmentById(R.id.viewpager);
if (articleFrag != null) {
articleFrag.changeText();
}
}
Here is the layout that contains the TextView:
<android.support.constraint.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior">
<LinearLayout
android:id="#+id/tab_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />
</LinearLayout>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.constraint.ConstraintLayout>
Here's where the button and ViewPager is. The button is the ImageButton:
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white">
<android.support.v7.widget.Toolbar
android:id="#+id/pullToolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:title="Information"
app:titleTextAppearance="#style/TextAppearance.Widget.AppCompat.Toolbar.Title.Montserrat">
<ImageButton
android:id="#+id/toolbarButtonPull"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="end"
android:layout_marginEnd="1dp"
android:background="#drawable/button_toolbar_default"
android:contentDescription="Pull"
android:padding="15dp"
android:src="#drawable/ic_arrow_downward_black_24dp" />
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabMode="fixed"
app:tabTextAppearance="#style/TextAppearance.Design.Tab.NoCaps">
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab1" />
<android.support.design.widget.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tab2" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="android.support.design.widget.AppBarLayout$ScrollingViewBehavior" />
</android.support.design.widget.CoordinatorLayout>
Looks like your textFV is inside layout that has visibility gone. Can it be the problem?
<LinearLayout
android:id="#+id/containerPull"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
**android:visibility="gone">**
<TextView
android:id="#+id/textsLabel"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="#dimen/label_left_margin"
android:layout_marginTop="5dp"
android:text="#string/label_common"
android:textColor="#color/colorLabelText"
android:textSize="18sp" />
<TextView
android:id="#+id/textFV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hello, World!"
android:textColor="#android:color/black"
android:textSize="18sp" />

ViewPager doesn't show Fragment

I'm trying to implement a Slinding Menu by using Pager Sliding Tab Strip (com.astuetz:pagerslidingtabstrip).
I set up my xml layout of the main screen:
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".97">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/logo_b"
android:src="#drawable/logo_b"
android:layout_weight=".05"
android:layout_gravity="right"
android:layout_marginRight="-15dp" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scrollMe"
android:layout_weight=".95"
android:fillViewport="false">
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal">
<com.astuetz.PagerSlidingTabStrip
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="48dip"
app:pstsIndicatorColor="#19B5FE"
app:pstsShouldExpand="true"
app:pstsTextAllCaps="false"
app:pstsIndicatorHeight="1dp"
/>
<android.support.v4.view.ViewPager
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</ScrollView>
</LinearLayout>
This xml is included into another View, but it handle the View Pager.
To handle the init of ViewPager and PagerSlidingTapStrip i have a FragmentActivity where during onCreate i have:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sliding_menu);
pager = (ViewPager) findViewById(R.id.pager);
CustomPagerAdapteradapter = new CustomPagerAdapter(getSupportFragmentManager());
adapter.setContext(getApplicationContext());
pager.setAdapter(adapter);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
tabs.setViewPager(pager);
}
And the adapter is:
public static class CustomPagerAdapteradapter extends FragmentPagerAdapter implements IconTabProvider {
private int[] ICONS = {
R.drawable.icon_menu0,
R.drawable.icon_menu1,
R.drawable.icon_menu2,
R.drawable.icon_menu3,
R.drawable.icon_menu4
};
private static Context CONTEXT;
private Fragment fragment[] = new Fragment[5];
public CustomPagerAdapteradapter (FragmentManager fm) {
super(fm);
for(int i = 0; i < 5; ++i) {
fragment[i] = Profile.newInstance();
}
}
public void setContext(Context c) { CONTEXT = c; }
#Override
public int getCount() {
return ICONS.length;
}
#Override
public Fragment getItem(int position) {
Toast.makeText(CONTEXT, "" + position, Toast.LENGTH_SHORT).show();
switch (position) {
case 0:
return fragment[position];
case 1:
return fragment[position];
case 2:
return fragment[position];
case 3:
return fragment[position];
case 4:
return fragment[position];
case 5:
return fragment[position];
default:
return fragment[position];
}
}
#Override
public int getPageIconResId(int position) {
return ICONS[position];
}
}
The Profile Fragment xml is:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight=".90">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".20">
<!-- Not filled yet -->
</RelativeLayout>
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight=".70"
android:background="#drawable/radius_background"
android:layout_margin="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="15dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="info#example.com"
android:id="#+id/user_mail" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="15/10/54"
android:id="#+id/user_birth" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Italy"
android:id="#+id/user_country" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Pa"
android:id="#+id/user_town_short" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Palermo"
android:id="#+id/user_town" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginTop="5dp"
android:textColor="#FFF"
android:textSize="16dp"
android:text="Male"
android:id="#+id/user_sex" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight=".05">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:textColor="#FFF"
android:background="#18333E"
android:text="Edit/Update Data"
android:id="#+id/updateData"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
Profile fragment onCreateView just inflate the xml:
return inflater.inflate(R.layout.fragment_profile, container, false);
Even if the code seems to be ok, I never see profile on the View Pager. What's wrong?
Final answer: remove the ScrollView as it is blocking with ViewPager.

Why the tab widget is above the content in android?

Currently I work on a tabhost layout.
In most android app the layout is:
tab1 | tab 2
____________
Tab 1 content
(if I press on tab1)
However, what I would like to achieve is
Tab 1 content
(if I press on tab1)
____________
tab1 | tab 2
Here is the main xml (backbone of app) :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<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" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"
android:orientation="horizontal" />
</LinearLayout>
The tab content layout
<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"
android:background="#31152C"
android:gravity="center"
tools:context=".MonitoringActivity" >
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/home" />
</LinearLayout>
</ScrollView>
</LinearLayout>
The main class:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ctx = this;
gs = (MyApp) getApplication();
tabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
tabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
tabHost.addTab(tabHost.newTabSpec("main").setIndicator(""),Home.class, null);
tabHost.addTab(tabHost.newTabSpec("carpark").setIndicator("",getResources().getDrawable(R.drawable.btn_park)), CarPark.class,null);
tabHost.addTab(tabHost.newTabSpec("shop").setIndicator("",getResources().getDrawable(R.drawable.btn_shop)), Shop.class,null);
tabHost.getTabWidget().setDividerDrawable(null);
tabHost.setCurrentTab(0);
}
Thanks for helping
you can add tabwidget to the bottom using the given code..
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent" >
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#android:id/tabs" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
</TabHost>
Found out the solution using the support fragment tab host:
<?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="vertical" >
<FrameLayout
android:id="#+id/realtabcontent"
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_weight="1" />
<android.support.v4.app.FragmentTabHost
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" />
</android.support.v4.app.FragmentTabHost>
</LinearLayout>
Try this way,hope this will help you to solve your problem.
activity_main.xml
<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">
<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"/>
<TabWidget
android:id="#android:id/tabs"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
</LinearLayout>
</android.support.v4.app.FragmentTabHost>
tab1_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Home"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
tab2_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Car Park"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
tab3_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#31152C"
android:gravity="center">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:textSize="20sp"
android:text="Shop"/>
</LinearLayout>
</ScrollView>
</LinearLayout>
MainActivity.java
public class MainActivity extends FragmentActivity {
private FragmentTabHost mTabHost;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mTabHost = (FragmentTabHost)findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
mTabHost.addTab(mTabHost.newTabSpec("tab1").setIndicator("Tab 1",getResources().getDrawable(R.drawable.ic_launcher)),
Home.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab2").setIndicator("Tab 2",getResources().getDrawable(R.drawable.ic_launcher)),
CarPark.class, null);
mTabHost.addTab(mTabHost.newTabSpec("tab3").setIndicator("Tab 3", getResources().getDrawable(R.drawable.ic_launcher)),
Shop.class, null);
}
}
Home.java
public class Home extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab1_view, container, false);
return V;
}
}
CarPark.java
public class CarPark extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab2_view, container, false);
return V;
}
}
Shop.java
public class Shop extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState) {
View V = inflater.inflate(R.layout.tab3_view, container, false);
return V;
}
}

Categories

Resources