I'm trying to do simple interacting interface, I linked the following method with the button by android: onClick so I should when I click the button it should display the alternative image and Text, but when I click on the button nothing happens, what is the missed part here
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView eated = new ImageView(this);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedtext = new TextView(this);
eatedtext.setText("i'm so full");
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
// TODO: Find a reference to the TextView in the layout. Change the text.
}
First problem:
Suppose, you created TextView and ImageView:
ImageView eated = new ImageView(this);
TextView eatedtext = new TextView(this);
In your Activity, I'm sure that you use or xml layout, or custom ViewGroup, so you missed the code, where you adding eated and eatedtext to your layout by calling addView method.
Second problem:
If we suppose that you really created views and added them, then you still have a problem, because you didn't specify layout params for eated and eatedtext by calling setLayoutParams method.
Solution
I think you need to write something like:
ImageView eated = (ImageView) findViewById(R.id.eated_image);
eated.setVisibility(View.VISIBLE);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedText = (TextView) findViewById(R.id.status_text_view);
eatedText.setVisibility(View.VISIBLE);
eatedText.setText("i'm so full");
And in your xml you need to set for those views attribute android:visibility="gone" or android:visibility="invisible"
I suppose, you have defined the TextView in the layout-XML. If so, you don't have to create a new TextView object but instead alter the existing.
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
eatedtext.setText("i'm so full");
Try this one
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
ImageView eated = new ImageView(this);
TextView eatedtext = new TextView(this);
eated.setImageResource(R.drawable.after_cookie);
eatedtext.setText("i'm so full");
linearLayout.addView(eated);
linearLayout.addView(eatedtext);
// TODO: Find a reference to the TextView in the layout. Change the text.
}
Please read the comments
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView eated = (ImageView)findViewById(R.id.android_cookie_image_view);
eated.setImageResource(R.drawable.after_cookie);
TextView eatedText = (TextView)findViewById(R.id.status_text_view);
eatedtext.setText("i'm so full");
}
Update your XML File android:layout_height="0dp" to android:layout_height="wrap_content"
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
thanks for helping out folks.
Here is the right answer, as you mentioned you don't need to identify the identified object in the xml code file like ImageView and TextView so the right code will be
public void eatCookie(View view) {
// TODO: Find a reference to the ImageView in the layout. Change the image.
ImageView image = (ImageView)findViewById(R.id.android_cookie_image_view);
image.setImageResource(R.drawable.after_cookie);
TextView text = (TextView) findViewById(R.id.status_text_view);
text.setText("i'm so full");
// TODO: Find a reference to the TextView in the layout. Change the text.
}
Related
I have a RelativeLayout called current_layout which I place my views on. When I attempt to addView(TextView) , nothing is displayed. However when adding an ImageView, it works just fine. Why is my TextView not displaying?
public static void draw_shard(int x, int y, int amount_collected){//X and Y are GAMESURFACE values. Needs to increment by gamesurface y.
ImageView shard = create_iv(); // Creates a new instance of an ImageView (parameter is the context of MainActivity)
shard.setBackgroundDrawable(shard_icon);
shard.setX(x);
shard.setY(y+ImageLoader.get_score_bar_height());
TextView tv = new TextView(MainActivity.current_context);
tv.setX(shard.getX() + shard.getWidth());
tv.setY(shard.getY());
tv.setTypeface(Variables.joystix);
tv.setTextSize(shard.getHeight());
tv.setLayoutParams(new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT));
tv.setText("+" + amount_collected);
tv.setTextColor(Color.WHITE);
current_layout.addView(shard);
current_layout.addView(tv);
}
I am adding the TextView on top of a black background also.
The problem was with shard.getWidth() and shard.getHeight() , which were returning 0.
An alternate and easy way to do that is:
Add the TextView in the layout file and set its visibility to gone, and when you need to show the TextView, just change the visibility of that TextView.
Sample code for XML file:
<TextView
android:id="#+id/textview"
android:layout_height="wrap_content"
android:layout-width="wrap_content"
android:visibility="gone">
<!-- Add other attributes too -->
And when you need that TextView, add this line of code:
findViewById("textview").setVisibility(View.VISIBLE);
findViewById("textview").setText("" + amount_collected);
// Create LinearLayout
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.HORIZONTAL);
// Create TextView
TextView product = new TextView(this);
product.setText(" Product");
ll.addView(product);
Please try it.
But why are you adding TextView using java code?
You can easily do it in XML.
<TextView
android:layout_width="wrap_content"
android:id="#+id/textView"
android:layout_height="wrap_content"
android:text="Hello World!"
android:background="#color/colorPrimary"
android:textColor="#color/colorPrimary" />
It will help you to understand
LinearLayout linearLayout = new LinearLayout(this);
setContentView(linearLayout);
linearLayout.setOrientation(LinearLayout.VERTICAL);
TextView textView = new TextView(this);
textView.setText("Your Text that you want to add");
linearLayout.addView(textView);
Thanks
I have a textview with an attribute drawable_right as shown below in the xml file. I want to make only this drawable clickable, so that when the user click the drawable part of the textview a certain aktion occure. Also i have checked some questions on stackoverflow but most of them are using spannable which needs a start and end parameters to be specified, and in my case, there are no start nor end to provide.
Is there any way to make the drawable part of the TextView clickable?
XML:
<TextView
....
....
android:clickable = "true"
android:drawable_right = "#drawable/accept"/>
Create a custom view extending LinearLayout like this:
public class CustomView extends LinearLayout{
public CustomView(Context context){
ImageView mImageView = new ImageView(context);
TextView mTextView = new TExtView(context);
addView(mImageView);
addView(mTextView);
}
public void setImageClickListener(OnClickListener mOnClickListener){
mImageView.setOnClickListener(mOnClickListener);
}
(....)
}
More on custom views:
http://developer.android.com/training/custom-views/create-view.html
I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout
How can I dynamically add a TextView to this? The commented out code doesn't work.
public class myTextSwitcher extends Activity {
private TextView myText;
public myTextSwitcher(String string){
//myText = new TextView(this);
//myText.setText("My Text");
}
}
You're creating a text view and setting its value but you're not specifying where and how it should be displayed. Your myText object needs to have a container of some sort which will make it visible.
What you're trying to do is dynamically layout a view. See here for a good starter article. From the article:
// This is where and how the view is used
TextView tv = new TextView(this);
tv.setText("Dynamic layouts ftw!");
ll.addView(tv);
// this part is where the containers get "wired" together
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
First of all, you shouldn't be adding it in the constructor, non-default constructors are pretty much useless for an Activity. Finally, you are correctly creating a new TextView but you are not adding it anywhere. Get ahold of some layout in your content view (probably with findViewById), and call layout.addView(myText) with it.
Did you add the your text view to the activity using setContentView(myText);
make this
myText = new TextView(this);
myText.setText("foo");
setContentView(myText);
in oncreate() method
final TextView tv1 = new TextView(this);
tv1.setText("Hii Folks");
tv1.setTextSize(14);
tv1.setGravity(Gravity.CENTER_VERTICAL);
LinearLayout ll = (LinearLayout) findViewById(R.id.lin);
ll.addView(tv1);
Your activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/lin"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_vertical|center_horizontal"
android:orientation="horizontal">
</LinearLayout>
my TextView that I add to my LinearLayout is not visible...why ?
layoutVenues = (LinearLayout) findViewById(R.id.layoutv);
layoutVenues.addView(genTextView(v.getName()));
layoutVenues.addView(genLineView());
and the genTextView Method:
public TextView genTextView(String text) {
TextView tv = new TextView(this);
tv.setText(text);
tv.setTextColor(Color.BLACK);
return tv;
}
You need to set layout parameters otherwise you will not have a proper layout
public TextView genTextView(String text) {
TextView tv = new TextView(this);
LayoutParams lp = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutPararms.WRAP_CONTENT);
tv.setLayoutParams(lp);
tv.setText(text);
tv.setTextColor(Color.BLACK);
return tv;
}
Try to add visiblity to your view. .setVisibility(View.VISIBLE);
or place it in your xml and instantiating in the code will also be good idea.
I build most of my view hierarchies for Android using XML layout files, so I'm not an expert on programmatically assembling view hierarchies. However, one thing that jumps out at me is that you don't appear to set any layout parameters on the TextView that is returned by genTextView(). Also, take a look at the layout parameters of the LinearLayout in your XML file and make sure that it is actually getting assigned screen real estate.
The default background is black I believe? So you have black text on a black background. Its probably not that easy though :P Might want to post the xml where the linearlayout is defined.