Fragments not showing content on viewPager scroll - Android - java

I'm new to Android..
I am trying to create a simple application with a sliding tab to scroll between 2 fragments. The fragments are populated with dummy buttons just to see if the scrolling works. However when I run all I see are the viewPager indexes on top without the fragments. I am also using these two files SlidingTabLayout.java and SlidingTabStrip.java that are given by Android. Here are my classes:
MyFragmentPagerAdapter.java
package billdozer.com.testxml;
import android.content.Context;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class MyFragmentPagerAdapter extends FragmentPagerAdapter {
// Holds tab titles
private String tabTitles[] = new String[] { "Frag #1", "Frag #2"};
private Context context;
public MyFragmentPagerAdapter(FragmentManager fm, Context context) {
super(fm);
this.context = context;
}
#Override
public int getCount() {
return 2;
}
// Return the correct Fragment based on index
#Override
public Fragment getItem(int position) {
if(position == 0){
return new TabFragment1();
} else if(position == 1) {
return new TabFragment2();
}
return null;
}
#Override
public CharSequence getPageTitle(int position) {
// Return the tab title to SlidingTabLayout
return tabTitles[position];
}
}
TabFragment1.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment1 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_1, container, false);
return view;
}
}
TabFragment2.java
package billdozer.com.testxml;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class TabFragment2 extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View view = inflater.inflate(R.layout.tab_fragment_2, container, false);
return view;
}
}
tab_fragment_1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_1" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_2" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_3" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_4" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_5" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_6" />
</TableRow>
</TableLayout>
</LinearLayout>
tab_fragment_2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TableLayout
android:id="#+id/tl"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:gravity="center" >
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:gravity="center" >
<Button
android:id="#+id/button1"
android:layout_weight="1"
android:text="#string/_7" />
<Button
android:id="#+id/button2"
android:layout_weight="1"
android:text="#string/_8" />
<Button
android:id="#+id/button3"
android:layout_weight="1"
android:text="#string/_9" />
</TableRow>
<TableRow
android:id="#+id/tableRow2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center" >
<Button
android:id="#+id/button5"
android:layout_weight="1"
android:text="#string/_10" />
<Button
android:id="#+id/button6"
android:layout_weight="1"
android:text="#string/_11" />
<Button
android:id="#+id/button7"
android:layout_weight="1"
android:text="#string/_12" />
</TableRow>
</TableLayout>
</LinearLayout>
MainActivity.java
package com.example.user.billdozer_ui;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import com.example.user.billdozer_ui.stab.SlidingTabLayout;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ViewPager viewPager = (ViewPager) findViewById(R.id.viewpager);
viewPager.setAdapter(new MyFragmentPagerAdapter(getSupportFragmentManager(),
MainActivity.this));
SlidingTabLayout slidingTabLayout = (SlidingTabLayout) findViewById(R.id.sliding_tabs);
slidingTabLayout.setViewPager(viewPager);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.example.user.billdozer_ui.MainActivity">
<com.example.user.billdozer_ui.stab.SlidingTabLayout
android:id="#+id/sliding_tabs"
android:layout_width="match_parent"
android:layout_height="match_parent"/>
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="0px" />
</LinearLayout>

Related

why cant my bottom navigation bar be shown?

Im wondering why my bottom navigation bar is not showing up. Can anyone help me out please. Im still learning Android so i apologise in advance if i seem to be asking the wrong questions!
This is my home.xml file
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/home"
android:title="Home"
android:icon="#drawable/ic_action_home"
/>
<item
android:id="#+id/Login"
android:title="Login"
android:icon="#drawable/ic_action_login"
/>
<item
android:id="#+id/About"
android:title="About"
android:icon="#drawable/ic_action_about"
/>
</menu>
This is my fragment_home.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".HomeFragment">
<!-- TODO: Update blank fragment layout -->
<ListView
android:id="#+id/listViewDetails"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
This is HomeFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class HomeFragment extends Fragment {
public HomeFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_home, container, false);
String[] menuItems = {"Do Something!", "Do something else!", "Do yet another thing!"};
ListView listView = (ListView) view.findViewById(R.id.listViewDetails);
ArrayAdapter<String> listViewAdapter = new ArrayAdapter<String>(
getActivity(),
android.R.layout.simple_list_item_1,
menuItems
);
listView.setAdapter(listViewAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
if (position == 0) {
Toast.makeText(getActivity(),"Hi! You CLikced the first item", Toast.LENGTH_LONG).show();
}else if (position == 1) {
Toast.makeText(getActivity(),"Hi! You CLikced the Second item", Toast.LENGTH_LONG).show();
}else if (position == 2) {
Toast.makeText(getActivity(),"Hi! You CLikced the Third item", Toast.LENGTH_LONG).show();
}
}
});
// Inflate the layout for this fragment
return view;
}
}
This is my AndroidManifest.xml file
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="sg.edu.rp.c346.a3pdwork">
<application
android:allowBackup="true"
android:icon="#mipmap/ic_launcher"
android:label="#string/app_name"
android:roundIcon="#mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="#style/AppTheme">
<activity android:name=".MainActivity"
android:label="#string/app_name">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
This is LoginFragment.java
package sg.edu.rp.c346.a3pdwork;
import android.graphics.Color;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
/**
* A simple {#link Fragment} subclass.
*/
public class LoginFragment extends Fragment {
Button b1, b2;
EditText ed1, ed2;
TextView tx1;
int counter = 3;
public LoginFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_login, container, false);
b1 = findViewByID(R.id.button);
ed1 = findViewByID(R.id.editText);
ed2 = findViewByID(R.id.editText2);
b2 = (Button)findViewById(R.id.button2);
tx1 = (TextView)findViewById(R.id.textView3);
tx1.setVisibility(View.GONE);
b1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (ed1.getText().toString().equals("admin") &&
ed2.getText().toString().equals("admin")) {
Toast.makeText(getApplicationContext(),
"Redirecting...", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Wrong Credentials", Toast.LENGTH_SHORT).show();
tx1.setVisibility(View.VISIBLE);
tx1.setBackgroundColor(Color.RED);
counter--;
tx1.setText(Integer.toString(counter));
if (counter == 0) {
b1.setEnabled(false);
}
}
}
});
b2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
finish();
}
});
}
}
This is my fragment_about.xml file
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".AboutFragment">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/hello_blank_fragment" />
</FrameLayout>
This is my fragment_login.xml
<?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"
tools:context=".LoginFragment">
<!-- TODO: Update blank fragment layout -->
<TextView android:text = "Tutor Seeker"
android:layout_width="wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/textview"
android:textSize = "35dp"
android:layout_alignParentTop = "true"
android:layout_centerHorizontal = "true" />
<EditText
android:layout_width = "wrap_content"
android:layout_height = "wrap_content"
android:id = "#+id/editText"
android:hint = "Enter Name"
android:focusable = "true"
android:textColorHighlight = "#ff7eff15"
android:textColorHint = "#ffff25e6"
android:layout_marginTop = "46dp"
android:layout_alignParentLeft = "true"
android:layout_alignParentStart = "true"
android:layout_alignParentRight = "true"
android:layout_alignParentEnd = "true" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="textPassword"
android:ems="10"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignRight="#+id/editText"
android:layout_alignEnd="#+id/editText"
android:textColorHint="#ffff299f"
android:hint="Enter Password" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Attempts Left:"
android:id="#+id/textView2"
android:layout_below="#+id/editText2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:textSize="25dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/textView3"
android:layout_alignTop="#+id/textView2"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/textView2"
android:layout_toEndOf="#+id/textview"
android:textSize="25dp"
android:layout_toRightOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="login"
android:id="#+id/button"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/textview"
android:layout_toStartOf="#+id/textview" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:id="#+id/button2"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/textview"
android:layout_toEndOf="#+id/textview" />
</RelativeLayout>
This is my AboutFragment.java file
package sg.edu.rp.c346.a3pdwork;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class AboutFragment extends Fragment {
public AboutFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_about, container, false);
}
}
This is MainActivity.java file
package sg.edu.rp.c346.a3pdwork;
import androidx.appcompat.app.AppCompatActivity;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.NavigationUI;
import android.graphics.Color;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import com.google.android.material.bottomnavigation.BottomNavigationView;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}
This is my activity_main.xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
android:id="#+id/nav_host_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:defaultNavHost="true"
app:navGraph="#navigation/nav_graph" />
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottom_nav"
app:menu="#menu/home"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</LinearLayout>
Try to use RelativeLayout instead of LinearLayout like below:
<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=".MainActivity">
<fragment
android:name="androidx.navigation.fragment.NavHostFragment"
... />
<com.google.android.material.bottomnavigation.BottomNavigationView
...
android:layout_alignParentBottom="true"
android:layout_width="match_parent"
android:layout_height="wrap_content"
/>
</RelativeLayout>
The fragment in your activity_main has android:layout_height="match_parent" and LinearLayout has vertical orientation. This means that your fragment takes all the place on the screen and your bottom navigation menu placed below your fragment. Also, you shouldn't set navigation menu height as match_parent. I suggest you to try to use ConstraintLayout as layout root.
NOTE! I use AndroidX, but it also works for AppCompat.
<androidx.constraintlayout.widget.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">
<com.google.android.material.bottomnavigation.BottomNavigationView
android:id="#+id/bottomNavigationView"
android:layout_width="match_parent"
android:layout_height="#dimen/bottom_menu_height"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/navigation" />
<fragment
android:id="#+id/fragment"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/bottomNavigationView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

How to create a URL in Java?

I want to make an app that shows a list of books that relate to a given keyword. I made the ListView, EditText view and a search button. The layout is given below:
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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"
android:orientation="vertical"
tools:context=".MainActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:orientation="horizontal">
<EditText
android:id="#+id/search_query_text_view"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="2"
android:hint="#string/hint" />
<Button
android:id="#+id/search_button1"
android:layout_width="42dp"
android:layout_height="42dp"
android:drawableLeft="#drawable/search"
/>
</LinearLayout>
<TextView
android:id="#+id/result"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="Results" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:visibility="invisible" />
</RelativeLayout>
</LinearLayout>
The XML for a single item in the list is given below.
list_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_height"
android:layout_margin="5dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="#dimen/list_item_height">
<ImageView
android:id="#+id/thumbnail_imageview"
android:layout_width="90dp"
android:layout_height="90dp"
android:layout_marginRight="10dp"
android:layout_marginTop="10dp"
android:src="#drawable/kite_runner" />
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="#dimen/list_item_height"
android:layout_margin="5dp">
<TextView
android:id="#+id/title_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="The Kite Runner"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TextView
android:id="#+id/author_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/title_textview"
android:layout_marginBottom="10dp"
android:text="Khaled Hosseini"
android:textColor="#000000"
android:textSize="17sp" />
<TextView
android:id="#+id/publisher_texview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/author_textview"
android:text="Penguin Books"
android:textColor="#000000"
android:textSize="15sp" />
</RelativeLayout>
</LinearLayout>
</LinearLayout>
I am using the Google Books API query to access the book list.
I have the base URL and I now need to add the value in the EditText view to this URL.
The custom adapter for the list view is given below.
package com.example.shara.booklistapp;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.SearchView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by shara on 12/17/2017.
*/
public class ListAdapter extends ArrayAdapter<Blist> {
public ListAdapter(#NonNull Context context, ArrayList<Blist> blists) {
super(context, 0, blists);
}
public String rslt;
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listitemview = convertView;
if (listitemview == null) {
listitemview = LayoutInflater.from(getContext()).inflate(R.layout.list_layout, parent, false);
}
Blist blist = getItem(position);
EditText search_query = listitemview.findViewById(R.id.search_query_text_view);
Button search_button = listitemview.findViewById(R.id.search_button1);
search_button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String searchquery = search_query.getText().toString();
}
});
ImageView bookthumbnail = listitemview.findViewById(R.id.thumbnail_imageview);
TextView title = listitemview.findViewById(R.id.title_textview);
TextView author = listitemview.findViewById(R.id.author_textview);
TextView publisher = listitemview.findViewById(R.id.publisher_texview);
return listitemview;
}
}
I am using another class named BookQueryUtils to create the URL and to do the HTTP request and JSON parsing.
I want to access the value of EditText view from BookQueryUtils class and then append it to the base URL.
Also how can I call the AsyncTaskLoader inside the BookQueryUtils class when the button is pressed. How can I do that?
Use Volley, example:
String url = "https://example.com";
StringRequest stringRequest = new StringRequest(Request.Method.GET,
url, new Response.Listener<String>() {
#Override
public void onResponse(String response) {
Log.e("RESPONSE", response);
}
}, new Response.ErrorListener() {
#Override
public void onErrorResponse(VolleyError error) {
}
});
stringRequest.setTag("LOL");
queue.add(stringRequest);

Android playing sounds from a fragment

I've found multiple similar posts but have been unable to resolve so posting my code for help.
I have 3 fragments that use a ViewPager with a PagerAdapter to allow for swiping between fragments. This project started with no fragments and I was able to play sounds from buttons clicked all from MainActivity.
Here's the code that worked.
MainActivity
import android.media.MediaPlayer;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
public class MainActivity extends AppCompatActivity {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
MediaPlayer example4Sound;
MediaPlayer example5Sound;
MediaPlayer example6Sound;
MediaPlayer example7Sound;
MediaPlayer example8Sound;
MediaPlayer example9Sound;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
example1Sound = MediaPlayer.create(this, R.raw.example1);
example2Sound = MediaPlayer.create(this, R.raw.example2);
example3Sound = MediaPlayer.create(this, R.raw.example3);
example4Sound = MediaPlayer.create(this, R.raw.example4);
example5Sound = MediaPlayer.create(this, R.raw.example5);
example6Sound = MediaPlayer.create(this, R.raw.example6);
example7Sound = MediaPlayer.create(this, R.raw.example7);
example8Sound = MediaPlayer.create(this, R.raw.example8);
example9Sound = MediaPlayer.create(this, R.raw.example9);
}
public void playExampleSound1(View view) {
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
public void playExampleSound4(View view) {
example4Sound.start();
}
public void playExampleSound5(View view) {
example5Sound.start();
}
public void playExampleSound6(View view) {
example6Sound.start();
}
public void playExampleSound7(View view) {
example7Sound.start();
}
public void playExampleSound8(View view) {
example8Sound.start();
}
public void playExampleSound9(View view) {
example9Sound.start();
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<TableLayout 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.anjosoft.MainActivity">
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2"
android:onClick="playExampleSound2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3"
android:onClick="playExampleSound3" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton4"
android:layout_column="0"
android:background="#drawable/button4"
android:onClick="playExampleSound4" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton5"
android:layout_column="1"
android:background="#drawable/button5"
android:onClick="playExampleSound5" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton6"
android:layout_column="2"
android:background="#drawable/button6"
android:onClick="playExampleSound6" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton7"
android:layout_column="0"
android:background="#drawable/button7"
android:onClick="playExampleSound7" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton8"
android:layout_column="1"
android:background="#drawable/button8"
android:onClick="playExampleSound8" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton9"
android:layout_column="2"
android:background="#drawable/button9"
android:onClick="playExampleSound9" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Send Sound"
android:id="#+id/checkBox"
android:layout_column="1" />
</TableRow>
</TableLayout>
I know it's not pretty. Should have made an array but nevertheless it worked.
So here's my problem. Using the same ideology I tied the same thing with fragment.
MainActivity.java
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.os.Bundle;
public class MainActivity extends FragmentActivity {
ViewPager viewpager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewpager = (ViewPager)findViewById(R.id.pager);
PagerAdapter padapter = new PagerAdapter(getSupportFragmentManager());
viewpager.setAdapter(padapter);
}
}
PagerAdapter.java
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter{
public PagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int position) {
switch (position){
case 0:
return new FragmentOne();
case 1:
return new FragmentTwo();
case 2:
return new FragmentThree();
default:
break;
}
return null;
}
#Override
public int getCount() {
return 3;
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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">
</android.support.v4.view.ViewPager>
FragmentOne.java
import android.app.Activity;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class FragmentOne extends Fragment {
MediaPlayer example1Sound;
MediaPlayer example2Sound;
MediaPlayer example3Sound;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
return inflater.inflate(R.layout.fragment_one_layout,container,false);
}
public void playExampleSound1(View view){
example1Sound.start();
}
public void playExampleSound2(View view) {
example2Sound.start();
}
public void playExampleSound3(View view) {
example3Sound.start();
}
}
fragnment_one_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#f70808">
<TableLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton"
android:layout_column="0"
android:background="#drawable/button1"
android:onClick="playExampleSound1"
android:nestedScrollingEnabled="false" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton2"
android:layout_column="1"
android:background="#drawable/button2" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageButton3"
android:layout_column="2"
android:background="#drawable/button3" />
</TableRow>
</TableLayout>
</RelativeLayout>
Clicking on imagebutton crashes the app. I know the MediaPlayer can work inside the app because I tested inserting example1Sound.start(); like this...
Snippet from FragmentOne.java
example1Sound = MediaPlayer.create(this.getActivity(), R.raw.example1);
example2Sound = MediaPlayer.create(this.getActivity(), R.raw.example2);
example3Sound = MediaPlayer.create(this.getActivity(), R.raw.example3);
example1Sound.start();
The sound plays when the fragment loads. How do I get the onclick to work?
You can use onClickListeners for each button, it works.
I think, fragnment_one_layout.xml is linked to FragmentOne.java using inflater
but the fragnment_one_layout.xml has no access to FragmentOne.java , and hence
the statement onClick="methodName" is not able to access the desired method.
mediaPlayer object has no connection with your problem.
You can make one function that redirect sound from Activity to Fragment!
in Activity
fun onSound(sound:Int){
mp = MediaPlayer.create(this,soundFile)
mp.start
}
from fragment
onResume{
(activity as ActivityName).onSound(R.raw.sound)
}

Unable to Load Fragment from Main Activity

I am trying to load a frame on the Onclicklistener of a Button in the main activity. In my view I have written all the code correctly of main activity and fragment that requires to attach a fragment to activity. But something seems off. Appreciate any thoughts or suggestions.
I have used this link as a reference.
Below are the code details.
MainActivity.java
import android.annotation.TargetApi;
import android.os.Build;
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.ActionBar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.app.ActionBar actionBar = getActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
Button btnLoad = (Button) findViewById(R.id.Button01);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager fragmentManager = getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
AboutFragment mAboutFragment = new AboutFragment();
fragmentTransaction.add(R.id.fragment_container, mAboutFragment,"About");
fragmentTransaction.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
fragmentTransaction.commit();
}
};
btnLoad.setOnClickListener(listener);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_about"
android:minHeight="92dp"
android:layout_marginTop="40dp"
android:layout_marginLeft="40dp"
android:textSize="22sp"></Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_report"
android:id="#+id/Button02"
android:minHeight="92dp"
android:textSize="22sp"
android:layout_marginTop="40dp"
android:layout_marginLeft="80dp"
android:layout_toRightOf="#+id/Button01"></Button>
</RelativeLayout>
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
AboutFragement.jave
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class AboutFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.about_fragment_main, container, false);
return view;
}
}
about_fragement_main.xml
LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<TextView android:id="#+id/about_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:paddingTop="30px"
android:text="header text"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:text="text here" />
<TextView android:id="#+id/about_textview2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya mission"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya vision"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="text here" />
<TextView android:id="#+id/about_textview4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:text="zariya values"
android:textStyle="bold|italic"/>
<TextView
android:id="#+id/text4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:paddingTop="10px"
android:text="sometezt." />
</LinearLayout>
The error is in the main XML file.
You root is
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
But then the first child have the height fill_parent this makes the first child take all the space of the LinearLayout so when you try to put your fragment in the container, it wont appear in the screen.
Instead try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
....
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent" >
Try to get used to use match_parent instead of fill_parent :)

Activities togglebutton when clicking a part of an image

I'm currently trying to make a small application in java, and I have encountered a small problem.
I need to activate a togglebutton when I touch a particular area of the image that I have provided.
I enclose the code of my pages.
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="vertical"
android:padding="4dip" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btnPag1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Lights"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Automation"
tools:ignore="HardcodedText" >
</Button>
<Button
android:id="#+id/btnPag3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Garden"
tools:ignore="HardcodedText" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="0px"
android:layout_weight="1" >
</android.support.v4.view.ViewPager>
</LinearLayout>
page1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 1"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 2"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 3"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Light 4"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/casa"
tools:ignore="ContentDescription" />
</LinearLayout>
page2.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Gate"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Activates"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
page3.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#000000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Irrigation"
android:textAppearance="?android:attr/textAppearanceLarge"
tools:ignore="HardcodedText" />
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="ToggleButton"
tools:ignore="HardcodedText" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=""
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
MercuryActivity.java
package it.anddev.bradipao.mercury;
// derived from http://thepseudocoder.wordpress.com/2011/10/05/android-page-swiping-using-viewpager/
import java.util.List;
import java.util.Vector;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MercuryActivity extends FragmentActivity implements Page1Fragment.OnPageListener {
// list contains fragments to instantiate in the viewpager
List<Fragment> fragments = new Vector<Fragment>();
// page adapter between fragment list and view pager
private PagerAdapter mPagerAdapter;
// view pager
private ViewPager mPager;
// activity data
public String p2text,p3text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
// creating fragments and adding to list
fragments.add(Fragment.instantiate(this,Page1Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page2Fragment.class.getName()));
fragments.add(Fragment.instantiate(this,Page3Fragment.class.getName()));
// creating adapter and linking to view pager
this.mPagerAdapter = new PagerAdapter(super.getSupportFragmentManager(),fragments);
mPager = (ViewPager) super.findViewById(R.id.pager);
mPager.setAdapter(this.mPagerAdapter);
// upper bar button listener, allows direct page access
Button button = (Button)findViewById(R.id.btnPag1);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(0); // go to first page
}
});
button = (Button)findViewById(R.id.btnPag2);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(1); // go to second page
}
});
button = (Button)findViewById(R.id.btnPag3);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
mPager.setCurrentItem(2); // go to third page
}
});
}
// page 1 fragment listener implementation
#Override
public void onPage1(String s) {
// set activity data with received string
p2text = new String(s+" 2");
p3text = new String(s+" 3");
// page 2 fragment update
Page2Fragment f2 = (Page2Fragment) fragments.get(1);
f2.ptext = p2text;
// if page 2 view is already created, update
View v2 = f2.getView();
if (v2!=null) {
}
// page 3 fragment update
Page3Fragment f3 = (Page3Fragment) fragments.get(2);
f3.ptext = p3text;
// if page 3 view is already created, update
View v3 = f3.getView();
if (v3!=null) {
}
}
}
Page1Fragment.java
package it.anddev.bradipao.mercury;
import android.app.Activity;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page1Fragment extends Fragment {
public String ptext="..PAGE 1..";
// activity listener interface
private OnPageListener pageListener;
public interface OnPageListener {
public void onPage1(String s);
}
// onAttach : set activity listener
#Override
public void onAttach(Activity activity) {
super.onAttach(activity);
// if implemented by activity, set listener
if(activity instanceof OnPageListener) {
pageListener = (OnPageListener) activity;
}
// else create local listener (code never executed in this example)
else pageListener = new OnPageListener() {
#Override
public void onPage1(String s) {
Log.d("PAG1","Button event from page 1 : "+s);
}
};
}
// onCreateView :
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page1,container,false);
// update text
return view;
}
// set text helper function
}
Page2Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
public class Page2Fragment extends Fragment {
public String ptext="..PAGE 2..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page2,container,false);
// update text
return view;
}
// set text helper function
}
Page3Fragment.java
package it.anddev.bradipao.mercury;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.LinearLayout;
public class Page3Fragment extends Fragment {
Button btnWrite;
public String ptext="..PAGE 3..";
public View onCreateView(LayoutInflater inflater,ViewGroup container,Bundle savedInstanceState) {
// fragment not when container null
if (container == null) {
return null;
}
// inflate view from layout
View view = (LinearLayout)inflater.inflate(R.layout.page3,container,false);
// update text
return view;
}
}
PageAdapter.java
package it.anddev.bradipao.mercury;
import java.util.List;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
public class PagerAdapter extends FragmentPagerAdapter {
// fragments to instantiate in the viewpager
private List<Fragment> fragments;
// constructor
public PagerAdapter(FragmentManager fm,List<Fragment> fragments) {
super(fm);
this.fragments = fragments;
}
// return access to fragment from position, required override
#Override
public Fragment getItem(int position) {
return this.fragments.get(position);
}
// number of fragments in list, required override
#Override
public int getCount() {
return this.fragments.size();
}
}

Categories

Resources