I can't seem to control the dialog width. I have a simple layout like so`
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:theme="#android:style/Theme.Dialog">
<ScrollView
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView
android:id="#+id/name_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/name_prompt"
android:padding="10dip"/>
<EditText
android:id="#+id/name_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text" />
<TextView
android:id="#+id/t1_prompt_view"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/t1_prompt"
android:padding="10dip"/>
<Spinner
android:id="#+id/t1_inp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:lines="1"
android:maxLines="1"
android:maxLength="48"
android:inputType="text"
android:singleLine="true"
android:layout_weight="1"
android:entries= "#array/t1_allowed_values" />
</LinearLayout>
</ScrollView>
</LinearLayout>
for some reason the dialog is only wide enough for the text input field about 11 chars wide. How do I make the dialog width fill the screen?
I had the same problem.
I used following code to make dialog fill_parent and it worked fine.
public class SharePost extends Dialog
{
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.adaptor_contentsharepost);
LayoutParams params = getWindow().getAttributes();
params.height = LayoutParams.FILL_PARENT;
getWindow().setAttributes((android.view.WindowManager.LayoutParams) params);
}
}
layout
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:id="#+id/dialogWidth"
android:layout_width="match_parent"
android:layout_height="match_parent">
contents here
</LinearLayout>
I'm using:
getWindow().setLayout(LayoutParams.MATCH_PARENT, LayoutParams.WRAP_CONTENT);
Set a minimum width at the top most layout.
android:minWidth="300dp"
For example:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android" android:minWidth="300dp">
<!-- Put remaining contents here -->
</LinearLayout>
As Matthias points at out How can I get a Dialog style activity window to fill the screen? UMAR's solution works, but only if the window attributes are set AFTER setContentView() is called.
Try this. It works for me.
dialog = new Dialog(Activity.this);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(R.layout.feedback_popup);
dialog.setCancelable(false);
dialog.setCanceledOnTouchOutside(false);
WindowManager.LayoutParams lp = new WindowManager.LayoutParams();
lp.copyFrom(dialog.getWindow().getAttributes());
lp.width = WindowManager.LayoutParams.MATCH_PARENT;
lp.height = WindowManager.LayoutParams.WRAP_CONTENT;
lp.gravity = Gravity.CENTER;
dialog.getWindow().setAttributes(lp);
Since API 8 FILL_PARENT is deprecated. Use MATCH_PARENT instead.
params.height = LayoutParams.MATCH_PARENT;
Related
In Recylerview: I am trying to hide and show a view
For hiding i am doing:
vh0.itemView.setVisibility(View.GONE);
vh0.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
But when i have to show back I am trying
vh0.itemView.setVisibility(View.VISIBLE);
RecyclerView.LayoutParams lpr = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lpr.setMargins(10,10,10,10);
vh0.itemView.setLayoutParams(lpr);
It does not show the save way as the below cardview.
My cardview is
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/cvsearch"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
card_view:cardBackgroundColor="#ffffff"
card_view:cardCornerRadius="0dp"
card_view:cardUseCompatPadding="false"
card_view:contentPadding="0dp">
<Button
android:id="#+id/buttonsuggest"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:clickable="false"
android:layout_marginRight="20dp"
android:text="Other Suggested Words" />
</android.support.v7.widget.CardView>
SOLVED: Mistake i Was doing:
I didnt added:
RecyclerView.LayoutParams lpr = new
RecyclerView.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
lpr.setMargins(10,10,10,10);
vh0.itemView.setLayoutParams(lpr);
I was trying only:
vh0.itemView.setVisibility(View.VISIBLE);
The above was not working because previously i did
vh0.itemView.setVisibility(View.GONE);
vh0.itemView.setLayoutParams(new RecyclerView.LayoutParams(0, 0));
So the margins are removed even I make VISIBLE its not shown.
I developed an iOS app and now working on the Android version.
I want to create the overlay view above the camera view (SurfaceView). The part which i cant replicate currently is the masked square overlay in the middle of the screen. I tried the android.support.percent.PercentFrameLayout but it doesn't work as what i want.
For iOS version, I simply create 8 rectangular UIViews to form that shape. Then layout them by setting their CGRect.
But for Android, I know I can create them runtime using code. But the part I dont know is how to layout them.
Would like to have some advises, like which way should i go?(XML, or programmatically render).
Thanks a lot!
]
Layout file
<FrameLayout 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"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.nerfire.hk.flexibill.ScanMain">
<!-- TODO: Update blank fragment layout -->
<SurfaceView
android:id="#+id/camera_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<!--<android.support.percent.PercentFrameLayout-->
<!--android:orientation="vertical"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="match_parent">-->
<!--<View-->
<!--android:layout_width="match_parent"-->
<!--app:layout_heightPercent="68%"-->
<!--/>-->
<!--</android.support.percent.PercentFrameLayout>-->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center_horizontal"
android:layout_marginBottom="10dp"
android:text="Scan and Pay!"/>
</FrameLayout>
Right inside the onCreateView (using Fragment)
Here are the code, without refining the code, putting them in a for-loop
DisplayMetrics metrics = new DisplayMetrics();
getActivity().getWindowManager().getDefaultDisplay().getMetrics(metrics);
FrameLayout viewLayout = (FrameLayout) view.findViewById(R.id.scan_main_frame_layout);
int overlayColor = Color.argb(127, 255, 255, 255);
int lineWidth = 20;
int lineLength = (int)(metrics.widthPixels*0.2);
float originX = (float) (metrics.widthPixels*0.25);
float endPointX = (float) (metrics.widthPixels*0.75);
float screenCentreY = (float)(metrics.heightPixels*0.5);
FrameLayout.LayoutParams horizontalLineFrame = new FrameLayout.LayoutParams(lineLength, lineWidth);
FrameLayout.LayoutParams verticalLineFrame = new FrameLayout.LayoutParams(lineWidth, lineLength-lineWidth);
View view1 = new View(getContext());
view1.setX(originX);
view1.setY(screenCentreY-originX);
view1.setBackgroundColor(overlayColor);
view1.setLayoutParams(horizontalLineFrame);
viewLayout.addView(view1);
View view2 = new View(getContext());
view2.setX(endPointX-lineLength);
view2.setY(screenCentreY-originX);
view2.setBackgroundColor(overlayColor);
view2.setLayoutParams(horizontalLineFrame);
viewLayout.addView(view2);
View view3 = new View(getContext());
view3.setX(originX);
view3.setY(screenCentreY+originX-lineWidth);
view3.setBackgroundColor(overlayColor);
view3.setLayoutParams(horizontalLineFrame);
viewLayout.addView(view3);
View view4 = new View(getContext());
view4.setX(endPointX-lineLength);
view4.setY(screenCentreY+originX-lineWidth);
view4.setBackgroundColor(overlayColor);
view4.setLayoutParams(horizontalLineFrame);
viewLayout.addView(view4);
View view5 = new View(getContext());
view5.setX(originX);
view5.setY(screenCentreY-originX+lineWidth);
view5.setBackgroundColor(overlayColor);
view5.setLayoutParams(verticalLineFrame);
viewLayout.addView(view5);
View view6 = new View(getContext());
view6.setX(endPointX-lineWidth);
view6.setY(screenCentreY-originX+lineWidth);
view6.setBackgroundColor(overlayColor);
view6.setLayoutParams(verticalLineFrame);
viewLayout.addView(view6);
View view7 = new View(getContext());
view7.setX(originX);
view7.setY(screenCentreY+originX-lineLength);
view7.setBackgroundColor(overlayColor);
view7.setLayoutParams(verticalLineFrame);
viewLayout.addView(view7);
View view8 = new View(getContext());
view8.setX(endPointX-lineWidth);
view8.setY(screenCentreY+originX-lineLength);
view8.setBackgroundColor(overlayColor);
view8.setLayoutParams(verticalLineFrame);
viewLayout.addView(view8);
Screenshot
ps: thanks for those who randomly left down votes without comment, without suggesting any solution or reading the question. Thanks mate:) nice android community.
Try this solution.
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.app.mynewproject.MainActivity">
<SurfaceView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/surfaceView" />
<ImageView
android:layout_height="250dp"
app:srcCompat="#android:drawable/ic_menu_mylocation"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/imageView"
android:layout_width="250dp" />
<TextView
android:text="SCAN AND PAY!"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:gravity="center"
android:textSize="25sp"
android:textColor="#android:color/white"
android:layout_centerHorizontal="true"
android:layout_marginBottom="15dp"
android:id="#+id/textView" />
</RelativeLayout>
This is working as required.
You can simply put a TextView whose property set to "android:layout_alignParentBottom="true" containg text Scan And Pay! in RelativeLayout which is also parent layout for SurfaceView.
<?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">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:textColor="#android:color/white"
android:layout_width="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="SCAN AND PAY!"
android:layout_height="wrap_content" />
</RelativeLayout>
Or Use FrameLayout if no other views relative to other are present:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<SurfaceView
android:id="#+id/surfaceView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
<TextView
android:textColor="#android:color/white"
android:layout_width="wrap_content"
android:layout_gravity="bottom |center_horizontal"
android:text="SCAN AND PAY!"
android:layout_height="wrap_content" />
</FrameLayout>
FrameLayout is pretty basic container so if you need some more advanced features like positioning you should root FrameLayout with RelativeLayout, then in place of your (commented out) android.support.percent.PercentFrameLayout element put ordinary ImageView showing your masking sqare. To center that ImageView ensure you got these set for it:
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
that should suffice.
I want to display a picture advertisement at the bottom of the display area. I've tried using bottom but it didn't work. Can you help me?
MainActivity.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent"
tools:context=".MainActivity">
<include
android:id="#+id/tool_bar"
layout="#layout/tool_bar"
></include>
<LinearLayout
android:id="#+id/adLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"></LinearLayout>
<ListView
android:id="#+id/ilcelerListview"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
Picture link : For Example
I can give your two tip to do that.
You can use RelativeLayout instead of LinearLayout at MainActivity.
For that, you can easy to add a view which you want to add to bottom.
Or
ViewGroup rootView = (ViewGroup) activity.findViewById(Window.ID_ANDROID_CONTENT);
LayoutParams params = new LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.MATCH_PARENT);
//you can use params to set in the bottom or other positions
//view is yours
rootView.addView(view, params);
I use the following layout (shortened):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" ...>
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
Like this (shortened also):
LayoutInflater inflater = parent.getLayoutInflater();
View layout = inflater.inflate(R.layout.merger_sale, null);
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
builder.setTitle(R.string.sell_stock_after_merger)
.setView(layout)
.setPositiveButton(R.string.sell_all, new OnClickListener() {...
.setNeutralButton(R.string.trade_sell, new OnClickListener() {...
.setNegativeButton(R.string.trade_all, new OnClickListener() {...
AlertDialog dialog = builder.create();
dialog.show();
The layout has no errors, nor does the code. The layout shows exactly as it should in Eclipse's layout editor.
The dialog appears when it should appear, buttons behave how they should, except for the texview (#+id/sell_text) [and the other textviews in the shortened <TableRow>s], which don't appear.
What am I overlooking? Thanks!
I think it would work if you change the width and height to wrap content instead of match_parent.
match_parent means it would be fillparent which may fill the entire layout.
Or try giving weights to each item like layout_weight with value as 0 .This may work either
try it:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
I have this XML:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:ads="http://schemas.android.com/apk/lib/com.google.ads"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/fondoverde3"
android:gravity="top"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<com.google.ads.AdView
android:id="#+id/ad"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
ads:adSize="BANNER"
ads:adUnitId="adasdasdasdas"
ads:loadAdOnCreate="true" />
</LinearLayout>
<ScrollView
android:id="#+id/ScrollView1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<RelativeLayout
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
......
<LinearLayout
android:id="#+id/LinearLayout2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearLayout4"
android:layout_marginTop="10dp"
android:orientation="vertical" >
</LinearLayout>
</RelativeLayout>
</ScrollView>
</LinearLayout>
So what I would like is to refer in my java code to the LinearLayout2 an add some things. I prove with an easy code to know that my code works (and it works!! I show the two textviews), exactly with this:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
TextView tituloIngr2 = new TextView(this);
tituloIngr2.setText("AAAAA");
ll.addView(tituloIngr2);
TextView tituloIngr1 = new TextView(this);
tituloIngr1.setText("BBBBB");
ll.addView(tituloIngr1);
But what I would like to do is to show in this linearLayout2 many Textviews that I take from a StringArray, so the code is the next:
LinearLayout ll = (LinearLayout)findViewById(R.id.LinearLayout2);
ll.setOrientation(LinearLayout.VERTICAL);
for (int m= 0; m<few.length; m++) {
TextView ingr= new TextView(this);
ingr.setText(few[m]);
ingr.setPadding(5, 5, 5, 5);
ingr.setGravity(0x11);//sacados todos los values de developers
ingr.setTextColor(0x96610098);
ingr.setBackgroundResource(android.R.color.white);
ingr.setTextSize(22);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(0, 25, 0, 0);
ll.addView(ingr, layoutParams);
}
this.setContentView(ll);
The error is a force close, and the log cat:
11-27 17:09:58.213: E/AndroidRuntime(1440): java.lang.RuntimeException: Unable to start activity ComponentInfo{back.now/back.now.Tree}: java.lang.IllegalStateException: The specified child already has a parent. You must call removeView() on the child's parent first.
Remove this call:
this.setContentView(ll);
ll is already part of the content view. Trying to add it again as the content view is likely what's triggering the exception. You also don't need this call:
ll.setOrientation(LinearLayout.VERTICAL);
The orientation is specified as vertical in the xml.