Display two TextViews from a Layout - android

In Xamarin, how can I display two objects (TextViews in this case) vertically that refer to the same Resource.Id?
Here is my Layout 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"
>
<TextView
android:id="#+id/TextViewAutoLink"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
</LinearLayout>
Here is my C# code:
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web.Text = "Test address of http://www.google.com";
TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web2.Text = "Test address of http://www.stackoverflow.com";
The Web2 TextView is the only TextView that is being displayed.
Can I have some help please?
Thanks in advance

You can use "\n" between both texts. Like:
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink);
Web.Text = "Test address of http://www.google.com"+"\n"+"Test address of
http://www.stackoverflow.com";

Yuo can add two TextViews in your Layout.
Layout 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"
>
<TextView
android:id="#+id/TextViewAutoLink1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
<TextView
android:id="#+id/TextViewAutoLink2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:autoLink="all">
</TextView>
</LinearLayout>
Source code
SetContentView (Resource.Layout.AutoLinkTextView);
TextView Web = (TextView)FindViewById(Resource.Id.TextViewAutoLink1);
Web.Text = "Test address of http://www.google.com";
TextView Web2 = (TextView)FindViewById(Resource.Id.TextViewAutoLink2);
Web2.Text = "Test address of http://www.stackoverflow.com";

You are getting same TextView id for two different TextView.
So when yousetting your text , it's overriding previous text.
So basically you need two different TextView.

Related

How to resolve hint character wrapping on autosizing TextView with empty text on android?

I want to use autosizing TextView for dynamic text content.
<?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">
<TextView
android:layout_width="match_parent"
android:layout_height="350dp"
android:autoSizeTextType="uniform"
android:hint="hint"
android:text="some dynamic text" />
</LinearLayout>
This works so far:
But there is an issue if the dynamic text content is empty. The TextView wraps each character of the hint content to a new line:
I expect hint to behave like text. How to resolve this without hacks like setting text content to hint content if the former is empty.
The same event happened to me.
I defined this and it fixed it.
app:autoSizeStepGranularity="1dp"
I'd definitely set the minimum and maximum sizes together.
android:hint="no message"
app:autoSizeMaxTextSize="20sp"
app:autoSizeMinTextSize="1sp"
app:autoSizeStepGranularity="1sp"
app:autoSizeTextType="uniform"
layout.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent"
tools:context=".MainActivity">
<TextView
android:id="#+id/show"
android:layout_width="match_parent"
android:layout_height="350dp"
android:hint="no message"
/>
</RelativeLayout>
MainActivity.java
TextView shows = findViewById(R.id.show);
TextViewCompat.setAutoSizeTextTypeWithDefaults(shows,
TextViewCompat.AUTO_SIZE_TEXT_TYPE_UNIFORM);
if(message.isEmpty()){
shows.setWidth(ViewGroup.LayoutParams.WRAP_CONTENT);
}else{
shows.setText(message);
shows.setWidth(ViewGroup.LayoutParams.MATCH_PARENT);
}
if you add this code please run the code.

How remove scrollview programmatically (e.q. disable/enable wordwrap)?

XML (variant 1) - text WORD WRAP OFF:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<HorizontalScrollView
android:layout_height="wrap_content"
android:layout_width="wrap_content">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</HorizontalScrollView>
</LinearLayout>
How remove HorizontalScrollView programmaticaly, after that XML should be, like :
XML (variant 2) - TEXT WORD WRAP ON
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<EditText
android:inputType="textMultiLine|textNoSuggestions"
android:layout_height="match_parent"
android:layout_width="match_parent"/>
</LinearLayout>
What the appropriate way for do this:
Maybe use dynamic fragment ? Maybe use View.removeView ?
Or....
Please help...
This code should remove the scroll view and put the item in it's place (For this to work be sure to add the id's to the view elements on the xml).
View bodyLayout = (View)m_dialog.findViewById(R.id.my_item);
ScrollView scroller = (ScrollView)m_dialog.findViewById(R.id.my_scroll);
ViewGroup topLayout = (ViewGroup)scroller.getParent();
ViewGroup.LayoutParams params = scroller.getLayoutParams();
int index = topLayout.indexOfChild(scroller);
scroller.removeAllViews();
topLayout.removeView(scroller);
topLayout.addView(bodyLayout, index, params);

Error when creating an Adapter

When I want to set a List of 'PaysItem' containing a String and Bitmap in a ListView.
I'm using a SherlockListFragment and i have the error on that line:
ArrayAdapter<PaysItem> adapter = new ArrayAdapter<PaysItem>(getActivity().getBaseContext(), R.layout.pays_item,list);
pays_item.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="wrap_content">
<ImageView
android:id="#+id/image"
android:layout_width="50dip"
android:layout_height="50dip" android:scaleType="centerCrop"/>
<TextView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/text_pays"
android:textColor="#FFFFFF"
android:background="#000000"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dp"
android:textSize="14sp" >
</TextView>
</LinearLayout>
The error is:
You must supply a resource ID for a TextView
The error is clear, I have to supply a TextView ID, but how to solve that?
Thank you for helping.
Error :
You must supply a resource ID for a TextView
means you need to pass an TextView id to ArrayAdapter . pass it as:
ArrayAdapter<PaysItem> adapter = new ArrayAdapter<PaysItem>
(getActivity().getBaseContext(), R.layout.pays_item,R.id.text_pays,list);
You need to implement your own adapter, otherwise you won't see any picture.
Please look here:
How to use ArrayAdapter<myClass>

how define a image in listview android?

I have a layout with this code:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
style="#style/fundo"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/consoleListaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<ListView
android:id="#+id/listaSimples"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</ListView>
</LinearLayout>
and for which item from the list a have this 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" android:background="#drawable/bk_principal_small">
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/imagemStatus"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/green"/>
<TextView
android:id="#+id/texto1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/padrao" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
and to create the itens I create a SimpleAdapter that I insert the data using the method ListAdapterCreater
this.adapter = new ListAdapterCreater(list.getContext(),itensList,
R.layout.list_simple_item,new String[] {"text","image"},
new int[]{R.id.texto1,R.id.imagemStatus});
to set the text works pretty, but doesn`t work for the image.
what have to be the value of image? or what should I change to works?
Refer the below links which have sample code snippets.
LINK1 it has one textview and one image view in each list item.
LINK2 it has two textview in each list item.
LINK3 it has two textview and one image view in each list item.
I believe you'll have to create new Adapter that extends BaseAdapter.
Here's a few tutorials for you:
http://blog.sptechnolab.com/2011/02/01/android/android-custom-listview-items-and-adapters/
http://w2davids.wordpress.com/android-listview-with-iconsimages-and-sharks-with-lasers/
If you look closely on those tutorials, the trick lies in how getView() work.

Problem with LayoutInflator to set TextView Android

Hey folks Im using two layouts, one embedded through an 'include' in the main layout file.
I wish to set the TextView in the embedded one within the activity for the main xml. Here's what I've come up with so far......
Main xml: date_list_layout.xml
<RelativeLayout android:id="#+id/RelativeLayout01"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/bgoption">
<include layout="#layout/cur"
android:layout_height="wrap_content"
android:layout_width="wrap_content" />
</RelativeLayout>
Embedded xml: cur.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">
<TextView android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/currency"
android:textSize="14px"
android:paddingLeft="10dp"
android:textColor="#d17375"
></TextView>
</LinearLayout>
Then my Activity code sets the content to the main xml but tries to inflate the embedded one to set the text as follows......
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.date_list_layout);
View v = LayoutInflater.from(getBaseContext()).inflate(R.layout.cur,
null);
TextView currency = (TextView) v.findViewById(R.id.currency);
currency.setText("test");
}
I'm not getting any errors but the TextView remains empty. Any ideas what I'm doing wrong
Thanks
A
You can use setText directly in TextView. Without calling LayoutInflater in activity.
setContentView(R.layout.date_list_layout);
TextView currency = (TextView) v.findViewById(R.id.currency);
currency.setText("test");
Just directly use the TextView:
TextView currency = (TextView) findViewById(R.id.currency);
Once you have included cur.xml you no longer need to inflate manually. It is automatically inserted into the structure, so you can safely remove the additional LayoutInflater call.

Categories

Resources