Guys I am trying to make a button that gives me two outputs but that also generates a plain text view, is this possible?
My buttons are
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Food"
android:id="@+id/button"
android:clickable="false" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/Food2"
android:id="@+id/button2" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="@+id/textView" />
and the value for each sting
<string name="Food">12</string>
<string name="Food2">15</string>
Well I wanted the each button to display a name in a plain text view when I click on it and its value but I seem to fail every time I do it... Can anyone help?
Where is the code that handles the buttons click events?
Related
Hi,
I am new to Android App Development and am having a bit of bother getting my screen layout the way I want it. I will do my best to explain the problem. I am doing a small app just to get used to Android Studio. I am following a tutorial to go through most of the features. I have inserted a TextView on the the screen firstly, in a root LinearLayout. I have then created an embedded LinearLayout with a button and an image. I will post the code below for this. In the tutorial it shows the TextView at the top of the screen and the Button and Image directly below it. My problem is that I can't move my Button and image(embedded LinearLayout) directly below. It has to be at the right hand side of the TextView.
The only way I can think on explaining a solution would be in Microsoft Word, no Text Wrapping allows you to drag the image anywhere on the page. Is there an option for something similar in Android Studio or am I missing something I should know? Any help would be appreciated. If I haven't explained in enough detail please let me know and I will do my best to correct.
This is the tutorial page(as a new member I can't post url):
www(dot)raywenderlich(dot)com/78576/android-tutorial-for-beginners-part-2
This is my code:
<LinearLayout (Cant post xml version url)
android:layout_width="wrap_content"
android:layout_height="500dp"
tools:context=".MainActivity"
androidrientation="horizontal"
android:id="@+id/">
<TextView android:id="@+id/main_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="20dp"
android:layout_marginTop="10dp"
android:text="@string/textview" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
androidrientation="horizontal"
android:id="@+id/">
<Button
android:id="@+id/main_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:text="@string/button" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:src="@mipmap/ic_launcher"/>
</LinearLayout>
<!-- Displays keyboard when touched -->
<EditText
android:id="@+id/main_edittext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:layout_marginLeft="20dp"
android:layout_gravity="center_vertical"
android:hint="@string/hint" />
<!-- Set OnClickListener to trigger results when pressed -->
</LinearLayout>
I am trying to make the buttons side by side but they would eather go above or under. Can anyone help? I am learning slowly so sorry for newb question
You can put them in an horizontal linear layout or use a relative layout.
Do what dancer_69 said and here is a sample you can copy/paste into your project...
Code:
<LinearLayout
android:orientation="horizontal"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="@+id/button4" />
</LinearLayout>
I can't figure out how to reduce the width of the combobox/spinner so that the button fits within the screen.
Changing the layout_width property of the spinner has no effect.
Sorry but the system is preventing me from posting an image of my layout so I will have to just leave it out and post the code.
www_gregsindigenouslandscapes_com_au/temp/App.jpg
Code:
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=".HexapodControls"
android:id="@+id/frameHexapodController"
android:nestedScrollingEnabled="false">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a bluetooth device..."
android:id="@+id/textBluetooth"
android:layout_gravity="left|top"
android:textSize="25dp"
android:typeface="sans"
android:textStyle="bold"
android:layout_column="0"
android:span="2" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerBluetooth"
android:spinnerMode="dropdown"
android:clickable="true"
android:contentDescription="Select a bluetooth device to connect to..."
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_column="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect..."
android:id="@+id/buttonBluetooth"
android:layout_column="1" />
</TableRow>
boylesg said:
I can't figure out how to reduce the width of the combobox/spinner so that the button fits within the screen.
Changing the layout_width property of the spinner has no effect.
Sorry but the system is preventing me from posting an image of my layout so I will have to just leave it out and post the code.
www_gregsindigenouslandscapes_com_au/temp/App.jpg
Code:
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=".HexapodControls"
android:id="@+id/frameHexapodController"
android:nestedScrollingEnabled="false">
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select a bluetooth device..."
android:id="@+id/textBluetooth"
android:layout_gravity="left|top"
android:textSize="25dp"
android:typeface="sans"
android:textStyle="bold"
android:layout_column="0"
android:span="2" />
</TableRow>
<TableRow
android:layout_width="match_parent"
android:layout_height="match_parent">
<Spinner
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/spinnerBluetooth"
android:spinnerMode="dropdown"
android:clickable="true"
android:contentDescription="Select a bluetooth device to connect to..."
android:focusable="true"
android:focusableInTouchMode="true"
android:layout_column="0" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Connect..."
android:id="@+id/buttonBluetooth"
android:layout_column="1" />
</TableRow>
Click to expand...
Click to collapse
Simpy set android:layout_width="wrap_content" to android:layout_width="xxxdp" xxx=> alias and similar xxx pixels!
The contents of my layout file is below.
Everything is good except that the control "android:id="@+id/text_bluetooth"" is superimposed over the top of the app heading and laucher icon.
I can't figure out how to move all the controls down below the app heading while retaining their same overall arrangement.
Code:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#0099cc"
tools:context=".HexapodControls"
android:id="@+id/frame_controls"
android:visibility="visible">
<!-- The primary full-screen view. This can be replaced with whatever view
is needed to present your content, e.g. VideoView, SurfaceView,
TextureView, etc. -->
<!-- This FrameLayout insets its children based on system windows using
android:fitsSystemWindows. -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:visibility="visible"
android:id="@+id/layout_controls">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="@string/str_select"
android:id="@+id/text_bluetooth"
android:visibility="visible"
android:layout_gravity="top|left"
android:autoText="false"
android:typeface="normal"
android:textSize="18dp" />
<Spinner
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/spinner_bluetooth"
android:visibility="visible"
android:clickable="true"
android:layout_gravity="top|left" />
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/layout_bluetooth"
android:keepScreenOn="true">
<Button
android:layout_width="160dp"
android:layout_height="match_parent"
android:text="@string/str_connect"
android:id="@+id/button_connectbt"
android:clickable="true"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="false"
android:gravity="center_vertical|right"
android:drawableLeft="@mipmap/ic_bluetooth"
android:layout_gravity="center_horizontal|top"
android:onClick="@string/str_onclick_connectbt"
android:visibility="visible"
android:textSize="18dp" />
<Button
android:layout_width="160dp"
android:layout_height="match_parent"
android:text="@string/str_disconnect"
android:id="@+id/button_disconnectbt"
android:enabled="false"
android:focusable="false"
android:focusableInTouchMode="true"
android:clickable="true"
android:gravity="center_vertical|right"
android:drawableLeft="@mipmap/ic_bluetooth"
android:onClick="@string/str_onclick_disconnectbt"
android:visibility="visible"
android:layout_gravity="top"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
</FrameLayout>
I think you must use :
android:layout_marginTop="xxxdp"
Try it.
Bye
cristaccio85 said:
I think you must use :
android:layout_marginTop="xxxdp"
Try it.
Bye
Click to expand...
Click to collapse
Thanks - that worked a treat.
Nothing. It0s very simply for managing layout but very hard managing listview ....
Hey, everyone
I'm a beginner trying to make my first app (a calculator) look fancier.
I've started with by adding colors to the buttons. Quickly I've noticed that by adding color to a button's background it looses its border with another button. So I've tried to use margins. Everything went great until I had to deal with the last LinearLayout, last line of the calculator buttons. It's different than the previous LinearLayouts and I guess the picture describes it well enough:
Turns out I cannot post the picture, so I'll try to describe it as detailed as possible: the thing is that my 3rd row includes 4 buttons(1, 2 ,3 and -) and so does the 4th, but there 2 out of 4 buttons are split vertically and I guess because of that the margins of the forth row do not allign with the ones from the 3rd even though their properties are the same (android:layout_marginVertical="3dp" and android:layout_marginHorizontal="5dp")
The buttons of the 3rd row do not allign with the ones from 4th
I've tried to manually make margins( to the left and right insted of "Horzontal"), however that does not seem to be a proper soliution, or maybe I'm just missing something
my code for the 4th row :
HTML:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:orientation="vertical">
<Button
android:id="@+id/dot"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:background="#D79E2D"
android:gravity="top|center"
android:text="."
android:textSize="30sp" />
<Button
android:id="@+id/equals"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:background="#D79E2D"
android:text="="
android:textSize="40sp" />
</LinearLayout>
<Button
android:background="#D79E2D"
android:id="@+id/n0"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:text="0"
android:textSize="25sp" />
<LinearLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:orientation="vertical">
<Button
android:background="#D79E2D"
android:id="@+id/changeSign"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:text="+/-"
android:textSize="30dp"
/>
<Button
android:background="#D79E2D"
android:id="@+id/Back"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:text="Del"
android:textSize="30sp"
android:gravity="top|center"
/>
</LinearLayout>
<Button
android:id="@+id/plus"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#D79E2D"
android:layout_marginVertical="3dp"
android:layout_marginHorizontal="5dp"
android:text="+"
android:textSize="25sp" />
</LinearLayout>
If you have an idea how to overcome this, please let me know. I'd really appreciate it.