I'm trying to hide two TextViews on screens which have a smaller width than 500px.
I tried the following (this is not my complete code, but essential):
public class HeaderFooterFragment extends Fragment {
private TextView lable;
private TextView app;
#Override
public void onActivityCreated(Bundle savedInstanceState)
{
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
// some code...
super.onActivityCreated(savedInstanceState);
}
public void setLableInvisible()
{
lable.setVisibility(View.INVISIBLE);
app.setVisibility(View.INVISIBLE);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
}
My XML is:
<TextView
android:layout_width="wrap_content"
android:layout_height="#dimen/toolbar_height"
android:id="#+id/lable"
android:layout_weight="0.3"
android:textAlignment="gravity"
android:gravity="left"
android:text="#string/app_name"
android:textSize="#dimen/site_fontsize"
android:paddingTop="#dimen/site_paddingtop"
android:paddingLeft="#dimen/site_paddingleft"
android:textColor="#color/site_color"
android:scaleType="fitCenter"
android:visibility="visible"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="#dimen/toolbar_height"
android:layout_weight="0.05"
android:id="#+id/app"
android:textAlignment="gravity"
android:gravity="left"
android:text="#string/app_name_extra"
android:textSize="#dimen/apptitlex_fontsize"
android:paddingTop="#dimen/apptitlex_paddingtop"
android:paddingLeft="#dimen/apptitlex_paddingleft"
android:textColor="#color/colorAccent"
android:textAllCaps="true"
android:layout_alignParentRight="true"
android:scaleType="fitCenter"
android:visibility="visible"
/>
When I start the app on a Smartphone with width 480px, I get a null object reference. If i start the app on a much more bigger screen, the app isn't crashing.
Thanks in advance.
Im' not sure that the view is created yet in OnActivityCreated, Try initialize your textView in OnCreateView
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
try this and delete onactivitycreated method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
// Inflate the layout for this fragment
fragmentView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) fragmentView.findViewById(R.id.lable);
app = (TextView) fragmentView.findViewById(R.id.app);
DisplayMetrics dm = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(dm);
int screenWidth = dm.widthPixels;
if(screenWidth < 500){
setLableInvisible();
}
return fragmentView;
}
Declare TextView onCreateView instead of onActivityCreated section
View RootView; //Declare Global
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState)
{
RootView = inflater.inflate(R.layout.fragment_header_footer, container, false);
lable = (TextView) RootView.findViewById(R.id.lable);
app = (TextView) RootView.findViewById(R.id.app);
// Add Your staff here
return RootView;
}
Instead of this
setLableInvisible();
Use below code to hide textview
app.setVisibility(View.INVISIBLE)
Similarly user it for second textview.
Thanks
Related
I want to set the height of the dialog fragment by finding the height of the layout.
That's five heights in the view.
I couldn't get the height of the layout using getHeight().
I for this I used getMeasuredHeight(). (getHeight() only returned 0.)
But getMeasuredHeight() doesn't seem to return the exact value either.
I tried to set it to a height of 5 times this layout, but the actual height is
Less than 5 heights are set. (Please refer to the photo)
Why is it like this?
writing_comment_xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="#+id/comment_edit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
EDITED
DialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
OnBackPressedCallback callback;
LinearLayout commentContainer; // input layout
private final List<Comment> comments = new ArrayList<>();
private LayoutInflater layoutInflater;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem();
return view;
}
#Override
public void onViewCreated(#NonNull View view, #Nullable Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
setDialogSize();
}
private void bindViews(View v) {
commentContainer = v.findViewById(R.id.container);
layoutInflater = LayoutInflater.from(getContext());
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
private void setDialogSize() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
new Handler().post(() -> {
int size = v.getHeight() * 5;
getDialog().getWindow().setLayout(1000, size);
});
}
}
ADDED
Layout Inspector
fragment_writing_comment_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<LinearLayout
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</LinearLayout>
</androidx.constraintlayout.widget.ConstraintLayout>
I think the underlying issue is that the dialog will slip inside another ViewGroup called the decorView which has some padding that is shrinking the dialog. Here is a simplified version of your code that resizing the dialog's window appropriately so the dialog is the size you want. Comments in the code explain things a little more.
public class WritingCommentDialogFragment extends DialogFragment {
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
setWindowSize();
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
return view;
}
private void setWindowSize() {
int[] widthHeight = getDialogSize();
Window window = getDialog().getWindow();
// Our dialog will go inside the decorView which may have padding. So, the window has
// to be the width of our dialog plus the padding.
View decorView = window.getDecorView();
int horizontalPadding = decorView.getPaddingStart() + decorView.getPaddingEnd();
int verticalPadding = decorView.getPaddingTop() + decorView.getPaddingBottom();
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(window.getAttributes());
lp.width = widthHeight[0] + horizontalPadding;
lp.height = widthHeight[1] + verticalPadding;
window.setAttributes(lp);
}
private int[] getDialogSize() {
View v = LayoutInflater.from(getContext())
.inflate(R.layout.writing_comment_item,
(ConstraintLayout) getView(), // This is the parent.
false); // Don't attach the view to the parent.
// Measure one of the five child views to be added. The width is match_parent and the
// height is wrap_content.
v.measure(View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED),
View.MeasureSpec.makeMeasureSpec(0, View.MeasureSpec.UNSPECIFIED));
int height = v.getMeasuredHeight();
return new int[]{DIALOG_WIDTH, height * 5};
}
private static final int DIALOG_WIDTH = 1000; // Maybe should be dp?
}
I'm going to use dialog fragments.
There is an item called writing_comment_item.xml in the dialogfragment, and I want to set the height of 5 items.
I used the inflate() function, I used getHeight() to get the layout.
But as a result of debugging, the height is 0 no matter how much.
I tried in onCreate(). Also tried onCreateView() as well.
But the result was the same
What should I do?
writing_comment_item.xml
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/constraintLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".fragment.WritingCommentDialogFragment">
<EditText
android:id="#+id/comment_edit"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:gravity="center_vertical"
android:drawableLeft="#drawable/ic_bullet_point"
android:drawablePadding="5dp"
android:layout_marginHorizontal="10dp"
android:background="#null"
android:textSize="15sp"
android:inputType="text"
android:maxLines="1"
android:maxLength="22"
android:imeOptions="actionNext"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent">
</EditText>
</androidx.constraintlayout.widget.ConstraintLayout>
WCDialogFragment.java
public class WritingCommentDialogFragment extends DialogFragment implements CommentModel.EditInputListener {
OnBackPressedCallback callback;
LinearLayout commentContainer; // input layout
private final List<Comment> comments = new ArrayList<>();
private LayoutInflater layoutInflater;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_writing_comment_dialog, container, false);
bindViews(view);
addCommentItem();
return view;
}
private void bindViews(View v) {
commentContainer = v.findViewById(R.id.container);
layoutInflater = LayoutInflater.from(getContext());
}
#NonNull
#Override
public Dialog onCreateDialog(#Nullable Bundle savedInstanceState) {
Dialog dialog = super.onCreateDialog(savedInstanceState);
dialog.setCanceledOnTouchOutside(false);
return dialog;
}
#Override
public void onResume() {
super.onResume();
setDialogSize();
}
private void setDialogSize() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
int h = v.getHeight() * 5;
getDialog().getWindow().setLayout(1000, h);
}
}
EDITED
private void setDialogSize() {
View v = LayoutInflater.from(getContext()).inflate(R.layout.writing_comment_item, null, false);
v.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
v.getMeasuredHeight();
int h = v.getMeasuredHeight() * 5;
getDialog().getWindow().setLayout(1000, h);
}
ADDED PIC
The mistake here is trying to get the height of a view that hasn't been added to the layout hierarchy. When you call
inflate(R.layout.writing_comment_item, null, false);
with attachToRoot being false, you are specifying that you don't want it to be added to the hierarchy yet so the system never measures the view.
You could instead call View.measure(int, int) yourself and then use View.getMeasuredHeight(). This question covers that in a bit more detail - Measuring a view before rendering it
I have an xml file as shown below:
One.xml
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="#drawable/additemsbackground"
android:orientation="horizontal"
android:layout_marginRight="20dp"
android:layout_marginLeft="20dp">
<ImageView
android:id="#+id/btnaddtxt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="right|center_vertical"
android:layout_marginRight="10dp"
android:adjustViewBounds="true"
android:src="#drawable/add" />
</LinearLayout>
This is my MainFragment:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LayoutInflater layoutInflater = LayoutInflater.from(MainActivity.this);
View myView = layoutInflater.inflate(R.layout.One,
null);
ImageView btnadd=(ImageView)myView.findViewById(R.id.btnaddtxt);
btnadd.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View arg0) {
Toast.makeText(getApplicationContext(),
"Clicked Add Text Button" , Toast.LENGTH_LONG)
.show();
}
});
}
When I'm trying to get the ImageView as shown in my MainFragment it always returns null.
Can anyone say me what's the correct way of doing this ?
Initialize your views in onCreateView(...) and not in onCreate(...) when using Fragments. Something like this
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.delivery_boys, container,
false);
ImageView btnadd=(ImageView)rootView .findViewById(R.id.btnaddtxt);
return rootView;
}
you are suppose to inflate view in onCreateView in your fragment class
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view= inflater.inflate(R.layout.delivery_boys, container,
false);
ImageView img=(ImageView)view.findViewById(R.id.your_img_view);
return view;
}
onCreateView function you get attached to the activity and you get you get view inflation opportunity in onCreate Method Fragment is initialized and not yet attached to activity.
#Override
public View setContentView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (mChangeVehiclesView == null) {
mChangeVehiclesView = inflater.inflate(
R.layout.frag_vehicles_registered, null);
} else {
((ViewGroup) mChangeVehiclesView.getParent())
.removeView(mChangeVehiclesView);
}
mvehicleListView = (ListView) mChangeVehiclesView
.findViewById(R.id.frag_vehicle_registeres_list);
return mChangeVehiclesView;
}
I have a problem.
I just created an app in which I can navigate through tabs and sections.
How can i tell eclipse to load me the "main.xml" layout and not to create a new Linear layout with a text view?
Is it possible guys?
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class FirstTabFragment extends Fragment
{
private Activity activity;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
this.activity = getActivity();
LinearLayout linearLayout = new LinearLayout(activity);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
linearLayout.setLayoutParams(params);
TextView textView = new TextView(activity);
textView.setText("This is a sample fragment. I am programmatically added");
linearLayout.addView(textView);
return linearLayout;
}
}
My main.xml code is:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:background="#drawable/screenshot1" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="167dp"
android:text="Button" />
<Button
android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/button1"
android:layout_below="#+id/button1"
android:layout_marginTop="44dp"
android:text="Button" />
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
if (container == null) {
return null;
}
return (RelativeLayout) inflater.inflate(R.layout.main, container,false);
}
final LayoutInflater inflater = (LayoutInflater)getSystemService("layout_inflater");
TextView textView = (TextView)inflater.inflate(R.layout.main,null);
I want to change the width of a fragment I'm using in my MaiActivity? But I can't find its LayoutParams for changing its default width:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
int widthFfragment = 300;
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
widthTot = size.x;
heightTot = size.y;
findViewById(R.id.f1).getLayoutParams().width = widthTot-widthFfragment;
}
}
This is my fragment's code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/f1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#330000"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Fragment 1"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
Fragment1.java
public class Fragment1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
//---Inflate the layout for this fragment---
return inflater.inflate( R.layout.fragment1, container, false);
}
}
Many thanks.
You can do it by working with the LayoutParams instance of the View obtained from Fragment.getView(). My sample code:
private void resizeFragment(Fragment f, int newWidth, int newHeight) {
if (f != null) {
View view = f.getView();
RelativeLayout.LayoutParams p = new RelativeLayout.LayoutParams(newWidth, newHeight);
view.setLayoutParams(p);
view.requestLayout();
}
}
And may be used as follows:
resizeFragment(myFragment, 200, 500);
resizeFragment(otherFragment, LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Your right, the Fragment does not have LayoutParams because it is a conceptual container, you either need to adjust the layout params of the inflated view in the fragment, or to the container you attach the fragment to.
public class Fragment1 extends Fragment{
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
ViewGroup view = (ViewGroup) inflater.inflate( R.layout.fragment1, container, false);
// TODO Adjust layout params of inflated view here
return view;
}
The example assumes you are inflating a ViewGroup.
Hope that helps.