My Code:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Inflate the layout for this fragment
Uri uri = Uri.parse("Home");
if (mListener != null) {
mListener.onFragmentInteraction(uri);
}
View rootView = inflater.inflate(R.layout.fragment_main, container, false);
LinearLayout linearLayout = (LinearLayout) rootView.findViewById(R.id.mesage_linear1);
final TextView message = new TextView(getActivity());
number_view.setText("Long Text");
int m_width = message.getMeasuredWidth();
int m_height = message.getMeasuredHeight();
Toast.makeText(getActivity(),"Height : "+m_height+"\nWidth : "+m_width,Toast.LENGTH_LONG).show();
return rootView;
}
I want to get the height and width of the programmatically created TextView.
Output is Height : 0 and Width : 0
At that point the View hasn't been laid out yet. You have to manually measure it (via View.measure()) or to wait until it is laid out:
textView.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
textView.getViewTreeObserver().removeOnGlobalLayoutListener(this);
int height = textView.getHeight();
int width = textView.getWidth();
}
});
TextView tv=(TextView)findViewById(R.id.text);
tv.setText("Android");
tv.measure(0, 0);
tv.getMeasuredWidth();
tv.getMeasuredHeight();
tv.getTextSize();
Related
I'm trying to add editText and textView to a fragment. The code to generate the editText and TExtView is correct because I've used it before, but in this case nothing appears. Any ideas?
Fragment code:
public class MarcadoresFragment extends Fragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// TODO Auto-generated method stub
View myFragmentView = inflater.inflate(R.layout.fragment_marcadores, container, false);
return myFragmentView;
}
}
Add the editText:
public void createEditText(int size){
Log.d("det", String.valueOf(size));
LayoutInflater inflater = this.getLayoutInflater();
View dialogView = inflater.inflate(R.layout.fragment_marcadores, null);
LinearLayout layout = (LinearLayout) dialogView.findViewById(R.id.marcadoresFrag); // this is whatever view you want to add them to
EditText editText;
TextView textView;
if(layout!=null)
for(int i=0;i<size;i++){
textView = new TextView(dialogView.getContext());
textView.setText("Descrição do marcador " + String.valueOf(i + 1));
textView.layout(0, 5, 0, 5);
// add to the view
layout.addView(textView);
editText = new EditText(dialogView.getContext());
editText.setId(i);
editText.setHint("Ex: Bebedouro ndsfhjdsjvdsbjvjhvjdfjbvfjfvjklbjklfv" + i + 1);
Log.d("det", "jsdgbiusdbgksjd");
allEds.add(editText);
// add to the view
layout.addView(editText);
}
}
I'm using V4.Fragments. So from activity i'm creating new instance of my fragment and after that i begin transaction.
The main problem,that my imageview control is defined(in axml) as Layout_width="match_parent"
and weightSum=1 (for height).
For some reason i need to know height/width of my imageView.
What i tried :
was created class that implements IOnGlobalLayoutListener
class GlobalLayoutListener : Java.Lang.Object, ViewTreeObserver.IOnGlobalLayoutListener
{
System.Action _OnGlobalLayout;
public GlobalLayoutListener (System.Action onGlobalLayout)
{
this._OnGlobalLayout = onGlobalLayout;
}
public void OnGlobalLayout ()
{
_OnGlobalLayout ();
}
}
After that,in my V4.Fragment class,i'm doing this:
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
Action CallBack = () =>
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
//new instance of class,that implements IOnGlobalLayoutListener
GlobalLayoutListener Global = new GlobalLayoutListener(CallBack);
imgView.ViewTreeObserver.AddOnGlobalLayoutListener(Global);
retun _View;
}
Trick doesnt worked. Always return zero(height/width).
Second variant,i implemented interface of IOnGlobalLayoutListener directly in MyFragment
public class MyCustomFragment : Android.Support.V4.App.Fragment,View.IOnTouchListener,ViewTreeObserver.IOnGlobalLayoutListener
{
int ImgHeight;
int ImgWidth;
ImageView imgView;
Bundle Arguments;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
ViewTreeObserver Vto = imgView.ViewTreeObserver; //also tried with _View.ViewTreeObserver; result same
Vto.AddOnGlobalLayoutListener(this);
retun _View;
}
Same problem,doesnt worked.
And my last variant is like :
public class MyCustomFragment : Android.Support.V4.App.Fragment
{
int ImgHeight;
int ImgWidth;
ImageView imgView;
Bundle Arguments;
public override View OnCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
Arguments = new Bundle();
View _View = inflater.Inflate(Resource.Layout.MyFragmentLayout, container, false);
imgView = _View.FindViewById<ImageView>(Resource.Id.imgView);
imgView.ViewTreeObserver.GlobalLayout += (sender, e) => //also tried with _View
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
retun _View;
}
Again no luck,what i'm doing wrong? Thanks.
PS Sorry for my eng.
All works fine,i was doing something wrong.
So this code works great :
imgView.ViewTreeObserver.GlobalLayout += (sender, e) =>
{
ImgHeight = imgView.Height;
ImgWidth = imgView.Width;
};
Enjoy!
I have this piece of code:
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
tableView = inflater.inflate(R.layout.fragment_table, container, false);
View layout = inflater.inflate(R.layout.fragment_1, container, false);
if (roomArray == null || roomArray.isEmpty()) {
roomArray = getTablesList(new VolleyCallback() {
#Override
public void onSuccess(ArrayList<RoomsClass> result) {
Toast.makeText(getActivity(), "onSuccess", Toast.LENGTH_LONG).show();
}
});
} else {
ArrayList<View> tableList = new ArrayList<>();
ArrayList<Tables> tables;
Toast.makeText(getActivity(), "IsNotNull", Toast.LENGTH_LONG).show();
tables = roomArray.get(0).getTables();
GridLayout gridLayout = (GridLayout) layout.findViewById(R.id.grid_layout);
for (int i = 0; i < 1; i++) {
tableView = inflater.inflate(R.layout.fragment_table, container, false);
TextView textView = (TextView) tableView.findViewById(R.id.table_number);
textView.setText(tables.get(i).getTableNumber());
GridLayout.Spec row = GridLayout.spec(i / 5);
GridLayout.Spec col = GridLayout.spec(i);
GridLayout.LayoutParams params = new GridLayout.LayoutParams(row, col);
gridLayout.addView(tableList.get(i), i, params);
}
}
return layout;
}
so I have onSuccess, but I cant add views to my fragment. I need to somehow call all methods after onSuccess, but its inner class so I can't handle anything there. So what is the right way of doing this?
Are you inflating two fragments in createView?
tableView = inflater.inflate(R.layout.fragment_table, container, false);
View layout = inflater.inflate(R.layout.fragment_1, container, false);
have u try to use handler to solve this problem , the use of handler can avoid do something in a inner class.
Handler myHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case a:
//do somthing
break;
}
super.handleMessage(msg);
}
};
This is the code:
public class HelpDetailsFragment extends Fragment
{
private static final String TAG = "MeMoGame";
public static HelpDetailsFragment newInstance(int index)
{
HelpDetailsFragment detailFragment = new HelpDetailsFragment();
Bundle bundleArgs = new Bundle();
bundleArgs.putInt("index", index);
detailFragment.setArguments(bundleArgs);
return detailFragment;
} // newInstance()
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
if(container == null)
{
Log.i(TAG, "Different layouts and in one this fragment's containing frame does not exist.");
return null;
} else {
// I checked that container is NOT null
Log.i(TAG, "This is the parent view that the fragment's UI should be attached to.");
}
View mView = new View(getActivity());
container.addView(mView);
return container;
}
This error message appears:
AndroidRuntime(785): Caused by: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Can / will someone please explain what I'm doing wrong?
When I do:
View mView = new View(getActivity());
TextView text = (TextView) new TextView()
mView.addView(text);
return mView;
I get the same error message.
My salvation lies inhere:
#Override
public View onCreateView(LayoutInflater inflater,
ViewGroup container,
Bundle savedInstanceState)
{
Context context = getActivity();
FrameLayout frameLayout = new FrameLayout(context);
int height = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 50, getActivity()
.getResources().getDisplayMetrics());
int padding = (int) TypedValue.applyDimension(
TypedValue.COMPLEX_UNIT_DIP, 10, context
.getResources().getDisplayMetrics());
// set the header
TextView headText = new TextView(context);
headText.setHeight(height);
headText.setPadding(padding, 0, 0, 0);
headText.setTextAppearance(context, R.style.details_Header);
headText.setText(HelpScreenData.HELP_HEADERS[getCurrentIndex()]);
frameLayout.addView(headText);
ScrollView scroller = new ScrollView(context);
// set detail text
TextView detailText = new TextView(context);
detailText.setPadding(padding, padding + height, padding, padding);
detailText.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 18);
detailText.setText(HelpScreenData.HELP_DETAILS[getCurrentIndex()]);
scroller.addView(detailText);
frameLayout.addView(scroller);
return frameLayout;
} // onCreateView()
An explanation would still be very welcome!
The issue here is you are returning the container-- return container; you should be returning mView in your case, the container is the actual container for the fragment in your activity layout
My note application has 2 fragments on a screen: A list of notes fragment and a note detail fragment to display the selected note in the second fragment, it has an input text field (android:inputType="textMultiLine"). As I want to handle event keyboard hidden so I can save the change user has made when they close the keyboard. Can anyone give me a clue to do this task?
I think that the best and universal solution for keyboard issue, is measure the VisibleDisplayFrame on layout change:
Rect visibleRect = new Rect();
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup mMain = (ViewGroup) inflater.inflate(R.layout.select_cover_album, container, false);
mMain.getWindowVisibleDisplayFrame(visibleRect);
mMain.addOnLayoutChangeListener(new View.OnLayoutChangeListener() {
#Override
public void onLayoutChange(View v,
int left,
int top,
int right,
int bottom,
int oldLeft,
int oldTop,
int oldRight,
int oldBottom) {
Rect r = new Rect();
mMain.getWindowVisibleDisplayFrame(r);
if(r.height() < visibleRect.height()){
//keyboard shown
}
if(r.height() == visibleRect.height()){
//keyboard hidden
}
}
});
return mMain;
}
Here's how i handle it on my Note Detail fragment:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
this.view = inflater.inflate(R.layout.fragment_note_detail_of_style_searching, container , false);
listviewFilter = (ListView) view.findViewById(R.id.listview_filter);
noteDetailView = view.findViewById(R.id.note_detail_of_style_view);
photoDetailView = view.findViewById(R.id.gv_note_detail_photo);
view.getViewTreeObserver().addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
int heightDiff = view.getRootView().getHeight() - view.getHeight();
if (heightDiff > 200) {
isWatchingKeyboard = true;
photoDetailView.setVisibility(View.GONE);
return; //must exit now
}
if(isWatchingKeyboard){
isWatchingKeyboard = false;
viewForFocus.requestFocus();
photoDetailView.setVisibility(View.VISIBLE);
}
}
});
return this.view;
}
I did it this way:
private int prevBottom;
#Override
public View onCreateView(LayoutInflater inflater, final ViewGroup container, Bundle savedInstanceState) {
view = inflater.inflate(R.layout.fragment_status, container, false);
prevBottom = view.getBottom();
view.getViewTreeObserver().addOnGlobalLayoutListener(new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
if (view.getBottom() > prevBottom) {
view.requestFocus();
}
prevBottom = view.getBottom();
}
});
return view;
}
and added this to the view I was inflating:
android:focusableInTouchMode="true"
and this to the activity in the AndroidManifest.xml
android:windowSoftInputMode="adjustResize"