merging contents run time error in android studio - java

I have problem with the XML file in android studio I'm very new to it and I want to design a text and below the text, there are two buttons but when it runs all content located at (0,0) location at the end only the last button appear in the device
here's my code
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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="com.example.tabarek.mysecondapplication.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Information!"
android:id="#+id/hello"
/>
<Button
android:id="#+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="SayHi"
android:text="About Me"
android:layout_below="#+id/hello"
/>
<Button
android:id="#+id/button2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="SayHi"
android:text="My Email"
android:layout_below="#+id/button"
/>
</android.support.constraint.ConstraintLayout>
at the end,
the result appears like this
can you help me, please?

try this use LinearLayout
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/hello"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Information!" />
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="SayHi"
android:text="About Me" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:onClick="SayHi"
android:text="My Email" />
</LinearLayout>

Related

Flexible button width according another buttons width

I try to make 3 buttons with vertical orientation, the problem is the first button have the widest than others, How I can make two others button automatic follow the first button width?
Should like this >
Below is the example of my code:
<?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:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS JUST A EXAMPLE BUTTON"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON 2"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BUTTON 3"/>
</LinearLayout>
</RelativeLayout>
Change your LinearLayout Like this, it will work fine
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="center_horizontal|center_vertical"
android:orientation="vertical">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="THIS IS JUST A EXAMPLE BUTTON" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON 2" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="BUTTON 3" />
</LinearLayout>
This is what you can do if you'll be willing to use ConstraintLayout. You just need to tweak some parts but it'll look like how your example looks if you don't really need it look like a table.
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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">
<Button
android:id="#+id/button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="8dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="THIS IS JUST A EXAMPLE BUTTON"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="#+id/button2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="BUTTON 2"
app:layout_constraintEnd_toEndOf="#+id/button"
app:layout_constraintStart_toStartOf="#+id/button"
app:layout_constraintTop_toBottomOf="#+id/button" />
<Button
android:id="#+id/button3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginStart="32dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="32dp"
android:text="BUTTON 3"
app:layout_constraintEnd_toEndOf="#+id/button"
app:layout_constraintStart_toStartOf="#+id/button"
app:layout_constraintTop_toBottomOf="#+id/button2" />
</android.support.constraint.ConstraintLayout>

MarginLeft From Button

I have create a RelativeLayout with buttons inside.
I'm using this xml code for create four buttons side by side:
<?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:minWidth="25px"
android:minHeight="25px">
<RelativeLayout
android:minWidth="25px"
android:minHeight="25px"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/relativeLayout1">
<Button
android:text="7"
android:layout_alignParentLeft="true"
android:layout_width="170dp"
android:layout_height="75.0dp"
android:id="#+id/button1" />
<Button
android:text="8"
android:layout_marginLeft="165dp"
android:layout_width="150dp"
android:layout_height="75dp"
android:id="#+id/button2"
android:layout_marginRight="119.0dp" />
<Button
android:text="9"
android:layout_marginLeft="310dp"
android:layout_width="150dp"
android:layout_height="75.0dp"
android:id="#+id/button3" />
<Button
android:text="Enter"
android:layout_marginLeft="455dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/button3" />
</RelativeLayout>
</LinearLayout>
The problem is that my margin is been started from RelativeLayout and not from my left object which in this case is Button.
So my question is: Will i have any problems if i will run application in different screen sizes??
The problem is that my margin is been started from RelativeLayout and
not from my left object which in this case is Button.
You could put your button to the right of the other button and the margin will be between them.
Example:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="100dp"
android:background="#color/blue" />
<Button
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_marginLeft="16dp"
android:layout_toRightOf="#id/button"
android:background="#color/accent" />
</RelativeLayout>
Please try that
-With linear layout you can make perfect drawings, I define every button "weight". That do thats They all share the line width evenly. If we say one to two, it will have twice as much space in the same line as the others
<?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:minWidth="25px"
android:minHeight="25px">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linearLayout1">
<Button
android:text="7"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/button1"
android:layout_weight="1" />
<Button
android:text="8"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/button2"
android:layout_weight="1" />
<Button
android:text="9"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/button3"
android:layout_weight="1" />
<Button
android:text="ENTER"
android:layout_width="wrap_content"
android:layout_height="75dp"
android:id="#+id/button4"
android:layout_weight="1" />
</LinearLayout>
</LinearLayout>

Relativeview 5 buttons, don't understand my mistake

I'm trying to make myself an app to track my progress at the gym since it's one of my new year resolution... I don't go to the gym, and I figure that if I can track progress, i could have some interest in going back.
Here's my issue:
I'm doing my main layout right now, and I want one large button that says: Start workout.
I would like to have 4 buttons centered next to each other with some spaces between each other and the four together would have the form of a square.
I wrote this code, and nothing is working at all right now. I just don't even know where to look anymore. I know I could use LinearLayout but I read everywhere that RelativeLayout should be prioritize
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical">
<Button
android:id="#+id/start_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:hint="#string/start_workout"
/>
<Button
android:id="#+id/log_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:hint="#string/log_button"
/>
<Button
android:id="#+id/progress_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:hint="#string/progress_button"
/>
<Button
android:id="#+id/cardio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/calendar_button"
android:layout_below="#id/progress_button"
android:hint="#string/cardio_button"
/>
<Button
android:id="#+id/calendar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/cardio_button"
android:layout_below="#+id/log_button"
android:hint="#string/calendar_button" />
</RelativeLayout>
Here it is.
The first button is screen wide and top aligned.
The other four occupy each 1/4th the screen width and are all below the top one
<RelativeLayout
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"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity"
>
<Button
android:id="#+id/start_workout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Start workout"
android:textSize="12sp"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/start_workout"
android:orientation="horizontal"
>
<Button
android:id="#+id/cardio_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Cardio"
android:textSize="12sp"
/>
<Button
android:id="#+id/progress_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Progress"
android:textSize="12sp"
/>
<Button
android:id="#+id/calendar_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Calendar"
android:textSize="12sp"
/>
<Button
android:id="#+id/log_button"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Log"
android:textSize="12sp"
/>
</LinearLayout>
</RelativeLayout>
Try this,but better to use linear layout in your case
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
<Button
android:id="#+id/cardio_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/log_button"
android:hint="cardio_button" />
<Button
android:id="#+id/progress_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/log_button"
android:layout_alignBottom="#+id/log_button"
android:layout_toRightOf="#+id/cardio_button"
android:hint="progress_button" />
<Button
android:id="#+id/calendar_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/cardio_button"
android:layout_alignBottom="#+id/cardio_button"
android:layout_toRightOf="#+id/cardio_button"
android:hint="calendar_button" />
<Button
android:id="#+id/log_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/start_workout"
android:layout_marginTop="50dp"
android:layout_toLeftOf="#+id/progress_button"
android:hint="log_button" />
<Button
android:id="#+id/start_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:hint="start_workout" />
</RelativeLayout>

Xml file image over image

i want to create a listView with swipe (front and background) from here:
https://github.com/47deg/android-swipelistview
https://github.com/47deg/android-swipelistview-sample/
i created an xml file which defines the row element:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/front"
android:tag="front"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#ffffff">
<ImageView
android:id="#+id/ImageView01"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
<TextView
android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/ImageView01"
android:layout_centerHorizontal="true"
android:textSize="18sp"
android:textStyle="bold"
android:inputType="textMultiLine" />
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/back"
android:background="#e3e3e3"
android:tag="back"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
>
<Button
android:id="#+id/deleteBtn1"
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Delete" />
<Button
android:id="#+id/UpdateBtn1"
android:layout_width="150dp"
android:layout_height="100dp"
android:text="Update" />
</LinearLayout>
</FrameLayout>
and the listview 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:layout_below="#+id/linearLayout1"
android:orientation="vertical" >
<EditText android:id="#+id/EditText01"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:hint="Search">
</EditText>
<com.fortysevendeg.android.swipelistview.SwipeListView
xmlns:swipe="http://schemas.android.com/apk/res-auto"
android:id="#+id/example_lv_list"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:background="#drawable/profile_listview_back"
android:listSelector="#00000000"
swipe:swipeActionLeft="dismiss"
swipe:swipeActionRight="reveal"
swipe:swipeBackView="#+id/back"
swipe:swipeCloseAllItemsWhenMoveList="true"
swipe:swipeFrontView="#+id/front"
swipe:swipeMode="both" />
</LinearLayout>
however when i run this listview, it shows me the background over the front(i want that only when i swipe the front it will show me the back)
i want that the delete and update button will show only when swipe the front..
how can i solve it? thanks alot
In your row-layout you should have the LinearLayout before RelativeLayout
It's important the order of elements
<LinearLayout>
.... buttons ....
</LinearLayout>
<RelativeLayout>
.... image and text ....
</RelativeLayout>

XML layout issue

I want to expand my XML layout as I have a number of buttons I need to put on one screen. I have added a scrollview under my textview and image view but cant seem to get the layout to expand? I'm also getting an XML error layout on my scrollview of:
error: Error parsing XML: mismatched tag
Here's my layout showing the expansion issue. Can anyone gibe me an Idea of what I'm doing wrong?:
Heres my XML - Im aware i have duplicate information. This is a test.:
<?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="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="84dp"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imgLink"
android:layout_width="60dp"
android:layout_height="match_parent"
android:layout_gravity="center_vertical"
android:layout_weight="0.91"
android:src="#drawable/menulist" />
<TextView
android:id="#+id/textView1"
android:layout_width="165dp"
android:layout_height="match_parent"
android:layout_weight="0.14"
android:gravity="center"
android:text="Menu"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textSize="40sp" />
</LinearLayout>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<Button
android:id="#+id/btnAdd"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/addconmenu"
android:src="#drawable/addconmenu"
android:text="Contacts List" />
<Button
android:id="#+id/btnView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/todo"
android:src="#drawable/todo"
android:text="&apos;To Do&apos; List" />
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/musicmenu"
android:src="#drawable/musicmenu"
android:text="Music List" />
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/shoppingmenu"
android:src="#drawable/shoppingmenu"
android:text="Shopping List" />
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/gymmenu"
android:src="#drawable/gymmenu"
android:text="Gym List" />
<Button
android:id="#+id/btnEdit"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="20dp"
android:drawableLeft="#drawable/appointmentmenu"
android:src="#drawable/appointmentmenu"
android:text="Appointment List" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It looks like you should reverse the order of the last two lines of your layout. You are closing the ScrollView tag after closing the top-level LinearLayout tag.
The Linear Layout height inside of your ScrollView is wrapcontent if you make it match parent or fillparent it will solve your problem.

Categories

Resources