Button overlaps the textview - java

I am just beginning android programming. Facing an error.
Problem is that the button covers the text in the textview.
I tried to put android:orientation="vertical".........but this android:orientation is not present in the xml...i mean it is not showing in the methods suggested.........Please help me hot to resolve the problem.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
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=".StartingPoint" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world"
android:textSize="45sp"
android:gravity="center"
/>
<Button
android:layout_width="250dp"
android:layout_height="wrap_content"
android:text="Add one" />

This is because you are using a relative layout and haven't specified what the button is relative to. I would recommend changing to a different layout (ie linear layout). There is a good tutorial here: http://developer.android.com/guide/topics/ui/declaring-layout.html

Related

What is the right way to use two scrollviews in a single activity?

This is my current design in my main activity.
<androidx.constraintlayout.widget.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=".MainActivity">
<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" >
<TextView
android:id="#+id/txtSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Subestacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spSubestacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<TextView
android:id="#+id/txtClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Clasificacion"
android:textSize="18sp" />
<Spinner
android:id="#+id/spClasificacion"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:spinnerMode="dropdown" />
<ScrollView
android:id="#+id/scrollEventos"
android:layout_width="match_parent"
android:layout_height="350dp"
android:paddingTop="30dp"
android:paddingBottom="10dp">
<LinearLayout
android:id="#+id/linearEventos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" />
</ScrollView>
<Button
android:id="#+id/btnRegistros"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Actualizar Registros" />
</LinearLayout>
</ScrollView>
I have a second scrollview inside the first one because this one is filled with multiple labels created from code. These labels can take up a lot of space, so a scrollview is suitable for proper navigation.
The first scrollview that contains everything is so that when changing the screen in landscape mode, you can continue browsing and access all the controls that are displayed.
The problem is that when changing the screen in landscape mode, only the first scrollview works, which allows you to see all the controls correctly. However, the second scrollview showing the records cannot be moved, and therefore the records are cut.
Is there a way to freeze the first scrollview when the user wants to slide the records and allow the second scrollview to be moved?
Or is it that I should create my design differently?
Note: Everything works correctly with the screen in portrait mode. All the controls are accessible at first instance and the second scrollview can be slid correctly allowing to see the records.
I've tried working with the layout_weight property but it didn't work for me or I just didn't know how to use it.
Use this:
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
<!-- Other Layout or scrollView-->
</android.support.v4.widget.NestedScrollView>
Android site: NestedScroll

How can I get a ZXing scanner in a section of a page?

I'm currently using this example source code to get a little bit familiar with ZXing. As of right now, the main screen looks like this:
When you press the 'Scan' button, it opens up the scanner in fullscreen and locks the orientation to landscape. Now instead of the scanner opening up fullscreen, I want it to open up only in a portion of the main screen. In other words, the scanner should only be inside the red box:
I've slightly altered the layout of the main screen, so that the fragment appears below the button:
<LinearLayout 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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin" tools:context=".HomeActivity"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal">
<TextView
android:text="#string/hello_world"
android:id="#+id/tv_welcome"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"/>
<TextView
android:text="#string/blog_url"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/tv_blog_url"
android:layout_marginBottom="#dimen/activity_vertical_margin"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/btn_scan_now"
android:onClick="scanNow"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:id="#+id/btn_scan_now" />
<TextView
android:id="#+id/scan_format"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:gravity="center_horizontal"
android:layout_marginBottom="#dimen/activity_vertical_margin" />
<TextView
android:id="#+id/scan_content"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:gravity="center_horizontal" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scan_fragment"
android:orientation="horizontal"></LinearLayout>
</LinearLayout>
In the first image below, you'll see what I'm trying to achieve in my own app. The second is what I have so far. The black section is where I want the scanner to go. Currently all that is, is a linear layout reserved for the fragment:
But this is where I've come to a stand still. I'm not sure how I can prevent the scanner from going fullscreen and landscape as well as locking it down to a specific area of a page. Can anyone provide some guidance on how to achieve this?
Thanks for any help in advance.

Android : Text in ForeGround of a FrameLayout (contain a googleMap)

Hello everyone
To start, sorry for my bad english
I first put you in position:
I'm using Java code for Android, in Eclipse.
My application needs to show a map (Google Maps V2) in a fragment to show the runner location. The map display is already achieved.
Here is my problem: I use a FrameLayout element, which contains my map and is therefore the whole page, however, my goal is to display over the map a text box centered at the bottom. I tried to use in the Foreground property option to set my text, but it does not work (application crash when arrived on the page of the map).
Here is my code fragment_carte:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/mapFrameLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:foreground="#id/txtViewCoureur"
tools:context=".CarteFragment" >
</FrameLayout>
I can't post an image to show you what I want because it's my first post
I tried many solutions on this forum before asking my question
Thank you for your help!
Enclose the map inside a RelativeLayout and the rest of the controls in a FrameLayout. Here is an example of one app I have:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<fragment
android:id="#+id/mapview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
class="MapFragment" />
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="#dimen/activity_vertical_margin"
android:layout_marginLeft="#dimen/activity_horizontal_margin"
android:layout_marginRight="#dimen/activity_horizontal_margin"
android:layout_marginTop="#dimen/activity_vertical_margin" >
<EditText
android:id="#+id/mapsearchbox"
android:layout_width="match_parent"
android:layout_height="40dp"
android:alpha="0.8"
android:background="#android:color/white"
android:hint="#string/map_search"
android:imeOptions="actionSearch"
android:inputType="text"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="30dp"
android:singleLine="true"
android:textColor="#android:color/black" >
</EditText>
<Button
android:id="#+id/mapsearchbtn
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="right|center_vertical"
android:layout_margin="8dp"
android:background="#android:drawable/ic_menu_close_clear_cancel" />
</FrameLayout>
</RelativeLayout>

android:gravity acting strangely

I am new to android and have written a sample code
<LinearLayout 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:padding="5dp"
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"
android:orientation="horizontal"
>
<TextView
android:text="sample1"
android:id="#+id/textView1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:gravity="right"
android:textStyle="bold"
android:layout_weight="4"
/>
<TextView
android:text="sample2"
android:id="#+id/textView2"
android:layout_width="0dp"
android:layout_height="fill_parent"
android:background="#F90"
android:textColor="#F09"
android:layout_weight="5"
android:gravity="center"
/>
<TextView
android:text="sample3"
android:id="#+id/textView3"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#F09"
android:layout_weight="3"
/>
<Button
android:text="Press"
android:id="#+id/button1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textColor="#F09"
android:layout_weight="4"
/>
</LinearLayout>
In textView2 id i changed layout_width to fill parent so that android:gravity attribute could be tested but when i set it to some value like center,bottom or top all other views also behave the same way?
When you use gravity its like real gravity, another planet inserted into the solar system and things go round different. If you have 2 items with gravity 1 they both are the same gravity they both take the same room. If another item enters the same system it depends how it compares to the gravity of whats there already as to what happens. Match parent expands to meet the outside. Wrap content shrink wraps the widget. I hope this explains it good enough in a non-technical kind of way.
Try this:
1. Set a Layout_gravity on your main layout
2. Then set gravities for each of the child components
Check this code (with only gravity attributes included):
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_gravity="center"
>
<TextView
android:text="sample1"
android:id="#+id/textView1"
android:gravity="left"
/>
<TextView
android:text="sample2"
android:id="#+id/textView2"
android:gravity="right"
/>
...
</LinearLayout>

Create gallery with elements floating side by side or table cells

Friends I'm new to Android and I'm facing all those beginner's problems such as overload of information in the first steps of the learning proccess.
I want to develop an activity where its content resembles a picture gallery, with 2 pics per row. With HTML + CSS I would create divs with float:left and half of the parent element's width. Or maybe creating a table with two cells per row. Wouldnt do that in HTML for a photo gallery but I know it works.
I want to do this programmatically and Ive seen solutions with TableLayout and RelativeLayout. But i dont understand how to create a container such as DIV in HTML for the data (will be a title and an image) i want to place in the layout. And i dont understand how to make every two of them side by side.
How would you do that in terms of layout and programming?
I will update this thread with an image to a better understanding of my problem later.
Use below XML code as template to inflate in your activity and then add to ListView
In Main Activity inflate view by LayoutInflater:
LayoutInflater inflater = getActivity() .getLayoutInflater();
View view = inflater.inflate(R.layout.my_stats_layout, null);
Use Above view as item in ListView - Use this tutorial for custom ListView
http://androidexample.com/How_To_Create_A_Custom_Listview_-_Android_Example/index.php?view=article_discription&aid=67&aaid=92
XML Layout:
<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" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/img1" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:src="#drawable/img2" />
</RelativeLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Img1" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Img2"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>

Categories