I have a problem with this piece of code. I have the ids registered in my XML, but the code isn't working. The main is in red, the add_photo is in red, the targeturi and targetimage are all in red (Everything in red is in italic).
setContentView(R.layout.*main*);
Button buttonAddPhoto = (Button)findViewById(R.id.*add_photo*);
textTargetUri = (TextView)findViewById(R.id.*targeturi*);
targetImage = (ImageView)findViewById(R.id.*targetimage*);
XML:
<TextView
android:text= "Thot of The Day!"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="60sp"
android:layout_centerHorizontal="true"
android:gravity="center"
android:id="#+id/text_shout_out"
/>
<Button
android:id="#+id/add_photo"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Add Photo"
android:gravity="center"
android:layout_below="#+id/text_shout_out"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_marginTop="131dp"
android:onClick="Image"/>
Since the name of your activity layout is activity_main, you need to do
setContentView(R.layout.activity_main);
You don't have TextViews with id of R.id.targeturi and R.id.targetimage anywhere in your layout file. You need to put them in your layout first.
Also, you need to declare your TextViews(unless you have declared them already before onCreate())
TextView textTargetUri = (TextView)findViewById(R.id.targeturi);
TextView targetImage = (ImageView)findViewById(R.id.targetimage);
I think you are having a problem because you didn't declare a type for your variables "textTargetUri" and "targetImage".
Related
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.
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.
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.
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 a set of Views consisting in an ImageButton, an ImageView and a TextView.
The code looks like this:
<ImageButton
android:id="#+id/imagebutton_com"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#drawable/bean_bg"
/>
<ImageView
android:id="#+id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/arrow_1"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="20dp"
/>
<TextView
android:id="#+id/textview_wizard_main_button"
android:text="Soybeans"
android:layout_toRightOf="#id/imageview_arrow"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:textStyle="bold"
android:textColor="#color/white"
android:layout_margin="10dp"
/>
All works as expected, but I want to change the textColor of the TextView when the Button is focused.
I tried writing a selector xml file to do the same, but nothing worked.
Any suggestion?
You need to do it in code. Use OnFocusChangeListener on your ImageButton to achieve this.
Create a folder color under resources and add selector.xml file to that folder
change property of TextView namely
android:textColor = "#color/white"
to
android:textColor = "#color/selector"