Im struggling to add additional fragments to a Tabbed Menu Navigation in my Android App. I've used the default 'Scrollable Tabs + Swipe' provided by ADT.
Below is my MainMenu.java
package com.example.universitybudget_v2_1;
import java.util.Locale;
import com.example.universitybudget_v2.R;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.app.NavUtils;
import android.support.v4.view.ViewPager;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
public class MainMenu extends FragmentActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link android.support.v4.app.FragmentPagerAdapter} derivative, which
* will keep every loaded fragment in memory. If this becomes too memory
* intensive, it may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main_menu);
// Create the adapter that will return a fragment for each of the three
// primary sections of the app.
mSectionsPagerAdapter = new SectionsPagerAdapter(
getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main_menu, menu);
return true;
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
// getItem is called to instantiate the fragment for the given page.
// Return a DummySectionFragment (defined as a static inner class
// below) with the page number as its lone argument.
Fragment fragment = new DummySectionFragment();
Bundle args = new Bundle();
args.putInt(DummySectionFragment.ARG_SECTION_NUMBER, position + 1);
fragment.setArguments(args);
return fragment;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
Locale l = Locale.getDefault();
switch (position) {
case 0:
return getString(R.string.title_section1).toUpperCase(l);
case 1:
return getString(R.string.title_section2).toUpperCase(l);
case 2:
return getString(R.string.title_section3).toUpperCase(l);
}
return null;
}
}
/**
* A dummy fragment representing a section of the app, but that simply
* displays dummy text.
*/
public static class DummySectionFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
public static final String ARG_SECTION_NUMBER = "section_number";
public DummySectionFragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.fragment_1, container, false);
TextView dummyTextView = (TextView) rootView.findViewById(R.id.section_label);
dummyTextView.setText(Integer.toString(getArguments().getInt(ARG_SECTION_NUMBER)));
return rootView;
}
}
}
activity_main_menu.xml
<android.support.v4.view.ViewPager xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainMenu"
android:background="#398940">
<!--
This title strip will display the currently visible page title, as well as the page
titles for adjacent pages.
-->
<android.support.v4.view.PagerTitleStrip
android:id="#+id/pager_title_strip"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="top"
android:background="#33b5e5"
android:paddingBottom="4dp"
android:paddingTop="4dp"
android:textColor="#fff" />
</android.support.v4.view.ViewPager>
Fragment_1.xml
<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"
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=".MainMenu$DummySectionFragment"
android:background="#398940">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/section_label"
android:layout_centerHorizontal="true"
android:text="#string/celebrate"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Fragment_2.xml
<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"
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=".MainMenu$DummySectionFragment"
android:background="#398940">
<TextView
android:id="#+id/section_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/section_label"
android:layout_centerHorizontal="true"
android:text="#string/celebrate2"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
My Question is how do I add fragment_2.xml to the slidable tabs menu? I've looked around but most of the examples use 'ActionBarSherlock' which I don't feel the necessity to use to make a working slidable menu and would rather stay with the android default options.
Currently this is all that is=t shows -
I want different sections to display different fragments, your help would be much appreciated! thanks in advance :)
SectionsPagerAdapter.getItem controls which Fragment is retrieved for each page:
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
return getFragment1Instance();
case 1:
return getFragment2Instance();
// etc
}
}
Related
I want to put tabbed layout inside a fragment layout, i have tried many methods but none of them worked.
Things to be noted first are I am already having a navigation drawer with 4 fragments and I want to apply tabbed layout to one of its fragment not All.
This is my JAVA code of the fragment on which i want to use Tabbed layout ,i am using android studio v3.3
package com.androidapp.kjsscapp;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class NoticeBoard extends Fragment {
public NoticeBoard() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_notice_board, container, false);
TabLayout tabLayout = v.findViewById(R.id.NoticeBoard_Tab);
final ViewPager viewPager = v.findViewById(R.id.Mpager1);
tabpageAdapter tabpageAdapter = new tabpageAdapter(getChildFragmentManager(),tabLayout.getTabCount());
viewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return v;
}
}
The currosponding xml file
<?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=".NoticeBoard">
<android.support.design.widget.TabLayout
android:id="#+id/NoticeBoard_Tab"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textAlignment="center"
android:visibility="visible"
app:tabBackground="#color/colorPrimary"
app:tabIndicatorColor="#android:color/holo_blue_dark"
app:tabTextColor="#color/White">
<android.support.design.widget.TabItem
android:id="#+id/TabItem_general"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="General" />
<android.support.design.widget.TabItem
android:id="#+id/TabItem_admissions"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Admissions" />
<android.support.design.widget.TabItem
android:id="#+id/TabItem_scholarship"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Scholarships" />
</android.support.design.widget.TabLayout>
<android.support.v4.view.ViewPager
android:id="#+id/Mpager1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/NoticeBoard_Tab" />
</RelativeLayout>
The tab controller:
package com.androidapp.kjsscapp;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class tabpageAdapter extends FragmentStatePagerAdapter {
int countTab;
public tabpageAdapter(FragmentManager fm, int tabCount) {
super(fm);
this.countTab = countTab;
}
#Override
public Fragment getItem(int i) {
Tab_General gen = new Tab_General();
Tab_Admissions add = new Tab_Admissions();
Tab_ScholarShip sch = new Tab_ScholarShip();
switch (i){
case 0:
return gen;
case 1:
return add;
case 2:
return sch;
default:
return gen;
}
}
#Override
public int getCount() {
return countTab;
}
}
I expect to view the fragments of tablayout inside the fragment of drawer layout as soon as the fragment of drawer is called. Kindly provide the solution if any
I'm having an issue in the Android app that I'm developing where a the text in a Fragment containing a TextView covers the bottom navigation if the text is too long. The issue looks like this:
TextView blocking bottom navigation
Here's the code for the Fragment:
package com.example.xxxxxx.loremipsum;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.Resources;
import android.net.Uri;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.constraint.ConstraintLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
* Activities that contain this fragment must implement the
* {#link LessonFragment.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {#link LessonFragment#newInstance} factory method to
* create an instance of this fragment.
*/
public class LessonFragment extends Fragment
{
private String lessonContent;
private OnFragmentInteractionListener mListener;
TextView lesson;
private int lessonID;
private boolean culture;
public LessonFragment()
{
}
#SuppressLint("ValidFragment")
public LessonFragment(int lessonID)
{
this.lessonID = lessonID;
culture = false;
}
#SuppressLint("ValidFragment")
public LessonFragment(int lessonID, int i)
{
this.lessonID = lessonID;
culture = true;
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment LessonFragment.
*/
public static LessonFragment newInstance(String param1, String param2)
{
return null;
}
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
if (getArguments() != null)
{
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.fragment_lesson, container, false);
ConstraintLayout.LayoutParams layoutParams = new ConstraintLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
view.setLayoutParams(layoutParams);
Button button = (Button) view.findViewById(R.id.backButton);
button.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View view)
{
if(!culture)
{
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
GrammarFragment GF = new GrammarFragment();
fragmentTransaction.replace(R.id.container, GF);
fragmentTransaction.commit();
}
else
{
FragmentManager fragmentManager = getActivity().getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
CultureFragment CF = new CultureFragment();
fragmentTransaction.replace(R.id.container, CF);
fragmentTransaction.commit();
}
}
});
lesson = (TextView) view.findViewById(R.id.grammarContent);
String[] lessons;
if(!culture)
lessons = getActivity().getResources().getStringArray(R.array.lessons);
else
lessons = getActivity().getResources().getStringArray(R.array.cultureLessons);
lessonContent = lessons[lessonID];
lesson.setText(lessonContent);
return view;
}
#Override
public void onAttach(Context context)
{
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener)
{
mListener = (OnFragmentInteractionListener) context;
}
else
{
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach()
{
super.onDetach();
mListener = null;
}
/**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*/
public interface OnFragmentInteractionListener
{
void onBackClickCulture();
}
}
Here's the XML for the Fragment:
<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="wrap_content"
tools:context="com.example.xxxxxx.loremipsum.LessonFragment">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/grammarContent"
android:textSize="18sp"
/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Back"
android:id="#+id/backButton"
tools:ignore="OnClick" />
</LinearLayout>
</ScrollView>
Finally, here's the XML for my MainActivity, where the Fragment is being added:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.xxxxxx.loremipsum.MainActivity">
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
EDIT: Here's a screenshot of a further issue:
Here's the bug I'm having
Please add one more layout in your MainActivity xml portion. And you also change in MainActivity R.id.fl_main with R.id.container
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.buttonnavifragment.MainActivity">
<FrameLayout
android:id="#+id/fl_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginStart="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<android.support.design.widget.BottomNavigationView
android:id="#+id/navigation"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="0dp"
android:layout_marginStart="0dp"
android:background="?android:attr/windowBackground"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:menu="#menu/navigation" />
</android.support.constraint.ConstraintLayout>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout>
<FrameLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"/>
<android.support.design.widget.BottomNavigationView
/>
</LinearLayout>
Your container should be an View which should be above BottomNavigationView. That's why you are having these conflicts. Because you just attach Fragment on parent view.
Set a background always to Fragment parent element. Because Fragment inflate on top of other to maintain stack.
How to get the data in a message when click in the textView from Fragment
i Work on android studio
I hope to get the code with all thanks
How to get the data in a message when click in the textView from Fragment
i Work on android studio
I hope to get the code with all thanks
<uses-permission android:name="android.permission.READ_CALL_LOG" />
Gratefully
class
import android.annotation.SuppressLint;
import android.content.CursorLoader;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.CallLog;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Created by TAREQALEID1 on 09/06/2017.
*/
public class Tab1 extends Fragment {
View rootView;
boolean mDualPane;
int mCurCheckPosition = 0;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
Button btnCheckFalAction ;
rootView=inflater.inflate(R.layout.tab1,container,false);
ArrayList<HashMap<String,String>> callLog=getCallLog();
CustomCallLogListAdapter adapter=new CustomCallLogListAdapter(getActivity(),R.layout.row_call_log_layout,callLog);
ListView list_calllog=(ListView)rootView.findViewById(R.id.list_calllog);
list_calllog.setAdapter(adapter);
btnCheckFalAction = (Button) rootView.findViewById(R.id.button2); // you have to use rootview object..
btnCheckFalAction.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v)
{
Toast.makeText(getActivity(), "Hello World", Toast.LENGTH_LONG).show();
}
});
return rootView;
}
#Override
public void onActivityCreated(final Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onActivityCreated(savedInstanceState);
ListView list_calllog=(ListView)rootView.findViewById(R.id.list_calllog);
list_calllog.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> arg0, View arg1, int position,
long arg3) {
Toast.makeText(getActivity(), "Item clicked : " + position, Toast.LENGTH_LONG).show();
}
});
}
#SuppressLint("NewApi")
public ArrayList<HashMap<String,String>> getCallLog()
{
ArrayList<HashMap<String,String>> callLog=new ArrayList<HashMap<String,String>>();
CursorLoader cursorLoader=new CursorLoader(getActivity(), CallLog.Calls.CONTENT_URI, null, null, null, null);
Cursor cursor=cursorLoader.loadInBackground();
if(cursor.moveToFirst())
{
while (cursor.moveToNext())
{
HashMap<String,String> hashMap=new HashMap<String, String>();
hashMap.put(CallLog.Calls.NUMBER, cursor.getString(cursor.getColumnIndex(CallLog.Calls.NUMBER)));
hashMap.put(CallLog.Calls.DATE, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DATE)));
hashMap.put(CallLog.Calls.DURATION, cursor.getString(cursor.getColumnIndex(CallLog.Calls.DURATION)));
callLog.add(hashMap);
}
}
return callLog;
}
}
import android.content.Context;
import android.provider.CallLog;
import android.text.format.DateFormat;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
public class CustomCallLogListAdapter extends ArrayAdapter<HashMap<String,String>>
{
private ArrayList<HashMap<String,String>> callLogData;
private Context context;
private int resource;
private View view;
private Holder holder;
private HashMap<String,String> hashMap;
public CustomCallLogListAdapter(Context context, int resource,ArrayList<HashMap<String, String>> objects)
{
super(context, resource, objects);
this.context=context;
this.resource=resource;
this.callLogData=objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
view=inflater.inflate(resource, parent,false);
holder=new Holder();
holder.text_number=(TextView)view.findViewById(R.id.text_calllog_number);
holder.text_date=(TextView)view.findViewById(R.id.text_calllog_date);
holder.text_time=(TextView)view.findViewById(R.id.text_calllog_time);
hashMap=callLogData.get(position);
Date date=new Date(Long.parseLong(hashMap.get(CallLog.Calls.DATE)));
java.text.DateFormat dateFormat=DateFormat.getDateFormat(context);
java.text.DateFormat timeformat=DateFormat.getTimeFormat(context);
holder.text_number.setText(hashMap.get(CallLog.Calls.NUMBER));
holder.text_time.setText(timeformat.format(date));
holder.text_date.setText(dateFormat.format(date));
return view;
}
public class Holder
{
TextView text_number;
TextView text_date;
TextView text_time;
}
}
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuItem;
public class Getfolder1 extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_getfolder1);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
mViewPager.addOnPageChangeListener(new TabLayout.TabLayoutOnPageChangeListener(tabLayout));
tabLayout.addOnTabSelectedListener(new TabLayout.ViewPagerOnTabSelectedListener(mViewPager));
}
#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_getfolder1, 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);
}
/**
* A placeholder fragment containing a simple view.
*/
public static class PlaceholderFragment extends Fragment {
/**
* The fragment argument representing the section number for this
* fragment.
*/
private static final String ARG_SECTION_NUMBER = "section_number";
public PlaceholderFragment() {
}
/**
* Returns a new instance of this fragment for the given section
* number.
*/
public static PlaceholderFragment newInstance(int sectionNumber) {
PlaceholderFragment fragment = new PlaceholderFragment();
Bundle args = new Bundle();
args.putInt(ARG_SECTION_NUMBER, sectionNumber);
fragment.setArguments(args);
return fragment;
}
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
Tab1 tab1= new Tab1();
return tab1;
case 1:
Tab2 tab2= new Tab2();
return tab2;
}
return null;
}
#Override
public int getCount() {
// Show 3 total pages.
return 2;
}
}
}
<!-- begin snippet: js hide: false console: true babel: false -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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">
<LinearLayout
android:id="#+id/lyt1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView5"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="TextView"
tools:ignore="HardcodedText"
tools:text="مرحبا" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="40dp"
android:text="Button"
tools:ignore="HardcodedText" />
<ListView
android:id="#+id/list_calllog"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
<!-- begin snippet: js hide: false console: true babel: false -->
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="imagingstudents2.com.imagingstudents2.Getfolder1">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways"
app:popupTheme="#style/AppTheme.PopupOverlay"
app:title="#string/app_name">
</android.support.v7.widget.Toolbar>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabItem
android:id="#+id/tabItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_1" />
<android.support.design.widget.TabItem
android:id="#+id/tabItem2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/tab_text_2" />
</android.support.design.widget.TabLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.design.widget.CoordinatorLayout>
row_call_log_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout 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/const1"
android:layout_width="match_parent"
android:layout_height="100dp"
android:layout_marginBottom="#dimen/activity_horizontal_margin"
android:layout_marginLeft="#dimen/activity_vertical_margin"
android:layout_marginRight="#dimen/activity_vertical_margin"
android:layout_marginTop="#dimen/activity_horizontal_margin">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/text_calllog_number"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/holo_orange_light"
android:textSize="20sp"
android:textStyle="bold"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<LinearLayout
android:id="#+id/lyott2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_weight="1"
android:orientation="horizontal"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/text_calllog_number">
<TextView
android:id="#+id/text_calllog_date"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
tools:ignore="NestedWeights" />
<TextView
android:id="#+id/text_calllog_time"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
Add an OnclickListener to the holder.text_number:
String message = "";
holder.text_number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
message = textview.getText().toString();
Toast.makeText(getActivity(), "Message : " + message, Toast.LENGTH_LONG).show();
}
});
Now do whatever you want with the message String.
final TextView text_number2=(TextView)view.findViewById(R.id.text_calllog_number);
holder.text_number.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText( context, "" + text_number2.getText().toString() , Toast.LENGTH_LONG).show();
}
});
Ten days I look for the answer .....Wadaane .... Thank you very much
I'm trying to add Parse API data with a tab Fragment. The ListView
is working fine and I need to add 2 filter Buttons between the
Fragment tab Buttons and the ListView. Below I attached what I'm
trying to get
MainActivity.java
import android.support.design.widget.TabLayout;
import android.support.v4.app.ListFragment;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import com.parse.Parse;
public class MainActivity extends AppCompatActivity {
/**
* The {#link android.support.v4.view.PagerAdapter} that will provide
* fragments for each of the sections. We use a
* {#link FragmentPagerAdapter} derivative, which will keep every
* loaded fragment in memory. If this becomes too memory intensive, it
* may be best to switch to a
* {#link android.support.v4.app.FragmentStatePagerAdapter}.
*/
private SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
private ViewPager mViewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create the adapter that will return a fragment for each of the three
// primary sections of the activity.
mSectionsPagerAdapter = new SectionsPagerAdapter(getSupportFragmentManager());
// Set up the ViewPager with the sections adapter.
mViewPager = (ViewPager) findViewById(R.id.container);
mViewPager.setAdapter(mSectionsPagerAdapter);
TabLayout tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(mViewPager);
Parse.initialize(new Parse.Configuration.Builder(this)
.applicationId("")
.server("")
.build()
);
}
#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);
}
/**
* A {#link FragmentPagerAdapter} that returns a fragment corresponding to
* one of the sections/tabs/pages.
*/
public class SectionsPagerAdapter extends FragmentPagerAdapter {
private EventFragment eventFragment = new EventFragment();
private IndividualsFragment individualsFragment = new IndividualsFragment();
private OrganizationFragment orgFragment = new OrganizationFragment();
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
OrganizationFragment tab1= orgFragment;
return tab1;
case 1:
EventFragment tab2= eventFragment;
return tab2;
case 2:
IndividualsFragment tab3= individualsFragment;
return tab3;
}
return null ;
}
#Override
public int getCount() {
// Show 3 total pages.
return 3;
}
#Override
public CharSequence getPageTitle(int position) {
switch (position) {
case 0:
return "Directory";
case 1:
return "EVENTS";
case 2:
return "INDIVIDUALS";
}
return null;
}
}
}
This is a Fragment tab.
I want to 2 Buttons between the Fragment tab Buttons and the ListView items
EventFragment.java
import android.app.ListActivity;
import android.app.ListFragment;
import android.database.DataSetObserver;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.PagerAdapter;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ListAdapter;
import android.widget.ListView;
import com.parse.FindCallback;
import com.parse.ParseException;
import com.parse.ParseObject;
import com.parse.ParseQuery;
import java.util.ArrayList;
import java.util.List;
public class EventFragment
extends android.support.v4.app.ListFragment
implements FindCallback<ParseObject> {
private List<ParseObject> mOrganization = new ArrayList<ParseObject>();
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
setListAdapter(adaptor);
// This is like calling fetchList()
ParseQuery.getQuery("Event").findInBackground(this);
}
/**
* This is needed by implementing the callback on the class
**/
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
Log.d("score", "Retrieved " + scoreList.size() + " Event");
mOrganization.clear();
mOrganization.addAll(scoreList);
((EventAdapter) getListAdapter()).notifyDataSetChanged();
} else {
Log.d("score", "Error: " + e.getMessage());
}
}
}
this is the xml file of the ListView
<?xml version="1.0" encoding="utf-8"?>
<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"
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.swipe.com.MainActivity">
<com.example.rihan.swip.RoundRectCornerImageView
android:id="#+id/imageView"
android:layout_gravity="center"
android:layout_height="110dp"
android:layout_width="110dp"
tools:minWidth="30dp"
tools:maxWidth="50dp"
tools:minHeight="30dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="19dp"
android:scaleType="fitXY"/>
<TextView
android:text="TextView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/organizationname"
tools:textSize="10sp"
android:textSize="10sp"
android:layout_below="#+id/idposition"
android:layout_alignLeft="#+id/idposition"
android:layout_alignStart="#+id/idposition"
android:paddingTop="10px" />
<TextView
android:id="#+id/user"
android:text="Username"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textStyle="bold"
android:textSize="12sp"
android:textColor="#000"
android:layout_alignBaseline="#+id/name"
android:layout_alignBottom="#+id/name"
android:layout_toRightOf="#+id/name"
android:layout_toEndOf="#+id/name"
android:paddingLeft="10dp"
android:fontFamily="sans-serif" />
<TextView
android:text="yy"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/idposition"
android:textSize="10sp"
android:layout_below="#+id/name"
android:layout_alignLeft="#+id/name"
android:layout_alignStart="#+id/name" />
<TextView
android:id="#+id/name"
android:text="Name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:minWidth="5dp"
android:layout_marginLeft="12dp"
android:layout_marginStart="12dp"
tools:textStyle="bold"
android:textColor="#000"
android:textSize="12sp"
android:textStyle="bold"
android:layout_alignTop="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:layout_toEndOf="#+id/imageView"
android:layout_marginTop="13dp"
android:fontFamily="sans-serif" />
</RelativeLayout>
this is activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout 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/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
tools:context="com.swipe.com.MainActivity">
<android.support.v4.view.ViewPager
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingTop="#dimen/appbar_padding_top"
android:theme="#style/AppTheme.AppBarOverlay">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:layout_height="40dp"
android:background="#drawable/nav"
android:id="#+id/button2"
android:layout_weight="1"
android:layout_width="40dp"
android:layout_alignBottom="#+id/button"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
tools:paddingLeft="25dp"
tools:layout_marginLeft="20dp" />
<Button
android:layout_width="40dp"
android:layout_height="40dp"
android:background="#drawable/i"
android:id="#+id/button"
android:layout_weight="1"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ImageView
app:srcCompat="#drawable/logo"
android:id="#+id/imageView2"
android:layout_weight="1"
tools:layout_marginLeft="15dp"
android:layout_width="280dp"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/button"
android:layout_toStartOf="#+id/button"
android:layout_height="50dp" />
</RelativeLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
If we quote Android | ListFragment
ListFragment has a default layout that consists of a single list view. However, if you desire, you can customize the fragment layout by returning your own view hierarchy from onCreateView(LayoutInflater, ViewGroup, Bundle).
And also from the documentation
your view hierarchy must contain a ListView object with the id "#android:id/list"
Let's call this **some_list_view.xml**
Notice not #+id/list, but #android:id/list
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="8dp"
android:paddingRight="8dp">
<!-- TODO: Add your buttons here -->
<ListView android:id="#id/android:list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"/>
<TextView android:id="#id/android:empty"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="No data"/>
</LinearLayout>
One of your error exists in onViewCreated. You have no inflater there.
Implement the other method like the documentation says.
And you don't need to find the ListView.
Use findViewById to find other views.
For example,
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return inflater.inflate(R.layout.some_list_view, parent, false);
}
#Override
public void onViewCreated(View view, Bundle b) {
super.onViewCreated(view, b);
// Button filter1 = (Button) view.findViewById(...);
// filter1.setOnClickListener....
EventAdapter adaptor = new EventAdapter(getActivity(), mOrganization);
setListAdapter(adaptor);
// This is like calling fetchList()
ParseQuery.getQuery("Event").findInBackground(this);
}
#Override
public void done(List<ParseObject> scoreList, ParseException e) {
if (e == null) {
...
}
}
getListView() already will return you your ListView, therefore, notice I didn't need to find it. But if you did, this is how.
(ListView) view.findViewById(android.R.id.list);
So, that's just a matter of making an XML file with that mentioned ID value set to a ListView like shown above.
The setup I have is a main_activity fragment that just has welcome text, and when you tap the start button, it's supposed to switch fragments. Instead of replacing the current fragment, it just overlays it on top of the main_activity fragment and won't allow me to perform the action associated with the new fragment.
Main Activity example
quote_fragment overlapping main activity
activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main_activity"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:contextClickable="false"
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="me.alexoladele.quotebook.MainActivity">
<TextView
android:id="#+id/welcome_screen_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="144dp"
android:gravity="center"
android:text="#string/welcome_screen_text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="36sp" />
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="47dp"
android:clickable="true"
android:enabled="true"
android:text="#string/start_button" />
</FrameLayout>
ActivityMain.java
package me.alexoladele.quotebook;
import android.os.Bundle;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
int count = 0;
final QuoteFragment quoteFragment = new QuoteFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Button startButton = (Button) findViewById(R.id.start_button);
final TextView welcomeText = (TextView) findViewById(R.id.welcome_screen_text);
if (startButton != null)
startButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
startSecondFragment(); // start your fragment from MainActivity
//startButton.setVisibility(v.GONE);
//welcomeText.setVisibility(v.GONE);
}
});
}
// This method will be in charge of handling your second fragment
private void startSecondFragment() {
android.support.v4.app.FragmentManager manager = getSupportFragmentManager();
manager.beginTransaction()
.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_OPEN)
.replace(R.id.main_activity, quoteFragment)
.commit();
}
}
quote_fragment.xml:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/quote_frag"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="true"
android:contextClickable="true">
<TextView
android:id="#+id/quote"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/tap_screen"
android:textColor="#a6a6a6"
android:textSize="26sp" />
<TextView
android:id="#+id/person"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:fontFamily="sans-serif-medium"
android:gravity="center"
android:text="#string/quotist_name"
android:textColor="#585858"
android:textSize="26sp"
android:visibility="visible" />
</FrameLayout>
QuoteFragment.java:
package me.alexoladele.quotebook;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
//import android.support.v4.app.Fragment;
public class QuoteFragment extends android.support.v4.app.Fragment {
int count = 0;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.quote_fragment,container, false);
FrameLayout touch = (FrameLayout) v.findViewById(R.id.main_activity);
final TextView quoteText = (TextView) v.findViewById(R.id.quote);
// Makes quotes array
//region Description
String[] quotes = {
(Quotes go here, but I didn't put them here to save space)
};
//endregion
// Put quotes array into arraylist
final List<String> quoteList = new ArrayList<>(Arrays.asList(quotes));
//
if (touch != null)
touch.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (count >= quoteList.size()) {
count = 0;
}
Quote q = new Quote("");
q.setQuote(quoteList.get(count));
quoteText.setText(q.getQuote());
count++;
v.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
return true;
}
});
}
});
// Inflate the layout for this fragment
return inflater.inflate(R.layout.quote_fragment, container, false);
}
}
welcome_screen_fragment:
<FrameLayout 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:clickable="true"
tools:context="me.alexoladele.quotebook.WelcomeScreen">
<TextView
android:id="#+id/welcome_screen_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|top"
android:layout_marginTop="144dp"
android:gravity="center"
android:text="#string/welcome_screen_text"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="36sp" />
<Button
android:id="#+id/start_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom"
android:layout_marginBottom="47dp"
android:clickable="true"
android:enabled="true"
android:text="#string/start_button" />
</FrameLayout>
WelcomeScreen.java:
package me.alexoladele.quotebook;
import android.content.Context;
import android.net.Uri;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
* Activities that contain this fragment must implement the
* {#link WelcomeScreen.OnFragmentInteractionListener} interface
* to handle interaction events.
* Use the {#link WelcomeScreen#newInstance} factory method to
* create an instance of this fragment.
*/
public class WelcomeScreen extends Fragment {
// TODO: Rename parameter arguments, choose names that match
// the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
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;
public WelcomeScreen() {
// Required empty public constructor
}
/**
* Use this factory method to create a new instance of
* this fragment using the provided parameters.
*
* #param param1 Parameter 1.
* #param param2 Parameter 2.
* #return A new instance of fragment WelcomeScreen.
*/
// TODO: Rename and change types and number of parameters
public static WelcomeScreen newInstance(String param1, String param2) {
WelcomeScreen fragment = new WelcomeScreen();
Bundle args = new Bundle();
args.putString(ARG_PARAM1, param1);
args.putString(ARG_PARAM2, param2);
fragment.setArguments(args);
return fragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mParam1 = getArguments().getString(ARG_PARAM1);
mParam2 = getArguments().getString(ARG_PARAM2);
}
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.welcome_screen_fragment, container, false);
}
/* // TODO: Rename method, update argument and hook method into UI event
public void onButtonPressed(Uri uri) {
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
}
#Override
public void onAttach(Context context) {
super.onAttach(context);
if (context instanceof OnFragmentInteractionListener) {
mListener = (OnFragmentInteractionListener) context;
} else {
throw new RuntimeException(context.toString()
+ " must implement OnFragmentInteractionListener");
}
}
#Override
public void onDetach() {
super.onDetach();
mListener = null;
}
*//**
* This interface must be implemented by activities that contain this
* fragment to allow an interaction in this fragment to be communicated
* to the activity and potentially other fragments contained in that
* activity.
* <p>
* See the Android Training lesson <a href=
* "http://developer.android.com/training/basics/fragments/communicating.html"
* >Communicating with Other Fragments</a> for more information.
*//*
public interface OnFragmentInteractionListener {
// TODO: Update argument type and name
void onFragmentInteraction(Uri uri);
}*/
}
Don't replace your top-level view group. Create an empty frame layout inside of the top-level frame layout and use that as the ID to replace.
If you have elements in the main layout you no longer want visible you'll have to hide them (or use a fragment to show them in the first place which will get replaced).