Getting ERROR: When im clickingone of my item in my Recyclerview - firebase

I want to make a theme app and I have a home page where I am trying to show some theme As a category, and there I am using RecyclerView for each category, and my Recyclerview get data from firebase real-time database where I use one image link, one text, and one URL which I want to show in my WebView through this code
Intent intent = new Intent(v.getContext().getApplicationContext(), BrowserActivity.class);
intent.putExtra("url_E", url);
v.getContext().startActivity(intent);
but the problem is all items are fetching perfectly from firebase, and showing the correct title and image each and every item of my Recyclerview but if I click the 6 or 7 items from Recyclerview then the link was not loaded correctly, The link that is loading is not that item link, The link is being replaced with another item link and that wrong link I can find in the database from another item in the same category.
****Please Help Me
If I add a Toast.makeText(, url, Toast.LENGTH_SHORT).show(); with setOnClickListener then the correct link is showing in Toast but the load link on the webpage is wrong same!!!! -- -- 🤦‍♀️🤦‍♂️🤦‍♂️🤦‍♂️🤦‍♂️
HErer is my activity :
enter image description here
layout:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="end"
android:orientation="horizontal">
<ImageView
android:layout_width="17dp"
android:layout_height="17dp"
android:layout_gravity="bottom"
android:layout_marginStart="5dp"
android:layout_marginEnd="10sp"
android:src="#drawable/theme_review" />
</LinearLayout>
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/RVModern"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="5dp"
android:layout_marginBottom="8dp"
android:paddingStart="10dp" />
MainFragemnt:
DataRef = FirebaseDatabase.getInstance().getReference("ThemeByStyle");
// Modern
DataRef.child("ModernTheme").addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if (dataSnapshot.exists()) {
for (DataSnapshot npsnapshot : dataSnapshot.getChildren()) {
ListDataMODERN = npsnapshot.getValue(ListData.class);
listData1.add(ListDataMODERN);
}
homeAdapter = new HomeAdapter(listData1);
RVModern.setAdapter(homeAdapter);
Collections.reverse(listData1);
RVModern.setLayoutManager(new LinearLayoutManager(getActivity(), LinearLayoutManager.HORIZONTAL, false));
homeAdapter.notifyDataSetChanged();
}
}
#Override
public void onCancelled(DatabaseError databaseError) {
}
});
adapter:
{
private final List<ListData> listData;
CardView ClickItem;
String url;
ImageView post_img;
TextView post_title;
Dialog OkBox;
public HomeAdapter (List<ListData> listData) {
this.listData = listData;
}
#SuppressLint("WrongConstant")
#NonNull
#Override
public HomeAdapter.ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.post_body_home,parent,false);
ClickItem = view.findViewById(R.id.cardview);
OkBox = new Dialog(view.getContext());
return new HomeAdapter.ViewHolder(view);
}/**************************************************************************/
#Override
public void onBindViewHolder(#NonNull ViewHolder holder, int position) {
final ListData listdata = listData.get(position);
holder.setUrlss(listdata.getUrls());
holder.setTitle(listdata.getTitle());
holder.setImgOne(listdata.getImg1());
// url = listdata.getUrls();
}
#Override
public int getItemCount () {
return listData.size();
// return 2;
}
public class ViewHolder extends RecyclerView.ViewHolder {
View vv;
public ViewHolder(View itemView) {
super(itemView);
vv = itemView;
}
public void setTitle(String title) {
post_title = vv.findViewById(R.id.post_title);
post_title.setText(title);
}
public void setImgOne(String imgone) {
post_img = vv.findViewById(R.id.imgOne);
Glide
.with(vv)
.load(imgone)
.into(post_img);
}
public void setUrlss (String setUrls) {
TextView urltxt = vv.findViewById(R.id.urltxt);
urltxt.setText(setUrls);
url = setUrls;
ClickItem.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(v.getContext().getApplicationContext(), BrowserActivity.class);
intent.putExtra("url_E", url);
v.getContext().startActivity(intent);
/* Okbox();
ClipboardManager clipboard = (ClipboardManager) v.getContext().getSystemService(Context.CLIPBOARD_SERVICE);
ClipData clip = ClipData.newPlainText("label", setUrlXX);
clipboard.setPrimaryClip(clip);
Toast.makeText(v.getContext().getApplicationContext(), setUrlXX, Toast.LENGTH_SHORT).show();*/
}
});
}
}
private void Okbox() {
OkBox.setContentView(R.layout.ok);
Window window = OkBox.getWindow();
window.setLayout(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.TOP);
TextView t0title = OkBox.findViewById(R.id.t0title);
t0title.setText(url);
OkBox.getWindow().setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));
OkBox.show();
}
}
HELP ME PLZ PLZ PLZ PLZ PLZ PLZ PLZ PLZ

Related

IllegalStateException : Activity has been destroyed . I created fragment manager in adapter class on click view

I created recycler view and perform onclick action to get fragment view. when I click on item it should show same data in fragment so I created an on click listener in Adapter class(onBindViewHolder).
package com.example.
public class Adapter extends
RecyclerView. Adapter<ChannelAdapter.ChannelViewHolder> {
private final Context context;
private List<ChannelMode> ;
public Channel Adapter(Context context, List<ChannelModel> channelList) {
this.context = context;
this.channelList = channelList;
}
#Override
public ChannelViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
LayoutInflater layoutInflater = LayoutInflater.from(context);
View view = layoutInflater.inflate(R.layout.layout_view, parent, false);
return new ChannelViewHolder(view);
}
#Override
public void onBindViewHolder(#NonNull final ChannelViewHolder holder, final int position) {
final ChannelModel channelModel = channelList.get(position);
holder.channel_name.setText(channelModel.getChannel_name());
holder.description.setText(channelModel.getDescription());
Picasso.get().load(channelModel.getLogo()).into(holder.logo);
holder.itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(v.getContext(), "Clicked", Toast.LENGTH_LONG).show();
Fragment fragment = new FragmentView();
AppCompatActivity activity = new AppCompatActivity();
FragmentManager fragmentManager = activity.getSupportFragmentManager();
FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
fragmentTransaction.replace(R.id.framelayout, fragment).addToBackStack(null);
fragmentTransaction.commit();
}
});
}
#Override
public int getItemCount() {
return channelList.size();
}
public class ChannelViewHolder extends RecyclerView.ViewHolder {
ImageView logo;
TextView channel_name;
TextView description;
public ChannelViewHolder(#NonNull final View itemView) {
super(itemView);
logo = itemView.findViewById(R.id.imageView);
channel_name = itemView.findViewById(R.id.textViewName);
description = itemView.findViewById(R.id.textViewDesc);
}
}
}
<FrameLayout
android:id="#+id/framelayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="#+id/recyclerView" />
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Fragment view when I click on recyclerview item.

Firebase ui 3.1.0 (Firebaselistadapter) No setter/field for mc_name found on class

I've tried my code in normal activity both images and text show. in fragment only image showing but the text is not showing (in both normal and fragment activity I've used same xml layout). I'm using firebaselistadpter for gridview and it works fine. I also tried using it with listview in fragment still images only showing but no text showing. (there is no Error message).
1- my java activity:
public class R_MainCat extends Fragment {
private GridView gridView;
private DatabaseReference myRef;
private FirebaseListAdapter<Sub_Category_Data> adapter;
private TextView mc_name;
private ImageView url;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.activity_gridview, container, false);
rootView.setBackgroundColor(Color.WHITE);
myRef= FirebaseDatabase.getInstance().getReference("Main_Category");
Firebase.getDefaultConfig().setPersistenceEnabled(true);
gridView = (GridView) rootView.findViewById(R.id.gridview);
FirebaseListOptions<Sub_Category_Data> options = new FirebaseListOptions.Builder<Sub_Category_Data>()
.setQuery(myRef, Sub_Category_Data.class).setLayout(R.layout.gridview_icon).build();
adapter = new FirebaseListAdapter<Sub_Category_Data>(options) {
#Override
protected void populateView(View v, Sub_Category_Data model, int position) {
mc_name = v.findViewById(R.id.cat_name);
url = v.findViewById(R.id.img);
mc_name.setText(model.getName());
Picasso.with(getActivity()).load(model.getUrl()).into(url);
}
};
gridView.setAdapter(adapter);
gridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int i, long id) {
switch(i)
{
case 0:
R_DairyProds f1 = new R_DairyProds();
FragmentTransaction fragTransaction1 = getFragmentManager().beginTransaction();
fragTransaction1.replace(R.id.container_view,f1 );
fragTransaction1.addToBackStack(null);
fragTransaction1.commit();
break;
case 1:
R_Bakery f2 = new R_Bakery();
FragmentTransaction fragTransaction2 = getFragmentManager().beginTransaction();
fragTransaction2.replace(R.id.container_view,f2 );
fragTransaction2.addToBackStack(null);
fragTransaction2.commit();
break;
case 2:
R_Grocery f3 = new R_Grocery();
FragmentTransaction fT3 = getFragmentManager().beginTransaction();
fT3.replace(R.id.container_view,f3);
fT3.addToBackStack(null);
fT3.commit();
break;
case 3:
R_FruitsNVegs f4 = new R_FruitsNVegs();
FragmentTransaction fT4 = getFragmentManager().beginTransaction();
fT4.replace(R.id.container_view,f4);
fT4.addToBackStack(null);
fT4.commit();
break;
case 4:
R_CleanNWash f5 = new R_CleanNWash();
FragmentTransaction fT5 = getFragmentManager().beginTransaction();
fT5.replace(R.id.container_view,f5);
fT5.addToBackStack(null);
fT5.commit();
break;
}
}
});
return rootView;
}
#Override
public void onStart() {
super.onStart();
adapter.startListening();
}
#Override
public void onStop() {
super.onStop();
adapter.stopListening();
}
}
2-my xml layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.v7.widget.CardView
xmlns:card_view="http://schemas.android.com/apk/res-auto"
android:id="#+id/card_view"
android:focusable="false"
android:focusableInTouchMode="false"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true"
card_view:cardCornerRadius="4dp">
<RelativeLayout
android:descendantFocusability="blocksDescendants"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/img"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"/>
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/img"
>
<TextView
android:id="#+id/cat_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="hello_world"
android:textColor="#000"
android:textSize="20sp"
android:gravity="center"
/>
</me.grantland.widget.AutofitLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
3- my Data model java:
public class Sub_Category_Data {
private String mc_name;
private String url;
public Sub_Category_Data() {
}
public void setName(String mc_name) {
this.mc_name = mc_name;
}
public String getName() {
return mc_name;
}
public void setUrl(String url) {
this.url = url;
}
public String getUrl() {
return url;
}
}
4- snapshot of the output:
text suppose to be showing under the image.
Output in fragment
I want my fragment to show the text data same as what is showing in this image
Output I've tested in normal Activity
5- database snapshot:
Database Image
does anyone have any clue? or have faced this issue before? because I'm lost since there is no error showing besides the fact it works fine in normal activity but text is not showing in the fragment. thank you in advance.
after looking over and over I noticed this in logcat
"No setter/field for mc_name found on class"
I've fixed the problem. It was in the model class getName() and SetName() methods I changed them to same name as the child key name in my firebase you can see picture of the database in the question above. so the new methods are getMc_name() and setMc_name().
public class Sub_Category_Data {
private String mc_name;
private String url;
public Sub_Category_Data() {
}
public String getMc_name() {
return mc_name;
}
public void setMc_name(String mc_name) {
this.mc_name = mc_name;
}
public String getUrl() {
return url;
}
public void setUrl(String url) {
this.url = url;
}
}
the output screen shot

FirebaseRecyclerAdapter - populateViewHolder is not populating the data for first time it runs?

FirebaseRecyclerAdapter - populateViewHolder is not populating the data for the first time it runs but when I closed the app and opened it, the data is binded in RecyclerView View Holder.
I am not getting why data is not populating for first time, it is showing blank screen
Here's my MainActivity
mDatabase = FirebaseDatabase.getInstance().getReference();
// [END create_database_reference]
mRecycler = (RecyclerView) findViewById(R.id.recycler_view);
mRecycler.setHasFixedSize(true);
setSupportActionBar(toolbar);
final LinearLayoutManager layoutManager = new LinearLayoutManager(getApplicationContext());
layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
layoutManager.setReverseLayout(true);
layoutManager.setStackFromEnd(true);
mRecycler.setLayoutManager(layoutManager);
// Set up FirebaseRecyclerAdapter with the Query
//Query postsQuery = mDatabase.child("EN").child("Courses") ;
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("EN").child("Courses");
ref.addListenerForSingleValueEvent(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
Toast.makeText(MainActivity.this, dataSnapshot.toString(), Toast.LENGTH_SHORT).show();
}
#Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(MainActivity.this, databaseError.toString(), Toast.LENGTH_SHORT).show();
}
});
Log.d("ref", String.valueOf(ref));
// Log.d("Query", String.valueOf(postsQuery));
mAdapter = new FirebaseRecyclerAdapter<Course, CourseViewHolder>(Course.class, R.layout.item_home,
CourseViewHolder.class, ref) {
#Override
protected void populateViewHolder(final CourseViewHolder viewHolder, final Course model, final int position) {
viewHolder.bindToPost(model);
}
};
mRecycler.setAdapter(mAdapter);
And the ViewHolder
public class CourseViewHolder extends RecyclerView.ViewHolder {
public TextView titleView;
public TextView descriptionView;
public ImageView IconView;
public TextView lessonCountView;
public CourseViewHolder(View itemView) {
super(itemView);
titleView = (TextView) itemView.findViewById(R.id.title);
descriptionView = (TextView) itemView.findViewById(R.id.description);
//IconView = (ImageView) itemView.findViewById(R.id.star);
}
public void bindToPost(Course post) {
// Log.d("Post", String.valueOf(post));
titleView.setText(post.getName());
descriptionView.setText(post.getDescription());
}
}
My Model Pojo
public class Course {
public String description;
public String title;
public Course() {
// Default constructor required for calls to DataSnapshot.getValue(Post.class)
}
public Course(String title, String description) {
// this.description = author;
this.title = title;
this.description =description;
}
public String getName() {
return title;
}
public String getDescription() {
return description;
}
}
Found the Solution.
The Problem is RecyclerView had a height of wrap_content. So Please make sure your RecyclerView height is set to match_parent. This will fix this issue.
<android.support.v7.widget.RecyclerView
android:id="#+id/recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" />
If you don't want to use match_parent you shouldn't call mRecycler.setHasFixedSize(true);

How to transfer variables to a fragment was created using the ActionBarActivity?

The activity is called from another activity delivering a string array, which needs to be displayed in a fragment of the target activity.
MainActivity (string array) -> Result2Activity: Tab1 should display string index0, Tab2 should display string index1 etc.
I followed a similar problem here:
Access Fragment View from Activity's onCreate
My problem is right now that the onActivityCreated method is never called in the Result2Activity.
I am also not sure if this method is the right way to address the fragment:
getSupportFragmentManager().findFragmentByTag("Out1");
What is the best way to achieve that ?
Result2Activity:
public class Result2Activity extends ActionBarActivity implements
ActionBar.TabListener {
/**
* 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}.
*/
SectionsPagerAdapter mSectionsPagerAdapter;
/**
* The {#link ViewPager} that will host the section contents.
*/
ViewPager mViewPager;
public List<String> fragments = new Vector<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.result2_activity);
// Set up the action bar.
final ActionBar actionBar = getSupportActionBar();
actionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
// 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.pager);
mViewPager.setAdapter(mSectionsPagerAdapter);
// When swiping between different sections, select the corresponding
// tab. We can also use ActionBar.Tab#select() to do this if we have
// a reference to the Tab.
mViewPager
.setOnPageChangeListener(new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
actionBar.setSelectedNavigationItem(position);
}
});
// For each of the sections in the app, add a tab to the action bar.
for (int i = 0; i < mSectionsPagerAdapter.getCount(); i++) {
// Create a tab with text corresponding to the page title defined by
// the adapter. Also specify this Activity object, which implements
// the TabListener interface, as the callback (listener) for when
// this tab is selected.
actionBar.addTab(actionBar.newTab()
.setText(mSectionsPagerAdapter.getPageTitle(i))
.setTabListener(this));
}
Log.d("DEBUG","1: onCreate finished");
}
public void onActivityCreated (Bundle savedInstanceState) {
Log.d("DEBUG","2: onActivityCreated finished");
//super.onActivityCreated(savedInstanceState);
// retrieve data from MainActivity
Intent intent = getIntent();
String message[] = intent.getStringArrayExtra(MainActivity.OUTPUT);
//send data to fragment Out1Fragment
Out1Fragment set_out1 =
(Out1Fragment)
getSupportFragmentManager().findFragmentByTag("Out1");
set_out1.settext(message[0]);
}
#Override
public void onTabSelected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
// When the given tab is selected, switch to the corresponding page in
// the ViewPager.
mViewPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabUnselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
#Override
public void onTabReselected(ActionBar.Tab tab,
FragmentTransaction fragmentTransaction) {
}
public class SectionsPagerAdapter extends FragmentPagerAdapter {
public SectionsPagerAdapter(FragmentManager fm) {
super(fm);
fragments.add(Out1Fragment.class.getName());
fragments.add(Out2Fragment.class.getName());
fragments.add(Out3Fragment.class.getName());
}
#Override
public Fragment getItem(int position) {
Fragment fragment = null;
switch (position) {
case 0:
fragment = new Out1Fragment();
break;
case 1:
fragment = new Out2Fragment();
break;
case 2:
fragment = new Out3Fragment();
break;
}
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.out_section1).toUpperCase(l);
case 1:
return getString(R.string.out_section2).toUpperCase(l);
case 2:
return getString(R.string.out_section3).toUpperCase(l);
}
return null;
}
}
}
Out1Fragment:
public class Out1Fragment extends Fragment {
private static TextView textview;
public Out1Fragment() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View dataView = inflater.inflate(R.layout.out1_fragment, container,
false);
textview = (TextView) dataView.findViewById(R.id.outPut1);
return dataView;
}
public void settext(String text)
{
textview.setText(text);
}
}
XML Resource:
result2_activity.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="dd.testing.finance.Result2Activity" />
out1_fragment.xml
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/ScrollView2"
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"
android:tag="Out1"
tools:context="dd.testing.finance.Result2Activity$Out1Fragment" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:orientation="vertical" >
<TextView
android:id="#+id/outPut1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginLeft="10sp"
android:layout_marginRight="10sp"
android:textSize="16sp" />
</LinearLayout>
</ScrollView>
Not really sure if this was the right way, but at least it does exaclty what I needed.
In the Out1Fragment.class added the onActivityCreated method:
public void onActivityCreated (Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
settext(((Result2Activity)getActivity()).getOutput1());
}
From there calling the added method getOutput1 in the Result2Activity:
public String getOutput1 () {
// retrieve data from MainActivity
Intent intent = getIntent();
String message[] = intent.getStringArrayExtra(MainActivity.OUTPUT);
return message[0];
}

Customizing the fonts?

How to customize the ttf fonts on tab fragments? I am using sherlockactionbar.
Here is my code,
<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=".MainActivity" >
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true" />
<FrameLayout
android:id="#+id/ptr_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
And in Main activity am using,
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/** to check whether user logged in or not */
session = new TwitterHomeActivity(getApplicationContext());
session.checklogin();
/** Getting a reference to action bar of this activity */
mActionBar = getSupportActionBar();
getSupportActionBar().setDisplayUseLogoEnabled(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
mActionBar.setDisplayOptions(0, ActionBar.DISPLAY_USE_LOGO);
/** Set tab navigation mode */
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_TABS);
/** Getting a reference to ViewPager from the layout */
mPager = (ViewPager) findViewById(R.id.pager);
/** Getting a reference to FragmentManager */
FragmentManager fm = getSupportFragmentManager();
/** Defining a listener for pageChange */
ViewPager.SimpleOnPageChangeListener pageChangeListener = new ViewPager.SimpleOnPageChangeListener() {
#Override
public void onPageSelected(int position) {
super.onPageSelected(position);
setTitle(getTitleFromPosition(position));
mActionBar.setSelectedNavigationItem(position);
// mActionBar.setBackgroundDrawable(R.drawable.ic_action_chance_icon);
}
private CharSequence getTitleFromPosition(int position) {
if (position == 0) {
title = "text";
} else if (position == 1) {
title = "text";
} else if (position == 2) {
title = "text";
} else if (position == 3) {
title = "text";
}
return title;
}
};
/** Setting the pageChange listner to the viewPager */
mPager.setOnPageChangeListener(pageChangeListener);
/** Creating an instance of FragmentPagerAdapter */
MyFragmentPagerAdapter fragmentPagerAdapter = new MyFragmentPagerAdapter(
fm);
/** Setting the FragmentPagerAdapter object to the viewPager object */
mPager.setAdapter(fragmentPagerAdapter);
mActionBar.setDisplayShowTitleEnabled(true);
/** Defining tab listener */
ActionBar.TabListener tabListener = new ActionBar.TabListener() {
#Override
public void onTabUnselected(Tab tab, FragmentTransaction ft) {
}
#Override
public void onTabSelected(Tab tab, FragmentTransaction ft) {
mPager.setCurrentItem(tab.getPosition());
}
#Override
public void onTabReselected(Tab tab, FragmentTransaction ft) {
}
};
/**/
Tab tab = mActionBar.newTab();
tab = mActionBar.newTab();
tab.setText(“text”);
tab.setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab();
tab.setText(“ttext”);
tab.setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab();
tab.setText(“text”);
tab.setTabListener(tabListener);
mActionBar.addTab(tab);
tab = mActionBar.newTab();
tab.setText(“text”);
tab.setTabListener(tabListener);
mActionBar.addTab(tab);
try {
ViewConfiguration config = ViewConfiguration.get(this);
Field menuKeyField = ViewConfiguration.class
.getDeclaredField("sHasPermanentMenuKey");
if (menuKeyField != null) {
menuKeyField.setAccessible(true);
menuKeyField.setBoolean(config, false);
}
} catch (Exception ex) {
// Ignore
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getSupportMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.camera:
startActivity(new Intent(MainActivity.this, Request_Broadcast.class));
overridePendingTransition(R.anim.right_slide_in,
R.anim.right_slide_out);
return true;
case R.id.find_people:
startActivity(new Intent(MainActivity.this, Find_people.class));
overridePendingTransition(R.anim.right_slide_in,
R.anim.right_slide_out);
return true;
case R.id.action_settings:
startActivity(new Intent(MainActivity.this, Pro_settings.class));
overridePendingTransition(R.anim.right_slide_in,
R.anim.right_slide_out);
return true;
}
return true;
}
#SuppressLint("NewApi")
public void setActionBarTitle(int feedPage) {
getActionBar().setTitle(feedPage);
}
#Override
public void onPageScrollStateChanged(int arg0) {
// TODO Auto-generated method stub
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
// TODO Auto-generated method stub
}
#Override
public void onPageSelected(int arg0) {
}
}
Now i want to customize the fonts on tab using typeface. How to do that and the text on tabs is displaying in uppercase letters. I want to display that in lowercase letters..
Here you have an efficent way to change the font for the entire application: Access a typeface once from asset and use it as a reference
EDIT
You can use the source code of the class I refer you to the link above. Now what you can do is change the font recursively starting from the parent layout and descending for each of their children. You should be aware that any view that can contain text is directly or indirectly a subclass of the TextView class (ie Button, EditText, CheckBox, CompoundButton, RadioButton, ToggleButton, etc). If you want to use different fonts in your application you can use for example the tag property of the view to define the font that will have that particular view. An example layout would look like this:
activity_main.xml
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="title"
android:text="This is the title screen" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="content"
android:text="This is the activity content." />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:tag="content"
android:text="This is a button" />
Below this is the recursive method through which you can change the font of all views of the activity. This method is an additional method defined in class FontUtils:
public static void setActivityTypeFace(Context context, ViewGroup viewGroup) {
Object tagAux;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View viewChild = viewGroup.getChildAt(i);
if (viewChild instanceof ViewGroup) {
setActivityTypeFace(context, (ViewGroup) viewChild);
} else if (viewChild instanceof TextView) {
tagAux = viewChild.getTag();
if (tagAux != null) {
if (((String) tagAux).compareTo("title") == 0)
{
((TextView) viewChild).setTypeface(getTypeface(context,
FontType.TITLE_FONT.toString()));
}
} else if (((String) tagAux).compareTo("content") == 0) {
((TextView) viewChild).setTypeface(getTypeface(context,
FontType.CONTENT_FONT.toString()));
}
}
}
}
MainActivity.java
public class MainActivity extends Activity
private ViewGroup mRootView;
...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
mRootView = (ViewGroup) findViewById(R.id.rootView);
...
FontUtil.setActivityTypeFace(this, mRootView);
}
}

Resources