layout in android - java

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)

Related

Need something Swipe Left from Main Activity to go into another Activity

I am making an Android Launcher, which simply has the App Names (Icons are hidden as I just want it to be very minimalist.
In the main activity, I have a list of all apps installed in a GridView.
(I want to change that to show only the selected Favourite apps)
I also have another activity where all apps are shown with Search functionality.
I want to be able to switch from Main Activity to the Full App page, as well as switch back to Main activity with swipes.
So Left swipe should bring app page which has (GridView of apps)
swipe right from that page brings me back (hopefully restore the previous activity)
I have tried a few different solutions, none of them is ideal as most do not work, and one only works when Swiping right from the top.
Swipe left-right changes activity
I have tried the first solution provided in the above link
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:id="#+id/MainActivity"
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"
tools:context=".MainActivity"
android:orientation="vertical">
<LinearLayout
android:id="#+id/top_bar"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextClock
android:id="#+id/clock"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="40sp" />
<TextView
android:id="#+id/dateView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#android:color/white"
android:textSize="20sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/MainActivityApp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="6">
<GridView
android:id="#+id/appGrids"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:numColumns="1"
android:stretchMode="columnWidth">
</GridView>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="2">
<ImageView
android:id="#+id/setting"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:contentDescription="#string/app_name"
app:srcCompat="#android:drawable/ic_menu_manage"
/>
</LinearLayout>
</LinearLayout>
Hi have a look and try this for gesture to left right bottom and call your next activity intent in on left section
Android: How to handle right to left swipe gestures

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 -

Android: Relative Layout

Been using android for about 3 months and always hardcode the width and height of elements in a layout, I know this is considered bad to do so I want to get a better understand of how to use relative layouts to position elements. I have looked at the documentation of relative layout but from my understanding, I cannot get it to work the way I would like. I come asking if anyone can provide help (not just suggesting documentation I have read it) with how I need to use relative layout. This is so I can have something basic to refer to in future work to help with my positioning.
The following is 3 elements contained on a relative layout, 2 expandable lists and 1 listview:
The listviews are all over the place in this example I would like to know how to position them something like this:
this is the current xml, what would I need to add and change to gain this type of layout?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_food_bible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#606E7F"
tools:context="com.example.mr_br.project.food_bible">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/exlistVtype"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/exlistVpref"
android:layout_alignParentTop="true" android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_marginLeft="114dp"
android:layout_marginStart="114dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="244dp"
android:id="#+id/listVresult" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true" android:layout_alignParentStart="true"/>
</RelativeLayout>
Thank you for any help.
Just don't use drag and drop and type the code by yourself. Use Ctrl + space and the list with all the attributes will show. Now you just need to know what you what to do. In your example you need one view "android:layout_toRightOf" other so just type right and android studio will automatically show you the attribute that you want. In case of placing a view below another view you can use "android:layout_below".
You can read all the attributes here
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_food_bible"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#606E7F"
tools:context="com.example.mr_br.project.food_bible">
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/exlistVtype"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
<ExpandableListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/exlistVpref"
android:layout_toRightOf="#id/exlistVtype"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" android:layout_marginLeft="114dp"
android:layout_marginStart="114dp"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="244dp"
android:layout_below="#id/exlistVtype"
android:id="#+id/listVresult" android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"/>
</RelativeLayout>
With RelativeLayout, your main tools at hand are:
1)android:layout_above ="#+id/theIdOfWhatYouWantBelowCurrentView"
2)android:layout_alignBottom = "#+id/IDofWhatYouWantTouchingBottomOfThisView
3)android:layout_alignLeft = "#+id/IDofWhatYouWantTouchingLeftOfThisView"
4)android:layout_below = "#+id/theIdOfWhatYouWantAboveCurrentView"
ETC. ETC. Check this: https://www.tutorialspoint.com/android/android_relative_layout.htm
So when using RelativeLayout, you specify the position of all the views relative to each other using the syntax given above. That's basically all to it.

Position Of Views in Android vs iOS?

I am new to Android development. I have been working in iOS since long. As in iOS when we want to put VIEW on xib on some exact position, we simply put it there, drag it up to that point.
For example say Two buttons at lower area in iOS, which look like below
As, I simply want them in middle, I will put them their. as below
Now same thing in Android environment, I go for following code,
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/db1_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/myAwesomeTextView"
android:layout_width="wrap_content"
android:layout_height="1dip"
android:layout_centerInParent="true"
android:text="Veer Suthar" />
<TextView
android:id="#+id/myAwesomeTextView1"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_below="#id/myAwesomeTextView"
android:layout_centerInParent="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/myAwesomeTextView1"
android:gravity="center_vertical"
android:orientation="vertical" >
<Button
android:id="#+id/button1"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:onClick="buttonPressed"
android:text="Button One" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|center"
android:onClick="buttonPressed"
android:text="Button Two" />
</LinearLayout>
</RelativeLayout>
It shows Activity Screen, like below
Now If I want to drag buttons, using GRAPHICAL LAYOUT, I can't move them as I want, and for spacing to put them into lower area, I need to put extra TextView .
Is there any better way to organise Android Activity GUI properly, like iOS?
I'll give you a brief example, since Android graphical layout is not as smooth as XCode.
To accomplish what you need, centering the two buttons in the screen, you can use a XML code 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" >
<LinearLayout
android:id="#+id/layout_center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_centerInParent="true">
<Button
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button One"/>
<Button
android:id="#+id/button_two"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button Two"/>
</LinearLayout>
</RelativeLayout>
The trick is to use android:layout_centerInParent="true" for the only component that you want to be centered in the screen all other components can use that one for reference to be placed in the screen.
For example
<TextView
android:id="#+id/myAwesomeTextView1"
android:layout_width="wrap_content"
android:layout_height="100dip"
android:layout_above="#+id/layout_center"
android:text="Veer Suthar"/>
This is one way for doing this, you can always find a better and more comprehensible way to do things.
Hope this helped.
Add this to your LinearLayout:
android:layout_alignParentBottom = "true"
Childs in a RelativeLayout can be "glued" to a particular position relative to the parent layout or to other elements in the same layout using the xml tags listed here: http://developer.android.com/reference/android/widget/RelativeLayout.LayoutParams.html

How to fix the position of Button in Android

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.

Categories

Resources