I want to give some styles to a Android button dynamically , based on what integer value entered. for example if user enter 25 my button shape will change to "quarter of a circle". like below picture.
quarter of a circle image
I try to use
GradientDrawable shape=new GradientDrawable();
shape.setShape(GradientDrawable.OVAL);
but i can't find useful method for make quarter of Oval
In other words i want to make a clickable pieChart
don't offer me github.io libraries.
Please read this notes.
1. I know how to used <selector> element and change button style based on that's state.
2. I know that how to using <shape> drawable resource. but I need change shape based on what value, user enter.
I am assuming you mean Edit Text View as you can not enter text on it. You can do this programmatically. You need to use instance of button as a reference and add a TextWatcher to your edit text view and add the if statement in there.
Use the following as an example:
Button button = findViewById(R.id.<id of button>);
EditText editText = findViewById(R.id.<id of edit text view>);
editText.addTextChangedListener(new TextWatcher() {
#Override
public void beforeTextChanged(CharSequence charSequence, int i,int i1, int i2) {}
#Override
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
if(charSequence.equals("25")){
button.setBackgroundResource(R.drawable.<id of custom button>);
}
}
#Override
public void afterTextChanged(Editable editable) {}
});
Related
How to make a custom Data Picker like Date Picker in Android so that I can get data (int data) by custom input or by using the buttons (- & +) so that it become easy for user to enter larger value !!
Has anyone any idea how to implement this?
please provide example, code snippet if possible.
I want to make like this
left - Button
middle - EditText
right - Button
I want Like this image--
This is the example of Date Picker in Android:-
then try this link
try on text changed listener as described in the link to update the variable whenever user changes the edit text, then variable will hold the changed value and so you can use your method to change values of that variable
Design a layout as you want with two buttons and one edittext in middle
Then give one button addition funtionality and other substraction functionality
Follow little code snippet, provided below
addButton.setOnClickListener(new OnClickListener(){
public void onClick(){
int valueInEditText = Integer.parseInt(etReference.getText().toString());
etReference.setText(String.valueOf(valueInEditText + 1));
}
});
Similaryly, do it for substration button.
Note: Do validation if you don't want negative values.
You might want to use the NumberPicker which will allow you to select any value.
It's not the same look but it might work for you:
https://android--examples.blogspot.com/2015/05/how-to-use-numberpicker-in-android.html
After reading through various posts, examples, articles and Android documentation I found solution to my own question, It can be achieved by two ways:
First by using NumberPicker
Secondly by adding TextChangedListener to the EditText
Here is the code how I achieved it :
int noOfCups = 2;
EditText editTextNoOfCups;
editTextNoOfCups = (EditText) findViewById(R.id.edit_text_no_of_cups);
editTextNoOfCups.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(s.length() != 0) {
Log.e("pss", String.valueOf(s));
noOfCups = Integer.parseInt(String.valueOf(s));
}
}
#Override
public void afterTextChanged(Editable s) {
}
});
Here is the link to official Android documentation :
TextWatcher | Android Developers
When the EditText line is blank (the default), I'd like the ImageView icon color to be white.
When the icon is pressed, I'd like the color to be black and then revert back to white because the press clears the EditText line, so it would then again be empty.
With the ImageView, I first set src equal to a white drawable. Any thoughts on how to switch the color state based on the length test of the EditText line?
I'm not too exactly sure what the question is asking for, I got confused when you brought in ImageView and hover because I don't believe phones support the onHoverListener as well.. Do you mean changing the EditText accent color when you click on it to type something? If so it will be under the colors.xml-->colorAccent
Also if you mean to change an images background color
public void onClick(View v) {
imageView.setBackgroundColor(Color.parseColor(EditText.getText().toString()));
}
Hopefully I provided you with some help to continue your project.
To update icon if textview empty, check Android: How can I validate EditText input? with addTextChangedListener() then
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {
Log.i("Text", charSequence + "");
if(charSequence.length()==0)
ib.setImageResource(R.drawable.button_unpressed);
else
ib.setImageResource(R.drawable.button_pressed);
}
and to change imagebutton background follow its state How to set image button backgroundimage for different state? with "create an xml in your drawable"
If you want to keep color of icon remain black after pressed, just override onClick() by
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
ib.setImageResource(R.drawable.button_pressed);
}
});
In my app, I want the users to give their answers in the form of text through edit text. So for the correct answer I want the letters to turn green (or red for incorrect) on the fly while typing.
For example, if the answer is DOG, I want the the text to turn green if the user types DOG dynamically. Even if the the first letter he types is D then I want the text color to be green. Only when the user's input text is not correct do I want it to be red. The text color should change on the fly while typing.
Create EditText and call addTextChangedListener for it supplying custom TextWatcher where you mostly need to override its onTextChanged.
In this method change your text color according to your logic.
Snapshot :
mEditBox = (EditText) findViewById(R.id.my_edit_box_id);
mEditBox.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
String currentText = mEditBox.getText().toString();
// highligt correct answer in green
if ("DOG".startsWith(currentText)) { // user starts typing "DOG"
mEditBox.setTextColor(Color.GREEN);
} else {
mEditBox.setTextColor(Color.RED); // incorrect input
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
}
#Override
public void afterTextChanged(Editable s) {
}
});
Hi I am learning Android programming and have run into an issue that I couldn't get a clear answer to through researching.
I have a TextView which serves as a label for my EditText. I have a method which checks if the EditText is an empty String. If the string is empty I want to be able to get a reference to the TextView that corresponds to that EditText in order to make a toast saying something like "please enter a value for ".
I've looked into getLabelFor/setLabelFor but is there a way to do this in the layout XML?
What is best practice for this type of functionality.
You're describing a functionally that is build in to EditText. There is a special field you can define in xml called hint, which is the recommended way to label an EditText rather than a nearby TextView. Additionally, EditText has a method called setError() (link). If the user attempts to hit a submit button, for example, you can check to see if the EditText is empty and if so, call setError().
I wonder if the following is the thing that you need
TextWatcher inputTextWatcher = new TextWatcher() {
public void beforeTextChanged(CharSequence s, int start, int count, int after){
}
public void onTextChanged(CharSequence s, int start, int before, int count) {
if (s.toString().equals("")) {
textView.setText("please enter a value for ..");
} else {
textView.setText("<the textedit is not empty>");
}
}
public void afterTextChanged(Editable s) {
}
};
editText.addTextChangedListener(inputTextWatcher);
Let's say that I want a user to change the background color of the app. I was thinking about creating several xml layout files and then have a ListView with the list of the layout files. Then, when the user selects one of those, the corresponding xml file would be loaded.
Is this possible? If yes, how should I do this?
The most simple way do it dynamic in code. Just save user color to SharedPreferences and use it in code as background for your Views.
So I suggest to use ColorPikerDialog to choose any color. You can use for example this library https://github.com/gsingh93/android-ColorPickerPreference
SharedPrefrences prefs = getSharedPrefrences(YOUR_SHARED_PREFS, 0);
int colorId = prefs.getInt(BACKGROUND_COLOR, 0);
if(colorId != 0) {
setBackgroundToColorId(colorId);
}
Good luck!
I have already did this, When the user select a color from the listview track that colorid and save it in the share preferences as #llya Demidov said.
editor = getSharedPreferences(PREFS_NAME, 0).edit();
editor.putString(PREF_COLOR, <userselectedcolor>);
before loading each activity do like this
pref = getSharedPreferences(PREFS_NAME, 0);
int color= pref.getString(PREF_COLOR, null);
if(color!=0)
{
yourlayoutid.setbackgroundColor(Color);
}
Consider this thing as the suggestion only,
You can put an edittext which take the color code of the color the user wants,With that you can use a TextWatcher as colors have hexadecimal values and each color has fix length of 6 alphanumeric character like "FF0000" ,as soon as the length goes to six the color will apply.
searchBox.addTextChangedListener(new TextWatcher() {
public void onTextChanged(CharSequence s, int start, int before, int count) {}
public void beforeTextChanged(CharSequence s, int start, int count, int after) {}
public void afterTextChanged(Editable editable)
{
if(editable.toString().length==6)
//set color of background
}
});
}