I have a TextView embedded in a RelativeLayout, embedded in a ScrollView, and I load a Spanned text object in it created from some HTML code.
I have set the android:autoLink attribute of the TextView to true, and I have tested in another part of the application that I can click on links in the text and they will take me to the target website.
The only difference is that this time the TextView is a child of a ScrollView. Does that screw up the android:autoLink process or am I really missing something here?
XML Code:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="fill_parent" android:layout_width="fill_parent"
android:padding="10dp" android:background="#FFF">
<RelativeLayout android:layout_width="fill_parent"
android:layout_height="wrap_content" android:id="#+id/RLNewsItem">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentLeft="true"
android:id="#+id/TVText" android:textColor="#000"
android:layout_below="#+id/TVTitle" android:linksClickable="true"
android:autoLink="web|email|map"></TextView>
</RelativeLayout>
</ScrollView>
Java code:
Spanned newsText = Html.fromHtml(htmlsource, this, null);
text.setText(newsText);
Try this
Code in String.xml :
<string-array name="description">
<item> <Data> <![CDATA[ Check this <u>Redirect to Next Activity</u> ]]></Data> </item>
Code in Java class:
ArrayList<String> title_list = new ArrayList<String>();
String[] description_Array = getResources().getStringArray(R.array.description);
String categoryAndDesc = null;
for(String cad : description_Array) {
categoryAndDesc = cad;
title_list.add(categoryAndDesc);
}
CharSequence sequence = Html.fromHtml(categoryAndDesc);
seperator_view.setText(strBuilder);
seperator_view.setMovementMethod(LinkMovementMethod.getInstance());
Related
I am trying to implement card text change on Sony SmartEyeglass, but I am having trouble with the layouts. I adopted the advanced layout sample from the SDK and modified it.
I have a default xml layout that displays a title and a body text. I put 'Title' and 'Body' as default strings in the xml file, and tried to update the title and body as 'Updated' and 'Updated body text'. However, the result shows me the default layout (with 'Title' and 'Body') and the text 'Updated body text' overlapping on top of them.
Why is the title not edited, and how come the body text is on top of the xml TextView?
Here is the relevant code:
#Override
public void onResume() {
showingDetail = false;
ControlListItem item = createControlListItem(namePosition);
showLayout(item.dataXmlLayout, null);
sendListCount(item.layoutReference, quotedPeople.size());
sendListPosition(item.layoutReference, namePosition);
//utils.sendTextViewLayoutId(R.id.names_body);
sendListItem(item);
}
private ControlListItem createControlListItem(final int position) {
Log.d(Constants.LOG_TAG, "position = " + position);
ControlListItem item = new ControlListItem();
item.layoutReference = R.id.names_gallery;
item.dataXmlLayout = R.layout.smarteyeglass_quotes_names_gallery;
item.listItemId = position;
item.listItemPosition = position;
List<Bundle> list = new ArrayList<Bundle>();
// Header data
Bundle headerBundle = new Bundle();
headerBundle.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.names_title);
headerBundle.putString(Control.Intents.EXTRA_TEXT, "Updated");
list.add(headerBundle);
// Body data
Bundle bodyBundle = new Bundle();
bodyBundle.putInt(Control.Intents.EXTRA_LAYOUT_REFERENCE, R.id.names_body);
bodyBundle.putString(Control.Intents.EXTRA_TEXT, "Updated body title");
list.add(bodyBundle);
item.layoutData = list.toArray(new Bundle[list.size()]);
return item;
}
Here is the xml layout file:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="#dimen/smarteyeglass_control_width"
android:layout_height="#dimen/smarteyeglass_control_height"
tools:ignore="PxUsage,UselessParent,HardcodedText" >
<TextView
android:id="#+id/names_title"
android:layout_width="400px"
android:layout_height="30px"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginTop="5px"
android:paddingLeft="6px"
android:background="#android:color/black"
android:text="Title"
android:textColor="#android:color/white"
android:textSize="#dimen/smarteyeglass_text_size_normal" />
<View
android:id="#+id/names_divider"
android:layout_width="400px"
android:layout_height="2px"
android:layout_alignParentLeft="true"
android:layout_below="#+id/names_title"
android:background="#android:color/white" />
<TextView
android:id="#+id/names_body"
android:layout_width="400px"
android:layout_height="78px"
android:layout_marginTop="5px"
android:layout_marginLeft="10px"
android:layout_alignParentLeft="true"
android:layout_below="#+id/names_divider"
android:textColor="#android:color/white"
android:textSize="#dimen/smarteyeglass_text_size_small"
android:text="Body" />
<Gallery
android:id="#+id/names_gallery"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
</Gallery>
</RelativeLayout>
we just tested your example and the text is changing fine. It looks like you might have picked the wrong XML for the gallery item. You started with smarteyeglass_layout_test_gallery instead using smarteyeglass_item_gallery.
I am attempting to achieve a result similar to:
Where contacts might or might not have email addresses or other profile information. Is it possible to achieve the majority of it through XML or does it all need to be done through Java?
Here is the pseudocode that I imagine would be a solution:
if (contact.hasPhoneNumber)
foreach number in contact
inflate r.layout.contact_number
settext phonenumber textview
add view to layout
Is there a smarter approach?
This was solved by having the following:
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/contactContent"
android:layout_below="#+id/profile_background">
<LinearLayout android:orientation="vertical" android:id="#+id/c_detail_content_main" android:paddingBottom="50.0dip" android:layout_width="fill_parent" android:layout_height="wrap_content">
<LinearLayout android:orientation="vertical" android:visibility="gone" android:id="#+id/c_detail_birthday_layout" android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_marginTop="20.0dip">
</LinearLayout
</ScrollView>
And the following code:
if (this.contact.hasPhoneNumber())
{
LinearLayout phoneNumberContent = (LinearLayout) view.findViewById(R.id.c_detail_phone_layout);
phoneNumberContent.setVisibility(View.VISIBLE);
for (PhoneNumber number : this.contact.getPhoneNumbers())
{
LinearLayout tempNumberContent = new LinearLayout(this.getActivity().getApplicationContext());
TextView type = new TextView(this.getActivity().getApplicationContext());
TextView phoneNo = new TextView(this.getActivity().getApplicationContext());
type.setWidth(LayoutTools.getDPMeasurement(this.getActivity(), 85));
phoneNo.setText(number.getNumber());
tempNumberContent.addView(type);
tempNumberContent.addView(phoneNo);
phoneNumberContent.addView(tempNumberContent);
}
}
I can't figure out why my EditText is null.
public ChoiceView(Context context) {
super(context);
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
final View view = inflater.inflate(R.layout.choice_view, this, true);
Log.d("TEST CHOICEVIEW", "1");
et = (EditText) view.findViewById(R.id.enterChoice);
if (et == null) Log.d("TEST CHOICEVIEW", "null"); // << this always produces "null" in Logcat
tv = (TextView) findViewById(R.id.choiceViewTextView);
Log.d("TEST CHOICEVIEW", "2");
choiceString = et.getText().toString();
Log.d("TEST CHOICEVIEW", "3");
tv.setText(choiceString);
Log.d("TEST CHOICEVIEW", "4");
requestLayout();
}
and the xml file where the EditText is found (is not choice_view...is that an issue?):
<?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" >
<ScrollView
android:id="#+id/scrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/enterChoice"
android:hint="#string/enterChoiceText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10" >
<requestFocus />
</EditText>
</ScrollView>
<Button
android:id="#+id/saveButton"
android:text="#string/save"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
</LinearLayout>
and just for the sake of thoroughness, the choice_view.xml file associated with the ChoiceView class:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/choiceViewTextView"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
When I run this, Logcat produces the following before crashing:
TEST CHOICEVIEW 1
TEST CHOICEVIEW null
TEST CHOICEVIEW 2
AndroidRuntime Shutting down VM
Let me know if you need any additional information. Thanks!
Additional info:
ChoiceView is called from the ChoiceList class from the following code:
Context context = ChoiceList.this;
ChoiceView cv = new ChoiceView(context);
You need the component you are trying to reference to be contained in the XML layout you are inflating.
It looks to me like you are trying to reference Views (EditText) from more than 1 XML file. This is not appropriate
This why you are not finding the view you are looking for - choiceViewTextView
It is in NOT in the choice_view.xml file (which is the one you are inflating). Changing this call to reference the layout.xml file (you didn't specify the name for this file) will fix your issue.
Have you tried inflating the view (assuming the code you posted is in code_view) without attaching it to a view group? Like so:
final View view = inflater.inflate(R.layout.choice_view, null);
Caused me major headaches in the past.
In my app i am using parse.com to import multiple string values and list them in a list view
see image - http://postimg.org/image/onpyj0ro5/
My aim here is to change the color of the two String values shown in the above image programatically (not in .xml files)
I have tried implementing SpannableString & SpannableStringBuilder but had no success.
.xml files:
listview_main_wednesday.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<ListView
android:id="#+id/listview_wednesday"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
</RelativeLayout>
listview_item_wednesday.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:padding="5sp"
android:textSize="15sp"
android:textColor="#color/list_background"
/>
my fragment code
protected void onPostExecute(Void result){
listview = (ListView) getView().findViewById(R.id.listview_wednesday);
adapter = new ArrayAdapter<String>(getActivity(), R.layout.listview_item_wednesday);
for (ParseObject country : ob) {
adapter.add((String) country.get("pSubject") + "\n" + "\n" + (String) country.get("pTime"):
}
listview.setAdapter(adapter);
mProgressDialog.dismiss();
}
}
Any help would be much appreciated :)
first get the text view,
TextView textView1= (TextView)findViewById(R.id.text);
and then,
textView1.setTextColor(getResources().getColor(R.color.mycolor))
or
textview1.setBackgroundColor(Color.parseColor("#ffffff"));
or
textview1.setBackgroundColor(Color.RED);
or
textView1.setBackgroundColor(R.color.black);
is this what you want?
you can use this to implement simple custom adapter.
I need to add text to a ScrollableLayout after it's remotely retrieved from an AsyncTask. Since I don't know the number of strings involved, I need to programmatically create as many TextViews as needed.
Layout code
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/view_phone_mainView"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/view_phone_linearLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="top" >
<TextView
android:id="#+id/view_phone_lblTitle"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal|center_vertical"
android:text="#string/view_phone_title"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/view_phone_lblWarning"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/view_phone_warning"
android:textAppearance="?android:attr/textAppearanceSmall"
android:visibility="invisible" />
</LinearLayout>
</ScrollView>
Activity
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
pDialog = new ProgressDialog(this);
pDialog.setIndeterminate(true);
pDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
pDialog.setMessage(getString(R.string.view_phone_wait));
pDialog.show();
task = new MessageLoaderTask(); //Returns String[] on callback
task.execute((Void) null);
}
public void onRulesLoaded(String[] messages) { //Directly called by OnPostExecute
LinearLayout container = (LinearLayout) findViewById(R.id.view_phone_linearLayout);
if (messages != null) {
for (String m : messages) {
TextView tv = new TextView(this);
tv.setText(m);
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
container.addView(tv, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
}
}
pDialog.dismiss();
}
I am sure, by debug, that strings are correctly valued. The result is that the title label gets displayed, the warning is hidden but below there is only white......
I tried to scroll and noticed that the scrolling area is veeeeeeeeeery long, compatible with the long Lorem ipsum stub text I used for testing. If I truncate via debug one of the strings to empty (there are only 2) the scrollable area is shorter in height. I use the light theme, so I expect black text on white background.
tv.setTextAppearance(this, android.R.attr.textAppearanceSmall);
tv.setVisibility(View.VISIBLE);
tv.setBackgroundColor(Color.TRANSPARENT);
tv.setTextColor(Color.BLACK);
were added at a second time when everything was failing. No difference whether they are in place or not. What can I do to fix?
Thanks
Not sure why your ScrollView would be increasing in length if this were the sole problem, but it seems like you need to set your LinearLayout's orientation to vertical.
Use the below code.
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textMultiLine" >
<requestFocus />
</EditText>
add all the Strings to this EditText by keeping '\n' between the strings. So that every thing will be aligned properly.