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
Related
I have created a textview dynamically and want to make it scrollable.
final RelativeLayout.LayoutParams params = parseLayoutParams(
frameMargins, context);
tv.setText(Utility.getpropertyString(controls.getText()));
final String textColor = Utility.getpropertyString(controls.getTextcolor());
tv.setTextColor(Color.parseColor(textColor));
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setTextSize(tSize);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setMaxLines(controls.getMaxlines());
tv.setTag(controls.getTagId());
tv.setLayoutParams(params);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setVisibility(controls.getVisibility());
tv.setVerticalScrollBarEnabled(isScrollable);
tv.setScroller(new Scroller(context));
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setScrollBarFadeDuration(0);
But I am not able to see scrollbar in the textview niether when not scrolling nor when we scroll it. Please help!
From Api 21, the View scrollbar visibility is only reserved for xml layouts because an important function called initializaScrollBars was removed due to an issue while passing TypeArray variable as a parameter.
So, to accomplish what you need programmatically, you can do it like this
Create an xml layout called scrolltextview.xml
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical"/>
Now, to create it programmatically
TextView tv = (TextView) LayoutInflater.from(this).inflate(R.layout.scrolltextview, null, false);
// now decorate it as your needs
tv.setText(m);
tv.setTextColor(Color.BLUE);
tv.setTextSize(23);
...
tv.setMovementMethod(new ScrollingMovementMethod());
// this is needed only if you want to show scrollbar also when text is not scrolling
tv.setScrollBarFadeDuration(0);
// thecontainer = the layout you want to add your new textview
thecontainer.addView(tv);
As Ferran noted, initializeScrollBars() has been removed. See here for the bug report and the rationale for its removal. As far as I can tell, there is no other strictly programmatic way to specify scollbars for a view. All paths go through XML. :-(
I think that Ferran's answer is a good way to go: it works, is easy to understand and should be easy to document. There are, however, other ways to programatically create a TextView with scrollbars with a slight assist from styles.
For API 21 and above
Define a style called "ViewWithScrollBars" as follows:
<style name="ViewWithScrollbars">
<item name="android:scrollbars">vertical</item>
</style>
We can now use the four-argument constructor for TextView to apply the style.
TextView tv = new TextView(this, null, 0, R.style.ViewWithScrollbars);
This method will create a TextView with scrollbars. There is at least one caveat however. When a TextView is created with a single argument
new TextView(Context)
The constructor telescopes through other constructors that add additional arguments. One of these constructors is defined as follows:
public TextView(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, com.android.internal.R.attr.textViewStyle);
}
The third argument com.android.internal.R.attr.textViewStyle is an Android internal style that will pick up a default textViewStyle from the theme. The call I suggest uses zero for the third argument, so any textViewStyle defined in the theme will not be applied.
A reasonable fix for this might be to do the following:
tv = new TextView(this, null, android.R.attr.textViewStyle, R.style.ViewWithScrollbars);
Unfortunately, if the third argument (defStyleAttr) is defined in the theme, then the fourth argument (defStyleRes) is not used. As a result, the scrollbars will not appear.
If you use textViewStyle then you will have to make adjustments or just use the following approach.
For all APIs
Using the style "ViewWithScrollBars" from above, we can define a ContextThemeWrapper that will install scrollbars into the theme that will be used to create the TextView.
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.ViewWithScrollbars); // "this" is the Activity
tv = new TextView(ctw);
I refer you to an article by Chris Banes entitled "Theme vs Style" that explains how theme overlays work.
The following puts all this together.
MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TextView tv = new TextView(this);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
// This will actually work for API 21 and above.
ContextThemeWrapper ctw = new ContextThemeWrapper(this, R.style.ViewWithScrollbars);
tv = new TextView(ctw);
} else {
tv = new TextView(this, null, 0, R.style.ViewWithScrollbars);
}
tv.setText(R.string.lorem);
tv.setTextColor(Color.parseColor("red"));
tv.setTextSize(12);
tv.setGravity(Gravity.CENTER_VERTICAL);
tv.setTextSize(TypedValue.COMPLEX_UNIT_SP, 24);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setMaxLines(7);
tv.setTag(View.generateViewId());
RelativeLayout.LayoutParams params =
new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,
RelativeLayout.LayoutParams.WRAP_CONTENT);
tv.setLayoutParams(params);
tv.setEllipsize(TextUtils.TruncateAt.END);
tv.setVisibility(View.VISIBLE);
tv.setVerticalScrollBarEnabled(true);
tv.setScroller(new Scroller(this));
tv.setMovementMethod(new ScrollingMovementMethod());
tv.setScrollBarFadeDuration(0);
((RelativeLayout) findViewById(R.id.layout)).addView(tv);
}
}
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.
}
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
I have a LinearLayout within a ScrollView in the xml file. I need to paint and write within the ScrollView so I've created a view in the layout and have been able to paint and write inside using: canvas.draw() and canvas.drawText(), my problem comes when writing text with canvas are not clearly the letters, so I need to add a TextView to the layout of the ScrollView from the class, without knowing very well how to do it.
Paste the code:
public class Calendario extends Activity{
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.calendar);
LinearLayout layout = (LinearLayout) findViewById(R.id.layout);
MyView v = new MyView(this);
layout.addView(v,250,2000);
}
private class MyView extends View{
public MyView(Context context) {
super(context);
}
protected void onDraw(Canvas canvas) {
onDrawMethod();
//Here draw and write text//
}
}
}
A picture of what I need: I need only add textviews inside
Thankss
As I understand you, you want to add those texts programmaticaly, by saying
so I need to add a TextView to the layout of the ScrollView from the class
If so, might be this post will help you.
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.