people!
Well, after googling for almost two days with no result, I decided to ask here. (It's my first question.)
I'm creating a quotes widget for Android (that shows a random quote each time), so it has two TextViews and a ImageButton to get a new random quote. The first TextView shows the quote text and the second shows the quote's author. And, when the user clicks on the widget, it calls a Activity showing the quote in full screen, with scroll bars if it is needed, and so on.
The thing is that the quote's text sometimes is much long, so I was wanting to ellipsize it. But since the ellipsize doesn't work with more than 2 lines... I'm looking for ideas.
I don't know if it's necessary, but here is the layout code.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/laySmall"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="#dimen/widget_margin"
android:background="#drawable/widgetback" >
<ImageButton
android:id="#+id/ibtNext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:background="#null"
android:contentDescription="#string/app_name"
android:src="#drawable/seta" />
<TextView
android:id="#+id/txtWdgQuote"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/ibtNext"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_marginBottom="8dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginTop="4dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textStyle="italic"
android:typeface="serif"
android:maxLines="3" />
<TextView
android:id="#+id/txtWdgAuthor"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:textColor="#000000"
android:textStyle="bold" />
</RelativeLayout>
Basically, I have 3 AppWidgets in this project, showing almost the same thing. All that change are the sizes and font sizes.
So... One idea that I had was using a custom TextView. But in a AppWidget, no way. :/
Another idea was doing something like this:
if (quoteText.length() > something) {
quoteText = quoteText.substring(0, something) + "\"[...]"
}
The problem of this solution is where is the line breaks, because it can change the size of the string that can be showed. For example, let say a string with small words: the lines will break next to the border. Meanwhile a string that has bigger words the line breaks will be further from the border.
So... I don't know what I do. Any ideas, please?
Thanks!
(I'm having problems with the android:layout_margin, so it can be a little smaller, but I didn't have time to work on this yet.
Related
not urgent as I have a work around, however I would really like to understand whether there is a reason for a behaviour I observe when i make the changes below.
I have a simple layout with an EditText and a Button in an XML file.
It displays fine, and as the code below it works fine (triggering onClick)
<RelativeLayout android:id="#+id/group"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<EditText android:id="#+id/Topic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#+id/AddTopic"
android:layout_alignParentLeft="true" android:ems="10" />
<Button android:id="#+id/AddTopic"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:onClick="onClick" android:textSize="10dp" android:text="Add Topic" />
</RelativeLayout>
However, if I flip the objects around, as below, the button does not trigger - onClick does not run and nothing happens.
<RelativeLayout android:id="#+id/group"
android:layout_width="wrap_content" android:layout_height="wrap_content">
<Button android:id="#+id/AddTopic"
android:layout_alignParentRight="true" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_weight="1"
android:onClick="onClick" android:textSize="10dp" android:text="Add Topic" />
<EditText android:id="#+id/Topic" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_toLeftOf="#+id/AddTopic"
android:layout_alignParentLeft="true" android:ems="10" />
</RelativeLayout>
I might be missing something really obvious here, and as I say it doesn't matter in the sense it works in the first structure, but it does matter in the sense I would really like to know why. Any ideas appreciated.
Combining the information posted in the comments, the likely reason is the fact that you mistakenly use #+id/ when referring to other objects, instead of #id/. Specificly EditText contains android:layout_toLeftOf="#+id/AddTopic". The + sign should only be used when introducing new ids.
In the first example it is not harmful for the listener, since the button is declared after the EditText and #+id/ works as should, but in your second example the id of the button might be overriden when you declare the EditText.
TL;DR: in EditTextreplace android:layout_toLeftOf="#+id/AddTopic" with android:layout_toLeftOf="#id/AddTopic". Do this even if you use your "work around".
I'm sure this has been answered before but I've searched relentlessly and can't find an answer that fixes my exact problem, which is:
I have a TextView on top of an ImageView setup like this:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ImageView
android:id="#+id/imageForCustomAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_ball_3" />
<TextView
android:id="#+id/textOnBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="1dp"
android:textColor="#000000"
android:textSize="50dp"
android:hint="10"
android:layout_alignParentBottom="false"
android:layout_centerInParent="true"
android:layout_alignTop="#+id/imageForCustomAdapter"
android:layout_alignLeft="#+id/imageForCustomAdapter"
android:layout_alignBottom="#+id/imageForCustomAdapter"
android:layout_alignRight="#+id/imageForCustomAdapter"
android:gravity="center" />
</RelativeLayout>
The TextView is aligned with the ImageView to make it the same size and I want the text to be centered both horizontally and vertically.
The render in Android Studio shows it to be centered correctly This Pic Shows That
But when I actually run the app on a real device the text only centers horizontally. I've tried all the different combinations of android:gravity= but nothing seams to work.
What am I doing wrong?
I'll preface this with saying I really dislike RelativeLayout, and here's a great example. Its powerful, but can be seriously confusing, and simplicity is important.
You've got many layout directives that can easily conflict with each other. Try something like:
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<ImageView
android:id="#+id/imageForCustomAdapter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/my_ball_3" />
<TextView
android:id="#+id/textOnBall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:textSize="50dp"
android:hint="10"
android:layout_gravity="center" />
</FrameLayout>
The 'wrap_content' is important on the FrameLayout, btw. Assuming the ImageView is always bigger than the TextView, it'll drive the parent height. If the TextView might be bigger, make sure to center the ImageView as well.
Why it works in studio and not in real life, no idea.
I had no idea what to call this question, what I am experiencing is a little odd, to say the least.
I have a layout with the following section part of it
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/activityProjectView_projectName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/activityProjectView_projectDueDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Due Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
</LinearLayout>
<TextView
android:id="#+id/activityProjectView_projectDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Description"
android:textAppearance="?android:attr/textAppearanceMedium" />
In my code I assign the variables into these TextView elements as follows
TextView projectName = (TextView) this.findViewById(R.id.activityProjectView_projectName);
TextView projectDesc = (TextView)
this.findViewById(R.id.activityProjectView_projectDescription);
TextView projectDueDate = (TextView)
this.findViewById(R.id.activityProjectView_projectDueDate);
projectName.setText(project.getName());
projectDesc.setText(project.getDescription());
projectDueDate.setText(Util.DATE_SHORT_UI.format(project.getDueDate()));
The data returned by the project getters is correct. When the view loads, the small textview is populated with the description, not the date and the medium textview is populated with the date not the description.
I thought this was odd as the view is correct, and the code by the looks of it is correct. At this point I switched the layout so that the date element was below as follows:
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/activityProjectView_projectName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project name"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/activityProjectView_projectDescription"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Description"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
<TextView
android:id="#+id/activityProjectView_projectDueDate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Project Due Date"
android:textAppearance="?android:attr/textAppearanceSmall" />
I expected this to switch the data round (showing that I had some sort of error in my code, or mis assignment somewhere). I was wrong. The data stayed exactly where it was previously.
By that I mean the date was now displayed in the other textview and same for description. So now even though I switched the textviews round, the date was now in the the medium textview not the small one and the description switched also. Oddly the view definitely updated as i saw the medium and small text views move places, but the data somehow also switched.
What is going on? Whatever I do to my layout file, I can get the style itself to change, and position of elements, but the data always somehow finds a way to display itself in the wrong textviews.
Apparently the issue was caused by a broken R.java file with references pointing to wrong views. A clean rebuild fixed the issue.
I'm working on a ListView where each item has an image and some text. On my older phone, HTC Evo 4G, things came up exactly as expected. Image is nicely sized and text looks fine. Then I loaded the same app on my new phone, HTC One, and the images are so tiny they are useless. I know the HTC One has a significantly higher resolution, but I cannot figure out how to make each item in the ListView larger.
I'm not sure what to post, but I think the layout xml files are the issue.
Here's activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="fill_parent"
android:orientation="vertical">
<LinearLayout android:id="#+id/LinearLayout01"
android:layout_height="wrap_content" android:orientation="horizontal"
android:layout_width="fill_parent" android:gravity="center_horizontal"
android:layout_marginTop="2dip" android:background="#669900">
<!--
Use layout_weight to stretch the EditText and compress the button.
To avoid text wrap, the editable text is forced to occupy only one
line
-->
<EditText android:layout_width="wrap_content"
android:typeface="normal"
android:layout_weight="1" android:lines="1" android:ellipsize="middle"
android:hint="#string/hint" android:id="#+id/search_key" android:layout_height="fill_parent" android:maxLines="1"></EditText>
<ImageButton android:layout_width="wrap_content" android:src="#drawable/ic_menu_search" android:adjustViewBounds="true"
android:layout_weight="0" android:id="#+id/btn_ok" android:layout_height="wrap_content"></ImageButton>
</LinearLayout>
<TextView android:layout_height="wrap_content" android:id="#+id/tweet_header"
android:gravity="right" android:layout_width="fill_parent" android:paddingRight="4dp"
android:background="#99cc33" android:textColor="#000000" android:textSize="21dp"></TextView>
<ListView android:layout_width="wrap_content" android:layout_height="wrap_content" android:id="#+id/tweet_list"></ListView>
</LinearLayout>
And the ListView is made up of this item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageView
android:id="#+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:scaleType="fitCenter"
android:src="#drawable/ic_launcher" />
<TextView
android:layout_height="wrap_content"
android:id="#+id/created_at"
android:text="now"
android:textSize="10sp"
android:layout_below="#id/itemImage"
android:layout_alignParentRight="true"
android:textStyle="italic"
android:layout_width="wrap_content"></TextView>
<TextView
android:id="#+id/itemURL"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="itemURL"
android:visibility="invisible" />
</RelativeLayout>
So the app does this: It taps ebay for car data and presents the search results as line 1 with a picture, line 2 with the ebay item title. A hidden value is the URL so users can tap the ListView line and launch a web browser.
Looks like the wrap_content setting for the ImageView's layout_width and layout_height are the issue. When I switched that to numerical values, suddenly I saw it change size. Finally setting it to 100dp made the images viewable.
As an aside, I had a terrible time with ADS and the emulator. It consistently failed to re-load my updated app. Often complaining about the Package Manager and suggesting the some process was running. I little idea what was running and how to fix it. When I switched to loading the app straight to the phone, then it worked fine.
So, I think now I need to create a system that is proportional, and takes in to consideration the displaying devices resolution.
Problem is the Relative Layout width being wrap_content. Try changing that to match parent. then you would not have to go thru this ordeal
I have a layout with a textview and a button as below.
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/info_layout">
<TextView
android:id="#+id/tv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp" />
<Button android:id="#+id/butn1"
android:layout_gravity="center_horizontal"
android:layout_marginTop="40dip"
android:layout_width="150dip"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/button1" />
</LinearLayout>
The above layout works fine for many languages but for arab its giving sorry pop up, but if i change textSize to 18sp it works fine. Even i tried removing margin from button before changing text size but still the problem raises.I'll set text for text View in java file.So im not understanding what's exact cause for this problem.
01-08 03:09:59.959: E/AndroidRuntime(1803): java.lang.StringIndexOutOfBoundsException: length=75; regionStart=52; regionLength=-18