I have a some fragment in my PageViewer.
In the main fragment, I would like to show a component ( TextView or imageView) if there is no connection.
In the code below, I can reach my textview, but I cannot get them disapperead.
public class MainFragment extends Fragment {
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View layout = inflater.inflate(R.layout.main, container, false);
// RelativeLayout mainLayout = (RelativeLayout)findViewById(R.layout.main);
TextView TxtIsNotConnected = (TextView) layout.findViewById(R.id.isNotConnected);
TextView TxtIsConnected = (TextView) layout.findViewById(R.id.isConnected);
// String text = TxtIsNotConnected.getText().toString(); // This is a test which works, return the text o my textview.
boolean isConnected = ConnectivityUtils.isConnected(getActivity()); // This Works fine
if (!isConnected) TxtIsNotConnected.setVisibility(View.VISIBLE); // NOT WORKING
else TxtIsConnected.setVisibility(View.VISIBLE); // NOT WORKING
return inflater.inflate(R.layout.main, container, false);
}
How should I do???
To make your TextView visible
yourTextView.setVisibility(View.VISIBLE);
To make Invisible
yourTextView.setVisibility(View.GONE);
From your code it seems that you have to keep one view visible and another invisible. So, please try this
if (!isConnected) {
TxtIsNotConnected.setVisibility(View.VISIBLE);
TxtIsConnected.setVisibility(View.GONE);
} else {
TxtIsConnected.setVisibility(View.VISIBLE);
TxtIsNotConnected.setVisibility(View.GONE);
}
Hope it helps...
Edit :
Well, your code is correct about visibility but you have made a silly mistake that's why it seems that your code is not working. Look at the first line and last line of your onCreateView method. You have inflated your R.layout.main in View object called "layout". You have set your actions within that layout. Finally you have returned a new instance of that view. So, your previous codes became useless. So, your return statement will be...
return layout ;
It should fix the problem.
Related
I have a problem, whenever i try to add an eventhandler to a button i get a null reference exception, i am trying to create a popup window with a DialogFragment, where inside it im calling the view PopUpWindow wich will show up on screen, but when i try to access the buttons by id and to assign them eventhandlers for example:
Button btnCopyText = dp.view.FindViewById<Button>(Resource.Id.btnCopyText);
btnCopyText.Click += BtnCopyText_Click;
then i get a null reference exception, can anyone help me, below is the necessary code.
class dialog_Popup:DialogFragment
{
public View view;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
base.OnCreateView(inflater, container, savedInstanceState);
view = inflater.Inflate(Resource.Layout.PopupWindow, container, false);
return view;
}
public override void OnActivityCreated(Bundle savedInstanceState)
{
Dialog.Window.RequestFeature(WindowFeatures.NoTitle);
base.OnActivityCreated(savedInstanceState);
}
public class MainActivity : Activity
{
protected override void OnCreate(Bundle bundle)
{
//some code
}
public string itemclicked;
dialog_Popup dp;
private void Lv_ItemLongClick(object sender, AdapterView.ItemLongClickEventArgs e)
{
//View popUpView = LayoutInflater.Inflate(Resource.Layout.PopupWindow,
//null); // inflating popup layout
Button height = FindViewById<Button>(Resource.Id.btnCopyText);
//Then: change the width of the button
FragmentTransaction transaction = FragmentManager.BeginTransaction();
dp = new dialog_Popup();
dp.Show(transaction,"Popup");
itemclicked = lv.GetItemAtPosition(e.Position).ToString();
Button btnCopyText = dp.view.FindViewById<Button>(Resource.Id.btnCopyText);
btnCopyText.Click += BtnCopyText_Click;
Button btnSaveCurrentAya = dp.view.FindViewById<Button>(Resource.Id.btnSaveCurrentAya);
btnSaveCurrentAya.Click += BtnSaveCurrentAya_Click;
Button btnsavingsAya = dp.view.FindViewById<Button>(Resource.Id.savingsAya);
btnsavingsAya.Click += BtnsavingsAya_Click;*
Button btnShareFB = dp.view.FindViewById<Button>(Resource.Id.fbShare);
btnShareFB.Click += BtnShareFB_Click;
}
}
There are several reasons why a NullReferenceException can occur with FindViewById:
The layout does not contain the id -> check that the correct layout and id is inflated/ referenced
The type like Button is incorrect
In your case, check that dp and dp.view is not null.
One thing to mention here is, that it is not the best implementation to reference the control of a fragment in you main view. A fragment is something that should be able to life on her own. So I see two ways of implementing your desired behavior:
1) The fragment gets an event and you listen to that. This means your main view will contain the logic to save something.
2) The logic moves into the fragment.
i have try a lot of example or tutorial on tablayout, all work fine
(https://www.simplifiedcoding.net/android-tablayout-example-using-viewpager-fragments/, http://www.androidbegin.com/tutorial/implementing-fragment-tabs-in-android/)
but now i encounter 1 problem, which is i dont know how to set/change the TextView/ImageView while onCreate/onLoad
so far, what i able to do is, write all my code under onTabSelected, but this is not my solution because i cant show empty or static values on the 1st tab, then have to wait until click or slide again then only load the real data.
i'm sure it have a way or solution for my problem, can anyone share it to me (code/website)
or in order to have this Tab feature with viewpager contain recyclerview, Tablayout is not the right way to code, perhaps it have another more correct way to code.
Do these changes inside of onCreateView of fragment that you want
Example:
//Our class extending fragment
public class Tab1 extends Fragment {
//Overriden method onCreateView
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//Change R.layout.tab1 in you classes
View view = inflater.inflate(R.layout.tab1, container, false);
TextView textView = (TextView) view.findViewById(R.id.you_textview_id);
textView .setText("Your text to show");
//Returning the layout file after inflating
return view;
}
}
The TextView need to be previous declared in tab.xml that you're inflating, in our example, tab1.
I am new in Android programming.
I created the main Activity of my app style google shop ussing ActionBarSherlock and a NavigationTabs, with fragments, each referencing another activity (Fragment 1 Fragment 2, etc) and each fragment inflating a layout.
However, I'm used to create layouts in xml and then customize them in java. To put a different text depending on the time of day, or according to some data in a database, giving function to buttons, etc.. But in a Fragment Class, I can not even use setContentView to work with each text or button, and set the context for using my database is giving me problems.
How I can customize a xml layout in a fragment?
Or what would be the right thing to do?
Here my Fragment:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
return inflater.inflate(R.layout.menu, container, false);
}
This is more simple then you think. onCreateView instanciate au returns the view for your Fragment. As you said, in a simple Activity you set (and instanciate) the view with setContentView() and then you get your Views with findViewById().
findViewById() asks for the view to return the view item that you want, you can call it from your view before returning it. Like this:
public class Fragment1 extends SherlockFragment{
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState){
View v = inflater.inflate(R.layout.menu, container, false);
// For example, getting a TextView
TextView tv = (TextView) v.findViewById(R.id.myTextView);
// do your job
return v;
}
so far so good, you just need to use the view you are inflating to get everything.
here is an example
View v = inflater.inflate(R.layout.menu, container, false);
Button b = (Button)v.findViewById(r.id.button1);
return v;
inside onActivityCreated you could use:
View mView = getView();
TextView textView = (TextView) view.findViewById(R.id.theIdOfTextView);
where theIdOfTextView is declared inside R.layout.menu.
getView() returns the View you inflated inside onCreateView. You use it only after onCreateView has been executed
When a layout , which is defined for a Fragment and used inside setContentView() method and it has no relation with Activity .
then why we need Activity Reference to access it .
so I want to create a ListView , inside a Fragment . than i have to create it inside Activity layout or inside Fragment layout .
Hey Neil , i have a question because every time I have made a mistake because , i am performing this
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
inside oncreateView() so it is creating problem but when ever i am implementing this inside
onActivityCreated() , it is working , so can you please tell me Why this is happening .
if you want to find the view by getActivity(), you should do that after onCreateView is called, because till onCreateView is called the view of fragment is null. So you can do:
#Override
public void onResume() {
super.onResume();
TextView txt = ( TextView ) getActivity().findViewById(R.id.txt);
txt.setText("nayak nahi , khalnayak hun mein");
}
You can define a ListView in a layout for your Fragment. Try something like this in your fragment to define a alternative layout
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
_baseView = inflater.inflate(R.layout.yourlayout, null); //where base view is your upper most parent in your layout
return _baseView;
}
Then in your onActivityCreated you can use _baseView.findViewById('yourListView') to set your ListView. I don't know if this is best practice but it works perfectly for me.
In Fragment you should use its View but getActivity:
public View onCreateView(…){
View rootView = inflater.inflate(R.layout.listview_xml_file, container, false);
ListView ListNewsBelarus = (ListView) rootView.findViewById(R.id.listViewId);
return rootView;
}
I have a Fragment which contain a TextView call as mTextView. I want to get height of mTextView after append content to it in onCreateView of Fragment like this:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
final View view = inflater.inflate(
R.layout.myfragment, container, false);
mTextView = (TextView) view.findViewById(R.id.myTextView);
int mHeight=mTextView.getHeight();
}
but it's always return 0 value.
Do you try to get it after
setContentView(R.layout.your_layout);
right?
If you try to get the height just after onCreate, you won't be able to get because still the window is not focused. Try like this :
#Override
public void onWindowFocusChanged(boolean hasFocus) {
// your code here
int mHeight=mTextView.getHeight();
}
The reason the View's dimentions are 0 is because when you are querying them, the view still haven't performed the layout and measure steps. You only told the view how it would "behave" in the layout, but it still didn't calculated where to put each view.
There are a few tricks to get that size though. You can use this question as a starting point although I don't like the accepted answer's approach very much.
How to get the width and height of an android.widget.ImageView?