I am aware you can apply a custom TypeFace to a TextView in Android like this:
TextView tv = findViewById(R.id.textview01);
Typeface tf = Typeface.createFromAsset(getAssets(), "fonts/BLACKROSE.TTF");
tv.setTypeface(tf);
Is there any way possible to do this for a DatePicker?
Here is my code. I hope it will be useful to someone.
DatePicker datePicker = (DatePicker) findViewById(R.id.datePicker1);
LinearLayout layout1 = (LinearLayout) datePicker.getChildAt(0);
LinearLayout layout = layout1.getChildAt(0);
// day
LinearLayout day = (LinearLayout) layout.getChildAt(0);
setNumberPicker(day);
// month
LinearLayout month = (LinearLayout) layout.getChildAt(1);
setNumberPicker(month);
// year
LinearLayout year = (LinearLayout) layout.getChildAt(2);
setNumberPicker(year);
...
private void setNumberPicker(LinearLayout ll) {
((ImageButton) ll.getChildAt(0)).setBackgroundResource(R.drawable.plus_button);
((ImageButton) ll.getChildAt(2)).setBackgroundResource(R.drawable.minus_button);
EditText et = (EditText) ll.getChildAt(1);
et.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 14);
et.setTypeface(youtTypeface);
}
After taking a look at the source, the Datepicker widget holds 3 NumberPicker widgets (for day, month, year)which in turn hold a TextView. So you are going have to set the Typeface for the TextView inside the NumberPickers inside the DatePicker.
I think you'll have to get the source for both NumberPicker and DatePicker and modify the source to achieve this, easier said than done I'm afraid.
Here is my solution:
private void overrideFonts(View v) {
ViewGroup picker;
try {
picker = (DatePicker) v;
} catch (Exception e) {
picker = (TimePicker) v;
}
LinearLayout layout1 = (LinearLayout) picker.getChildAt(0);
if (picker instanceof TimePicker) {
if (layout1.getChildAt(1) instanceof NumberPicker) {
NumberPicker v1 = (NumberPicker) layout1.getChildAt(1);
final int count = v1.getChildCount();
for (int i = 0; i < count; i++) {
View child = v1.getChildAt(i);
try {
Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
((EditText) child).setTypeface(// your font here);
v1.invalidate();
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
}
LinearLayout layout = (LinearLayout) layout1.getChildAt(0);
for (int j = 0; j < 3; j++) {
try {
if (layout.getChildAt(j) instanceof NumberPicker) {
NumberPicker v1 = (NumberPicker) layout.getChildAt(j);
final int count = v1.getChildCount();
for (int i = 0; i < count; i++) {
View child = v1.getChildAt(i);
try {
Field wheelpaint_field = v1.getClass().getDeclaredField("mSelectorWheelPaint");
wheelpaint_field.setAccessible(true);
((Paint) wheelpaint_field.get(v1)).setTypeface(//your font here);
((Paint) wheelpaint_field.get(v1)).setColor(getResources().getColor(R.color.colorOrange));
((EditText) child).setTypeface(//your font here);
v1.invalidate();
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
} catch (Exception e) {
//TODO catch.
//If java cant find field then it will catch here and app wont crash.
}
}
}
If you can use DatePickerDialog, then u can simply get the buttons of the dialog, like:
DatePickerDialog datePickerDialog; // GET your dialog
datePickerDialog.show();
datePickerDialog.getButton(DatePickerDialog.BUTTON_POSITIVE).setTextSize(30); // 30 is your text size
datePickerDialog.getButton(DatePickerDialog.BUTTON_NEGATIVE).setTextSize(30);
Kotlin-Anko equivalent to SKG's solution
startTimePicker = timePicker {
this.applyRecursively {
when(it) {
is NumberPicker -> {
val paintField = it.javaClass.getDeclaredField("mSelectorWheelPaint")
paintField.isAccessible = true
(paintField.get(it) as? Paint)?.typeface = //your font here
}
is TextView -> {
it.typeface = //your font here
}
}
}
Related
I have a fragment with about 30 EditText inside it. I want to know is there any way to grab all EditTexts and clear the text inside it without doing it one by one.
thanks.
This should work if all your edittexts are within the same layout, for example a relativelayout
ViewGroup group = (ViewGroup)findViewById(R.id.your_group);
for (int i = 0, count = group.getChildCount(); i < count; ++i) {
View view = group.getChildAt(i);
if (view instanceof EditText) {
((EditText)view).setText("");
}
}
Try something like this :
ArrayList<EditText> editTextsList = new ArrayList<EditText>();
for( int i = 0; i < myLayout.getChildCount(); i++ )
if( myLayout.getChildAt( i ) instanceof EditText )
editTextsList.add( (EditText) myLayout.getChildAt( i ) );
The iterate on editTextsList and do your actions on each of EditText on the list
I hope this helps
Yes, you can do with the following method.
public void clearStudentInfo(ViewGroup textViewsGroup) {
for (int i = 0, count = textViewsGroup.getChildCount(); i < count; ++i) {
View view = textViewsGroup.getChildAt(i);
// Check and confirm, is it is the TextView or not?
if (textViewsGroup instanceof EditText) {
((EditText)textViewsGroup).setText("");
}
}
}
You can call the method when you click on clear button as given below.
...
Button btnClear = findViewById(R.id.btnClr);
...
btnClear.setOnClickListener(new View.OnClickListener) {
#Override
void onClick(...) {
clearStudentInfo((ViewGroup) findViewById(R.id.student_info));
}
Thanks 😎😎
Is it possible to get all the TextView in an activity and change their values without knowing any ID? May be something like UI automtor tool that return UI hierarchy but at Java programming level. I tried to Google this but couldn't find any solution.
Edit
What I am trying to achieve here is, I have an external library/SDK which modifies all textView values. So I am planning to initialise it on top of each activity and let the SDK do the work of modifying all the TextViews value.
You can use this code to find all TextViews in your layout, just pass the parent view and it will do the rest:
public static void findViews(View v) {
try {
if (v instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) v;
for (int i = 0; i < vg.getChildCount(); i++) {
View child = vg.getChildAt(i);
// recursively call this method
findViews(child);
}
} else if (v instanceof TextView) {
//do whatever you want ...
}
} catch (Exception e) {
e.printStackTrace();
}
}
try this
First initialize the parent control and use this
ArrayList<TextView> list = new ArrayList<TextView>();
for( int i = 0; i < layout.getChildCount(); i++ ){
if( layout.getChildAt( i ) instanceof TextView )
{
// change the values of text view here
((TextView)layout.getChildAt( i )).setText("your text here");
}
}
If you have activity layout then try as follows
ArrayList<EditText> myEditTextList = new ArrayList<EditText>();
for( int i = 0; i < activityLayout.getChildCount(); i++ ){
if( activityLayout.getChildAt( i ) instanceof EditText )
myEditTextList.add( (EditText) activityLayout.getChildAt( i ) );
}
and getting their values
for(int i=0;i < myEditTextList.size();i++){
System.out.println(myEditTextList.get(i).getText().toString());
}
Hope this will helps you.
I have a ScrollView with LinearLayout into it.After adding the Layout into my LinearLayout, I wanted to remove the view but it removes only the first view.
I have tried parent.removeView(myView)
My Code for Layout adding :
LayoutInflater inflater = getLayoutInflater();
final View view_top = inflater.inflate(R.layout.layout_top, linear_layout,false);
final View view_bottom = inflater.inflate(R.layout.layout_bottom,linear_layout,false);
final RelativeLayout rel_layout_bottom = (RelativeLayout) view_bottom.findViewById(R.id.relative_bottom);
Button btn_update = (Button) rel_layout_bottom.findViewById(R.id.lst_todo_update);
ImageButton btn_remove = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_delete);
ImageButton btn_Color = (ImageButton) rel_layout_bottom.findViewById(R.id.lst_btn_color);
final RelativeLayout rel_layout_top = (RelativeLayout) view_top.findViewById(R.id.relative_top );
final TextView note_Title = (TextView) rel_layout_top.findViewById(R.id.lst_title );
final TextView note_color = (TextView) rel_layout_top.findViewById(R.id.lst_color_type );
note_Title.setText(note_title);
linear_layout.addView(rel_layout_bottom, 0);
JSONArray jsonArray=queryResult.getResultObjects().get(count).getJSONArray("todo_item");
for (int i = 0; i < jsonArray.length(); i++) {
final View view_middle = inflater.inflate(R.layout.layout_middle, linear_layout, false);
final RelativeLayout rel_layout_middle = (RelativeLayout) view_middle.findViewById(R.id.relative_middle);
final CheckBox note_check ;
final TextView note_content ;
note_content = (TextView) rel_layout_middle.findViewById(R.id.lst_content);
note_check = (CheckBox) rel_layout_middle.findViewById(R.id.lst_check);
btn_remove.setOnClickListener(null);
try {
//Getting data correctly here
linear_layout.addView(view_middle,0);
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
} catch (JSONException e) {
e.printStackTrace();
}
}
linear_layout.addView(rel_layout_top , 0);
}
Any answer Appreciated...Thks
Something like below will solve your problem.
final View[] view_middleArr;
for (int i = 0; i < jsonArray.length(); i++) {
view_middleArr = new View[jsonArray.length()];
view_middleArr[i] = inflater.inflate(R.layout.layout_middle, linear_layout, false);
// ...
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
for (View view_middle : view_middleArr) {
linear_layout.removeView(view_middle);
}
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
}
Edit: You are using same view_middle reference for all your view_middle istances inside your loop, so you can only remove last view_middle instance with your code.
The code
btn_remove.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
linear_layout.removeView(view_middle); //Not able to remove the view here i.e view_middle
linear_layout.removeView(view_top);
linear_layout.removeView(view_bottom);
}
});
is in a for loop.
It means the click of the button will only remove the view_middle that was lastly added to the LinearLayout, as you are using the same object name.
Is this code completely correct or not?
myRLayout = (RelativeLayout) findViewById( R.id.rel_layout );
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
TextView res = (TextView) findViewById(R.id.result_text);
String str1 = ((EditText) findViewById(R.id.edittext_1)).getText().toString();
String str2 = ((EditText) findViewById(R.id.edittext_2)).getText().toString();
res.setText(str1+str2);
}
}
Why I ask? For each iteration only one EditText gets in action or all together at once?
use v instance of Child to get value from all EditText and use TextView.append for showing values in TextView. try it as:
TextView res = (TextView) findViewById(R.id.result_text);
res.setText(""); //<< clear textview here
for(int i = 0; i < myRLayout.getChildCount(); i++) {
View v = myRLayout.getChildAt(i);
if(v instanceof EditText) {
String str1 = v.getText().toString(); //<< get text from ith EditText
res.append(str1); // append value to result_text TextView
}
}
Hello I am inflate xml file like..
List<EditText> allEds3 = new ArrayList<EditText>();
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int hh1 = 57; hh1 <= 76; hh1++) {
try {
View view = inflater.inflate(R.layout.form6, null);
form6_linear.addView(view);
lbl1 = (TextView) view.findViewById(R.id.lbl1);
lbl2 = (TextView) view.findViewById(R.id.lbl2);
txt1 = (TextView) view.findViewById(R.id.txt1);
txt1.setId(hh1);
txt1.setText(txt_array.get(hh1));
txt1.setOnClickListener(onclicklistener);
_txt2 = (EditText) view.findViewById(R.id.txt2);
lbl1.setText(lbl_array.get(hh1));
lbl2.setText(lbl_array.get(hh1 + 1));
_txt2.setText(txt_array.get(hh1 + 1));
allEds3.add(_txt2);
hh1++;
nn1++;
} catch (Exception e) {
// TODO: handle exception
}
}
Now I have one button in my main xml file and onclick of that button I have to get all values of this _txt2 of above.
Please help.
EDIT:
List<EditText> allEds3 = new ArrayList<EditText>();
LayoutInflater inflater = inflater = (LayoutInflater) getApplicationContext()
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int hh1 = 57; hh1 <= 76; hh1++) {
try {
View view = inflater.inflate(R.layout.form6, null);
form6_linear.addView(view);
lbl1 = (TextView) view.findViewById(R.id.lbl1);
lbl2 = (TextView) view.findViewById(R.id.lbl2);
txt1 = (TextView) view.findViewById(R.id.txt1);
txt1.setId(hh1);
txt1.setText(txt_array.get(hh1));
txt1.setOnClickListener(onclicklistener);
_txt2 = (EditText) view.findViewById(R.id.txt2);
lbl1.setText(lbl_array.get(hh1));
lbl2.setText(lbl_array.get(hh1 + 1));
_txt2.setText(txt_array.get(hh1 + 1));
allEds3.add(_txt2);
hh1++;
nn1++;
} catch (Exception e) {
// TODO: handle exception
}
}
OnClick()
public void onClick(View v) {
switch (v.getId()) {
case R.id.btn_continue1:
final String[] strings2 = new String[allEds3.size()];
for (int i = 0; i < allEds3.size(); i++) {
strings2[i] = allEds3.get(i).getText().toString();
}
int ii = 0;
for (int db = 57; db <= 76; db++) {
try {
j.remove(txt_array.get(db));
j.put(txt_array.get(db), strings2[ii]);
} catch (JSONException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
ii++;
}
}
}
Error :-
ArrayIndexOutOfBounds at
j.put(txt_array.get(db), strings2[ii]);
If _txt2 is defined as a field in your activity class, then you can simply get it on the button's onClick:
// in onCreate block of your activity
Button button = (Button) findViewById(R.id.button_to_click);
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
// here you can do anything with _txt2 which is just executed when button is clicked
}
}
EDIT: I didn't figure out your question properly, so I should add this:
You can set an ID to any EditText you create. then find them by findViewById() when needed. or you can store all EditText(s) in an array of EditText and get them later.