How to fix the position of Button in Android - java

I am working on a project in which I need to dynamically add TextView and Spinner as well through the program. I was able to add these two things dynamically from my program successfull.
Below is the XML layout I have used for that.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:gravity="center_vertical|center_horizontal"
android:text="Save" />
</LinearLayout>
</ScrollView>
Problem Statement:-
So what I want is- All the TextView and Spinner should get shown firstly then in the last Button should get shown.
But what is happening currently Save Button gets shown at the Top and then all the other TextView and Spinner gets shown.
How can I make sure, that Button comes at the bottom of screen.
Below is the screenshot from which you can figure out the problem. I want to show the Save button at the bottom of screen.

Or you can create a button like this in the code and added to your layout after adding all the spinners and textviews like below, you will have to remove the declaration of the button from your xml:
Button button = new Button(this) ///this is a context object
button.setWidth(100);
button.setText("Save");
layout.addView(button);
You can dynamically create the layout and add the button to that layout and then add this layout to the Super layout
Or what you can do is Declare a new xml file in the res/layout/savebutton_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:id="#+id/lLayoutBT"
>
<Button
android:id="#+id/saveBT"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/save" />
</LinearLayout>
Now in your code you will have to find this layout containing the button and then add this layout to your main layout (just like you were adding your button)
For example
LinearLayout buttonLayout =(LinearLayout) view.findViewById(R.id.lLayoutBT);
views.add(buttonLayout);

If you use a RelativeLayout (instead of a LinearLayout) you have access to properties like:
<Button
android:id="#+id/button1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
...
</Button>

If you change your LinearLayout to a RelativeLayout then add this in your Button
<Button
...
android:layout_alignParentBottom="true"/>
it should give you what you want. I use RelativeLayout instead of LinearLayout as often as possible anymore because in most instances, it is the more flexible and efficient. They take a little more time to get used to but are definitely worth taking the time to understand and implement
Edit
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/llayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button1"
android:layout_width="100px"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"/>
android:text="Save" />
</RelativeLayout>
</ScrollView>
Something like that should work. You may have to play with it a little to get exactly what you want but that should get you there.

Related

Images won't show and/or won't work with buttons

I have added one image at the moment but it's grey. Button won't work as intended too, but it may be a reason of broken image (because code seems to be right).
U can change drawable for button by android:background="#android:drawable/..." property. But I think u want to obtain switch or ToogleButton so it should be different View.
1> You can use Switch for that.
2> Change your ConstraintLayout to LinearLayout if you want your layout to appear.
like
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:layout_height="match_parent"
>
<ImageView
android:src="#mipmap/ic_launcher"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
and it looks like -

How to stretch a list view to the full screen in Android

Supose I have a layout oriented vertically with one button in the top, a list view in the middle, and a button in the bottom:
------
BUTTON
LISTVIEW
LISTVIEW
LISTVIEW
------
I would like to make the whole layout scrollable, to be able to see the full list when scrolling down it:
------
LISTVIEW
LISTVIEW
LISTVIEW
BUTTON
------
I think I found it, here's a solution for you, without using ScrollView (which is actually not needed):
<LinearLayout android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button android:id="#+id/btn1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0"/>
<ListView android:id="#android:id/list1"
android:layout_height="0dip"
android:layout_width="match_parent"
android:layout_weight="1" />
<ListView android:id="#android:id/list2"
android:layout_height="0dip"
android:layout_width="match_parent"
android:layout_weight="1" />
<ListView android:id="#android:id/list3"
android:layout_height="0dip"
android:layout_width="match_parent"
android:layout_weight="1" />
<Button android:id="#+id/btn2"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
You can add a button to the header of a ListView and a button to the bottom of the ListView
Add a Button as header and a Button as footer to the ListView.
Write a custom Adapter for the ListView, return Button view at 0 and last position in getView().
I'm not really sure if you want to be able to scroll the buttons too, depending on that there are two ways which you can try. First where the button won't change their position while list view is scrolling. In this situation your xml should look 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" >
<Button
android:id="#+id/top_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<ListView
android:id="#+id/my_listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/bottom_button"
android:layout_below="#+id/top_button" />
<Button
android:id="#+id/bottom_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true" />
</RelativeLayout>
The second option is when you want these two buttons to scroll while list view is scrolling too. To achieve this you should add Buttons to your list view as header and footer :
mListView.addHeader(myFirstButton);
mListView.addFooter(mySecondButton);
And that should do the trick for you in both scenarios.
You should use android:layout_height="wrap_content"

layout in android

i have several xml layouts on my project. when the application start the main.xml layout is load on the device. when i load a second xml layout it get infront from the main but do not cover all the screen and also you are albe to see that behind is the main.xml layout
the second layout am using match_parent and the xml code is
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent" android:weightSum="1">
//something
</LinearLayout>
this tham am trying to do is that the second layout to take all the screen and not be able to see the main on the back .
is this possible to be done?
EDIT
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TableLayout android:layout_width="match_parent"
android:layout_height="match_parent" >
<TableRow >
<TextView android:text="Password Recovery" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="match_parent"></TextView>
</TableRow>
<TableRow >
<TextView android:text="Username: " android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
<EditText android:text=" " android:id="#+id/UsernameRecovery" android:layout_width="100dp" android:layout_height="45dp"></EditText>
</TableRow>
<TableRow >
<Button android:text="Submit" android:id="#+id/SubmitRecoveryPass" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
<Button android:text="Cancel" android:id="#+id/CancelRecoveryPass" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</TableRow>
</TableLayout>
</LinearLayout>
what i have to change in order to be full screen and not to be able see on the back
If you want is to be done on Single activity then use to hide the Layout while showing another layout.
Means hide main Layout while open the Second Layout. And Same thing for Main Layout also.
Adnd if you dont want it to be done in Single activity then try this.
Create two xml. as like main.xml and second.xml
now in calling of first activity setContentLayout(R.layout.main);
and while call second activity then setContentLayout(R.layout.second);
By defalut android hide the first activity and show the second activity.
Yes but make sure that you are using fill_parent on the parent layout of the both the xml.
Hope it will help you.
Enjoy. :))
It sounds like what you need is two activities. Launch your second activity, which has the layout you want on top, from the first, which uses the main.xml.
See this for more on starting activities: http://developer.android.com/reference/android/app/Activity.html#startActivity(android.content.Intent)

Overwriting paddingLeft in a LinearLayout on Android

i am currently making one android application, i moded title bar, so all content for title bar is holded in window_title.xml where i have LinearLayout set: paddingLeft="5dip" . And now i need to load one imageView on the right side of this titleBar how i am able to some kind of overwrite this paddingLeft i cant delete it as it required for text and image on the left but i need it on right too...
Here is current xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:background="#222222">
<ImageView
android:id="#+id/header"
android:src="#drawable/header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<TextView
android:text="TextTexast"
android:textSize="10pt"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:paddingLeft="5dip"
android:textColor="#F7F7F7"
/>
</LinearLayout>
If I understand your question correctly, you're looking to apply some padding to an ImageView that you display to the right of the titlebar TextView. You can use android:paddingRight in your root LinearLayout like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="50dip"
android:gravity="center_vertical"
android:paddingLeft="5dip"
android:paddingRight="5dip
android:background="#222222">
Alternatively, you can specify the android:paddingRight attribute in the ImageView that you want to align to the right.
To move the grey figure to the right just set android:layout_gravity for that to "right".

Button not appearing in my Linear Layout

I have a button inside of a LinearLayout that is oriented vertically. Not sure why the button doesn't show up?
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:orientation="vertical">
<com.commonsware.cwac.tlv.TouchListView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tlv="http://schemas.android.com/apk/res/org.stocktwits.activity"
android:id="#android:id/list"
android:layout_width="fill_parent"
android:drawSelectorOnTop="false"
tlv:normal_height="64dip"
tlv:grabber="#+id/icon"
tlv:remove_mode="slideRight"
android:layout_height="wrap_content"/>
<Button android:text="Button" android:id="#+id/button1" android:layout_width="wrap_content" android:layout_height="wrap_content"></Button>
</LinearLayout>
android:height="wrap_content" has no meaning for vertically-scrollable widgets like ListView. Use android:layout_weight with your LinearLayout or a RelativeLayout to achieve whatever look you are trying to achieve.
Try adding this to your LinearLayout:
android:layout_width="fill_parent"
android:layout_height="fill_parent"
I'm guessing the layout is just not showing?

Categories

Resources