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
Related
I was running into some trouble making a navbar in a fragment that was loaded by a different navbar.
Basically trying to solve the problem of having a menu in a fragment that was accessed by a navbar.
The easiest thing turned out to be creating a tablayout in the fragment. Here's the tutorial I followed: https://www.youtube.com/watch?v=pIKdHeOjYNw
Here's the basic code:
Main Fragment
public class HomeFragment extends Fragment {
private FragmentHomeBinding binding;
public View onCreateView(#NonNull LayoutInflater inflater,
ViewGroup container, Bundle savedInstanceState) {
binding = FragmentHomeBinding.inflate(inflater, container, false);
View root = binding.getRoot();
TabLayout tabLayout = root.findViewById(R.id.tabLayout);
ViewPager2 viewPager2 = root.findViewById(R.id.viewPagerHomeScreen);
ViewPagerAdapter viewPagerAdapter = new ViewPagerAdapter(this);
viewPager2.setAdapter(viewPagerAdapter);
tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
#Override
public void onTabSelected(TabLayout.Tab tab) {
viewPager2.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(TabLayout.Tab tab) {
}
#Override
public void onTabReselected(TabLayout.Tab tab) {
}
});
return root;
}
#Override
public void onDestroyView() {
super.onDestroyView();
binding = null;
}
private class ViewPagerAdapter extends FragmentStateAdapter {
public ViewPagerAdapter(#NonNull Fragment fragment) {
super(fragment);
}
#NonNull
#Override
public Fragment createFragment(int position) {
switch (position){
case 0 :
return new HomeScreenMembership();
case 1 :
return new HomeScreenCreatedClubs();
default :
return new HomeScreenMembership();
}
}
#Override
public int getItemCount() {
return 2;
}
}
Main Fragment XMl
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ui.home.HomeFragment">
<com.google.android.material.tabs.TabLayout
android:id="#+id/tabLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/membership_tab" />
<com.google.android.material.tabs.TabItem
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/created_clubs_tab" />
</com.google.android.material.tabs.TabLayout>
<androidx.viewpager2.widget.ViewPager2
android:id="#+id/viewPagerHomeScreen"
android:layout_width="match_parent"
android:layout_height="0dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintTop_toBottomOf="#+id/tabLayout" />
</androidx.constraintlayout.widget.ConstraintLayout>
One of the Fragments For One Of the Tabs
(the rest were almost identical)
package com.freedommuskrats.clubbub.ui.home;
import android.os.Bundle;
import androidx.fragment.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.freedommuskrats.clubbub.R;
public class HomeScreenCreatedClubs extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_home_screen_created_clubs, container, false);
}
}
XML for that fragment
<?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=".ui.home.HomeScreenCreatedClubs">
<!-- TODO: Update blank fragment layout -->
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Created" />
</FrameLayout>
This worked well and I'm even thinking of ditching the navbar in the future because it was so much more complex.
How do I create android programming, If I click,
tab 1 - ViewPager1 Appears Fragment1 and ViewPager2 Appears Fragment1A
and
If I click,
tab 2 - ViewPager1 Appears Fragmen2 and ViewPager2 Appears Fragmen2A.
PagerAdapter.java
package com.papaozi.ground;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
public class PagerAdapter extends FragmentStatePagerAdapter {
int mNumOfTabs;
public PagerAdapter(FragmentManager fm, int NumOfTabs) {
super(fm);
this.mNumOfTabs = NumOfTabs;
}
#Override
public Fragment getItem(int position) {
switch (position) {
case 0:
Satu tab1 = new Satu();
return tab1;
case 1:
Dua tab2 = new Dua();
return tab2;
default:
return null;
}
}
#Override
public int getCount() {
return mNumOfTabs;
}
}
ProfilActivity.java
package com.papaozi.ground;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.provider.ContactsContract;
import android.support.annotation.DrawableRes;
import android.support.design.widget.CoordinatorLayout;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentPagerAdapter;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.os.Bundle;
import android.support.design.widget.TabLayout;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.text.Layout;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class ProfilActivity extends AppCompatActivity {
ViewPager simpleViewPager;
private TabLayout tabLayout, tabLayout2;
private ViewPager viewPager, viewPager2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profil);
viewPager = (ViewPager) findViewById(R.id.viewpager);
createViewPager(viewPager);
viewPager2 = (ViewPager) findViewById(R.id.viewpager2);
createViewPager2(viewPager2);
tabLayout = (TabLayout) findViewById(R.id.tabs);
tabLayout.setupWithViewPager(viewPager);
tabLayout2 = (TabLayout) findViewById(R.id.tabs);
tabLayout2.setupWithViewPager(viewPager2);
createTabIcons();
}
private void createTabIcons() {
LinearLayout tabLinearLayout = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabOne = (TextView) tabLinearLayout.findViewById(R.id.tabContent);
tabOne.setText("Paslon 1");
tabOne.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icontoast, 0, 0, 0);
tabLayout2.getTabAt(0).setCustomView(tabOne);
tabLayout.getTabAt(0).setCustomView(tabOne);
LinearLayout tabLinearLayout2 = (LinearLayout) LayoutInflater.from(this).inflate(R.layout.custom_tab, null);
TextView tabTwo = (TextView) tabLinearLayout2.findViewById(R.id.tabContent);
tabTwo.setText("Paslon 2");
tabTwo.setCompoundDrawablesWithIntrinsicBounds(R.drawable.icontoast, 0, 0, 0);
tabLayout2.getTabAt(1).setCustomView(tabTwo);
tabLayout.getTabAt(1).setCustomView(tabTwo);
}
private void createViewPager(ViewPager viewPager) {
ViewPagerAdapter adapter = new ViewPagerAdapter(getSupportFragmentManager());
adapter.addFrag(new SatuA(), "Tab 1");
adapter.addFrag(new DuaA(), "Tab 2");
viewPager.setAdapter(adapter);
}
class ViewPagerAdapter extends FragmentPagerAdapter {
private final List<Fragment> mFragmentList = new ArrayList<>();
private final List<String> mFragmentTitleList = new ArrayList<>();
public ViewPagerAdapter(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mFragmentList.get(position);
}
#Override
public int getCount() {
return mFragmentList.size();
}
public void addFrag(Fragment fragment, String title) {
mFragmentList.add(fragment);
mFragmentTitleList.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mFragmentTitleList.get(position);
}
}
private void createViewPager2(ViewPager viewPager2) {
ViewPagerAdapter2 adapter = new ViewPagerAdapter2(getSupportFragmentManager());
adapter.addFrag(new Satu(), "Tab 1");
adapter.addFrag(new Dua(), "Tab 2");
viewPager2.setAdapter(adapter);
}
class ViewPagerAdapter2 extends FragmentPagerAdapter {
private final List<Fragment> mSilit1 = new ArrayList<>();
private final List<String> mSempak1 = new ArrayList<>();
public ViewPagerAdapter2(FragmentManager manager) {
super(manager);
}
#Override
public Fragment getItem(int position) {
return mSilit1.get(position);
}
#Override
public int getCount() {
return mSilit1.size();
}
public void addFrag(Fragment fragment, String title) {
mSilit1.add(fragment);
mSempak1.add(title);
}
#Override
public CharSequence getPageTitle(int position) {
return mSempak1.get(position);
}
}
}
fragmen1.java
package com.papaozi.ground;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Satu extends Fragment {
public Satu() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dua, container, false);
}
}
fragmen2.java
package com.papaozi.ground;
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
public class Dua extends Fragment {
public Dua() {
// Required empty public constructor
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_dua, container, false);
}
}
activity_profil.xml
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/main_content"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nestedscrollview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/backgroudscrollgrid_color"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager2"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior" />
</android.support.v4.widget.NestedScrollView>
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar"
android:layout_width="match_parent"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar"
android:fitsSystemWindows="true">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="206dip"
android:background="#color/background"
app:layout_scrollFlags="scroll|exitUntilCollapsed"
android:fitsSystemWindows="true"
app:contentScrim="#color/putih"
app:expandedTitleMarginStart="48dp"
app:expandedTitleMarginBottom="20dp"
app:expandedTitleMarginEnd="64dp">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<ImageView
android:id="#+id/backdrop"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:fitsSystemWindows="true"
app:layout_collapseMode="parallax" />
<TextView
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
<android.support.design.widget.TabLayout
android:id="#+id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
app:tabBackground="#android:color/darker_gray"
app:tabIndicatorColor="#f00"
app:tabSelectedTextColor="#f00"
app:tabTextColor="#000" />
</android.support.design.widget.AppBarLayout>
</android.support.design.widget.CoordinatorLayout>
custom_tab.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"
android:gravity="center">
<TextView
android:id="#+id/tabContent"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:textAlignment="center"
android:textColor="#android:color/white"
android:gravity="center"/>
</LinearLayout>
fragmen1.xml and fragmen2.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"
tools:context="info.androidhive.materialtabs.fragments.OneFragment">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Satu Fragment"
android:textColor="#color/putih"
android:textSize="40dp"
android:textStyle="bold" />
</RelativeLayout>
thanks for helpme.
There is a simple tutorial for your purpose. You can check this link.
But it replace fragments when normal button clicked. You can just simply move the codes into your tab's click event. Just like that : See the link
If you already added fragment1 and fragment2 into viewpager1 and fragment1A and fragment2A into viewpager2 and if you want to just simply switch the fragments programmatically when you clicked the tabs, then you can use viewpager1.setCurrentItem(1)
viewpager1.setCurrentItem(0) in tabs' click events.
I am trying to show a Bottom Navigation Bar with 3 items inside a fragment, and it shows correctly without errors in my code. But the thing is that when I click on one of the options, the code supposed to be run doesn't work, it doesn't even log anything on the console.
TransporteFragment.Java:
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.view.LayoutInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
/**
* A simple {#link Fragment} subclass.
*/
public class TransporteFragment extends Fragment {
public TransporteFragment() {
// Required empty public constructor
}
private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {
#Override
public boolean onNavigationItemSelected(#NonNull MenuItem item) {
switch (item.getItemId()) {
case R.id.action_busC1:
return true;
case R.id.action_busC2:
BusC2Fragment busC2Fragment = new BusC2Fragment();
FragmentManager fragmentManager = getChildFragmentManager();
fragmentManager.beginTransaction().replace(R.id.espacioLineas, busC2Fragment).commit();
return true;
case R.id.action_busC3:
return true;
}
return false;
}
};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View v = inflater.inflate(R.layout.fragment_transporte, container, false);
BottomNavigationView navegacion = (BottomNavigationView) v.findViewById(R.id.navbartransporte);
navegacion.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_transporte, container, false);
}
}
fragment_transporte.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"
tools:context="com.gberti.smarthuesca.TransporteFragment">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/espacioLineas">
</FrameLayout>
<android.support.design.widget.BottomNavigationView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navbartransporte"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_alignParentBottom="true"
android:background="#color/white"
app:menu="#menu/navbar_transporte_items" />
</RelativeLayout>
Thank you.
In your onCreateView method instead of return inflater.inflate(R.layout.fragment_transporte, container, false); try return v;
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 do not speak English well. So a lot of it is awkward. Also, I wish me to understand, so I used the google translator.
I had the source code from the Null Pointer Error.
how can I it fix?
First It's Xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="${relativePackage}.${activityClass}" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ToggleButton
android:id="#+id/toggleButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="ToggleButton" />
<ToggleButton
android:id="#+id/toggleButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:text="ToggleButton" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/pager2"
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</LinearLayout>
It's java File
package ui.tab;
import android.os.Bundle;
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.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.LinearLayout;
import com.kdevproject.game.mining.R;
public class Tab2Activity extends Fragment{
// newInstance constructor for creating fragment with arguments
public static Tab2Activity newInstance() {
Tab2Activity tab2Activity = new Tab2Activity();
return tab2Activity;
}
// Store instance variables based on arguments passed
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
// Inflate the view for the fragment based on layout XML
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_tab2, container, false);
if (container == null) {
return null;
}
ViewPager mViewPager = (ViewPager) getView().findViewById(R.id.pager2);
return view;
}
}
class MyPagerAdapter extends FragmentPagerAdapter {
private static int NUM_ITEMS = 6;
public MyPagerAdapter(FragmentManager fragmentManager) {
super(fragmentManager);
}
// Returns total number of pages
#Override
public int getCount() {
return NUM_ITEMS;
}
// Returns the fragment to display for that page
#Override
public Fragment getItem(int position) {
switch (position) {
case 0: // Fragment # 0 - This will show FirstFragment
return Tab2_1Activity.newInstance();
case 1:
return Tab2_1Activity.newInstance();
}
return null;
}
// Returns the page title for the top indicator
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
Use view instead of getView() in onCreateView to initialize ViewPager :
ViewPager mViewPager = (ViewPager)view.findViewById(R.id.pager2);