My ListView item has two TextViews. I want to show one of them at a time using XML. I don't want to do this programmatically. How to do it?
For example:
When TextView A has text, TextView B disappears. When TextView A is empty, TextView B appears.
My code
<?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="horizontal" >
<TextView
android:id="#+id/groupNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/ForestGreen"
/>
<TextView
android:id="#+id/topRatedPlaceNameTextView"
android:layout_width="wrap_content"
android:layout_height="50dip"
android:text="TextView"
android:textSize="20dip"
android:textColor="#color/Azure"
/>
</LinearLayout>
Sorry, you cant do that with xml, by Programmatically in your getView() of List's Adapter check like
if(textview1.getText().toString.length()>0)
textview2.setVisibilty(View.GONE);
else
textview1.setVisibilty(View.GONE);
TextView A has a text then in TextView B set setVisibility(View.VISIBLE) or setVisibility(View.VGONE) and same for TextView B. you do this in your CustomAdapter Class getView Method.
Thanks
Will you ever have both of them shown with text at the same time ? If not, and if you don't have style difference (text size/color), you could just use a single text view ?
Else, if A and B have different size / color / font / whatever, and assuming they will never be having text at the same time, using wrap_content as you do should ensure that A gets a width of 0 when it has no text, and B will take all the space. Else, if B has no text, A will take as much space as needed by its content.
Related
I have a TextView that displays an error message beside 2 Buttons. They are currently inside a horizontal LinearLayout. The problem is if the TextView is too wide, the 2 Buttons will be pushed off the screen. Is it possible to push the elements downwards in those cases?
If the text is short there are no problems:
(Textview text) (Button1) (Button2)|(Edge of screen)
If the textview is long, I want to push the 2 buttons down a "row"
(Realllllllllllly long text that may|(Edge of screen)
span 2 lines)
(Button1) (Button2)|(Edge of screen)
I think you need to keep one more Linear layout below to your horizontal linear layout and need to check text size runtime if it's width is greater than required two button space then need to hide horizontal linear layout buttons and need to show below layout buttons
to refer how to check text size runtime refer below link :
Refer this link
Try this way: Use FlowLayout
<org.apmem.tools.layouts.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
</org.apmem.tools.layouts.FlowLayout>
Inside FlowLayout you can put your view's and it will auto move to next line if not fit.
Yes you can do that, flexbox-layout is the solution.
How to use
Gradle dependency
dependencies {
implementation 'com.google.android:flexbox:0.3.2'
}
And xml code
<com.google.android.flexbox.FlexboxLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:flexWrap="wrap"
app:alignItems="stretch"
app:alignContent="stretch" >
<TextView
android:id="#+id/textview1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_alignSelf="flex_end"
/>
</com.google.android.flexbox.FlexboxLayout>
There are few other attributes also [read documentation], which you can try and find what works more suitable in you case.
you can use the TextView predefined method, to gave validation to end user like this
TextView textView=(TextView)findViewById(R.id.text_view);
textView.setError("Your Text is very wide please provide short text");
setError put red mark on textview view, with that we can tell the end user. provided text is wide
I am developing an Android application with a textview updated by one event and at the same place where the textview is present, I want 1 more textview so that other event can update this new textview. How do i achieve in having 1 textview on other
I'm assuming that you're asking how you could have two TextView components overlaying each other. There are a few way you could do this.
Frame Layout
Use a Frame Layout to determine the area in which the TextViews will occupy. Like this...
<FrameLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/tv1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<TextView
android:id="#+id/tv2"
android:layout_width="fill_parent"
android:layout_height="wrap_content" />
</FrameLayout>
Credit goes to https://stackoverflow.com/a/2634059/3769032
Create a Compound View
This is fairly in-depth for the type of question you are asking. CompoundViews are a collection of typical views, such as a TextView, that you can create if you plan on re-using the view frequently.
If you plan on overlaying the TextViews often, I recommend this. So check out this tutorial.
Use only one TextView
Having two overlayed textviews can become messy really quickly. If you have two pieces of text overlayed is becomes impossible to read. So since the content of your textview is based on an event. Use the same event listener in your java code to determine the content of your TextViews.
For example, in your on click listener you might have...
TextView tv1 = (TextView) findViewByID(R.id.tv1);
public void onClick(View view){
if (first_event_happened){
tv1.setText("One event happened");
} else if(second_event_happened){
tv1.setText("A different event happened");
}
}
These conditions might mean checking the type of view that was clicked, or checking its id (what I usually do). Please comment if things aren't clear. Some clarification on your question would be helpful too.
use relative layout and also you can set text on exiting textView like when event one triggered textView.setText(your text) and same when event two triggered textView.setText(your text)
There is no trick to this. Just put two TextViews in a RelativeLayout at the same position and they will draw overtop of one another. Like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="first textview"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="second textview"/>
</RelativeLayout>
You can make each one visible or invisible by using TextView.setVisibility(...) or you can set their text with TextView.setText(...).
This may seem like a duplicate but I can't find an answer anywhere.
It seems everyone has "TextView" available to edit in their activity xml file, whereas I only have the following:
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/liv1"
android:layout_below="#+id/lt1"
android:layout_marginTop="29dp"
android:layout_alignRight="#+id/lt1"
android:layout_alignEnd="#+id/lt1"
android:layout_alignLeft="#+id/lt1"
android:layout_alignStart="#+id/lt1"
/>
Using android:gravity = centerdoes nothing when I put it in between the "ListView" brackets.
So what is it I should do?
You should create a custom ListView adapter that fits your needs, and also create an inflater's xml layout and align the text there whatever you want.
More info here:
Custom Adapter for List View
http://www.androidinterview.com/android-custom-listview-with-image-and-text-using-arrayadapter/
You should set gravity in your item, that you using in your adapter. And there modify your textView like this way:
<TextView
android:id="#+id/textView"
android:layout_width="match_parent" <= set this
android:layout_height="wrap_content"
android:text="#string/invalid"
android:gravity="center" <=this your gravity
/>
As I add text to my text view with java, I want some text to be smaller and some to be bigger. Is there a way I can change the text size as I add it with java?
You can Use Spannable String for this purpose and for Tutorial you can see it here
Each text view has a value for the size of the text contained in that view.
Depending on what you are trying to do, you could add several text views in a row with different text sizes to simulate what it sounds like you want.
Use a relative view, add a text view to it and align it however you want with wrap_content for width and then just add the other text views using an alignment toRightOf previous textview.
Use webview and html to make a richtextbox like view. Text widget don't have this feature. Maybe there would be 3rd party widget for this also.
Create 2 separate TextView's, place them next to each other and set different text size on each:
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_1);
TextView myTextView1 = (TextView) findViewById(R.id.my_text_view_2);
float textSize1 = 20.0;
myTextView1.setTextSize(textSize1);
float textSize2 = 30.0
myTextView2.setTextSize(textSize2);
XML file:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/my_text_view_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 1"/>
<TextView
android:id="#+id/my_text_view_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Test 2"/>
</LinearLayout>
I have a textview, created inside a shape. I want to write text above this text view not inside it. !
I have added image for it. The outer rectangle displayed is a dialog box and inner rectangle is textview. : http://i.stack.imgur.com/tLUgM.png
My xml looks like
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="fill_parent" android:layout_gravity="top"
android:background="#drawable/mydialogbox"
android:orientation="horizontal" >
<TextView
android:id="#+id/textview_name"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
/>
Now I want to write text above this inner rectangle and inside outer rectangle which is a dilog box.
You can't do that. You will have to add other TextView dynamically if you don't want to add it in the xml file.
And then set the text to newly added TextView. But the TextView must be there to hold text
use two textview one for shape and other one for text.