Cannot getHeight of TextView when Fragment onCreateView - android

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?

Related

getting a view using getResources()

I want to set text of a TextView that is inside a layout file,
I tried setContentView() but it isnt working since i am using fragments.
I tried using getResources().getLayout(R.layout.abc);
It returns null
I tried setContentView() but it isnt working since i am using
fragments
That's wrong. You have to override onCreateView and inflate and return the layout you want to show, and you can use onCreateView, and use its first parameter, View view, to call findViewById and access the widgets in your layout. You can read more here and here
inside fragment you can set view inside function onCreateView(), use below code
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle b) {
View view = (LinearLayout) inflater.inflate(R.layout.my_layout, container, false);
return view;
}
here my_layout should be the name of layout file,
now you can get view of it inside function onviewcreated()
public void onViewCreated(View view, Bundle savedInstanceState){
// here you can get your textview and set its value
}
thumbs up, if you find my answer correct
You can inflate your layout like this:
ViewGroup group = LayouInflate.from(context).inflate(R.layout.abc,null);
TextView tv = group.findViewById(R.id.xxx);
you have to inflate the layout through the onCreateView method and then return the View.
#Override
public View onCreateView(LayoutInflater inflater,ViewGroup container, Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.your_layout, container, false);
TextView yourTextView = (TextView)rootView.findViewById(R.id.yourTextViewId);
return rootView;
}

Change a TextView Visibility in Fragment

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.

Android: preserving activity/fragment state

If I want to keep track of an int value in an activity/fragment, is this approach incorrect:
In layout XML, have:
<TextView
android:id="#+id/int_id"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone" />
On fragment's onCreateView or activity's onCreate, have the following code:
TextView intId = (TextView)getView().findViewById(R.id.int_id);
intId.setText(String.valueOf(<integer_value_to_keep_track_of>));
Then, whenever I need to use the int value later in the code, access it by doing the following:
int accessId = Integer.valueOf(((TextView)getView().findViewById(R.id.detail_column_id)).getText().toString());
Something tells me this is not the best way to preserve state. Would declaring a class member (e.g. private int accessId) and assigning that be better? Thanks!
Usually, if you are inside onCreateView() you get a reference of the Views that are part of the Fragment's View hierarchy only once when you inflate the layout. When you call infalter.inflate() a View instance is returned; the parent of the hierarchy. You can use that View's findViewById() to get a reference of the TextView.
For example:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle SavedInstanceState) { // TODO Auto-generated method stub
View view = (View) inflater.inflate(R.layout.me_layout, container,false);
TextView intId = (TextView)view.findViewById(R.id.int_id);
return view;
}
Then once you get the TextView reference you can simply do
String text = intId.getText().toString();
inside onCreateView();

android ViewPager customizable

I need to implement a scroll view as shown:
That is, in "idle" state image "1" is visible in full size and image "2" is visible partially (thus giving a clue to the user that he can scroll the content). After scrolling scrool view must not stay in intermediate state and scrolling must be completed (like iOS's Scroll View does when "Paging Enabled" is turned on):
I refused to use HorizontalScrollView, because it has nothing similar to "Paging Enabled" property.
After googling, I came across android.support.v4.view.ViewPager. It's scrolling behavior is perfectly what I want, but I have no good idea how to support "partially visible" next image in ViewPager? Technically, what should I return in the
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)?
For the present, my code is
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
View view = inflater.inflate(R.layout.stage_select_image_layout, container, false);
ImageView imageView = (ImageView) view.findViewById(R.id.stage_select_image_layout_image);
imageView.setImageResource(m_imageResourceId);
return view;
}
But it results in "exactly one image per page" behavior, not what I want (see the very first figure).
This does the trick:
ViewPager pager = ...;
pager.setOffscreenPageLimit(2);
pager.setPageMargin(-200);
The easiest thing to do is, is in your PagerAdapter, implement an override for getPageWidth. The return value is a percent that the view takes up of the total space.
#Override
public float getPageWidth(int position) {
return 0.75f;
}
Use the widthFactor attribute of the ViewPager's LayoutParams. It should scale it so you can see a little of the next page.

Replace a view with other view By code

I want to replace a view with other other by code
For example i need to change a imageview to progressBar by code.
public void changeToProgressBar(ImageView imageview,Context context){
ProgressBar one = new ProgressBar(context);
one.setLayoutParams(imageview.getLayoutParams());
imageview.setVisibility(View.GONE);
one.setVisibility(View.VISIBLE);
//NOW I NEED TO PLACE 'one' EXACTLY THE PLACE WHERE 'imageview' WAS THERE
}
I need to use this many place. I know to set by sending the parent viewgroup.
Is there anyway to get the parent viewgroup from imageview
Thanks
This is trivial to do and other posters have linked to or hinted at the solution. You can get the parent of a View by View.getParent(). You need to cast the returned value to a ViewGroup (q: can a View's parent be anything else than a ViewGroup?). Once you have the parent as a ViewGroup you can do ViewGroup.removeChild(viewToRemove) to remove the old view and add the new one using ViewGroup.addChild(viewToAdd).
You might also want to add the new view at the same index as the remove view to make sure that you don't put the new view on top of or below other views. Here's a complete utility class for you to use:
import android.view.View;
import android.view.ViewGroup;
public class ViewGroupUtils {
public static ViewGroup getParent(View view) {
return (ViewGroup)view.getParent();
}
public static void removeView(View view) {
ViewGroup parent = getParent(view);
if(parent != null) {
parent.removeView(view);
}
}
public static void replaceView(View currentView, View newView) {
ViewGroup parent = getParent(currentView);
if(parent == null) {
return;
}
final int index = parent.indexOfChild(currentView);
removeView(currentView);
removeView(newView);
parent.addView(newView, index);
}
}
Something to consider is that you'll lose any positioning in a relative layout when you replace one view with another. One solution to this would be to make sure that the view you want to replace is wrapped in a another view and that wrapped container view is the one that is positioned in a relative layout.
Retrieve the view you would like to change by calling findViewById() from the activity level http://developer.android.com/reference/android/app/Activity.html#findViewById(int) Then find the sub view you would like to change http://developer.android.com/reference/android/view/View.html#findViewById(int)
Then use the functions from http://developer.android.com/reference/android/view/ViewGroup.html to manipulate the view.
Just as User333 described that could be one solution..
It's also possible to delete the imageview by calling yourview.removeView(imageview) and then create your progress bar and put that inside the view instead by yourview.addView(progressbar)
You can change Android Activities view from any other simple java class or in other activity.
you only need to pass current view and get your element by this view you want to change As :
public class MainActivity extends Activity{
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Setting UI
View currentView = getWindow().getDecorView().getRootView();
//showHeaderLayout is simple java class or it can be any activity
changeLayout.setView(currentView);
}
Class : changeLayout
public class changeLayout{
public static View setView(final Activity activity, final View myView)
{
// myView helps to get Activity view , which we want to change.
TextView tv = (TextView) myView.findViewById(R.id.tv);
tv.setText("changs Text Via other java class !!");
}
}
Note : Passing view makes you able to change any activity view outside an Activity.
I think following ould be the best approach as in this we don't need to set layout params.
setvisibility(Visibility.GONE)
setvisibility(Visibility.VISIBLE)
I am sure that following link not only help you, but even shows you the direction for your requirement. https://stackoverflow.com/a/3760027/3133932
For this, take both imageview and progressBar and set the visibility according to your requirements.
For example
If you want progressBar to be visible, put setvisibility(Visibility.GONE) for imageview and put setvisibility(Visibility.VISIBLE) for progressBar

Categories

Resources