I have a Button that I am using 13 times in my Android application's main.xml file. I would like to have the XML for it defined once, so that I can make changes in one place instead of 13. Each of the 13 instances needs to have its own ID, though. What should I do to simplify my XML? I've tried using <include> but it hasn't worked for me. I must have been doing something wrong. I'd appreciate it if anyone could show me how to do it correctly. Thanks.
Here's the XML for the button that I'd like to reuse:
<Button
android:width="70dp"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp" />
You should use Styles and Themes for that sort of things.
Related
So I would like to see my layout preview with the fields filled with something like default placeholders but if I use bindings the settext attribute is already used and the fields are showing empty since there is no info from the models yet.
<TextView
android:id="#+id/tv_user_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="5dp"
android:gravity="center"
**android:text="#{showBlueportSpotViewModel.name}"**
android:textAllCaps="true"
android:textSize="20sp"
android:textStyle="bold"/>
I tried this:
android:text="#{showBlueportSpotViewModel.name ?? #string/blueport_placeholder_name}"
but I still see the view empty.
Do you guys any workaround? I guess once a workaround is found, it can be used to ImageView and src for example and etc..
Thank you!
You can use the tools attribute to define properties that will appear in the layout preview but will not appear when you run the app.
Add the following to your root view:
xmlns:tools="http://schemas.android.com/tools"
Then use the tools attribute to define text that will only appear in the layout preview:
tools:text="placeholder text"
The tools attribute is very useful when mocking up views in the editor. All of the tools attributes are stripped when the app is packaged. More information here: http://tools.android.com/tech-docs/tools-attributes
I found a workaround
I added
xmlns:bind="http://schemas.android.com/apk/res/android"
to the layout
and just having duplicated declarations in the view like:
android:text="#string/blueport_placeholder_name"
bind:text="#{showBlueportSpotViewModel.name}"
or
android:src="#{showBlueportSpotViewModel.blueportImageDrawable}"
bind:src="#drawable/android_menu_header"
I don't really know if this has secondary wrong consequences so I won't accept this answer until somebody can comment and say if it is okay.. Thanks!
I am a first time developer for Android, so you can say I've been learning as I was developing. For most of my code that doesn't have to do with the XML layout, I had no problem patching my rookie mistakes. With that said, my rookie mistakes has caught up to me in regards to two TextViews when I initially designed them with the GUI interface designer (my major rookie mistake).
My display_city tv and display_today_date tv seem to have a symbiotic relationship with each other. Removal of either one would crash the app. They seem so dependent on each other that changing each other's positioning is impossible (at least from the myriad of things I have tried such as setting layout margins).
<TextView
android:id="#+id/display_city"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dip"
android:layout_above="#+id/display_today_date"
android:layout_below="#+id/get_data"
android:layout_centerHorizontal="true"
android:gravity="center_horizontal" />
<TextView
android:id="#+id/display_today_date"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_below="#+id/display_pollen_type"/>
My question is - how do I simply position display_today_date immediately after my display_city? When I first started this Android app, I relied a lot on the GUI builder. That was my first rookie mistake, which resulted in this symbiotic relationship I explained.
Currently this is what my app looks like:
I have tried changing display_today_date's layout to android:layout_below="#+id/display_city. This results in a crash. I checked logcat, but it did not give me relevant information to the reason of the crash within the XML file.
P.S. get_data is my TextEdit box.
You already have the city to show above the date with the line android:layout_above="#+id/display_today_date". You can't have 2 views in a relative layout each reference the other, or it won't be able to figure out what to do. If you don't want to put the city above the date, delete that line then add the code to place it where you want.
You could use a LinearLayout with the orientation set to horizontal. That way there is no reference to another view. So if you delete one the other one won't cause the app to crash.
I think I might be doing something stupid but I cannot find the answer. Obviously ht ebelow thing doesn't work but how would I do it?
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="+#id/date"
android:text="+#id/heading"
android:text="+#id/subheading"
android:drawableLeft="+#id/featuredimage"
/>
I'm basically making an articlebutton to represent an article for a blog. I tried putting textviews inside but it miscounted the ending tags and threw an error.
What is that best way to create one of these buttons?
Dont use Button.
Use a Linear/Relative layout, with the attribute:
android:clickable="true"
Inside this layout add all your TextViews or any other thing you want.
I am new to Android development and Eclipse. I have been coding on ASP.Net and MS Visual Web Developer for years. In VWD, when you add a control to the design view, double clicking on it will automatically bring you to code view for the OnClick function of the control you have just created. You can also see the list of possible event handlers for a control from the design view.
But I can't seem to find this feature in Eclipse. Is there such a thing? I did a search on Google and the best I found is this (same question but without an answer).
http://www.techrepublic.com/forum/questions/101-341077/event-handlers-in-eclipse
Anyone to advice please?
Thanks!
What you're talking doesn't quite exist in Eclipse. You'll have to manually open your java class and add the method to the corresponding java activity there.
For example, if you set the android:onClick XML attribute to "myAwesomeMethod" in your layout XML file, in the corresponding Activity that uses that layout, you'll need to make sure you have a "myAwesomeMethod" method defined.
<?xml version="1.0" encoding="utf-8"?>
<!-- layout elements -->
<Button android:id="#+id/mybutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Click me!"
android:onClick="myAwesomeMethod" />
<!-- even more layout elements -->
In your java Activity class:
public void myAwesomeMethod(View v) {
// does something very awesome
}
Note: you can also do this programmatically, which is what I generally do. However, defining the android:onClick method will save you a few lines of code.
For more information, check out this post. It gives a lot more detail on how to assign onClick handlers to a button and the two ways you can do so.
No, that is not how Eclipse works. You add the control in the xml file, then in the activity that you are going to load that layout in you add the onClickListener on the element you want to respond to clicks for,
I like the title bar style from the Android preference category.
In my Activity (not a PreferenceActivity) How can I use the same style?
Since I just spent the last few hours trying to answer this old question, I'll do it here for anyone else.
It turns out the resource the preference category style is using is listSeparatorTextViewStyle.
You use it like this:
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Hello, World"
style="?android:attr/listSeparatorTextViewStyle"/>
Using style="?android:attr/preferenceCategoryStyle" didn't work.
The main layout is most likely a ScrollView with a LinearLayout. As for the individual layout, I believe (just guessing after looking at the documentation) that you can use the various attributes in android.R.attr - look here: http://developer.android.com/reference/android/R.attr.html. There are attributes like preferenceCategoryStyle, preferenceStyle, etc. You can apply any style to any of your views.