How to use layouts in .xml files : Android application - android

How can I use any layout in .xml files. Let I want to use Tablelayout, so I dragged it on Graphical Layout of an xml file, and then I want that layout should be in two columns ( if I want add "Name : " in first column and "EditText" in another column.) but I am anable to do it.I am using Graphical layout. I want do like
Name : EditText (A textbox for Name entry)
Age : EditText (A textbox for Age entry)
Button (to save user entry)
How to design it.

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:isScrollContainer="true"
>
<EditText
android:id="#+id/et_name"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:visibility="visible"
android:maxLines="1"
android:nextFocusDown="#+id/mark"/>
<EditText
android:id="#+id/et_age"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:inputType="numberDecimal"
android:visibility="visible"
android:maxLength="3"
android:maxLines="1"/>
<Button
android:id="#+id/btn_save"
android:text="#string/save"
android:layout_width="fill_parent"
android:onClick="onButtonClick"
android:layout_height="wrap_content"
android:enabled="false" />
</LinearLayout>
Put this into your XML
There you go ;)

I would consider the same as ehudokai. If you are roughly familiar with XML, then try to use the graphical layout just to control if everything is working es expected.
In case you don't know how to create what you need in XML drag it to somewhere and cut & paste the code to the correct possition.
I think the graphical layout is quite good for editing the properties, so you see directly what you are changing.

try out droiddraw...here is d link..
http://www.droiddraw.org/

Related

Cannot move the text box on Android Layout page

I am trying to design the layout of my Android project, but I must have done something wrong. Now when I try to drag the text boxes around, they just would not move on the screen. I am guessing it is one of the format setting, but not sure which one it is.
This is the XML file of how it looks:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.oldimagerevieal.MainActivity"
tools:ignore="MergeRootFrame" >
<TextView
android:id="#+id/textLat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/textLong"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</FrameLayout>
FrameLayouts use gravity to position items on a screen using the
android:layout_gravity="left|top" //whatever you want.
Unless you need the FrameLayout, you can change the root element to a RelativeLayout or LinearLayout (don't forget to specify orientation), and you should be able to drag things around like normal.
I would high recommend getting familiar with building views in XML. It will significantly help you.
Enable "Autoconnection to parent" by clicking on the magnet button in the design tools section.

Editbox Hint - Always show hint

I have a textbox with a hint but I want the hint to always be shown, even when there is an input in the TB. Example is the "To" field in the Gmail app.
There are 3 approaches you could use (spoiler - use number 3), since as mentioned in my comment, in the Gmail example , it is not an actual hint:
Using a Linear Layout, getting a cleaner look in my opinion:
<LinearLayout android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:layout_margin="5dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
</LinearLayout>
Using a Relative Layout, getting a result that mimics the Gmail App:
Note: might be problematic since the text will be displayed on top of the hint, see solution 3.
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:text="#string/Hint"
android:paddingLeft="10dp"
android:layout_marginBottom="0dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:layout_height="wrap_content"/>
Result are as shown in this image:
Edit:
using a Drawable:
This seems a better solution (I personally just created it from snipping the 1:1 display of the TextView, will be in correct measurements this way), this will allow a cleaner layout code, and the text will be aware of the drawable:
<RelativeLayout android:layout_marginTop="10dp"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
>
<EditText android:layout_width="fill_parent"
android:inputType="text"
android:gravity="top"
android:drawableLeft="#drawable/Hint"
android:layout_height="wrap_content"/>
The first thing that came to mind is to insert two editText layout relative layout.
First over second.at the top of that - put background as
android.R.color.transparent
Work with top elevent of relative layout! In item below - set hint(that you want) and try to put text. May be you will have specify padding of first element of top to greate display.
Oh. And one more thing.
Maybe put to editText background background image - but is the bad to scalability.

RelativeLayout crashes, LinearLayout works fine! WHY?

I'm new to Android programming (although I'm an expert in C and intermediate in Java).
I don't understand why my app crashes when I try to setContentView() with the default RelativeLayout that Eclipse automatically generates when I create a new project.
It crashes both in Emulater and on my phone.
But if I change the layout to a LinearLayout it works perfectly fine.
Can anybody give me a clear explanation to why this hapens?
Thanks
Firstly let me explain you about layouts and follow the given example:
Now let’s turn our attention to those helpful layout controls that organize other controls. The most commonly used layout classes are:
FrameLayout – designed to display a stack of child View controls. Multiple view controls can be added to this layout. This can be used to show multiple controls within the same screen space.
LinearLayout – designed to display child View controls in a single row or column. This is a very handy layout method for creating forms.
RelativeLayout – designed to display child View controls in relation to each other. For instance, you can set a control to be positioned “above” or “below” or “to the left of” or “to the right of” another control, referred to by its unique identifier. You can also align child View controls relative to the parent edges.
TableLayout – designed to organize child View controls into rows and columns. Individual View controls are added within each row of the table using a TableRow layout View (which is basically a horizontally oriented LinearLayout) for each row of the table.
try the following code for relative layout:
<?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"
android:layout_gravity="center"
android:orientation="vertical" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/textMsg"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="14dp" />
</LinearLayout>
<Button
android:id="#+id/btn2"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Contacts"
android:textColor="#342D7E" />
<Button
android:id="#+id/btn1"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Send"
android:textColor="#342D7E" />
<EditText
android:id="#+id/edittext1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/btn2"
android:layout_alignParentLeft="true"
android:ems="10"
android:hint="Type a Message"
android:maxLines="5" >
<requestFocus />
</EditText>
</RelativeLayout>
let me know if it crashes again
I just had the same problem.
How I fixed it:
After changing the layout in main.xml I had the layout declared in the mainActivity.java file still as a LinearLayout.
Go into your mainActivity.java file and change it to relatively out.
Check yours it might solve it

Clickable Button (or any View) inside EditText in android

I want to have a Button or a clickable View in my EditText so that I can perform some action on click of it. I was able to put a drawable inside my EditText thanks to Marcosbeirigo for this answer. But, now I want to make it clickable so that I can perform some action on it. I know this is possible as HTC uses buttons inside EditText in their stock sms app as shown in the following image-
In my case, the button will be positioned anywhere, can be in the center also. But the main thing is how can I put a button in EditText?
Use RelativeLayout. The Send and Attach buttons are simple Android Buttons and the 32/160 is a TextView. Put the buttons and the textview on the EditText object, play with the layout arrangments and set some right padding to the EditText object so that the text inside it won't go under the buttons.
You don't need any drawable and listening to the click event of the android buttons is not a question anymore.
Its absolutely correct
Use this , It worked for me -
<RelativeLayout android:layout_width="match_parent"
android:layout_height="wrap_content">
<EditText android:id="#+id/id_search_EditText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:paddingRight="40dp"
android:hint="Enter your feelings and connect" />
<ImageButton android:id="#+id/id_search_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/id_search_EditText"
android:layout_alignBottom="#+id/id_search_EditText"
android:layout_alignRight="#+id/id_search_EditText"
android:background="#drawable/ic_launcher"/>
</RelativeLayout>
I think you try to search set click event for compound drawable. You can find some solutions here
handling-click-events-on-a-drawable-within-an-edittext
there is no button inside editText It's just an editText with white background so you don't see it's margins and a simple layout arrangement.
The only possible solution is to use a RelativeLayout that allow you to put also Views in overlay to others. Other Layout wont allow you to do such things.
Also I found this tutorial: http://developer.android.com/resources/articles/layout-tricks-merge.html maybe it can helps you :)
Use this this code may work.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/REFReLayTellFriend"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:paddingBottom="5dp"
android:paddingTop="5dp">
<EditText
android:id="#+id/txtSearch"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/editext_rounded"
android:drawableLeft="#android:drawable/ic_menu_search"
android:gravity="start"
android:hint="Search"
android:singleLine="true"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/txtSearch"
android:background="#drawable/ic_action_content_filter_list"/>
</RelativeLayout>
</LinearLayout>

Textview placing each character on newline

I want to place each character on new line.
I am using following xml.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<TextView
android:layout_width="12dip"
android:layout_marginLeft="12dip"
android:textSize="12dip"
android:layout_height="fill_parent"
android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"/>
</LinearLayout>
but I am getting output as follow(On some line 2 character are coming)
.
I know that I can use \n but anybody have better option.
Here's kind of a hack that'll work:
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:ems="1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:typeface="monospace"
android:gravity="center_horizontal"
android:text="ABCDEFGHIJKLMNOPQRSTUVWXYZ"
android:textColor="#android:color/white"
/>
This relies on the monospace typeface, where all characters have the same width.
The reason your getting 2 chars on each line is due to the static width of the view. I really wouldn't approach this problem with a single view with a static width.
There are a lot of solutions to this, but based on the info you gave, I would probably create a single textview layout like you've done and dynamically inflate an array of textviews via code. AKA each char in your string gets assigned to it's own textview.

Categories

Resources