I needed to correlate checkboxes with their position in the list view in the checkboxes' onClickListener. My first solution was to make a very short custom view that extended checkbox but had an extra variable (position) that would be set in getView().
public class ListCheckBox extends CheckBox {
private int position = -1;
public ListCheckBox(Context context)
{super(context);}
public ListCheckBox(Context context, AttributeSet attrs)
{super(context,attrs);}
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr)
{super(context,attrs,defStyleAttr);}
#TargetApi(21)
public ListCheckBox(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)
{super(context,attrs,defStyleAttr,defStyleRes);}
public int getPosition() {
return position;
}
public void setPosition(int position) {
this.position = position;
}
}
It worked, except it changed the checkboxes' color to black, and nothing I did (including changing android:buttonTint) could change it. For now my solution is a HashMap with a view key and integer value that keeps track of the checkboxes' and their positions, but if anyone has a less ugly solution or any idea as to why I couldn't change the color of the checkboxes, it would be very much appreciated.
You can use the method of setTag() to past a extra variable to a view,then you can use getTag() to get the extra variable. This does not need to custom CheckBox.
//set tag
checkBox.setTag(position);
//get tag
int position = (int)checkBox.getTag();
Related
This question already has answers here:
Android set height and width of Custom view programmatically
(10 answers)
Closed 2 years ago.
I am trying to resize a textbox dynamically using Java code. I want the width to not use wrap content but using a static number in dp. I want this to be done in Java code instead of the XML file. I want it like this is because I want to apply it to each item in the recyclerview. It needs to work with multiple screens sizes. It will work like a min Length for a textfield box size. If you know how to do this would be much appericaiated.
this is called auto sizing and it can be done by adding TextChangedListener which is a listener for Edit Tex. this listener watch the changes of editText and it has three different states. also you can create a component(custom view) and extend it from AppCompatTextView name as you want; in its initialization you can add below code:
public class CustomTextView extends AppCompatTextView {
Context ctx;
public CustomTextView(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
ctx = context;
init();
}
public CustomTextView(Context context, AttributeSet attrs) {
super(context, attrs);
ctx = context;
init();
}
public CustomTextView(Context context) {
super(context);
ctx = context;
init();
}
public void init() {
setOnTouchListener(null);
addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (getText().toString().length() > 10){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeSmall);
}
else if (getText().toString().length() > 5){
setTextSize(TypedValue.COMPLEX_UNIT_SP, textSizeMedium);
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
}
also check these out, there is a tone of documentation for it:
Autosizing TextView Tutorial for Android
Autosizing TextViews
I initially set background of button with this code at onCreateView.
uc.setBackgroundResource(R.drawable.saat_button_none);
If I initially set background or textColor of button I want to prevent style change when I use onClick
public void onClick(View v) {
switch (v.getId()) {
case R.id.bir:
uc.setBackgroundResource(R.drawable.saat_button); //Should not work
dort.setBackgroundResource(R.drawable.saat_button_sel);
bes.setBackgroundResource(R.drawable.saat_button_sel);
}
}
Is that possible?
Edit: I don't want to use if statement since I have lots of buttons I just want to lock style of button.
To do this, create a custom view simply by extending View and override all methods related to background and put your logic their if background has changed once then overridden method should throw exception saying that you can't change the style as it has been changed while setup the default look and feel.
public class CustomButton extends Button {
boolean backgroundChanged = true;
public CustomButton(Context context) {
this(context, null);
}
public CustomButton(Context context, #Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public CustomButton(Context context, #Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void setBackgroundResource(int resid) {
if(backgroundChanged){
throw new RuntimeException("you can't change the style as it has been changed while setup the default look and feel");
}
super.setBackgroundResource(resid);
}
}
At last in layout file replace <Button tag with <CustomButton
I am trying to add an onClick on the soft keyboard for an activity. The reason why is that i want to check if the user is currently active. So what i have done is that if the user clicks on the app i will reset a inactivity timer. The problem is that when a user interacts with the soft keyboard it doesn't call the function onUserInteraction() which is a function I override in the activity. So i need help to find a way to keep track if the soft keyboard has been clicked for every textfield etc I have in the activity. (I know that i can insert a onclick listerner on every EditText field but i rather not do that, because if I would use many EditText fields it would not be so nice)
So this is what i ended up with. I was hoping for something else, but this solves the problem. Thanks for the help!
public class ActivityEditText extends android.support.v7.widget.AppCompatEditText {
private TextWatcher tw;
public ActivityEditText(Context c)
{
super(c);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs) {
super(context, attrs);
this.setOurTCL();
}
public ActivityEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.setOurTCL();
}
private void setOurTCL()
{
this.tw = new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
InactivityManager.resetTime();
}
#Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
#Override
public void afterTextChanged(Editable s) {
}
};
this.addTextChangedListener(this.tw);
}
#Override
public void removeTextChangedListener(TextWatcher watcher) {
if(!watcher.equals(this.tw))
super.removeTextChangedListener(watcher);
}
}
To handle an individual key press, implement onKeyDown() or onKeyUp() as appropriate. Usually, you should use onKeyUp() if you want to be sure that you receive only one event. If the user presses and holds the button, then onKeyDown() is called multiple times.
Create a class some thing like UserInteractionEditText and extend EditText and set the onclick lisetener in that class use that class in all the XML layouts as you use EditText you can do some thing like this:
public class UserInteractionEditText extends EditText implements View.OnClickListener {
public UserInteractionEditText(Context context) {
super(context);
}
public UserInteractionEditText(Context context, AttributeSet attrs) {
super(context, attrs);
}
public UserInteractionEditText(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
#Override
public void onClick(View v) {
//TODO:: Handle user Click Events
}
}
You can use onUserInteraction() function of activity. You need to override this function in your activity.
This function get calls when you perform any kind of interaction with your activity.
#Override
public void onUserInteraction() {
super.onUserInteraction();
// Your code goes here
}
You can refer this example and also this answer, refer the docs here
Hope this helps.
I want to have a custom TextView called MyTextView and i want to set a default TextSize in it's constructor only if no TextSize has been set in it's XML definition. How can i detect if a TextSize has been set in it's XML definition?
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
//How can i read TextSize from AttribureSet??
//if no TextSize has been set then SetTextSize(defaultTextSize);
}
}
Can any one help me please?
You can obtain style attributes like
public class MyTextView extends TextView{
public MyTextView(Context context, AttributeSet attrs) {
super(context, attrs);
String size = attrs.getAttributeValue("http://schemas.android.com/apk/res/android", "textSize");
}
}
Here's example:
final Resources.Theme theme = context.getTheme();
TypedArray array = theme.obtainStyledAttributes(attrs, R.styleable.YourStylable, R.attr.YourDefStyleAttr, 0);
Try this float size = new TextView(this).getTextSize();
How can I create a seekbar that has numbers above it. For example if the SeekBar has Max=10, it will have 0 to 10 above the seekbar.
I tried to do this by creating a custom view that sits above the Seekbar, this view extends linear layout(horizontal), n number textviews (n = seekbar max attribute) are added to the LinearLayout. The problem is that I couldnt get the numbers to sit directly above the 'notches' on the seekbar.
Am I approaching this in the correct way or should I be creating a customview that extends Seekbar and adding the numbers by drawing them onto a canvas? Could someone please give me a few pointers on how this problem can be solved.
Here is the code I have so far, the prpblem is, as stated above, no matter how I try to play around with the padding on both seekbar and my custom view, I cannot get the numbers to align perfectly with the seekbar notches(selected index)
public class SeekBarNumberIndicator extends LinearLayout
{
TextView[] mNumbers;
public SeekBarNumberIndicator(Context context)
{
super(context);
// TODO Auto-generated constructor stub
}
public SeekBarNumberIndicator(Context context, AttributeSet attrs)
{
super(context, attrs);
initView(context, attrs);
}
#SuppressLint("NewApi")
public SeekBarNumberIndicator(Context context, AttributeSet attrs, int defStyle)
{
super(context, attrs, defStyle);
initView(context, attrs);
}
protected void initView(Context context, AttributeSet attrs)
{
super.setOrientation(LinearLayout.HORIZONTAL);
mNumbers = new TextView[11];
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.WRAP_CONTENT, 1);
for(int x = 0; x < 11; x++){
mNumbers[x] = new TextView(context);
mNumbers[x].setLayoutParams(lp);
mNumbers[x].setTextColor(Color.BLUE);
mNumbers[x].setText(String.valueOf(x));
mNumbers[x].setGravity(Gravity.LEFT);
//mNumbers[x].setPadding(10, 0, 0, 0);
super.addView(mNumbers[x]);
}
}
}
UPDATE
I tried the link you posted, the same problem occurs, the sliding text view doesnt appear directly under the thumb, the offset gradually increases. What extra measures could I take to make the textview appear directly under the thumb(if I can solve this then Im sure the solution can be adapted to suite the custom view in my OP). I know I can start adding some logic to the padding that increases by some exponential value but this seems 'hacky' thanks