Setting Custom Text in DialogFragment - android

Good morning guys, its quite long time I'm trying to show a custom text in my DialogFragment, but still I got nothing, I have multiple tabs inside an acitivty that extends FragmentActivity and implements TabListener, here is how I call the dialog:
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_contact:
QuickContactFragment dialog = new QuickContactFragment();
dialog.show(getSupportFragmentManager(), "QuickContactFragment");
return true;
}
return super.onOptionsItemSelected(item);
}
and here is my dialog body:
public class QuickContactFragment extends DialogFragment {
View view;
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private ContactPagerAdapter adapter;
public static QuickContactFragment newInstance() {
QuickContactFragment f = new QuickContactFragment();
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState) {
if (getDialog() != null) {
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
getDialog().getWindow().setBackgroundDrawableResource(android.R.color.transparent);
}
View root = inflater.inflate(R.layout.fragment_quick_contact, container,
false);
tabs = (PagerSlidingTabStrip) root.findViewById(R.id.tabs);
pager = (ViewPager) root.findViewById(R.id.pager);
adapter = new ContactPagerAdapter();
pager.setAdapter(adapter);
tabs.setViewPager(pager);
return root;
}
#SuppressWarnings("deprecation")
#Override
public void onStart() {
super.onStart();
// change dialog width
if (getDialog() != null) {
int fullWidth = getDialog().getWindow().getAttributes().width;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB_MR2) {
Display display =
getActivity().getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
fullWidth = size.x;
} else {
Display display =
getActivity().getWindowManager().getDefaultDisplay();
fullWidth = display.getWidth();
}
final int padding = (int)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 24, getResources()
.getDisplayMetrics());
int w = fullWidth - padding;
int h = getDialog().getWindow().getAttributes().height;
getDialog().getWindow().setLayout(w, h);
}
}
public class ContactPagerAdapter extends PagerAdapter implements IconTabProvider {
private final int[] ICONS = { R.drawable.ic_launcher_gplus,
R.drawable.ic_launcher_gmail,
R.drawable.ic_launcher_gmaps,
R.drawable.ic_launcher_chrome };
public ContactPagerAdapter() {
super();
}
//here is implemention too
//here shows how many tabs should it predict.. i can set N=0 and every
time a notification comes, set N=N+1, and then whenever delete happenes, N=N-1 ;D
#Override
public int getCount() {
return ICONS.length;
}
////this is implemented method by the icontabprovider.. just figure it to set image
on IF
down there,
#Override
public int getPageIconResId(int position) {
return ICONS[position];
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
// looks a little bit messy here
TextView v = new TextView(getActivity());
v.setBackgroundResource(R.color.background_window);
if (position == 2){
v.setText("this is page 3 :D");
TextView tv =(TextView)getActivity().findViewById(R.id.quickcontacttext);
tv.setText("have you heared it? just bieber is pragnent !");
}
else{
v.setText("PAGE " + (position + 1));
}
final int padding = (int)
TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 16, getResources()
.getDisplayMetrics());
v.setPadding(padding, padding, padding, padding);
v.setGravity(Gravity.CENTER);
container.addView(v, 0);
return v;
}
#Override
public void destroyItem(ViewGroup container, int position, Object view) {
container.removeView((View) view);
}
#Override
public boolean isViewFromObject(View v, Object o) {
return v == ((View) o);
}
}
}
Where I need to set text , is where page changes, but as more as I try , more I get fraustrated, could you guys throw some help here please? thanks by the way

Inside onCreateView of you DialogFragment you should do:
View v = inflater.inflate(R.layout.hello_world, container, false);
View tv = v.findViewById(R.id.text);
((TextView)tv).setText("This is an instance of MyDialogFragment");
Check the DialogFragment Reference

Related

How to avoid going to previous slide in Viewpager in android

I have implemented a ViewPager in my application which consist of two slides. I can go from first slide to second slide and reverse as well. But I have a special case here. If the user goes from First Slide to Second Slide. He should not be able to go back to the First slide. I need to disable going back to the First Slide once he enters the Second Slide.
Here is my code:
public class BotConnectionDialog extends DialogFragment {
private RelativeLayout toolbar;
private ImageView toolbarCloseButton;
private View layoutView;
private ViewPager viewPager;
private BotConnectionDialogAdapter myViewPagerAdapter;
private LinearLayout dotsLayout;
private TextView[] dots;
int width, height;
DisplayMetrics metrics;
private int[] layouts;
private final String TAG = BotConnectionDialog.class.getSimpleName();
private Button btnSkip, btnNext;
View.OnClickListener myListener = new View.OnClickListener() {
#Override
public void onClick(View view) {
Handler handler = new Handler();
handler.post(new Runnable() {
#Override
public void run() {
getActivity().getSupportFragmentManager().popBackStackImmediate();
}
});
}
};
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, android.R.style.Theme_Black_NoTitleBar_Fullscreen);
}
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.botconnection_layout, container, false);
Typeface typeface = Typeface.createFromAsset(getActivity().getAssets(), "fonts/corbert.otf");
toolbar = (RelativeLayout) rootView.findViewById(R.id.customAppBarLayout);
toolbar.setBackgroundColor(Color.parseColor("#00000000"));
toolbarCloseButton = (ImageView) rootView.findViewById(R.id.toolbarCloseButton);
toolbarCloseButton.setOnClickListener(myListener);
viewPager = (ViewPager) rootView.findViewById(R.id.view_pager);
dotsLayout = (LinearLayout) rootView.findViewById(R.id.layoutDots);
btnSkip = (Button) rootView.findViewById(R.id.btn_skip);
btnSkip.setTypeface(typeface);
btnNext = (Button) rootView.findViewById(R.id.btn_next);
btnNext.setTypeface(typeface);
metrics = getResources().getDisplayMetrics();
width = metrics.widthPixels;
height = metrics.heightPixels;
// layouts of all welcome sliders
// add few more layouts if you want
layouts = new int[]{
R.layout.slide1,
R.layout.slide2};
// adding bottom dots
addBottomDots(0);
myViewPagerAdapter = new BotConnectionDialogAdapter();
viewPager.setAdapter(myViewPagerAdapter);
viewPager.addOnPageChangeListener(viewPagerPageChangeListener);
btnNext.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// checking for last page
// if last page home screen will be launched
int current = getItem(+1);
if (current < layouts.length) {
// move to next screen
viewPager.setCurrentItem(current);
} else {
}
}
});
return rootView;
}
private void addBottomDots(int currentPage) {
dots = new TextView[layouts.length];
int[] colorsActive = getResources().getIntArray(R.array.array_dot_active);
int[] colorsInactive = getResources().getIntArray(R.array.array_dot_inactive);
dotsLayout.removeAllViews();
for (int i = 0; i < dots.length; i++) {
dots[i] = new TextView(getActivity());
dots[i].setText(Html.fromHtml("•"));
dots[i].setTextSize(35);
dots[i].setTextColor(colorsInactive[currentPage]);
dotsLayout.addView(dots[i]);
}
if (dots.length > 0)
dots[currentPage].setTextColor(colorsActive[currentPage]);
}
private int getItem(int i) {
return viewPager.getCurrentItem() + i;
}
// viewpager change listener
ViewPager.OnPageChangeListener viewPagerPageChangeListener = new ViewPager.OnPageChangeListener() {
#Override
public void onPageSelected(int position) {
addBottomDots(position);
// changing the next button text 'NEXT' / 'GOT IT'
if (position == layouts.length - 1) {
// last page. make button text to GOT IT
btnNext.setText(getString(R.string.start));
btnSkip.setVisibility(View.GONE);
/**
* bluetooth connection the bot
*/
Toast.makeText(getActivity(), "Finished", Toast.LENGTH_SHORT).show();
} else {
// still pages are left
btnNext.setText(getString(R.string.next));
btnSkip.setVisibility(View.VISIBLE);
/**
* First page
*/
Toast.makeText(getActivity(), "2nd page", Toast.LENGTH_SHORT).show();
}
}
#Override
public void onPageScrolled(int arg0, float arg1, int arg2) {
}
#Override
public void onPageScrollStateChanged(int arg0) {
}
};
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
//dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
return dialog;
}
#Override
public void onResume() {
super.onResume();
// getDialog().getWindow().setLayout((7 * width) / 7, (4 * height) / 5);
}
public class BotConnectionDialogAdapter extends PagerAdapter {
/**
* View pager adapter
*/
private LayoutInflater layoutInflater;
public BotConnectionDialogAdapter() {
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater = (LayoutInflater) getActivity().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate(layouts[position], container, false);
container.addView(view);
return view;
}
#Override
public int getCount() {
return layouts.length;
}
#Override
public boolean isViewFromObject(View view, Object obj) {
return view == obj;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
View view = (View) object;
container.removeView(view);
}
}
}
/**
* To disable and enable scrolling on ViewPager, just set shouldScroll to true or false.
*/
public class ScrollableViewPager extends ViewPager {
private boolean shouldScroll = true;
public ScrollableViewPager(Context context, AttributeSet attrs) {
super(context, attrs);
}
public void setShouldScroll(boolean shouldScroll) {
this.shouldScroll = shouldScroll;
}
#Override
public boolean onTouchEvent(MotionEvent ev) {
return shouldScroll && super.onTouchEvent(ev);
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
return shouldScroll && super.onInterceptTouchEvent(ev);
}
}
Now use this ScrollableViewPager in place of the default ViewPager and in your onPageSelected() callback
check for the position. If it is the last position, call the setShouldScroll(false) method on your viewPager instance, which will make shouldScroll to false in our ScrollableViewPager class and eventually make the scrolling invalid as we have overridden onTouchEvent() and onInterceptTouchEvent() method, which checks for the value of shouldScroll.

Android animating ImageViews within a ViewPager

Hi i have a view pager that loads fragments and i'm wanting to add a bit of animation to an ImageView make it move up as the ViewPager scrolls. problem i'm having is that the viewPager creates the views for the first two pages so the animation doesnt happen on those pages. Is there is a way to detect if viewpager is scrolling or if the page has changed?
heres what i have tried so far - heres my viewPagerAdapter
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
if(page == 0){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
}else if(page == 1){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
tutImage = (ImageView)view.findViewById(R.id.tutImage);
int imageResource = getResources().getIdentifier("tut_view_2", "drawable", context.getPackageName());
Drawable tutImageDrawable = getResources().getDrawable(imageResource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tutImage.setBackground(tutImageDrawable);
}else{
tutImage.setBackgroundDrawable(tutImageDrawable);
}
}else if(page == 2){
view = inflater.inflate(R.layout.layout_tutorial, container, false);
tutImage = (ImageView)view.findViewById(R.id.tutImage);
int imageResource = getResources().getIdentifier("tut_view_3", "drawable", context.getPackageName());
Drawable tutImageDrawable = getResources().getDrawable(imageResource);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
tutImage.setBackground(tutImageDrawable);
}else{
tutImage.setBackgroundDrawable(tutImageDrawable);
}
heres my viewpager
public static class MyPagerAdapter extends FragmentPagerAdapter {
private Context context;
public MyPagerAdapter(android.support.v4.app.FragmentManager fragmentManager,Context c) {
super(fragmentManager);
context = c;
}
#Override
public int getCount() {
int levelCount = 4;
return levelCount;
}
// Returns the fragment to display for that page
#Override
public TutorialAdapter getItem(int position) {
String data = null;
return TutorialAdapter.newInstance(position, data, context);
}
#Override
public CharSequence getPageTitle(int position) {
return "Page " + position;
}
}
You need to use a OnPageChangeListener, set it to your viewpager:
viewPager.setOnPageChangeListener(myOnPageChangeListener);
The OnPageChangeListener implements some methods like:
#Override
public void onPageScrollStateChanged(int state) {}
#Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {}
#Override
public void onPageSelected(int position) {}
They can help you. Check this for more information: ViewPager.OnPageChangeListener!

android pager sliding tabstrip fragment changer

I want to change the layout by swyping over the tabs with fragments. Can anybody help me solve my problem?
Here is my fragmentactivity:
public class MainActivity extends FragmentActivity {
private final Handler handler = new Handler();
private PagerSlidingTabStrip tabs;
private ViewPager pager;
private MyPagerAdapter adapter;
private Drawable oldBackground = null;
private int currentColor = 0xFF666666;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
tabs = (PagerSlidingTabStrip) findViewById(R.id.tabs);
pager = (ViewPager) findViewById(R.id.pager);
adapter = new MyPagerAdapter(getSupportFragmentManager());
pager.setAdapter(adapter);
final int pageMargin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 4, getResources()
.getDisplayMetrics());
pager.setPageMargin(pageMargin);
tabs.setViewPager(pager);
changeColor(currentColor);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.action_contact:
QuickContactFragment dialog = new QuickContactFragment();
dialog.show(getSupportFragmentManager(), "QuickContactFragment");
return true;
}
return super.onOptionsItemSelected(item);
}
private void changeColor(int newColor) {
tabs.setIndicatorColor(newColor);
// change ActionBar color just if an ActionBar is available
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
Drawable colorDrawable = new ColorDrawable(newColor);
Drawable bottomDrawable = getResources().getDrawable(R.drawable.actionbar_bottom);
LayerDrawable ld = new LayerDrawable(new Drawable[] { colorDrawable, bottomDrawable });
if (oldBackground == null) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
ld.setCallback(drawableCallback);
} else {
getActionBar().setBackgroundDrawable(ld);
}
} else {
TransitionDrawable td = new TransitionDrawable(new Drawable[] { oldBackground, ld });
// workaround for broken ActionBarContainer drawable handling on
// pre-API 17 builds
// https://github.com/android/platform_frameworks_base/commit/a7cc06d82e45918c37429a59b14545c6a57db4e4
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
td.setCallback(drawableCallback);
} else {
getActionBar().setBackgroundDrawable(td);
}
td.startTransition(200);
}
oldBackground = ld;
// http://stackoverflow.com/questions/11002691/actionbar-setbackgrounddrawable-nulling-background-from-thread-handler
getActionBar().setDisplayShowTitleEnabled(false);
getActionBar().setDisplayShowTitleEnabled(true);
}
currentColor = newColor;
}
public void onColorClicked(View v) {
int color = Color.parseColor(v.getTag().toString());
changeColor(color);
}
#Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt("currentColor", currentColor);
}
#Override
protected void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
currentColor = savedInstanceState.getInt("currentColor");
changeColor(currentColor);
}
private Drawable.Callback drawableCallback = new Drawable.Callback() {
#Override
public void invalidateDrawable(Drawable who) {
getActionBar().setBackgroundDrawable(who);
}
#Override
public void scheduleDrawable(Drawable who, Runnable what, long when) {
handler.postAtTime(what, when);
}
#Override
public void unscheduleDrawable(Drawable who, Runnable what) {
handler.removeCallbacks(what);
}
};
public class MyPagerAdapter extends FragmentPagerAdapter {
private final String[] TITLES = { "Categories", "Home", "Top Paid", "Top Free", "Top Grossing", "Top New Paid",
"Top New Free", "Trending" };
public MyPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public CharSequence getPageTitle(int position) {
return TITLES[position];
}
#Override
public int getCount() {
return TITLES.length;
}
#Override
public Fragment getItem(int position) {
return SuperAwesomeCardFragment.newInstance(position);
}
}
}
And here is my fragment that writes the numbers of the positions in a text view:
import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.FrameLayout;
import android.widget.FrameLayout.LayoutParams;
import android.widget.TextView;
public class SuperAwesomeCardFragment extends Fragment {
private static final String ARG_POSITION = "position";
private int position;
public static SuperAwesomeCardFragment newInstance(int position) {
SuperAwesomeCardFragment f = new SuperAwesomeCardFragment();
Bundle b = new Bundle();
b.putInt(ARG_POSITION, position);
f.setArguments(b);
return f;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
position = getArguments().getInt(ARG_POSITION);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
LayoutParams params = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
FrameLayout fl = new FrameLayout(getActivity());
fl.setLayoutParams(params);
final int margin = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 8, getResources()
.getDisplayMetrics());
TextView v = new TextView(getActivity());
params.setMargins(margin, margin, margin, margin);
v.setLayoutParams(params);
v.setLayoutParams(params);
v.setGravity(Gravity.CENTER);
v.setBackgroundResource(R.drawable.background_card);
v.setText("CARD " + (position + 1));
fl.addView(v);
return fl;
}
}
the library and the full code of the sample you can found here:
https://github.com/astuetz/PagerSlidingTabStrip
I know its late but might help someone:
Here is the quick fix:
Create other layouts first, let's assume that you create 3: I am also assuming the layout to give a clear idea
layout/lay1.xml - has a textview only
layout/lay2.xml - has an imageview only
layout/lay3.xml - has both textview and imageview
now inside oncreateView:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// if else or switch case, your wish
View view;
if (position == 0) {
// means lay1.xml
view = inflater.inflate(R.layout.lay1, container, false);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("Content not available");
return view;
} else if (position == 1) {
// means lay2.xml
view = inflater.inflate(R.layout.lay2, container, false);
ImageView imagebg = (ImageView) view.findViewById(R.id.image1);
imagebg.setImageResource(R.drawable.ic_launcher);
return view;
} else {
// means lay3.xml
view = inflater.inflate(R.layout.lay3, container, false);
ImageView imagebg = (ImageView) view.findViewById(R.id.image1);
imagebg.setImageResource(R.drawable.ic_launcher);
TextView tv = (TextView) view.findViewById(R.id.tv);
tv.setText("this is working wow, thanks.");
return view;
}
}

Android Fragments lose date

Hiho!
I use a ViewPager with fragmenst in my Android App. The fragments are working, but lose the displayed data. So at the firts call off my View Adapter, I can set data to the LinearLayout from fragments 1 and 2, the fragment 3 is not available. So I put data on fragment 1 and 2. When I now switch the fragmenst from one to three, the fragment on number one loses the data and get blank. How can I fix the data to display that it es displayed all of the time? If to create the information dynamicly and not in a xml file.
I hope you understand me and can help me.
public class PageAdapter extends FragmentStatePagerAdapter {
private int num_pages;
private String[] titles;
public PageAdapter(FragmentManager fm) {
super(fm);
// TODO Auto-generated constructor stub
}
#Override
public Fragment getItem(int pos) {
PageFragment f = new PageFragment(pos);
return f;
}
#Override
public int getCount() {
// TODO Auto-generated method stub
return num_pages;
}
#Override
public CharSequence getPageTitle(int position){
return titles[position];
}
public void set_num_pages(int num){
num_pages=num;
}
public void set_titles(String[] a_titles){
titles=a_titles;
}
}
Next class:
public class PageFragment extends Fragment {
private int fragmentNR;
public PageFragment(){}
public PageFragment(int nr) {
this.fragmentNR = nr;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = new View(getActivity());
if (fragmentNR == 0)
v = inflater.inflate(R.layout.info_1, container, false);
else if (fragmentNR == 1)
v = inflater.inflate(R.layout.info_2, container, false);
else if (fragmentNR == 2)
v = inflater.inflate(R.layout.info_3, container, false);
return v;
}
}
Next class:
public class MannschaftInfos extends FragmentActivity {
private String mannschaftInfos;
private int mannschaftID;
private int anzahl;
private String[] titel = new String[3];
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// set the callback listener, to receive a message when an ad was loaded
mannschaftID = getIntent().getExtras().getInt("id");
titel[0]=(String) this.getText(R.string.Info);
titel[1]=(String) this.getText(R.string.aufstellung);
titel[2]=(String) this.getText(R.string.miStatistik);
anzahl=3;
new AbrufMannschaftInfo(this).laden(mannschaftID);
AbrufMannschaftInfo.setMannschaftListesListener(new Datenlistener());
}
private class Datenlistener implements MannschaftInfosListener {
public void mannschaftInfosListenerrueck(String rueck) {
setContentView(R.layout.info_mannschaft);
PageAdapter mPageAdapter = new PageAdapter(getSupportFragmentManager());
ViewPager mViewPager = (ViewPager) findViewById(R.id.viewpager);
mPageAdapter.set_num_pages(anzahl);
mPageAdapter.set_titles(titel);
mViewPager.setAdapter(mPageAdapter);
mannschaftInfos=rueck;
LinearLayout llf1 = (LinearLayout) findViewById(R.id.ll_fragment1);
llf1.removeAllViews();
llf1.setPadding(5, 5, 5, 5);
llf1.setOrientation(LinearLayout.VERTICAL);
LinearLayout llf2 = (LinearLayout) findViewById(R.id.ll_fragment2);
llf2.removeAllViews();
llf2.setPadding(5, 5, 5, 5);
llf2.setOrientation(LinearLayout.VERTICAL);
LinearLayout llf3 = (LinearLayout) findViewById(R.id.ll_fragment3);
llf3.removeAllViews();
llf3.setPadding(5, 5, 5, 5);
llf3.setOrientation(LinearLayout.VERTICAL);
View ruler_1 = new View(MannschaftInfos.this); ruler_1.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_2 = new View(MannschaftInfos.this); ruler_2.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_3 = new View(MannschaftInfos.this); ruler_3.setBackgroundColor(Color.parseColor("#FFFFFF"));
View ruler_4 = new View(MannschaftInfos.this); ruler_4.setBackgroundColor(Color.parseColor("#FFFFFF"));
MannschaftInfosAufbereiten mInfos = new MannschaftInfosAufbereiten(mannschaftInfos);
mInfos.show_wappen(llf1, MannschaftInfos.this);
llf1.addView(ruler_1,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 3));
mInfos.show_stadion(llf1, MannschaftInfos.this);
llf1.addView(ruler_2,new ViewGroup.LayoutParams( ViewGroup.LayoutParams.FILL_PARENT, 3));
mInfos.show_ligen(llf1, MannschaftInfos.this);
mInfos.show_stat(llf2, MannschaftInfos.this);
}
}
}

Call Method From Activity to Fragments

i want to build app with viewpager and display image and text from DB, and i had a button to resize Text to Small,Medium,Big size, but my problem how to make that methode and call it? this is my code
Phrase.java
public void onCreate(Bundle savedInstanceState) {
setContentView(R.layout.phrase);
mViewPager = (ViewPager) findViewById(R.id.viewPager);
mMyFragmentPagerAdapter = new MyFragmentPagerAdapter(getSupportFragmentManager());
mViewPager.setAdapter(mMyFragmentPagerAdapter);
text = (ImageView)findViewById(R.id.ibText);
/* call db and store in arraylist */
....
}
private static class MyFragmentPagerAdapter extends FragmentPagerAdapter {
public MyFragmentPagerAdapter(FragmentManager fm) {
super(fm);
}
#Override
public Fragment getItem(int index) {
var = arrayphrase.get(index);
phrase = var.phraseKey;
title_phrase = var.titleKey;
sound_id = var.indexkey;
return PageFragment.newInstance(index,sound_id,phrase,title_phrase);
}
#Override
public void destroyItem(View container, int position, Object object) {
// TODO Auto-generated method stub
super.destroyItem(container, position, object);
}
#Override
public int getCount() {
return NUMBER_OF_PAGES;
}
}
CharSequence[] sizetext = {"Small", "Medium", "Big","Cancel"};
#Override
public void onClick(View v) {
case R.id.ibText:
AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item)
{
if(sizetext[item].equals("Big"))
{
sizeText=24;
onUpdateSize(sizeText);
}
else if(sizetext[item].equals("Medium"))
{
sizeText=20;
onUpdateSize(sizeText);
}
else if(sizetext[item].equals("Small"))
{
sizeText=16;
onUpdateSize(sizeText);
}
}
});
AlertDialog alertList = listBuilder.create();
alertList.show();
break;
}
PageFragment.java
public class PageFragment extends Fragment {
public static PageFragment newInstance(int index,String sound, String phrase, String title_phrase) {
PageFragment pageFragment = new PageFragment();
Bundle bundle = new Bundle();
bundle.putString("title_phrase", title_phrase);
bundle.putString("sound", sound);
bundle.putString("phrase", phrase);
bundle.putInt("index", index);
pageFragment.setArguments(bundle);
return pageFragment;
}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.imageView1);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 2;
options.inScaled = false;
options.inPreferQualityOverSpeed=true;
options.inJustDecodeBounds = false;
options.inDither = false;
Bitmap bitmap = BitmapFactory.decodeResource(this.getResources(), Imgid[(int)(Math.random()*Imgid.length)], options);
//imageView.setScaleType(ScaleType.FIT_XY);
imageView.setImageBitmap(bitmap);
ScrollView sv = (ScrollView)view.findViewById(R.id.scrollView1);
LinearLayout.LayoutParams lp2= new LinearLayout.LayoutParams((int)(TorasanApp.device_width*0.7), (int) (TorasanApp.device_height*0.65),Gravity.CENTER);
sv.setLayoutParams(lp2);
sv.setBackgroundColor(Color.argb(20, 65, 65, 65));
TextView tvdata = (TextView)view.findViewById(R.id.tvDetailPhrase);
tvdata.setText(getArguments().getString("phrase"));
TextView tvtitle = (TextView)view.findViewById(R.id.tvTitlephrase);
tvtitle.setText(getArguments().getString("title_phrase"));
return view;
}
}
you see in my Phrase when onclick i have onUpdateSize(sizeText) and i want call it to change TextView tvdata fontsize that was in PageFragment.java , so how to make that method and call it? because i try but still cant
In your problem,
You can have a Fragment instance in onClick method for which you want to update textview and from there you can call any method of that class(you have to create such method in Fragment class which will update textview.).
For more understanding refer this link.
in above link Step 10 contain the method calling(viewer.updateUrl(tutUrl);) to fragment class from FragmentActivity class
And in step 8 defination of updateUrl method given which is in Fragment class
In your Fragment's onCreateView, do
mButton.setOnClickListener(new View.OnClickListener() {
AlertDialog.Builder listBuilder = new AlertDialog.Builder(Phrase.this);
listBuilder.setItems(sizetext, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int item)
{
if(sizetext[item].equals("Big"))
{
sizeText=24;
onUpdateSize(sizeText);
}
else if(sizetext[item].equals("Medium"))
{
sizeText=20;
onUpdateSize(sizeText);
}
else if(sizetext[item].equals("Small"))
{
sizeText=16;
onUpdateSize(sizeText);
}
}
});
AlertDialog alertList = listBuilder.create();
alertList.show();
});
Then create accessor methods in your Activity so that your Fragment can retrieve the required fields.

Categories

Resources