Android : setVisibility not work - android

I want change view linearlayout from class
But not work this code
main.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb();
}
}
And :
ComNet.java
public class ComNet {
public static Context context;
public static void readDb() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewMain = inflater.inflate(R.layout.main, null);
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
lnrPart.setVisibility(View.GONE);
}
}
How change lnrPart from main.xml in class ( ComNet ) ?

View viewMain = inflater.inflate(R.layout.main, null);
with this line you are creating a new View, with the content of main.xml. This object is different from the one you are seeing at screen in your MainActivity. setVisibility is working. You are calling it on the wrong instance

Send the activities rootView to readDB function.
main.java
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ComNet.readDb(findViewById(R.id.main));
}
}
ComNet.java
public static void readDb(View viewMain) {
LinearLayout lnrPart = (LinearLayout) viewMain.findViewById(R.id.lnrPart);
viewMain.setVisibility(View.GONE);
}

Related

Get to a function on a fragment

I am trying to get to a function on a fragment.
Please look at the function msgFromTopFragment at main activity.
The declaration of bottomFragment results in an error message – incompatible types.
And I can’t understand why. How can I fix it?
public class MainActivity extends AppCompatActivity
implements TopFragment.onBtnListener{
TopFragment topFragment = new TopFragment();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}//onCreate
#Override
public void msgFromTopFragment(String msg) {
BottomFragment buttomFragment = getFragmentManager().findFragmentById(R.id.bottomFragment);
buttomFragment.updateResult(msg);
}//msgFromTopFragment
}//MainActivity
Code for bottom fragment:
public class BottomFragment extends Fragment {
TextView resultTv;
public BottomFragment() {
// Required empty public constructor
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
View view = inflater.inflate(R.layout.fragment_bottom, container, false);
resultTv = view.findViewById(R.id.resultTv);
return view;
}
public void updateResult(String msg){
resultTv.setText(msg);
}
}//BottomFragment
BottomFragment buttomFragment = (BottomFragment) getSupportFragmentManager().findFragmentById(R.id.bottomFrag‌​ment);
this solved it :) thank you

Android pass View as an object to fragment

I'm more googling to find how can i pass simple view as an object to fragment, but i can't.
for example in MainActivity i have simple view as :
TextView text = (TextView) findviewById(R.id.tv_text);
now i want to pass that to fragment. this below code is my attach Fragment on MainActivity
MainActivity :
public void attachFragment() {
fts = getActivity().getFragmentManager().beginTransaction();
mFragment = new FragmentMarketDetail();
fts.replace(R.id.cardsLine, mFragment, "FragmentMarketDetail");
fts.commit();
}
and this is my Fragment:
public class FragmentMarketDetail extends Fragment implements ObservableScrollViewCallbacks {
public static final String SCROLLVIEW_STATE = "scrollviewState";
private ObservableScrollView scrollViewTest;
private Context context;
private int scrollY;
public static FragmentMarketDetail newInstance() {
FragmentMarketDetail fragmentFirst = new FragmentMarketDetail();
return fragmentFirst;
}
#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_online_categories, container, false);
scrollViewTest = (ObservableScrollView) view.findViewById(R.id.scrollViewTest);
scrollViewTest.setScrollViewCallbacks(this);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
context = getActivity().getBaseContext();
}
}
It wouldn't be good practise to pass a view that way. If you want to access the view in your activity from within your fragment class, use getActivity() to access the activity to which your fragment is attached, and from there you find your TextView.
TextView text = (TextView) getActivity().findViewById(R.id.tv_text);
How about adding a set function in your custom fragment
e.g.
public void setTextView(TextView tv){
this.tv = tv
}
and then calling it after
mFragment = new FragmentMarketDetail();
mFragment.setTextView(textView)
Find fragment by tag and invoke a function on it:
mFragment = (FragmentMarketDetail ) getActivity().getFragmentManager().findFragmentByTag(FragmentMarketDetail .class.getSimpleName());
mFragment.passTextView(textView);
Of course fragment must be added to backstack.

Why is ViewPager doesn't running class?

I have an activity that has a ViewPager, as following:
public class timetables extends FragmentActivity {
private ViewPager viewPager;
private RelativeLayout page1;
private RelativeLayout page2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timetables);
Intent intentTimetables = getIntent();
setTitle(Global.lineName);
viewPager = (ViewPager) findViewById(R.id.pager);
viewPager.setAdapter(new MainPageAdapter());
}
class MainPageAdapter extends PagerAdapter {
#Override
public int getCount() {
return 2;
}
#Override
public Object instantiateItem(ViewGroup collection, int position) {
View page;
switch (position) {
case 0:
if (page1 == null) {
page1 = (RelativeLayout) LayoutInflater.from(timetables.this).inflate(R.layout.activity_timetable_1, collection, false);
}
page = page1;
break;
((ViewPager) collection).addView(page, 0);
return page;
}
}
And when I run it, it loads good activity_timetable_1, but it doesn't run the code I have in that class, for example:
public class timetable_1 extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_timetable_1);
GridView gridWorking = (GridView) findViewById(R.id.gridWorking);
gridWorking.setAdapter(new textAdapter(this));
}
}
Could anyone tell me what I am doing wrong? Thank you so much
if (page1 == null) {
page1 = (RelativeLayout) LayoutInflater.from(ctx).inflate(R.layout.activity_timetable_1, collection, false);
}
Do you expect when this inflates the custom view you have will be called?
Replace it with:
if (page1 == null) {
page1 = new Activity1Form(this);
}
Activity1Form = timetable_1
Also, extends ViewGroup instead of FragmentActivity at timetable_1, with this you should modify the class, remove the onCreate and place the code inside it on the constructor.
public class Activity1Form extends ViewGroup {
public Activity1Form(Context ctx) {
LayoutInflater.from(timetables.this).inflate(R.layout.activity_timetable_1, collection, true);
GridView gridWorking = (GridView) findViewById(R.id.gridWorking);
gridWorking.setAdapter(new textAdapter(this));
}
}
You should instantiate your class instead of the layout.
EDIT:
Also, your activity_1 (awful name by the way), is a Activity and cant be placed inside another Activity or used as a Fragment, you must extende Fragment or View (or any View child such RelativeLayout/ViewGroup)

onCreateView fragment on tabhost called every time I change the tab

I am using tab host with fragment, the following is the code of the main activity
public class HomeActivity extends Activity{
private FragmentTabHost mTabHost;
private ArrayList<CustomTabIndicator> mCustomTabIndicator;
private ArrayList<BaseFragment> mTabFragments;
private class CustomTabIndicator {
private int mIdResId;
private int mTitleResId;
private int mIconResId;
public CustomTabIndicator(int idResId, int titleResId, int iconResId) {
this.mIdResId = idResId;
this.mTitleResId = titleResId;
this.mIconResId = iconResId;
}
}
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_screen);
mTabHost = (FragmentTabHost) findViewById(android.R.id.tabhost);
mTabHost.setup(this, getSupportFragmentManager(), R.id.realtabcontent);
initializeTabIndicatorsAndFragments();
addTabIndicatorsToTabHost();
}
private void initializeTabIndicatorsAndFragments() {
mCustomTabIndicator = new ArrayList<HomeActivity.CustomTabIndicator>();
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_dashboard,
R.string.tab_dashboard, R.drawable.tab_dashboard));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_feed,
R.string.tab_feed, R.drawable.tab_feed));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_lists,
R.string.tab_lists, R.drawable.tab_lists));
mCustomTabIndicator.add(new CustomTabIndicator(R.string.tab_me,
R.string.tab_me, R.drawable.tab_me));
mTabFragments = new ArrayList<BaseFragment>();
mTabFragments.add(new DashboardFragment());
mTabFragments.add(new FeedFragment());
mTabFragments.add(new ListsFragment());
mTabFragments.add(new MeFragment());
}
private void addTabIndicatorsToTabHost() {
for (int i = 0; i < mCustomTabIndicator.size(); i++) {
mTabHost.addTab(
mTabHost.newTabSpec(
getString(mCustomTabIndicator.get(i).mIdResId))
.setIndicator(
createTabView(
this,
mCustomTabIndicator.get(i).mTitleResId,
mCustomTabIndicator.get(i).mIconResId)),
mTabFragments.get(i).getClass(), null);
}
}
#SuppressLint("InflateParams")
private View createTabView(final Context context, final int textStringId,
final int imageResId) {
View view = LayoutInflater.from(context).inflate(
R.layout.fragment_tab_header_image_text_layout, null);
ImageView tabIV = (ImageView) view.findViewById(R.id.tab_icon);
tabIV.setImageResource(imageResId);
//TextView titleTV = (TextView) view.findViewById(R.id.tab_title);
//titleTV.setText(textStringId);
return view;
}
}
This is the code of one of the fragements
public class MeFragment extends BaseFragment {
private View mFragementView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);
initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();
return mFragementView;
}
}
The problem now that onCreateView called every time I change the tab, and take long time to render and draw the content of fragment.
So I have tried the following code
public class MeFragment extends BaseFragment {
private View mFragementView;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mFragementView != null) {
return mFragementView;
} else {
Log.i("AMIRA", "MeFragment - onCreateView");
mFragementView = inflater.inflate(R.layout.fragment_me_screen,container, false);
initializeUIComponents();
initializeUIComponentsData();
initializeUIComponentsTheme();
initializeUIComponentsAction();
return mFragementView;
}
}
}
and i got the following exception
java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
so can anyone help here ?
First, about the java.lang.IllegalStateException,
You haven't initialized mFragmentView.
Say something like:
mFragmentView = (View)getActivity().findViewById(R.id.mFragmentView);
or:
mFragmentView = (View)getActivity().findFragmentById(R.id.mFragmentView);
Second about the Fragment changing every time you change the tab.
Try this:
add setRetainInstance(true); to the Fragments onAttach() or onCreateView().
Open for correction, as always!
Regards,
Edward Quixote.

Event Handler inside for views inside a fragment

I am going through my first viewpager example. It consists of two fragement, one with a textview and one with an imageview. Here is the code for fragment with imageview:
public class ImageFragment extends Fragment {
private final int imageResourceId ;
public ImageFragment (int imageResourceId)
{
this.imageResourceId = imageResourceId;
}
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
Log.e("Test","Hello");
}
public void onActivityCreated(Bundle savedInstanceState)
{
super.onActivityCreated(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle
savedInstanceState)
{
View view = inflater.inflate(R.layout.image_layout, container,false);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView1);
ImageView.setImageResource(imageResourceId);
return view;
}
}
How can I implement a click handler for this imageview?
Do you mean onClickListener?
You can set:
imageView.setClickable(true);
imageView.setOnClickListener(new OnClickListener{...});
as usual, It is doesn't master whether it is in a fragment or whatever.

Categories

Resources