I am new in Android. I have been trying to make a game. I have many questions and when user reply these, screen will change and show another questions. How can I do it without changing activity? Thanks
I want to change only this part of view in every question when push the button.
<TableRow
android:id="#+id/typeRow1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:gravity="center" >
<TextView
android:id="#+id/flagPart1"
android:layout_width="76dp"
android:layout_height="120dp"
android:background="#drawable/back"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/flagPart2"
android:layout_width="76dp"
android:layout_height="120dp"
android:layout_marginLeft="-1dp"
android:background="#drawable/back"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/flagPart3"
android:layout_width="76dp"
android:layout_height="120dp"
android:layout_marginLeft="-1dp"
android:background="#drawable/back"
android:gravity="center"
android:textAppearance="?android:attr/textAppearanceMedium" />
</TableRow>
Get a handle on your TextView by calling TextView textView = (TextView) findViewById(R.id.yourtextview) in your onCreate() Method.
You can then use textView.setText("Your Text here") to change the value.
If you make the textView a class attribute you can change its value from other methods, too. Please keep in mind that you still have to init the variable with the findViewById method in your onCreate().
Keep a reference to the TextViews you want to fill with data in your Activity
Fill these TextViews with different text
You can use visibilty ..u can hide or show ur view like
textview.setVisibility(View.VISIBLE);
textview.setVisibility(View.INVISIBLE);
You can simply update text in TextViews:
TextView textView = (TextView) findViewById(R.id.textView);
textView.setText("text");
More complex solution to changing UI inside the Activity is Fragments.
Related
I have something like "There are 200€" but I want to style the value (ex. textColor Red)
Is there any way of having two textViews to seem like an entire one?
A textView inside a textView or a textViewContainer or something.
I can achieve it on the run like this answer: Change text color of one word in a TextView
But I want to do from the layout file. Any chance?
Thanks
EDIT
<TextView
android:id="#+id/lastIncome"
android:text="Tu último ingreso fueron"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_font_size"/>
<TextView
android:id="#+id/lastIncomeValue"
android:text="200€"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="#dimen/dash_value_font_size"
android:textColor="#color/greensea"
android:layout_gravity="right"/>
You can achieve this with a horizontal LinearLayout. The LinearLayout is the container for the two side by side TextViews. This layout can be placed in any other container (LinearLayout, RelativeLayout, etc.) in your XML.
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="There are " />
<TextView
android:id="#+id/amount"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="200€"
android:textColor="#ff0000"/>
</LinearLayout>
You can use a easy approach to do this within one textview using SpanableString Builder Click here to get this approach
I need to have 3 text in layout. the textView in the center is red in color and the other 2 are black. so i added it to a relativelayout and set the layout as textView2 to the right of textView1 and textView3 to the right of textView2
But when the text in the 3rd text view is bigger it should come textview one. insted I get this.
now what should i do to get somthing like this?
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/rel_lay"
android:background="#DFDFDF"
android:padding="2dip" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="2dip"
android:text="TextView"
android:textColor="#000000" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#+id/textView1"
android:text="TextView"
android:textColor="#FF0000" />
<TextView
android:id="#+id/textView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="false"
android:layout_toRightOf="#+id/textView2"
android:text="TextView when the third text is longer"
android:textColor="#000000" />
</RelativeLayout>
this is the relative layout with 3 textView.
Just use one TextView and set text as per you need. You know which text to set for Red color. So what you need to have is index of starting text and ending text.
Use following code.
SpannableString spanText = new SpannableString("My Name is Chintan Rathod");
spanText.setSpan(new ForegroundColorSpan(Color.parseColor("#FF0000")), 5 /*start index*/, 10 /*end index*/, 0);
textView.setText(spanText);
Here, I have passed 5 and 10, but you can pass your own indexes. But make sure index within range which will not fire IndexOutOfBoundException
You might have better luck with using Spanned classes and one TextView. The way you are trying to implement it, TextView #3 would have to fill_parent on the width and be able to know where TextView #2 ends.
I have a relative layout
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/ivLogo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:src="#drawable/ic_launcher" />
<Button
android:id="#+id/b2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:text="Button" />
<Button
android:id="#+id/b1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_toLeftOf="#+id/b2"
android:text="Button" />
<TextView
android:id="#+id/tvName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="21dp"
android:layout_toRightOf="#+id/ivLogo"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
which contains an imageview, textview, and two buttons, and what I want is to make a list of a info to be filled in this layout auto .. umm, like making in the java file "Heart.png", heart, www.heart.com, 69552874 .. then when I start the app the info I typed in the java class gets in the activity .. the "heart.png" gets in the ivLogo imageview, and the www.heart.com gets into b1 button, like this ..
I can achieve what i want using the xml but it will take a long of time and it'll be very slow because ill be many imageviews .. I want to make a list of things with their logo and name and num. and website , like can the java make a new relative layout for every info ..
like if i write
"Heart.png", heart1, www.heart.com, 69552874
"Heart.png", heart2, www.heart.com, 69552874
"Heart.png", heart3, www.heart.com, 69552874
I see in the app 3 relative layouts for each heart 1,2, and 3 ..
I want a method to do that not using the xml for each one make a new relative layout
isn't there something like makenewLayout etc ... ?
sorry for taking long, a two words hint will be nice to me <3
Yes, you can do this in code.
RelativeLayout layout = new RelativeLayout(this);
ImageView iv = new ImageView(this);
iv.setId(ivid);
....
....
TextView tv = new TextView(this);
tv.setId(tvid);
tv.setText("My text");
...
layout.addView(iv);
layout.addView(tv);
setContentView(layout);
Put this in a function, and call the function for various images, textviews, etc.
I have two text views(TV_1,TV_2).I want to set the TV_2 is immediate right of the TV_1.
It means
suppose TV1 occupy the one entire line in the screen then TV2 will set below the TV1.
Suppose Tv1 occupy the one and half line then tv2 will occupy the rest of the space. How to implement this? i tried but i am not getting. It is similar to one paragraph.
<RelativeLayout
android:id="#+id/xrays_dicom_alert_TnC"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="20dp">
<CheckBox
android:id="#+id/xrays_dicom_alert_cb"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"/>
<TextView
android:id="#+id/xrays_dicom_alert_TnC_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:text="I have read I agree to the following"
android:textColor="#color/black"
android:textAppearance="?android:attr/textAppearanceMedium" />
<TextView
android:id="#+id/xrays_dicom_alert_statement"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/xrays_dicom_alert_cb"
android:layout_below="#id/xrays_dicom_alert_TnC_text"
android:layout_alignRight="#id/xrays_dicom_alert_TnC_text"
android:text="Statement"
android:textColor="#color/black"
android:layout_weight="1"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
Thats impossible act in Android.
A View occupies a rectangular area on the screen.
http://www.androidadb.com/class/android/view/View.java.html
Or you have to do something like this
To make clickable some text in TextView one can go with Spannable concept.
http://developer.android.com/reference/android/text/Spannable.html
use URLSpan http://www.androidadb.com/class/ur/URLSpan.html
I think it's not possible in the pattern what you have asked.
I would like to add button myButton to TextView. How to get it in a single line so that it looks like one set of lines. Here is sample of the text to be added
Please press this here to refresh the set of values.
I want to have "here" as clickable button. How can I do it. Any snippet on it would be helpful.
Set background of Button as null by android:background="#null" give the same text color as given to other TextView's.
or You can take 3 TextView's and setOnClickListener to middle one.
Set style of Button with borderless android attribut to remove background and border:
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
style="?android:attr/borderlessButtonStyle"
android:text="here" />
make three textView ex: textView1, textView2 and textView3.
<RelativeLayout android:id="#+id/textLay"
android:layout_width="fill_parent" android:layout_height="wrap_content"
>
<TextView android:id="#+id/textView1" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Please press this"/>
<TextView android:id="#+id/textView2" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="here"
android:layout_toRightOf="#+id/textView1" />
<TextView android:id="#+id/textView3" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="to refresh the set of values."
android:layout_toRightOf="#+id/textView2" />
</RelativeLayout>
Just add below attribute to TextView in XML layout file to make it clickable.
android:clickable="true"
In your Java file add OnClickListener to complete click action on text view.
Add android:clickable="true" and android:onClick="yourclickname" for the TextView in the XML. Then define yourclickname in your activity. This should make your TextView clickable and triggers the specified method when clicked.