I am designing an app in Android Studio, but I am having trouble creating a clear option in the menu of my app.
Can someone show me how to add a menu option that will clear all the <EditText> items in my app I am creating?
You can try with below code and change the id of "your_group" with your main layout id.
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("");//here it will be clear all the EditText field
}
}
Please let me know if have any doubts.
Related
Context: I am trying to make an application that allows me to have multiple edit texts (in a to do list format). I am using a linear layout with a scroll view inside of it so that I can allow users to have as many notes as they need.
Question: How can I put each editText inside an array, furthermore, how can I put the string contents of each of the editText in an array.
Any help would be much appreciated!
You need to get text from your EditText like this
EditText et = FindViewById<EditText>(Resource.Id.et_nnnn);
string a = et.Text;
And then add to your array
You can get all child views of parent layout in an array. use something as below. pass parent layout view to follow function
SparseArray<Edittext> array = new SparseArray<Edittext>();
.
.
.
private void findAllEdittexts(ViewGroup viewGroup) {
int count = viewGroup.getChildCount();
for (int i = 0; i < count; i++) {
View view = viewGroup.getChildAt(i);
if (view instanceof ViewGroup)
findAllEdittexts((ViewGroup) view);
else if (view instanceof Edittext) {
Edittext edittext = (Edittext) view;
array.put(edittext.getId(), edittext);
}
}
}
To get all edittexts text you can loop over that array and store in different array or list using gettext on each child.
Stackflowers!
I've a problem with my listview. I load data(text) from a webpage/url and load them in my listview. I have a title and a blog with the text (sometimes more blogs as shown below..).
But the problem is, he loads only 1 time the title, but shows it multiply times. (Shown below).
BBC News:
BBC Newsnight's political editor Emily Maitlis told the programme that she had spoken to the lawyers who are preparing the test case for the female prisoner, who is identified as BGJ and is serving a life sentence.
BBC News:
"She is an epilepsy sufferer, very highly qualified and she has said her life is in despair without access to these books, which have really been taking her through this life sentence that she will serve," she said.
BBC News:
Lawyers for the woman argue that the effects of the policy are particularly hard felt by women who depend on what they receive from the outside world to keep them motivated.
BBC News:
The MoJ said the lawyers had no grounds for their case because they had run out of time to make it.
How can I filter my listview? For only 1 title?
Thanks,
P.S. If somebody need some code, please ask! Thank you.
Update:
Part of code:
public class second_activity extends Activity {
private List<post_second> my_post_second = new ArrayList<post_second>();
String title[] = new String[counter_title];
String blog[] = new String[counter_blog];
private void populate_post_list() {
for (int i = 0; i < 1; i++) {
for (int j = 0; j < counter_blog_stop; j++) {
my_post_second.add(new post_second(title[i], blog[j]));
}
}
}
private void populate_list_view() {
ArrayAdapter<post_second> adapter = new my_list_adapter_second();
global.list = (ListView) findViewById(R.id.post_second_list_view);
global.list.setAdapter(adapter);
}
private class my_list_adapter_second extends ArrayAdapter<post_second> {
public my_list_adapter_second() {
super(second_activity.this, R.layout.item_view_second, my_post_second);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View item_view_second = convertView;
if (item_view_second == null) {
item_view_second = getLayoutInflater().inflate(R.layout.item_view_second, parent, false);
}
// Find the Post to work with.
post_second current_post = my_post_second.get(position);
// Title:
TextView title_text = (TextView) item_view_second.findViewById(R.id.item_title);
title_text.setText(Html.fromHtml(current_post.get_title()));
// Blog:
TextView blog_text = (TextView) item_view_second.findViewById(R.id.item_blog);
blog_text.setText(Html.fromHtml(current_post.get_blog()));
return item_view_second;
}
}
Here you have added the title for each and every row.
for (int i = 0; i < 1; i++) {
for (int j = 0; j < counter_blog_stop; j++) {
my_post_second.add(new post_second(**title[i]**, blog[j]));
}
}
So you can hide the title if the row is not the first one by
// Title:
TextView title_text = (TextView) item_view_second.findViewById(R.id.item_title);
if(position == 0){
title_text.setVisibility(View.VISIBLE);
title_text.setText(Html.fromHtml(current_post.get_title()));
}
else{
title_text.setVisibility(View.GONE);
}
Its better you can use Expandable ListView for your case. Else you can hide the title layout in your adapter wherever you don't need of it. We can't conclude until you post your code. Better you can post your code.
I need android list view project with following details:
should contain 10 rows and should show only 3 rows at a time,
should have up and down arrows to it which moves rows up or down to view ,
if it is top of list up arrow should disabled and its bottom of list down arrow should disabled.
Any one please help me and make this code thanks in advance
For a ListView with 3 rows you'll need to have a custom adapter and override it's getView() method. You can inflate a layout with 2 buttons here and add OnClickListener to both of them. Your OnClickListener must override onClick() method whose body must be something like below:
public void onClick(View v) {
int id = v.getId();
int currentIndex = listView.indexOfChild(v.getParent());
View view = listView.getChildAt(currentIndex);
switch(id) {
case upButtonId:
listView.removeView(view);
listView.addView(view, currentIndex--);
if(currentIndex == 0)
//disable Up Button
break;
case downButtonId:
listView.removeView(view);
listView.addView(view, currentIndex++);
if(currentIndex == listView.getChildCount() - 1)
//disable Down Button
break;
}
}
I'm trying to set a font across all TextView's by iterating through the LinearLayout's views and using instanceof.
The form currently consists of 4 TextView's and one Button.
The below code is detecting all Views, even the Button as a TextView?
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v instanceof TextView) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
If I log each view's class name it returns the TextView and the Button so I know the correct controls are being picked up.
The problem is the Button and TextView's fonts are being set, and I only want the TextView's.
I have found a work around and that is to do the following but am intrigued as to why the above code does not work as expected.
/* Set fonts */
LinearLayout ll = (LinearLayout) findViewById(R.id.ll_screenincourse_wrapper);
for (int i = 0; i < ll.getChildCount(); i++) {
View v = ll.getChildAt(i);
if (v.getClass().getName().contains("TextView")) {
((TextView) v).setTypeface(Fonts.get3dDumbFont(this));
}
}
Is it because both Button and TextView are of type View? If so, what would be the correct way about doing this?
Any help appreciated, thanks. Ricky.
Actually, Button is a subclass of TextView! That's why you see it as a TextView (it is also a TextView).
http://developer.android.com/reference/android/widget/Button.html
public class Button extends TextView
You could make a second if instanceof to exclude Buttons or use
if (v.getClass() == TextView.class)
But this won't match any other subclasses of TextView (if you use them).
It is simple
Button Class extends TextView Class
Check the documentaion
i'm having listview with some items. i placed a Checkbox to display the checkbox with that listitem. I've write onclick method to checkall the checkbox. But, it'll select the item which was in top of the list. What's the problem? Anyone clear me? Thanks in Advance. Sample Code : -
CheckBox c = (CheckBox)findViewById(R.id.checkBox1);
if (c.isChecked())
{
c.setChecked(false);
}
else
{
c.setChecked(true);
}
private OnClickListener checkAllCheckboxes = new OnClickListener(){
public void onClick(View v) {
ListView lv = getListView();
int size = getListAdapter().getCount();
boolean check = lv.isItemChecked(0);
for(int i = 0; i <= size; i++)
lv.setItemChecked(i, !check);
}
}
OR
for(int i=0; i < listView.getChildCount(); i++){
RelativeLayout itemLayout = (RelativeLayout)listView.getChildAt(i);
CheckBox cb = (CheckBox)itemLayout.findViewById(R.id.MyListViewCheckBox);
cb.setChecked(true);
android-unable-to-check-all-the-checkboxes-in-a-custom-listview-because-of-recy
Example2
with that snippet its difficult to point out the issue.
one, the listview reuses the view, maybe that while scrolling the same view is appearing else where.
here is a tutorial http://www.marvinlabs.com/2010/10/custom-listview-ability-check-items/
check it out. or post additional code. particularly, getView in the adapter code.
It happened because at calling of Adapter view it execute at the time of your list view.
So after the you clicked on check box (true) at same number of below screen will be true. for that you have to maintain the Vector.
if(tempVector.get(position)){
holder.box.setChecked(true);
}
else{
holder.box.setChecked(false);
}
try this after your clicking event.