I am trying to resize the inflated view but not working
I am adding this view to AlertDialog
LayoutInflater inflater = getActivity().getLayoutInflater();
View view = inflater.inflate(R.layout.popup_util, null,false);
view.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, 200)); //Set params here
......
dialog.setView(view);
popup_util
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/setting_password_popup_element"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/txtPopUpTitle"
android:padding="10dp"
android:layout_centerHorizontal="true"
android:textColor="#000000" android:textSize="24dp" android:background="#color/colorAccent"/>
<TextView android:padding="10dp" android:layout_width="match_parent" android:layout_below="#+id/txtPopUpTitle" android:id="#+id/txtPopUpMessage" android:layout_height="wrap_content" android:textSize="22dp"/>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:gravity="center_horizontal|bottom">
<Button android:id="#+id/btnPopUpCancel"
android:layout_width="match_parent" android:layout_height="50dp"
android:layout_weight="1"
android:textSize="24dp"
android:visibility="gone"
android:layout_centerHorizontal="true"
android:background="#android:color/transparent"
android:text="#string/btn_cancel_text" />
<Button android:id="#+id/btnPopUpOk"
android:layout_width="match_parent" android:layout_height="50dp"
android:textColor="#color/colorAccent"
android:textSize="24dp"
android:layout_centerHorizontal="true"
android:text="#string/btn_ok_text"
android:background="#android:color/transparent"
android:layout_weight="1"/>
</LinearLayout>
</RelativeLayout>
But it is not working.. What i m missing ?
u cane try this code:
View view = inflater.inflate(R.layout.active_slide, this);
view.setMinimumWidth(200);
You can use this code its works good in my case
this.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, theSizeIWant));
NOTE: Be sure to use the parent Layout's LayoutParams. Mine is LinearLayout.LayoutParams!
You can do that by setting the width and height to the window of the dialog itself.
alertDialog.getWindow().setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, 200);
You must do that after showing the dialog using dialog.show()
Related
The main problem here is that the parent layout is nested scroll view so the height must be wrap content
so the view is like first image
what I want is to make the "don't have an account? register" TextView
to be at the bottom of the screen so I calculate the height of my
phone then the height of layout to subtract to subtract them and
assign the result to the top margin of textview
and here's my code
ViewTreeObserver observer = binding.parentConstraint.getViewTreeObserver();
if (observer.isAlive()) {
observer.addOnGlobalLayoutListener(() -> {
DisplayMetrics displayMetrics = getContext().getResources().getDisplayMetrics();
float parentViewHeightInDP = (int) (binding.parentConstraint.getHeight() / displayMetrics.density);
if (getContext() != null) {
float heightInDP = displayMetrics.heightPixels / displayMetrics.density;
float distanceBetweenLayoutBottomAndParenBottom = heightInDP - parentViewHeightInDP;
if (distanceBetweenLayoutBottomAndParenBottom > 32) {
if (topMargin == 0) {
topMargin = 1;
ConstraintLayout.LayoutParams layoutParams = (ConstraintLayout.LayoutParams) binding.loginTextDontHaveAccount.getLayoutParams();
layoutParams.topMargin = Math.round(distanceBetweenLayoutBottomAndParenBottom);
Handler handler = new Handler();
handler.postDelayed(() -> {
binding.loginTextDontHaveAccount.requestLayout(); },
50);
}
}
but that the result I got
so my real question here is why there still some space here, I mean
the TextView should be exactly at the bottom
What I would suggest you to make NestedScrollView as a child instead of parent.
Make RelativeLayout as your parent and use layout_alignParentBottom to set TextView at bottom. Something like this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="?android:colorBackground"
android:clickable="true"
android:fillViewport="true"
android:focusable="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/img1"
android:layout_width="wrap_content"
android:layout_height="650dp"
android:background="#mipmap/ic_launcher" />
<ImageView
android:layout_width="match_parent"
android:layout_height="650dp"
android:layout_below="#+id/img1"
android:background="#color/colorPrimary" />
</LinearLayout>
</RelativeLayout>
</androidx.core.widget.NestedScrollView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
Try this:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="vertical">
<AutoCompleteTextView
android:id="#+id/emailtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Email"
android:inputType="textEmailAddress"
android:textColorHint="#80000000" />
<EditText
android:id="#+id/passwordtext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical"
android:hint="Password"
android:inputType="textPassword"
android:textColorHint="#80000000" />
<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center">
<Button
android:id="#+id/enter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Enter"
android:textAllCaps="false"
app:layout_constraintBottom_toTopOf="#id/textView"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="This is my app"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#id/enter" />
</androidx.constraintlayout.widget.ConstraintLayout>
</LinearLayout>
</ScrollView>
</androidx.constraintlayout.widget.ConstraintLayout>
You can use constraint layout. Pay attention to layout_constraintBottom_toTopOf button and layout_constraintTop_toBottomOf textview.
Is there a way I can position generated text fields to my layout programmatically. Here is what the generated text field currently looks like:
Here is the code I have for generating these two fields:
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.floatingButton);
fab.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v)
{
LinearLayout linearLayout = new LinearLayout(AddSongActivity.this);
EditText artist = new EditText(AddSongActivity.this);
EditText songTitle = new EditText(AddSongActivity.this);
artist.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
songTitle.setLayoutParams(new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT));
linearLayout.addView(artist);
linearLayout.addView(songTitle);
((RelativeLayout)findViewById(R.id.add_song_layout)).addView(linearLayout);
}
});
Here is the add song layout that goes along with the above code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/add_song_layout"
tools:context="com.example.android.playmymusicapp3.AddSongActivity">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
</ScrollView>
<include
layout="#layout/app_bar_main"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<com.github.clans.fab.FloatingActionButton
android:id="#+id/floatingButton"
android:src="#mipmap/plus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:onClick ="onClick"
android:layout_marginBottom="16dp"
android:layout_marginRight="16dp"
app:backgroundTint="#color/colorPrimary">
</com.github.clans.fab.FloatingActionButton>
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="82dp"
android:text="Add songs to your event..."
android:textSize="20sp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_marginTop="37dp"
android:layout_below="#+id/textView3"
android:layout_alignParentStart="true">
<EditText
android:id="#+id/editText8"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:ems="10"
android:hint="Artist"
android:inputType="textPersonName" />
<EditText
android:id="#+id/editText7"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:ems="10"
android:hint="Title"
android:inputType="textPersonName" />
</LinearLayout>
</RelativeLayout>
Is there a way where I can tell Android to take those two generated text fields and place them exactly like the ones I have that are centered?
You should try new Constraint Layout. You can really good position your elements.
ConstraintLayout Tutorial
https://developer.android.com/training/constraint-layout/index.html
https://developer.android.com/reference/android/support/constraint/ConstraintSet.html
Example:
The following code configures a constraint set in which the left-hand
side of a Button view is connected to the right-hand side of an
EditText view with a margin of 70dp:
set.connect(button1.getId(), ConstraintSet.LEFT,
editText1.getId(), ConstraintSet.RIGHT, 70);
You can do the same for TOP and BOTTOM values.
Why Snackbar cover my view
here is my xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/fullview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/dark_gray"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<TextView
android:id="#+id/reload"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:layout_centerInParent="true"
android:text="#string/reload"
android:visibility="visible" />
<ProgressBar
android:id="#+id/progressbar"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:gravity="center"
android:visibility="gone" />
<android.support.v7.widget.RecyclerView
android:id="#+id/complist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:fadeScrollbars="false" />
</RelativeLayout>
<LinearLayout
android:id="#+id/buttons"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_margin="5dp"
android:orientation="horizontal">
<Button
android:id="#+id/send"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/blue"
android:text="#string/send"
android:textColor="#android:color/white" />
<Button
android:id="#+id/cancel"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:layout_weight="1"
android:background="#color/green"
android:text="#string/cancel"
android:textColor="#android:color/white" />
</LinearLayout>
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
java code used to show snake bar
private CoordinatorLayout _CoordinatorLayout;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.compition, null);
_CoordinatorLayout = (CoordinatorLayout) v.findViewById(R.id.fullview);
}
Snackbar.make(_CoordinatorLayout , message , Snackbar.LENGTH_LONG).show();
here is screen shot
what I miss ??
The documentation here says:
Snackbars appear above all other elements on screen and only one can
be displayed at a time.
So, Snackbar will cover your view, just like a Toast.
Hey guys I am trying to display a custom message inside of my app but I keep getting this error while setting the padding to the layout programaticaly:
Fatal Exception:java.lang.ClassCastExceptionandroid.widget.RelativeLayout$LayoutParams cannot be cast to android.widget.FrameLayout$LayoutParams
Here is how I try to set the padding:
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
if(alignTop){
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
int actionBarHeight = 0;
TypedValue tv = new TypedValue();
if (getContext().getTheme().resolveAttribute(android.R.attr.actionBarSize, tv, true))
{
actionBarHeight = TypedValue.complexToDimensionPixelSize(tv.data,getContext().getResources().getDisplayMetrics());
}
params.setMargins(0, actionBarHeight, 0,0);
}else{
params.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
}
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
params.addRule(RelativeLayout.ALIGN_PARENT_START);
MessageView.this.setLayoutParams(params);
And here is my XML file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:roboto="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1e1e1e"
android:id="#+id/messageViewBackground"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:padding="10dp">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/messageViewSpinnerLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp">
<ImageView
android:id="#+id/messageViewSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_spinner_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon_spinner_logo_small"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"/>
</RelativeLayout>
<ImageView
android:id="#+id/messageViewIcon"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:src="#android:drawable/stat_sys_warning"
android:visibility="gone"/>
<com.myapp.widgets.RobotoText
android:id="#+id/messageViewText"
roboto:font="Light"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#5B5B5B"
android:layout_gravity="center_vertical" />
</LinearLayout>
</RelativeLayout>
What am I doing wrong?
Just a guess but if MessageView extends FrameLayout that's why the exception might be happening, and you don't need to add the params to MessageView.this, but to the RelativeLayout view, one of the 2 from your layout with id android:id="#+id/messageViewBackground" or android:id="#+id/messageViewSpinnerLayout"
Been switching from linear layout and relative layout, because I wanted to attain the centering of the image.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:scrollbars="none"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ImageView
android:id="#+id/itemItem"
android:src="#drawable/content_picture"
android:tag="image_item_grid_image"
android:layout_width="wrap_content"
android:background="#drawable/layout_bg"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_gravity="center|center_horizontal"
android:adjustViewBounds="true"
android:contentDescription="Desc"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout >
</ScrollView>
Display Image:
DisplayMetrics displaymetrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(displaymetrics);
int height = displaymetrics.heightPixels;
int width = displaymetrics.widthPixels;
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params.height = height / 2;
params.width = width / 2;
params.setMargins(0, 50, 0, 0);
//mImageView.get().setImageBitmap(bitmap.get());
if(bitmap!=null){
Log.d("#ImageValue: ", ""+bitmap.toString());
mImageView.get().setImageBitmap(bitmap.get());
mImageView.get().setLayoutParams(params);
}
The image is not centering for some reason, and I tried as much as I know but seems there is lacking.
Try this..
Remove gravity add android:layout_centerHorizontal="true" and android:layout_centerVertical="true"
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/itemItem"
android:src="#drawable/content_picture"
android:tag="image_item_grid_image"
android:layout_width="wrap_content"
android:background="#drawable/layout_bg"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:adjustViewBounds="true"
android:contentDescription="Desc"
android:scaleType="fitCenter" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
<RatingBar
android:id="#+id/ratingBar1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout >
</RelativeLayout>
EDIT:
add below lines also in your params
params.addRule(RelativeLayout.CENTER_VERTICAL,RelativeLayout.TRUE);
params.addRule(RelativeLayout.CENTER_HORIZONTAL,RelativeLayout.TRUE);
Your Relative Layout is set to:
<RelativeLayout
android:layout_width="wrap_content" // will not fill parent so children will centre in relative layout, not the whole view
android:layout_height="wrap_content"
android:orientation="vertical" >
you should either set the Relative Layout to fill parent for width and height or set the Scroll Views layout_gravity to center so that the Relative Layout centers within the scrollview.
Try using
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
instead of
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
And comment out the logic to centre image view programmatically.