How to modify ID of controls in relative layout? - android-layout

I am new to Android and working with Relative Layout. Now I am trying to change the ID of controls but as soon as I change the ID, other controls get re-positioned as they are relative to each other. Is their any way to change ID of controls without re-positioning them?

Because every object in your relative layout is arranged base on object ID. Once you changed one of the object ID, you will need to change its object ID on the other side as well. If not the layout will be pretty messed up and need to rearrange.
The best way to avoid this is to declare every object ID before you proceed.
My advice is that you changed the object ID one by one. Change all the field of the object ID to the latest.
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/textView2"
android:layout_below="#+id/login_email"
android:layout_marginTop="46dp"
android:text="#string/password_text" />
<EditText
android:id="#+id/login_password"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
android:layout_alignRight="#+id/login_email"
android:layout_toRightOf="#+id/textView3"
android:ems="10"
android:password="true"
android:singleLine="true" />
<Button
android:id="#+id/register_button"
android:layout_width="100dip"
android:layout_height="40dip"
android:layout_alignRight="#+id/login_password"
android:layout_below="#+id/login_password"
android:text="#string/signup_text" />
For example, based on the code above, once you change the TextView id from #+id/textView3 to #+id/password_text. You would need to change one by one in those object that contains the of #+id/textView3 to #+id/password_text. Which is in
<EditText
android:layout_alignBaseline="#+id/textView3"
android:layout_alignBottom="#+id/textView3"
By the way try tag Android in your question as well. More people will notice your question that way.

Related

Is there a good way to make a button in Android do different things depending on if you click on the left half or right half of the button?

In an android app I am making, I would like to make a button that does different things depending on whether the user presses the left half of the button or the right half of the button. Right now I'm trying to figure out what would be the best way to accomplish this.
Some other specific requirements:
1. I'm planning on using the button 1 to 4 times per Activity.
2. It would be very helpful if I could be able to rotate the button (e.g. 90 degrees, 180 degrees so it's upside-down)
One idea I have had is putting two buttons side by side and putting a text-view on the top to make it look like one button. I found this doesn't really work well. It requires lots of effort to get it to show up properly and gets messed up when even small changes are made.
Another idea I had was making a custom button by extending the view class. Problem is I have no experience doing something like that and most of the tutorials I've seen use it to make paint programs.
What would be the best way to create something like this??
Edit: When I say rotate the button, I don't mean that the button needs to rotate when clicked or when some other action is performed. Just that it is facing in the direction I need it to be when the app loads. Also it only needs to be facing in the standard 4 directions (i.e. Down, Up, Left, Right). Sorry I wasn't more clear on that.
i created the first approach using framelayout as follow:
<FrameLayout
android:id="#+id/frame"
android:layout_width="match_parent"
android:layout_height="70dp"
android:onClick="rotate">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
app:layout_constraintBottom_toTopOf="#+id/view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/imageView2">
<Button
android:id="#+id/button"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorPrimary" />
<Button
android:id="#+id/button2"
style="#style/Widget.AppCompat.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#color/colorAccent" />
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:gravity="center"
android:text="Button text"
android:textAppearance="#style/TextAppearance.AppCompat.Display1"
android:textColor="#android:color/white" />
</FrameLayout>
for rotating any view create your anim.xml file inside anim folder in res
right click on res -> new android res dir-> choose anim then create anim file right click on anim folder you just created and new anim res file and the past the code in side it , this will rotate your framelayout in this certain code by 180 degeree
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:fillAfter="true">
<rotate
android:duration="500"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="180" />
</set>
use it as follow on kotlin
fun rotate(view: View) {
val rotate = AnimationUtils.loadAnimation(
applicationContext,
R.anim.rotate
)
frame.startAnimation(rotate)
}

i cant add button to linear layout

I have a listActivity that displays via an adapter an xml feed fetched from the web, adn the layout file activity_list_feed.xml :
`<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>`
In the graphic editor i cannot drag a button into this layout, and when i try to hardcode as per this file :
`<?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" >
<Button
android:id="#+id/addBtn"
android:text="Add New Item"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:onClick="addItems"/>
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:drawSelectorOnTop="false"
android:transcriptMode="normal"
/>
</LinearLayout>`
i get a compile error message :
java.lang.ClassCastException: android.widget.LinearLayout cannot be cast to android.widget.TextView
How can i add a button on top of the list because i want to refresh the pull.thank you.
The code that you provided actually works in its current state in Android Studio:
Make sure that in your java code, you are referencing the correct item. Make sure that you're setting the overall layout to be that layout, and THEN doing findViewById(R.layout.addBtn).
Also, try changing the Android Version in your IDE to 22 (as I have it set in the top right corner of the picture). That may solve your error.
I managed to add the button in the editor and this without setting to API 22 as per your picture. However the program didn't compile still.
The message error "ArrayAdapter requires the resource ID to be a TextView" meant i didn't provide the right argument to the adapter.
According to this answer :
"ArrayAdapter requires the resource ID to be a TextView" xml problems
it appears that the choice of the constructor is important, since i wanted to add a button in the view, i must use the constructor with 4 arguments (the additional argument being the id of the view):
http://developer.android.com/reference/android/widget/ArrayAdapter.html#ArrayAdapter(android.content.Context,int,int,java.util.List)
Using this constructor solved the problem.

Android - ListViews inside tableLayout

I'm trying to build and app that shows organized data so I'm thinking that basically a TableLayout would be a good idea, just to keep them in rows, so I'm wondering whats the correct way to do it? I have this in my xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TableLayout
android:id="#+id/tableLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TableRow>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="#string/hello" >
</TextView>
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="233dp"
android:layout_marginTop="44dp" >
</ListView>
</TableRow>
</TableLayout>
</LinearLayout>
then it's just that the TableLayout tag and TableRow tag displays a warning that says:
"This TableLayout layout or its LinearLayout parent is possibly useless"
so what I understand is that is not picking up the TableLayout. how can I fix this? is there another way to do this easly?
so what i understand is that is not picking up the tablelayout. how can i fix this? is there another way to do this easly?
Not quite, the renderer is taking account of the TableLayout but because the lint tool has detected that you have two views when you only need one (A LinearLayout and TableLayout) it is warning you that you should remove one of these layouts as it will boost performance while not effecting the functionality of the page.
I will add, you have a TableLayout with just a single row. TableLayout's are designed to allow their contents to format their columns based upon all of their rows. With a single Row the TableLayout & Row together are acting as a LinearLayout:
<LinearLayout>
<LinearLayout>
<ListView>
<TextView>
Since your TableLayout isn't adding any addition layout in that case it becomes obsolete and you will gain a resource boost from removing it and changing the orientation of LinearLayout to horizontal.
If you're planning to add more TableRow's later, then you should instead remove the LinearLayout for the same reason - it will then become the view which is not adding extra information: your table layout will be entirely responsible for the layout so you might as well make it the parent of the layout file (remember to add xmlns:android="http://schemas.android.com/apk/res/android" attribute to it if you do.)
In general I've seen the stronger warning:
This LinearLayout layout or its RelativeLayout parent is useless
when this is not the case. For example, this can happen if I've nested a linear layout inside a relative layout. The relative layout positions the linear layout exactly where I want it, the linear layout takes up space. Both are non-useless uses to me.
1) it says possibly so avoid drawing conclusions, trust yourself young padawan! But yes, the parent looks useless to me too :)
2) Using Table layout outside of your ListView wont change the rows' layout in the list, in case this is what you want to achieve.
You might have seen this but the Developers page offers a really good tutorial that can help you create a good base for your ListView (this example uses a ListActivity). Then on you can modify the rows' layouts using TableLayout ...etc.

Android text input - field name shown in field itself

I am trying to achieve a log-in screen like the following:
Where the field's name is displayed in the field itself. I am using the ADT plugin in Eclipse. I tried setting the android:text attribute of the text input, but I don't think this is correct because through the password field, only the dots show (the redacted text), and not the text itself. What is the attribute then. I have looked in the documentation for the EditText widget.
The XML I currently have for the two EditText fields are:
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/editText1" android:inputType="textEmailAddress" android:text="#string/username">
<requestFocus></requestFocus>
</EditText>
<EditText android:layout_width="fill_parent" android:layout_height="wrap_content" android:id="#+id/editText2" android:inputType="textPassword" android:text="#string/password" ></EditText>
You should be able to set default values for the fields with android:hint or android:sethint I think its the first.
android:hint="Please enter your password"
its the first one, thanks to a search on SO. Just for additional info it came up with this.
SO Question about android:hint
Share Java code for when click on Submit Button then Show the Name
<EditText
android:id="#+id/name_enter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="#string/Name"
android:inputType="textCapSentences|textAutoCorrect"
/>

Where is a reference to the android XML UI layout?

I am looking for a spec or reference of all the possible options for the various XML layout attribute settings that typically come with an android UI. Google seem to be good at burying it. This is similar to this question but remains in-effectively answered.
Such as what are my options available to me for the TextView layout_width definition ? There must be a complete definition published ... somehwere....
layout_* attributes aren't directly part of the view they appear on, which is why you won't find them in TextView's documentation. (TextView is not a ViewGroup.) They are arguments to the parent view, also known as LayoutParams. Take a look at the "Known Subclasses" sections at the top of the linked page for a list of them. They're instructions about how a ViewGroup should arrange each child view, and each parent type can recognize different ones depending on what kinds of layout options it supports.
For example, LinearLayout.LayoutParams supports the android:layout_weight parameter. Children of a LinearLayout can specify weight to request a proportion of the remaining space after all children have been measured. You can give equal weight to two sibling TextViews with a base width of 0 to give them each half of the available space within the parent.
<LinearLayout android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="Hello" />
<TextView android:layout_width="0dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="World" />
</LinearLayout>
Normally developer.android.com is your site. Maybe this helps:
http://developer.android.com/reference/android/view/View.html
If you use Eclipse, then the autocomplete suggestions may help you as well in adding the right parameter.
...and the options you have for layout_width are
wrap_content (as large as the content of the View)
fill_parent (extends to the whole size - width or height - of its parent)
Layout parameters are pretty well described in the documentation for ViewGroup.LayoutParams and its subclasses. For the truly strong of heart, you can always browse the source for attr.xml.

Resources