I am applying images to my ImageButtons in code, but I'm wondering if there is a better way than shown in my example. I definitely know that it's not the correct way to do it as it can take way too much time with large numbers of views.
btn1.setImageResource(R.drawable.plus);
btn2.setImageResource(R.drawable.plus);
btn3.setImageResource(R.drawable.plus);
btn4.setImageResource(R.drawable.plus);
btn5.setImageResource(R.drawable.plus);
btn1.setAdjustViewBounds(true);
btn1.setPadding(0,0,0,0);
btn2.setAdjustViewBounds(true);
btn2.setPadding(0,0,0,0);
btn3.setAdjustViewBounds(true);
btn3.setPadding(0,0,0,0);
btn4.setAdjustViewBounds(true);
btn4.setPadding(0,0,0,0);
btn5.setAdjustViewBounds(true);
btn5.setPadding(0,0,0,0);
you need to get the root view in the activity, cast it to ViewGroup, get all children, check their type and update accordingly. Namely as follows,
ViewGroup rootView = (ViewGroup) findViewById(android.R.id.content);
int children = rootView.getChildCount();
for (int i = 0; i < children; i++) {
View view = rootView.getChildAt(i);
if (view instanceof ImageButton) {
((ImageButton) view).setImageResource(R.drawable.plus);
((ImageButton) view).setAdjustViewBounds(true);
((ImageButton) view).setPadding(0,0,0,0);
}
}
Related
I am having so many views to in a relative layout which is able to zoom and rotate. what I am trying to achieve is, when I long click a view it should able to send that particular view to last .
what I do is
private void moveToBack(View currentView) {
ViewGroup vg = ((ViewGroup) currentView.getParent());
for (int i = 0; i < vg.getChildCount(); i++) {
View v = vg.getChildAt(i);
if (!v.equals(currentView)) {
vg.bringChildToFront(v);
break;
}
}
}
This will set the another view above the selected view I need to send that particular view to last. MIn sdk is 17 and I can't change that.
We don't have something like bringToback() method so only we could modify the order of childviews. I tried once to achieve it using tricky way :
public void sendViewToBack(View child) {
final ViewGroup mParent = (ViewGroup)child.getParent();
if (null != mParent) {
mParent.removeView(child);
mParent.addView(child, 0);
}
}
I have been trying to hack a foreach to all elements of type label in screen or layout, with no luck!
My goal is to translate all screen1.labels.text, the translation are in list which has lists of pair (label.text, translation).
Is this possible in App-inventor?
Have you thought of getting an array list of sub view and iterating thought them. the below will work for direct children of the screen. if you need sub view as well you can use recursion
RelativeLayout layout = (RelativeLayout)findViewById(R.id.your_screen_to_search);
for (int i = 0; i < layout.getChildCount(); i++)
{
View child = layout.getChildAt(i);
if(child instanceOf TextView)
{
txtView = (TextView)child;
if(txtView.getText().length!=0)
{
yourTranslateFunction(txtView.getText());
}
}
}
I have a long scroll view where it contains Text views,edit texts,radio buttons,check boxes,Sliders etc..
I want to show the preview of the scroll view and the contents whatever the user had entered and which should be non editable.
Is there any possibility in android that we can do this?.
I have two suggestions
1.Disabling the scroll view from entering inputs and
2.Showing the preview of the scroll view.
is there any possibilities of these two or else some other methods to achieve this in android.
need help, thanks in advance..
This method helped to disable the views.
private static void setViewAndChildrenEnabled(View view, boolean enabled) {
view.setEnabled(enabled);
if (view instanceof ViewGroup) {
ViewGroup viewGroup = (ViewGroup) view;
for (int i = 0; i < viewGroup.getChildCount(); i++) {
View child = viewGroup.getChildAt(i);
setViewAndChildrenEnabled(child, enabled);
}
}
}
hope this will help some one else.check this
I have problem finding childviews of parent view in android, i do the following
i have a relative layout with 2 textviews and 1 edit text view(for example call this whole view as c1)
i add the above view c1 to a linear layout using linearlyt.addView(c1)
i add multiple such child views to parent view
After adding many such child views to linearlyt, i start filling out the edit text of each child view, the question is How can i get the content/text set in Textview of child view whose edit text is being filled out.TIA
Try this..
public void findEditText(View parentview) {
if (!(parentview instanceof EditText)) {
System.out.println(((EditText)parentview).getText());
}
// If a layout container, iterate over children and seed recursion.
if (parentview instanceof ViewGroup) {
for (int i = 0; i < ((ViewGroup) parentview).getChildCount(); i++) {
View innerView = ((ViewGroup) parentview).getChildAt(i);
findEditText(innerView);
}
}
}
The first answer is good. If you need something more specific to your case given the constant structure of your rows...:
for (int i = 0; i < yourLinearLayout.getChildCount(); i++) {
RelativeLayout relativeLayout = (RelativeLayout) yourLinearLayout.getChildAt(i);
TextView textView1 = relativeLayout.getChildAt(0);
TextView textView2 = relativeLayout.getChildAt(1);
EditText editText = relativeLayout.getChildAt(2);
...
}
I have to add multiple view programatically to a HorizontalScrollView but i can not add the same inflated view twice. So i have to re-inflate my XML layout N times. An adapter solution is not an option.
There is a way to recycle this layout without re-inflating?
Thanks to all.
UPDATE: My code is something like this.
View view = getLayoutInflater().inflate(R.layout.my_view, null);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
for(int i = 0; i < 25; i++)
{
// Process textview and images inside the view
h.addView(view);
}
I am not sure this what you are looking for we can inflate layout like this
LayoutInflater.from(context).inflate(R.layout.abc_action_bar_decor, parent, true);
HorizontalScrollView h = (HorizontalScrollView)findViewById(R.id.scroll);
LinearLayout parent= new LinearLayout(context);
parent.setOrientation(LinearLayout.HORIZONTAL);
h.addView(parent);
for(int i = 0; i < 25; i++)
{
View view = getLayoutInflater().inflate(R.layout.my_view, parent, true);
// Process textview and images inside the view
}