Android Fragment/Dialog - android

I have 1 Class that are opened two different ways. One way is it's opened from a sliding drawer and another way is as a dialog. Below you can see both of them. However, you can see that the edittext does not look the same in both. How can I modify the dialog style to look like the fragment?
Here is how it is made:
final EditText editTextView = new EditText(a);
editTextView.setHint(R.string.hintNote);
editTextView.setTag(tag); editTextView.setId(_id);

You can add the style attribute to the layout XML. They will both then use the style specified:
style="#android:style/Widget.EditText"

Related

How to add a drawable at the right end of a "TextInputLayout" dynamically?

I am trying to make something like this example:
I want the drawable to show when I verify the user name from the server.
I am using Material Design Text Input Field & EditText. Yes, I can do it with a simple EditText and an ImageView, but I want to use the standard elements.
I have looked at the official documentation, there is a way to add the image at the Right corner using XML, but I want to add it programmatically.
Use the setEndIconDrawable method:
textInputLayout.setEndIconDrawable(R.drawable.xxxx);
textInputLayout.setEndIconMode(TextInputLayout.END_ICON_CUSTOM);
or in a layout:
<com.google.android.material.textfield.TextInputLayout
app:endIconDrawable="#drawable/xxxxx"
app:endIconMode="custom"
More info in the official doc.
You need to to set your drawable programmatically like below
inputTextEditText.setCompoundDrawablesWithIntrinsicBounds(null, null, ContextCompat.getDrawable(context,R.drawable.drawableRight), null)

How can I change the EditText style programatically in Android?

Hi I have a view with a form, and all button, labels, EditText, etc, are defined in a styles files, for example, for EditText I have app_edit_text and app_edit_text_error, my question is how can change in EditText the style from app_edit_text to app_edit_text_error?.
Thanks!
To be honest you can't change style programaticaly. You can only change text
appearance using setTextAppearance(int resId) method:
https://developer.android.com/reference/android/widget/TextView#setTextAppearance(int)
For your purpose you can use setError(String error) method of EditText or implement self states and handle it in EditText subclass. Here is good post about custom states: How to add a custom button state
You can't change styles programatically. You can either replace them with a new Instance (you can specify the styles in the constructor only) or you can use it with a new theme since theme can be changed programatically.
Themes

Android checkboxes added by code don't have the right appearance?

I've got a really basic app in which I'm trying to insert dynamic checkboxes, and I can get them to appear, but they're not showing with the correct styling.
See below - Foo is in the LinearLayout by definition; Bar is being added programmatically. Foo is showing with a grey box for the check, Bar is showing with a white box.
Link to image...
Here's the code that's creating these:
for (Integer i=0; i < arArray.length;i++) {
CheckBox cb = new CheckBox(getApplicationContext());
cb.setText("Bar");
cb.setTextColor(getResources().getColor(android.R.color.black));
cb.setOnCheckedChangeListener(clListener);
llDeckChecks.addView(cb);
}
I've tried setting the drawable to some android.R.drawable types, but nothing matches the Foo checkbox, so I'm entirely stumped at this point.
AppCompat replaces the default widgets with tinted, consistent styles, as mentioned in the Android Support Library 22.1 blog post:
This is done automatically when inflating layouts - replacing Button with AppCompatButton, TextView with AppCompatTextView, etc. to ensure that each could support tinting.
If you'd like to create these programmatically, you can use AppCompatCheckBox in place of Checkbox in your code, passing in your current Context such as your AppCompatActivity.
Note: it is very important to not use getApplicationContext() as it does not have the styling information required.
You should use the activity context to create the button instead of the application context. If you use getApplicationContext() it does not extend ContextThemeWrapper.
Try changing
CheckBox cb = new CheckBox(getApplicationContext());
to
CheckBox cb = new CheckBox(YourActivity.this); // or getActivity() if in a fragment.
Use cb.setButtonDrawable(R.drawable.cbOutline); where "cbOutline" is your drawable for the checkboxes outline you defined in your XML.

How to change existing TextView style in action

I have some intent with set of TextViews and a Button. If user clicks the button and there is some error I want to change look of one TextView. E.g. change the border to red and make font bold. I wrote a style for it but I have not found method setStyle on the TextView. After some self study I realized that Android does not support setting the style programmatically. There are some workarounds, when you create the intent source. But my intent already exists, it seems odd to recreate it.
Could you tell me the proper way?
use the workaround and create the TextView again
forget the styles and use java methods to decorate existing TextView
something else
Changing the style of the textview directly does not work as you know. But you can create a second textview with other styles in your layout, which you can show up if needed.
Just add this xml attribute android:visibility="gone" to the second textview, so this second textview is not displayed at first, but available.
When you now want to change the style of your textview, you simple need to swap the two textviews by hidding the first one and showing the second one
textView1.setVisibility(View.GONE);
textView2.setVisibility(View.VISIBLE);
I used these two answers to make it work:
https://stackoverflow.com/a/5488652/1639556
https://stackoverflow.com/a/14195090/1639556
and the code is:
ViewManager parent = (ViewManager) unknown.getParent();
parent.removeView(unknown);
TextView newUnknown = (TextView)getLayoutInflater().inflate(R.layout.tvtemplate, null);
newUnknown.setId(unknown.getId());
parent.addView(newUnknown, unknown.getLayoutParams());
unknown = newUnknown;
You can try using setTextAppearance() on the textview. The link is: setTextAppearance
Your style will need TextAppearance.SomeThing.SomeOtherThing as the parent.
Use R.style.YourStyleName as the integer argument.

How do I used values declared in XML on my custom layout?

Im creating a custom layout and I want to use the text declared in the layout.xml file in my layout.
Like when I create a TextView in XML and set android:text=#string/text1 when I run the app text view automatically loads the text from android:text. So how can I access attributes declared in the layout XML file in my custom component.
I have learn how to do it from this:
WebImageView: Buy one, get two!
In simple steps:
Define the string of your XMLS.
Get the attributes in the code.
Did you complete the Android Developer Tutorials? The Hello, Views | Relative Layout tutorial will walk you through how to do this.

Categories

Resources