I wolud like to set button's gravity value from default center to right using TextView.setGravity() method, but when I call it nothing happens. In LogCat output using getGravity() method I still see the first value - 17 (center).
Tell me please, how can I change button's gravity value, using setGravity() method?
Here is code snippet which describes setting the view's gravity:
public void setGravity(String gravitation) {
if(controlledView != null) {
String gravityValues[] = gravitation.split("|");
for(String gravity : gravityValues) {
if(gravity.equals("center")) {
controlledView.setGravity(Gravity.CENTER);
}
else if(gravity.equals("top")) {
controlledView.setGravity(Gravity.TOP);
}
else if(gravity.equals("bottom")) {
controlledView.setGravity(Gravity.BOTTOM);
}
else if(gravity.equals("right")) {
controlledView.setGravity(Gravity.RIGHT);
}
else if(gravity.equals("left")) {
controlledView.setGravity(Gravity.LEFT);
}
}
}
}
where controlledView is instance of Button class.
TextView textView = (TextView)findViewById(R.id.mytextviewid);
textView.setGravity(Gravity.RIGHT);
I'm assuming you weren't using Gravity.RIGHT; other valid values can be found in the Gravity constants.
I'm sory for this post, found my mistake myself. I pass to split() method Java Regex API's metacharacter '|' and get all symbols of string. Should choose any other split symbols for string.
You can use in XML layout's like > (RightClick->Properties->Gravity->"your option")
Related
I know that it is possible to perform a click on a view like this :
view.PerformClick()
How do I do it on TextInputLayout EndIcon button?
Update
The problem is that I have a bunch of InputLayouts and use a generic function to set the click listeners on them like so
fun setTextInputLayoutListeners(
inputLayout: TextInputLayout, editText: TextInputEditText,
actionSet: () -> Unit,
actionClear: () -> Unit
) {
with (inputLayout) {
setOnClickListener { actionSet() }
setEndIconOnClickListener { actionClear() }
}
editText.setOnClickListener { actionSet() }
}
and call it with different parameteres like this
setTextInputLayoutListeners(
categoryInputLayout, categoryEditText, { onCategoryClick() }, { onCategoryClear() }
)
setTextInputLayoutListeners(
dateInputLayout, dateEditText, { onDateClick() }, { onDateClear(calendar) }
)
so I'm looking for a generic solution, sort of
inputLayout.EndIcon.PerformClick()
textinput.setEndIconOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// do some code
}
});
hope it helps..
EndIcon in TextInputLayout is of type CheckableImageButton and its id is R.id.text_input_end_icon (use R.id.text_input_start_icon for StartIcon). To simulate clicks, you need to find the button using findViewById, then cast it as a CheckableImageButton and call performClick().
In your case:
inputLayout.findViewById<CheckableImageButton>(R.id.text_input_end_icon)?.performClick()
Thank you so much for you clear and helpful answer.I did a little trick that worked and i wanna share my answer to everyone who will need it.
First thing i did is get the string value of my view :
Log.d("tag", String.valueOf(v));
i got that : com.google.android.material.internal.CheckableImageButton{ba604a VFED..C.. ...P.... 8,3-104,99 #7f090125 app:id/text_input_end_icon}
and like i suspected then icon is a different view of the text field layout with different id (in the end of the string value of the view).
So i changed my if condition from if (v.getId() == R.id.start_date_Layout) to if (v.getId() == R.id.text_input_end_icon), and it work now
i hope this answer will be helpful to someone, thank you again for all your answer
I want to get the current background to do a condition base on it.
For example, I have a xml with a next arrow, if the background=R.drawable.A, I want to change the background to R.drawable.B when next Button is pressed.
I defined my relative layout as follows :
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
if (rl.getBackground()== R.drawable.A){ //here the error
rl.setBackgroundResource(R.drawable.B);
}
The error is :
incompatible operands types int and drawable.
If there a way to get the current background and base on it do something?
Actually I don't know why they didn't override the equals method in the Drawable class. So you should use getConstantState() method from the Drawable object that returns a Drawable.ConstantState instance that holds the shared state of this Drawable to be able to compare it.
Kotlin
val drawableAConstantState = ContextCompat.getDrawable(this, R.drawable.A)?.constantState
rl.setBackgroundResource(if (rl.background?.constantState == drawableAConstantState) R.drawable.B else R.drawable.A)
Java
if (rl.getBackground() != null && rl.getBackground().getConstantState().equals(getResources().getDrawable(R.drawable.A).getConstantState()) {
rl.setBackgroundResource(R.drawable.B);
} else {
rl.setBackgroundResource(R.drawable.A);
}
You can check if the background drawable is null or not like this:
if (rl.getBackground() != null){
rl.setBackgroundResource(R.drawable.B);
}else{
// do whatever you want
}
Well to remedy the error just change it to:
final RelativeLayout rl = (RelativeLayout) findViewById(R.id.myLayout);
if (rl.getBackground() == getResources().getDrawable(R.drawable.A){ //<-- fixes the error
rl.setBackgroundResource(R.drawable.B);
}
a side note: I suggest you check which drawable should be set against a logical component and not it's manifestation in the UI, but this is up to you.
I build EditText dynamically. Among other things, I set 2 properties: hint(.setHint) and inputType(.setInputType). My problem: when I invoke setInputType, setHint has no effect: blank edittexts remain blank with no hint. Once I comment out setInputType, I see all hints. I need both input type and hint. What to do? My code:
private EditText buildTextBox(Property property)
{
EditText control = new EditText(this);
control.setInputType(getInputTypeByPropertyInputType(property.getType()));// android.text.InputType.
control.setHint(property.getDisplayName());
return control;
}
private int getInputTypeByPropertyInputType(String type)
{
if (type.equals("integer"))
{
return android.text.InputType.TYPE_CLASS_NUMBER;
}
else
{
return android.text.InputType.TYPE_CLASS_TEXT;
}
}
#Eugene
Ensure you call control.SetHint() just before you call the control.setGravity() and control.setInputType(); and it works for me verrry much!
column1 = new EditText(this);
column1.setId(i);
column1.setHint("Value");
column1.setInputType(InputType.TYPE_CLASS_NUMBER);
column1.setGravity(Gravity.RIGHT);
I agree with Eugene.
Remove the gravity(just don't use CENTER) and the hint texts will come back as normal.
Nice find!
I wanted to put a TextView into a CustomView. I had put it by using findViewById. That TextView I had directly inserted into the xml. Now i wanted to add text when a touch event is made.
Inside the ViewActivity I had put this.
public void getTextv(TextView tv)
{
tv1=tv;
}
int hit;
public void setText()
{
if(touch==true)
tv1.setText(hit);
}
Inside the main i had put ViewActivity.getTexttv(tv);
Then i got a error whenever text was added.
Is your error a nullpointerexception maybe?
shouldn't you set touch=true in getTextv(..)?
tv.setText(String.valueOf(hit))
you should parse it to String firstly.
for the setText(int), the parameter should be a string resource.
Use
tv.setText(hit+"");
As textview also takes an integer valuw which it thinks as an id from R.java file. So , you will get the error like :
Resourse id 0x02 was not found
i'm defining my own list adapter and i want an image inside it to be shown OR hidden based on a value what i've noticed that its always invisible or visible disregarding the value
Here's my code , this code is inside the getView method
singleRow=data.get(position);
readit = singleRow.getRead();
Log.i("readit","" + readit );
//NotificationID=singleRow.getId();
holder.title.setText(singleRow.getAttach_title());
holder.date.setText( singleRow.getAttach_created());
holder.dueDate.setVisibility(ImageView.INVISIBLE);
holder.course.setText(singleRow.getCourse_title());
if(readit==1)
{
//holder.read.setImageResource(IGNORE_ITEM_VIEW_TYPE);
holder.read.setVisibility(ImageView.INVISIBLE);
}
else
{
holder.read.setImageResource(R.drawable.unread);
}
holder.dueDate.setVisibility(View.GONE);
You're never setting your read image back to VISIBLE after you once set it to INVISIBLE. In else you, probably, should have setVisibility(VISIBLE)
Use this code.It will solve your problem
singleRow=data.get(position);
readit = singleRow.getRead();
Log.i("readit","" + readit );
//NotificationID=singleRow.getId();
holder.title.setText(singleRow.getAttach_title());
holder.date.setText( singleRow.getAttach_created());
holder.dueDate.setVisibility(ImageView.INVISIBLE);
holder.course.setText(singleRow.getCourse_title());
holder.read.setImageResource(R.drawable.unread);
if(readit==1) {
//holder.read.setImageResource(IGNORE_ITEM_VIEW_TYPE);
holder.read.setVisibility(View.INVISIBLE);
}