One of my textViews is not being adjusted when I call the function:
public void wordList() {
setContentView(R.layout.activity_main);
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
Here's the xml:
If I add android:text="Text" to the xml it works.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity"
android:gravity="center_horizontal"
android:background ="#268496" >
<LinearLayout android:id="#+id/linear"
android:orientation="horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TextView
android:id="#+id/prefixText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
<EditText
android:focusable="true"
android:focusableInTouchMode="true"
android:id="#+id/input"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:inputType="text"
android:textSize="12pt"
android:maxLength="1"
android:typeface="sans" />
</LinearLayout>
<TextView
android:id="#+id/listText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/linear"
android:textColor="#FFFFFF"
android:textIsSelectable="true"
android:textSize="12pt"
android:typeface="sans" />
</RelativeLayout>
If you call setContentView() after running it here then it will overwrite this call and set the content to its default (what's in the xml). If you call setContentView() say in onCreate() then you don't need to call it again as long as the TextView is inside that xml.
So, call setContentView() in onCreate() then call your function
wordList();
then
public void wordList() {
TextView lv = (TextView) findViewById(R.id.listText);
lv.setText("Text");
}
You have white color of textview's text. So if the background color is white, then you just don't see it.
You're not doing anything "wrong" in your posted example. The kinds of things that may be in play here are:
a) is your TextView within the visible area (hard code some text to see if it shows up)
b) are you sure you're calling the method that sets the text?
c) do you have some other overlapping ID somewhere else that is confusing this process?
It has to be something like that.
Related
I would like to set text for the spinners in the layout reused. However, only the first spinner is set. How to set text for all spinners with same id?
Also, I would like to ask how add another skillfield.xml to fragment_skill.xml when clicking the imageview?
Thank you.
fragment_skill.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/skillInfo" >
<RelativeLayout
android:id="#+id/OCC"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/occ"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:text="#string/occSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/occPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/occ"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oocSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/occ"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOccSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oocSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ORI"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/OCC" >
<TextView
android:id="#+id/ori"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:text="#string/oriSkill"
android:textSize="20sp" />
<TextView
android:id="#+id/oriPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:layout_toEndOf="#id/ori"
android:text=""
android:textSize="20sp" />
<include
android:id="#+id/oriSkill"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ori"
layout="#layout/skillfield" />
<ImageView
android:id="#+id/addOriSkill"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_below="#id/oriSkill"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_input_add" />
</RelativeLayout>
</RelativeLayout>
skillfield.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/addSkillField" >
<Spinner
android:id="#+id/selectSkill"
android:layout_width="180dp"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp" />
<TextView
android:id="#+id/skillPt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="10dp"
android:text=""
android:textSize="20sp" />
<ImageView
android:id="#+id/addSkill"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_gravity="bottom"
android:contentDescription="#string/delSkill"
android:src="#android:drawable/ic_delete" />
</LinearLayout>
In MainActivity.class
skill = (Spinner) findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
how to setup Spinner with the same ID
It is true that if you do this:
skill = (Spinner) findViewById(R.id.selectSkill);
You will be only able to get the first Spinner. I don't think you can add new Spinner dynamically by using <include>.
Here's the main idea of how to achieve what you need:
In fragment_skill.xml, you need to add an empty container view. This container should be inserted in the place you want to add your Spinner every time the button is clicked. This container will hold your Spinners that are added.
Let's say you want to insert here:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<!-- here is where you want to insert your Spinner -->
<ImageView.../>
</RelativeLayout...>
So you add a LinearLayout at that spot, like this:
<RelativeLayout...>
<TextView.../>
<TextView.../>
<LinearLayout
android:id="#+id/container"
android:orientation="vertical
... />
<ImageView.../>
</RelativeLayout...>
You can add any other type of layout, depending what you need, it doesn't have to be LinearLayout. After that, you grab that LinearLayout from your activity/fragment like this:
LinearLayout mContainer; // make this instance variable
mContainer = (LinearLayout)findViewById(R.id.container);
Also grab the button so that you can set up a listener, let's assume it's called Button mButton:
mButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
addNewSpinner();
});
So when the button is clicked, you will call addNewSpinner(). Now, make a method called addNewSpinner(). Inside, the method, we will be inflating skillfield.xml. The method should look something like this:
public void addNewSpinner(){
View view = getLayoutInflater().inflate(R.layout.skillfield, null);
Spinner skill = (Spinner) view.findViewById(R.id.selectSkill);
ArrayAdapter<CharSequence> skillAdapter = ArrayAdapter.createFromResource(context, R.array.skills, R.layout.spinner);
skillAdapter.setDropDownViewResource(R.layout.spinner_down);
skill.setAdapter(skillAdapter);
mContainer.addView(skill);
}
If you are not sure what inflate means, here is the explanation:
Instantiates a layout XML file into its corresponding View objects.
Hope it helps!
I am new to Android development. I have created many screens and all are working quite nicely except one.
I am not sure why screen is not coming up as per XML. Below is my code :
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="10dip"
android:id="#+id/linearMain">
<TextView
android:id="#+id/courseTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textColor="#2258A2"
android:layout_marginBottom="10dp"
/>
<TextView
android:id="#+id/assignmentTitle"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
/>
<TextView
android:id="#+id/lesson"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Lesson : Lecture 5"
/>
<TextView
android:id="#+id/dueDate"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Due Date : Nov 20, 2014"
/>
<TextView
android:id="#+id/marks"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="14sp"
android:textColor="#000000"
android:text="Marks : 20"
/>
<Button android:id="#+id/btnDownload"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Download"
/>
</LinearLayout>
And here is main class :
public class AssignmentDetail extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_announcement_detail);
Intent intent = getIntent();
//String course = intent.getStringExtra(Assignments.COURSE);
//TextView textview = (TextView) findViewById(R.id.courseTitle);
//textview.setText(course);
String announcementTitle = intent.getStringExtra(Announcement.ANNOUCEMENT_TITTLE);
TextView announcementTitleTextView = (TextView) findViewById(R.id.annoucementTitle);
announcementTitleTextView.setText(announcementTitle);
}
And here is output :
You can see there are 4 textView and one button in my xml but i can see only onr textView in output, why it is so and how to fix it.
Thanks..
Anjum
Make sure following stuff is correct in ur activity :
Make some changes in layout file and see it display that changed stuff
Correct xml file layout is specified at setContentView.
It seems that you are using some other layout xml..try removing textview and run it..if it still shows that Assignment #1 then ur xml itslef is wrong
I think you are not inflating the right XML layout file.
You can see that the TextView that you modify at the end of your code is not present in the XLM file. Therefore the layout that you inflate cannot be the one you are showing us or you would have an error at run time.
Given the following code:
<RelativeLayout
android:id="#+id/rel"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textPersonalInfoEmpty"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="20dp"
android:hint="Edit to set your personal info"
android:textSize="16sp"
android:visibility="gone" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:hint="Name"
android:visibility="gone"
android:textSize="16sp" />
<Button
android:id="#+id/insertGameId"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/editName"
android:layout_marginTop="20dp"
android:text="Insert" />
</RelativeLayout>
The TextView and the EditText are visible depending the user interaction and both will never be visible at the same time. One per time only!
The problem is the third component(Button) need to change the attribute android:layout_below depending on which component is visible.
I need to this programmatically.
Any help?
Use Relativelayout for your textview and edittext. And relativelayout's height:wrap-content
Then give an id to your relativelayout
and then add this line your xml file for your button
<Button
android:layout_below="#id/RealtiveLayoutid" />
Thats all
First, use a ToggleButton instead of Button. It can have all the attributes that you assigned to it.
Next add this to the ToggleButton:
<ToggleButton
....
android:textOn="#string/ifTextView"
android:textOff="#string/ifTextBox"
android:onClick="onToggleClicked"
/>
Then this for the activity
public void onToggleClicked(View v) {
boolean on = ((ToggleButton) v).isChecked();
if(on) {
theTextView.setVisibility(View.VISIBLE);
theTextBox.setVisibility(View.INVISIBLE);
} else {
theTextBox.setVisibility(View.VISIBLE);
theTextView.setVisibility(View.INVISIBLE);
}
}
Remember to import the required classes.
Please tell me, how i can to put text inside a EditText like this
And only right text can be edit.
Keep TextView and EditText inside Relative Layout and Adjust them according to your requirement.
Example:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<EditText
android:id="#+id/Edittext"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="20dp"
android:ems="10"
android:hint="To my first character" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/Edittext"
android:layout_alignBottom="#+id/Edittext"
android:layout_alignParentRight="true"
android:text="123123" />
</RelativeLayout>
Change it if you have any changes
Try this...
Layout:
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp" >
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/mobile"
android:inputType="phone"
android:background="#drawable/some_edittext_background"
android:labelFor="#+id/editText"
android:maxLength="9"
android:paddingBottom="8dp"
android:paddingStart="48dp"
android:paddingEnd="8dp"
android:paddingTop="8dp" >
<requestFocus />
</EditText>
<TextView
android:id="#+id/prefix"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:paddingStart="8dp"
android:text="+971" />
</FrameLayout>
Result:
EditText editText = (EditText)findViewById(R.id.edit_text);
editText.setText("Количество", TextView.BufferType.EDITABLE);
You need to combine 3 components:
a container : use a RelativeLayout since it allows views overlapping.
a TextView align to the right edge of the RelativeLayout (width = wrap_content)
an EditText taking the full width of the RelativeLayout (width = match_parent)
To put a text in the EditText: myEditText.setText("myText")
why not just setting the text using setText() and keeping the left part constant?
Im assuming your asking how to put text inside a TextView, programatiacally it is done this way
textView.setText("anything you want..."); //enter a string into parameters
where textView is a instance of the TextView class.
this is my main .xml 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"
android:padding="5dip"
>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:textColor="#color/mbackground1"
android:gravity="center_horizontal"
android:text="#string/decode_label"
android:padding="5dip"
/>
<TextView
android:id="#+id/mytext"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#color/mbackground2"
android:textColor="#color/mytextcolor"
android:padding="5dip"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label"
android:gravity="center_horizontal"
android:textColor="#color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="#+id/webbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/web_button"
android:textColor="#color/mytextcolor"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label1"
android:gravity="center_horizontal"
android:textColor="#color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="#+id/callbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/call_button"
android:textColor="#color/mytextcolor"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/continue_label2"
android:gravity="center_horizontal"
android:textColor="#color/mytextcolor"
android:padding="5dip"
/>
<Button
android:id="#+id/emailbutton"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/sendemail_button"
android:textColor="#color/mytextcolor"
/>
</LinearLayout>
i want that based on output at runtime it should show only one textview and button corresponding to that output. im defining layout in main.xml file and also i am ew in this field.
does any one have any idea.
thanks in advance
I assume you know how to get a reference to the views you defined, for example:
Button button = (Button)findViewById(R.id.emailbutton)
You will need to define an id to each and every view you want to use in the code, just like you did to the emailbutton:
android:id="#+id/emailbutton"
In order to set the visibility of a view you call:
button.setVisibility(View.GONE);
you have the option to set the visibility to INVISIBLE and VISIBLE.
Then you can play with the visibility as you like.
The differnece between INVISIBLE and GONE is that GONE removes the view completely from the layout while INVISIBLE "saves" the space this view takes.
You can see that in the API examples.
To remove yourview in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.GONE);
To transparent yourview in java code:
Button btn=(Button)findViewById(R.id.btn);
btn.setVisibility(View.INVISIBLE);
To remove yourview in Xml file:
<yourView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"/>
To transparent button in Xml file:
<yourView
android:id="#+id/btn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="invisible"/>
to make a view visible or invisible try this:
yourView.setVisibility(View.GONE);
yourView.setVisibility(View.VISIBLE);
Use textView.setVisibility(View.GONE); - to make View Gone and textView.setVisibility(View.INVISIBLE); - to make view INVISIBLE
The view's visibility can be changed using the View.setVisibility() method, check this link for more info. Hope this helps.
Get the View by ID and make it invisible. For your "mytext" TextView for example:
TextView my = (TextView) findViewById(R.id.mytext); // Get the view you want to manipulate
my.setVisibility(View.INVISIBLE); // Make it invisible
my.setVisibility(View.VISIBLE); // Make it visible again
Always check the documentation first!