Android Studio Layout Auto Alignment - java

I am new to Android Studio, and no matter what I do whenever I add something such as a textview or button to it, I cannot make it the size I want it to. It always auto aligns itself to the sides or top. When I want to make a square it doesn't allow me to do it. It never allows me to put my button or textview where I want it to. How do I make it so I can place my stuff anywhere I want and whatever size I choose it to be?

You are probably using linear layout,You can use relative layout instead for start, although if you want to put a lot of buttons its getting harder to work with relative layout and linear layout is more organized go to you'r xml and there edit the in the first line where it says <LinearLayout... to <RelativeLayout> and don't forget to do the same at closing,I recommend you to watch a video on youtube on how to work with linear layouts.

In RelativeLayout, it enable the attributes like alignParentRight="true/false" for exact right position and so on...
layout_below="#id/tool/viewID_below_you_want_to_put" and same for other side
so you can use it
For Example,
<RelativeLayout
...>
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_toRightOf="#id/txt1"
android:alignParentRight="true"
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</RelativeLayout>
which will make your textview in left and then button in right of textview
If you want to set it in center tou can set gravity="center | center_vertical | center_horizontal"
also you can set size as you want in width or height like
android:layout_width="50dp"

Related

how to adjust spinner text like Google translate?

I want to adjust my spinner like Google android translate
I want to adjust my left spinner text like the right image, below the given code is my left side spinner
<Spinner
android:id="#+id/idFromSpinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="end"
android:layout_marginStart="-3dp"
android:paddingEnd="30dp"
android:paddingStart="15dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
/>
Please help me in this project actually my task is to make a spinner like Google translate
I want to make left spinner left side and right spinner right side margin fix it should not change on increasing or decreasing text length
You can add in android:layout_marginRight="10dp". Just adjust " 10dp" time and time again, good luck on that spinner!
Edit:
The solution is a good use if the left-side spinner is android:layout_toLeftOf="#+id/middle_switcher but if not. You could just use any other Layouts like the GridLayout...

How to set text position to the right of a Switch (like CheckBox)

How to set the text position to the right of a Switch without align a Switch and a TextView in a LinearLayout ? I tried to override the CheckBox button style in my style.xml button but I don't know how to get the Switch button reference ?
I've recently faced the same issue and as I didn't want to wrap my layout in an extra container like all the answers I came across suggested.
I found out that by adding android:layoutDirection="rtl" in the switch I can change the direction of the text because Android understands that this view is supposed to be read/used by a person who reads from right to left.
See full snippet below including an extra padding between the thumb and the text
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:switchPadding="16dp"
android:layoutDirection="rtl"
android:text="Your Text" />
Update
I decided to update this for future reference as the claims in the comments are wrong and the op "unaccepted" this answer even when the question states "without align a Switch and a TextView in a LinearLayout" which the currently accepted answer does.
What the solution provided does is to tell that specific widget to change its layout orientation and by doing that it flip it from "LTR" (Left to right) to "RTL" (Right to left).
If you are not willing to nest layouts, this is a doable solution.
Testing
Set the widget layout orientation to "RTL"
Run the app
Go to Settings > Developer Options > Force RTL layout direction
Observe the layout
The direction of all the other widgets will flip to "RTL" as by the default they were on "LTR" and nothing will happen with the Switch as it is already in the right configuration. Check the images below.
Layout orientation "LTR" default
Layout orientation "RTL"
Two ways are there but as negative padding is not recommended so the 2nd way.
=> Using a separate text view
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:layout_height="wrap_content">
<Switch
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/switch1" />
<TextView
android:layout_width="wrap_content"
android:text="This is a switch"
android:layout_height="wrap_content" />
</LinearLayout>
EDIT - There's no direct way but using Negative padding, A parent Layout or Any custom library.
See here and here.
Forget this and use this library.

Is it possible to make android:drawableLeft fill the parent on a Button View?

So I've defined a simple Button in xml like so:
<Button
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
I have several of these in a LinearLayout and as expected they fill the screen, each taking up an equal amount of space, each one looking similar to this: http://i.imgur.com/fMNldop.jpg
The problem I'm having here is as you can see the drawable is only the same height as the text, I'd like to know if there's a way to make the drawable scale to the height of the button itself WITHOUT using a workaround like creating my own Button using another LinearLayout, I'd like to stick to using a Button.
I have different versions of the image in my res folder under the correct folders (xxhdpi, xhdpi, and so on) but it only seems to want to use the smallest one.
EDIT: It seems like this may be due to the image supplied being too small, as I said just above I have the image in a whole range of sizes, how can I tell android to use a larger one on a larger screen instead of defaulting to the smaller one? I thought that was the whole point of having multiple sizes in the first place.
The drawable will only be the size the bitmap is. Just provide larger image and it should be fine.
For that you need to change your layout_height from fill_parent to wrap_content like this
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:gravity="center"
android:text="#string/home_button_student"
android:textSize="#dimen/abc_text_size_headline_material"
android:onClick="buttonClick"
android:id="#+id/home_button_student"
android:drawableLeft="#drawable/ic_action_emo_cool"/>
If you want a button real big in height, then make sure you have an equal heighted drawable image.and also there's no need to give weight here,
you can use your custom layout for that like drawable left or right whatever you want just use linear layout put in this imageview and button for the view. so you can put image that you wants

Removing all other areas except selected areas in mapview

Suppose I have made a diagram e.g a circle on android mapView. Now I want to remove/blackout/hide all other maps except that portion. I can zoom in out that portion too
Is this possible?
Best Regards
I have a similar problem of make map view inside certain area.not sure it will help you or not but you can try it.
What you should do is take image of circle(Background is Transparent) and put your image as background in Linear layout. and inside that LinearLayout you can put map view.
your xml will Look like below.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/Circle_back" >
<com.google.android.maps.MapView
android:id="#+id/whereami_mapview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:apiKey="YOUR API KEY"
android:clickable="true" />
</LinearLayout>
</LinearLayout>
i have to view it inside rectangle box. below is the similar screen shot you will get while running. you d'nt need to hide rest of the part of Map.
hope it will help some how what are you looking for.
Make a trick
Zoom will work in your case. Set zoom level and
Use android:clickable="false" in your layout file.
;)

Android. How to make dialog buttons appear a bit outside the dialog

I need to have a dialog (it's a game dialog) where buttons are at the lower corners of the dialog. Not inside the dialog but rather on the very corners (i.e. part of the button will reside over the dialog and the part will be outside of it).
First, as far as I know you can't move layout children outside their parent.
I've never tried exactly what you're going for, but I think it can be done. The trick would be to go with an activity with a dialog theme (you can find examples of these on the developer site or the API demos). Make sure your layout's root node has width and height set to wrap_content. Your root layout should be a RelativeLayout and have NO background (android:background="#0000").
Next, add another layout to your root node (FrameLayout would probably work) with a custom drawable for a background (or use the one that the default dialog uses from the framework) and width and height set to fill_parent or match_parent. Set android:padding to some dip value which pulls the background in from the edge of the dialog.
The only thing left to do would be to add your other layout elements to the root node. The FrameLayout will be drawn beneath everything else, and the padding will create the illusion of borders which do not encompass your UI.
Update
Yikes, just tried the above with good and bad results. First, you'll definitely want to look at the "Custom Dialog" example from the API demo, which makes use of:
CustomDialogActivity.java
layout/custom_dialog_activity.xml
xml/styles.xml
drawable/filled_box
Create an activity which uses the above xml layout file, and set the style for the activity to Theme.CustomDialog that you defined in xml/styles.xml. This will get you a red background for your activity. You can then edit the filled_box shape file to just have one background attribute set to invisible ("#0000"). The result should be an dialog-shaped activity with no background.
Next I tried to hack a background using my thoughts from above. The idea should be that there's a phony background drawn behind the other UI elements which does not encompass them, so it could be "shrunk" using layout_margin and not affect them. The problem here is that the phony background needs to have width and height set to relative to the other UI elements, so it sort of HAS to encompass them, so it can properly measure its own width and height relative to them.
So I think the solution could be to do most of what I've said above, except don't try the phony background thing. Just use a 9-patch drawable for your root layout background, and shrink the edges of your background to be drawn farther in than your content. You'd still use the custom theme stuff from above with an invisible window theme.
Here is a sample layout which i tried:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<FrameLayout
android:orientation="vertical"
android:id="#+id/ll1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:drawable/dialog_frame">
</FrameLayout>
<Button android:layout_width="wrap_content"
android:id="#+id/button1"
android:layout_height="wrap_content"
android:layout_marginTop="35dp"
android:layout_centerHorizontal="true"
android:text="Button"></Button>
</RelativeLayout>
here is the screenshot:
hope u get the hint , goodluck

Categories