LinearLayout not working when creating text view2 - java

I'm facing a problem where the code works fine but my text (textview) doesn't appear in the linearlayout which I'm not sure why
this is my code:
LinearLayout linearScrollableMin = findViewById(R.id.linearScrollableMin);
TextView tryingTV1 = new TextView(this);
tryingTV1.setBackgroundColor(000);
tryingTV1.setText("1");
tryingTV1.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
((LinearLayout) linearScrollableMin).addView(tryingTV1);
and a picture of the component tree:

There are some things I want to note here:
Always set the background color using java binary numbers (here 0x000000) --> int. Setting the color black would work with 000, because black has the ID 0, but it wouldn't work for any other colors.
When you add a TextView to a ViewGroup (and LinearLayout extends ViewGroup), its LayoutParams are automatically overridden. The LayoutParams are always given to a view by its parent and describe all properties of a view in relation to the parent / siblings
You are casting a LinearLayout to LinearLayout ;)
Actual solution:
Most probably, your LinearLayout is not displayed itself, so its childs can't be either. You can check whether this is true by either calling linearScrollableMin.setBackgroundColor(Color.BLUE) or just add android:background="#color/blue" to your resource file (and remove it later).
If this is the case, work yourself up in the parent tree and see if a sibling blocks out a view in a parent (by f.e. having the width and height MATCH_PARENT). In this case, just use RelativeLayout and you'll be fine.
Another possible reason
could be, that the textColor of tryingTV1 is Color.BLACK (which it is in this case) and your background is also Color.BLACK (which the background of your TextView is). You can solve this by calling tryingTV1.setTextColor(Color.WHITE);.
Edit (how to copy a TextView)
If you want to clone a TextView that you created in your layout file (NOT by calling new TextView(this);), you copy it by calling:
TextView realTextView = findViewById(R.id.whateverView);
ViewGroup layout = getLayoutInflater().inflate(R.layout.whateverLayout);
tryingTV1 = layout.getChildAt(0); // I assume that the TextView is your first child in the layout, if it is somewhere inside of a nested layout, you have to call `getChildAt(index)` multiple times and always cast to `ViewGroup`, or simply just use my suggestion below.
There is no way of cloning a TextView that you created by calling new TextView(this);, but you might try to create one by copying all the nessesary attributes:
TextView realTextView = new TextView(this);
// do something with the realTextView
copy = new TextView(this);
copy.setText(realTextView.getText());
copy.setTextColor(realTextView.getTextColor());
copy.setBackground(realTextView.getBackground());
...
tryingTV1 = copy;

Related

Want to make button that grabs values from dynamicaly created editTexts [duplicate]

For an Android App, I'm using a GridView and extending BaseAdapter to organize its contents. For the function getView that I override in my extended BaseAdapter class, I create a LinearLayout, which I attach an ImageView and 3 TextViews to. Now, I need to implement convertView, but because I created my views programmatically, I didn't think I can use findViewById to find these child views to change their properties (like text and bitmaps).
I had the idea of assigning a unique ID pertaining to different types of views for each one when I create them (example: give my name textview the id of 1, description textview the id of 2, etc), but I was not sure if the ids have to be unique among every view, whether they're the same kind of view or not.
Ultimately, how do I find a child view that's part of a linearlayout view which were all created programmatically?
You can get children of LinearLayout via getChildAt() but it's hard to distinguish what child exactly you get.
I think assigning IDs is better way. You can assign IDs to your views and later get views via findViewById(). IDs doesn't need to be unique for each LinearLayout.
Something like this:
// Give more sensible names to ID's
int IMAGEVIEW_ID = 0;
int TEXTVIEW1_ID = 1;
int TEXTVIEW2_ID = 2;
int TEXTVIEW3_ID = 3;
imageView.setId(IMAGEVIEW_ID);
textView1.setId(TEXTVIEW1_ID);
textView2.setId(TEXTVIEW2_ID);
textView3.setId(TEXTVIEW3_ID);
...
// somewhere later
ImageView imageView = (ImageView) convertView.findViewById(IMAGEVIEW_ID);

Difrences between changing layout background color in java

I'm a newbie to the android development.
I have two approaches to change the layout background color via Java. Which one could be more appropriate and why?
The first approach is:
LinearLayout layout = (LinearLayout) convertView.findViewById(R.id.translation_container);
layout.setBackgroundColor(mColor);
and the second one:
View view = convertView.findViewById(R.id.translation_container);
int color = ContextCompat.getColor(getContext(),mColor);
view.setBackgroundColor(color);
Well for one there is something very wrong between the two, and that is the fact that you are calling ContextCompat.getColor(getContext(), mColor) with one and directly setBackgroundColor(mColor) on the other.
ContextCompat.getColor is supposed to be called with a color resource ID, ie. a R.color.value. It returns the color value from your resources that corresponds to that resource ID.
Depending on what you have in the value of ```mColor''', there is a good chance that one of these options is not setting the correct color you are expecting.
If mColor is a color resource ID, then the second one is correct. If mColor is the color you want to set, then the first is correct.
When it comes to the cast you are doing into a LinearLayout it is unnecessary. Both options call the same method View.setBackgroundColor. You can go with View for now as the type of your view variable.
LinearLayout extends ViewGroup which extends View. So LinearLayout also a View.
So there is no difference between View.setBackgroundColor() and LinearLayout.setBackgroundColor(). LinearLayout did not override this method.
See View.setBackgroundColor()
See: https://developer.android.com/reference/android/widget/LinearLayout.html

How do I add text to a ScrollView at run time?

I would like to know how to add text to a scroll view at run time. I am working in Android Studio. I have a file that I am reading from and have put the lines of text into a StringBuilder buffer object.
Now how do I display the text in my ScrollView? Do I need to use .AddView?
Scrollview is a single-element container, meaning you should only have one element in it.
A ScrollView is a FrameLayout, meaning you should place one child in it containing the entire contents to scroll; this child may itself be a layout manager with a complex hierarchy of objects. A child that is often used is a LinearLayout in a vertical orientation, presenting a vertical array of top-level items that the user can scroll through.
So you should add a LinearLayout in your ScrollView and add views to that instead.
Add TexView inside ScrollView:
TextView tv = new TextView(this);
tv.setText("Your string");
scrollView.addView(tv);

Is it possible to add buttons to a framelayout that was set programmatically?

this is somewhat of of difficult to describe issue but I'll do my very best:
I am developing a an android app that uses a custom camera activity. In this camera activity I use create a surface view programmatically and set it to the framelayout (covers full screen) that was defined in the xml layout file.
My question now is, how can I add other elements to the frame layout? Only programmatically? I am asking because as of now I was only able to add other elements programmatically. Elements that i added in the xml layout didn't appear on screen.
Is it possible that they are just behind the surface view that i add to the frame layout? If so, would it be possible to bring them to the front?
Thank you guys!
Sure, you can add as many buttons and other widgets to the FrameLayout you have. Since the FrameLayout allows stacking of views, the components that you added in the xml file are now behind the View that you added programmatically. Here's how you can create and add widgets dynamically:
// find your framelayout
frameLayout = (FrameLayout) findViewById(....);
// add these after setting up the camera view
// create a new Button
Button button1 = new Button(this);
// set button text
button1.setText("....");
// set gravity for text within button
button1.setGravity(Gravity.....);
// set button background
button1.setBackground(getResources().getDrawable(R.drawable.....));
// set an OnClickListener for the button
button1.setOnClickListener(new OnClickListener() {....})
// declare and initialize LayoutParams for the framelayout
FrameLayout.LayoutParams params = new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.WRAP_CONTENT,
FrameLayout.LayoutParams.WRAP_CONTENT);
// decide upon the positioning of the button //
// you will likely need to use the screen size to position the
// button anywhere other than the four corners
params.setMargins(.., .., .., ..);
// use static constants from the Gravity class
params.gravity = Gravity.CENTER_HORIZONTAL;
// add the view
fl1.addView(button2, params);
// create and add more widgets
....
....
Edit 1:
There's a trick you can use here:
// Let's say you define an imageview in your layout xml file. Find it in code:
imageView1 = (ImageView) findViewById(....);
// Now you add your camera view.
.........
// Once you add your camera view to the framelayout, the imageview will be
// behind the frame. Do the following:
framelayout.removeView(imageView1);
framelayout.addView(imageView1);
// That's it. imageView1 will be on top of the camera view, positioned the way
// you defined in xml file
This happens because:
Child views are drawn in a stack, with the most recently added child on top (from android resource page on FrameLayout)
http://developer.android.com/reference/android/widget/FrameLayout.html
"FameLayout is designed to block out an area on the screen to display a single item. Generally, FrameLayout should be used to hold a single child view, because it can be difficult to organize child views in a way that's scalable to different screen sizes without the children overlapping each other. You can, however, add multiple children to a FrameLayout and control their position within the FrameLayout by assigning gravity to each child, using the android:layout_gravity attribute."

Making a layout fit views regardless of their order added to the layout?

I have a LinearLayout oriented vertically, and I'm trying to add three sub-views to it. One view is a small, 50px high view that just draws a box in it, the second view is a horizontal list of radio buttons, and the third view is a graph.
If I add the graph last, everything fits on screen, but if I add the graph first, it pushes everything off screen and it becomes the only view on the screen. The graph is a custom control of mine that wants to be as big as it can (its onMeasure function just returns the provided width and height), which I think is where the problem lies. But I want to keep it as big as possible, and have the layout fit the views regardless of their order added.
Is this possible? If so, how can I accomplish it? Ultimately, I'm trying to put the graph on top and the other two controls below it, but if I add the graph first it consumes the whole screen.
Here's the code I'm using to create the views. This is a school assignment and I can't use XML layouts for it. I've been assigned to make a control, which I've completed. I'm just trying to make a simple activity to show the control off.
public class MainActivity extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout layout = new LinearLayout(this);
layout.setOrientation(LinearLayout.VERTICAL);
layout.addView(new MovingBox(this));
RadioGroup radioGroup = new RadioGroup(this);
radioGroup.setOrientation(RadioGroup.HORIZONTAL);
radioGroup.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.WRAP_CONTENT));
RadioButton button = new RadioButton(this);
button.setText("Light");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
button = new RadioButton(this);
button.setText("Dark");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
button = new RadioButton(this);
button.setText("Colorful");
button.setLayoutParams(new RadioGroup.LayoutParams(RadioGroup.LayoutParams.MATCH_PARENT, RadioGroup.LayoutParams.WRAP_CONTENT, 1));
radioGroup.addView(button);
layout.addView(radioGroup);
// If I move these next to lines just below layout.setOrientation(...) it becomes the only thing visible
TweenerControl tw = new TweenerControl(this);
layout.addView(tw);
setContentView(layout);
}
}
Adding the graph last: (everything fits but I'd like the graph on top)
Adding the graph first: (only the graph is visible)
I've worked on this more, and the solution I've come up with is to not use LinearLayout and use RelativeLayout instead. RelativeLayout tries to position and size things so they properly fit, whereas LinearLayout just gets the size of things and lays them out in top to bottom order, where items higher in the LinearLayout don't care about the sizing of items lower in the LinearLayout.

Categories

Resources