Android - Add view dynamically - android

I have a LinearLayout that I am extending and I want to add a ViewFlipper and Views in it dynamically. Can anyone tell me why this is showing a blank screen?
protected void onLayout(boolean changed, int l, int t, int r, int b)
{
ViewFlipper vf = new ViewFlipper(context);
vf.layout(l, t, r, b);
this.addView(vf);
TextView tv = new TextView(context);
tv.setText("FOO");
tv.setBackgroundColor(Color.WHITE);
vf.addView(tv, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
}

I made this sample code:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//setContentView(R.layout.main);
ViewFlipper vf = new ViewFlipper(this);
vf.layout(10, 100, 100, 150);
LinearLayout ll = new LinearLayout(this);
TextView tv = new TextView(this);
tv.setText("Prueba");
ll.addView(tv);
ll.addView(vf);
TextView tv2 = new TextView(this);
tv2.setText("FOO");
tv2.setBackgroundColor(Color.WHITE);
vf.addView(tv2, 0, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.FILL_PARENT, ViewGroup.LayoutParams.FILL_PARENT));
setContentView(ll);
}
And obtained the desired result, not the one you found out.
Maybe you should check the filling type stuff inside you layouts.

Have you overriden the onMeasure() method? I think you need to do this otherwise the View is not correctly sized. Someone please correct me if I am wrong

Related

How to implement CustomView that contains other views (such as a Button, TextView, CheckBox, etc) without adding up to the view hierarchy in android?

I'm trying to create a view that contains TextView (on the left) and Button (on the right) and a SeekBar at the Bottom, something like this.
|-----------------------------------|
| <TextView> <Button> |
| < SeekBar > |
|-----------------------------------|
Note that angle brackets represents the width, just used for demonstration.
I can do that by creating a CompoundView but I wanted to keep things flat.
I'm going to create others similar to this one and there will be a lot of these.
Please ask for any further clarifications (if needed).
Thank you.
What about using a Framelayout and adding your views on the go?
public class Cell extends FrameLayout {
private TextView tv;
private Button btn;
private ProgressBar progressBar;
public Cell (Context context, int width, int height) {
super(context);
getLayoutParams().width = width;
getLayoutParams().height = height;
tv = new TextView(context);
FrameLayout.LayoutParams tvParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP|Gravity.LEFT);
tv.setLayoutParams(tvParams);
btn = new Button(context);
FrameLayout.LayoutParams btnParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.TOP|Gravity.RIGHT);
btn.setLayoutParams(btnParams);
progressBar = new ProgressBar(context);
FrameLayout.LayoutParams progressParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.MATCH_PARENT, FrameLayout.LayoutParams.WRAP_CONTENT, Gravity.BOTTOM);
progressBar.setLayoutParams(progressParams);
addView(tv);
addView(btn);
addView(progressBar);
}
public void setText (String text) {
tv.setText(text);
}
public void setProgress (int progress) {
progressBar.setProgress(progress);
}
}
Note that you might need to use other constructors as well.

Create buttons in sequential order programmatically

I want to parse text, and create for each word - button, but i don't know how to arrange them one after the other
String s = "Alice was beginning to get very tired of sitting";
String[] q = s.split(" ");
for (int i = 0; i < q.length; i++) {
Button myButton = new Button(this);
myButton.setText(q[i]);
RelativeLayout layout = (RelativeLayout) findViewById(R.id.layout1);
layout.addView(myButton, params);
}
See this custom library: FlowLayout
While you're adding views inside FlowLayout, it automatically wraps when there is no space for the next item.
There's not much wrong about your approach, it's only that relative layout as name suggests requires child views to have some parameters to align the views relative to them e.g. above, below etc. As a result you are getting views overlapping each other and hence only the last added view is visible being on top.
Use FlowLayout instead and you'll be fine.
You need to define RelativeLayout parameters as in example below
Heres an example to get you started, fill in the rest as applicable:
TextView tv = new TextView(mContext);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
params.leftMargin = 107
...
mRelativeLayout.addView(tv, params);
The docs for RelativeLayout.LayoutParams and the constructors are
here
From: How to add a view programmattically to RelativeLayout?
Check the link below to get more useful informations.
Hope it will help
In the following code, you should change the upper limits of the for, to a variable.
public class MainActivity
extends Activity
implements View.OnClickListener {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(4,5) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=13; f++) {
TableRow tr = new TableRow(this);
for (int c=0; c<=9; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setOnClickListener(this);
tr.addView(b, 30,30);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
public void onClick(View view) {
((Button) view).setText("*");
((Button) view).setEnabled(false);
}
} // class

Android add ImageView programmatically

I currently add images through xml with the whole R.id.x method with the following function:
public void Image(int ID, int x, int y){
ImageView iv = (ImageView) findViewById(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
}
I've written a new function to get these images on screen programmatically instead of parsing them in XML, with help from afore-mentioned topics/questions I searched and came up with this:
public void ImageRAW(int ID, int x, int y){
ImageView iv = new ImageView(c);
iv.setImageResource(ID);
RelativeLayout.LayoutParams position = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
position.setMargins(x, y, 0, 0);
iv.setLayoutParams(position);
rl.addView(iv);
}
But it did not work. I also tried adding the following line, to no avail: iv.setVisibility(View.VISIBLE);
And in regards to the variables rl and c:
private Context c;
private RelativeLayout rl;
public void SetUtilContext(Context context){
c = context;
rl = new RelativeLayout(context);
}
The above function is called in every Activity's onCreate() function and sets the UtilLib's current Context and RelativeLayout for drawing accordingly.
The function ImageRAW() is something I would like to use to replace the old Image() function, to make things easier for me. How would/could I get this working?
Try add this before your SetUtilContext():
RelativeLayout menu = findViewById(R.layout.menu);
And this at the end of your ImageRAW() method:
menu.addChild(rl);
As per Incredible's request, my onCreate function where I'm testing the functions:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.menu);
SetUtilContext(this);
Image(R.id.logo, 0, 0);
GetScreenSize();
ImageButton(R.id.start_btn
, (screenWidth/2)-(GetImageWidth(R.id.start_btn)/2)
, 48+GetImageHeight(R.id.logo));
ImageButton(R.id.options_btn
, (screenWidth/2)-(GetImageWidth(R.id.options_btn)/2)
, 96+GetImageHeight(R.id.logo)+GetImageHeight(R.id.start_btn));
ImageButton(R.id.about_btn
, (screenWidth/2)-(GetImageWidth(R.id.about_btn)/2)
, 144+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*2));
ImageButton(R.id.exit_btn
, (screenWidth/2)-(GetImageWidth(R.id.exit_btn)/2)
, 192+GetImageHeight(R.id.logo)+(GetImageHeight(R.id.start_btn)*3));
PlayAudio(R.raw.theme, true);
ImageRAW(R.drawable.head, 0, GetImageHeight(R.id.logo));
}

Android - Layout in JAVA File

As per my requirement, i should give the orientation in horizontal only, if i give 3 sentences in 3 text views, if there is no space for third sentence then it should come in the next line in the first position ...
public class MainActivity extends Activity {
private LinearLayout layout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById();
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
layoutParams.setMargins(10, 15, 10, 10);
layout.setOrientation(LinearLayout.HORIZONTAL);
android.view.ViewGroup.LayoutParams params;
TextView tvTextsecond = new TextView(this);
tvTextsecond.setText("Heywhatrudoingtoday");
tvTextsecond.setLayoutParams(layoutParams);
tvTextsecond.setBackgroundColor(Color.RED);
tvTextsecond.setTextColor(Color.WHITE);
TextView tvTextthird = new TextView(this);
tvTextthird.setText("Haiitssundaytowork");
tvTextthird.setLayoutParams(layoutParams);
tvTextthird.setBackgroundColor(Color.BLUE);
tvTextthird.setTextColor(Color.WHITE);
TextView tvTextfourth = new TextView(this);
tvTextfourth.setText("Owebullshitruuselessfellow");
tvTextfourth.setLayoutParams(layoutParams);
tvTextfourth.setBackgroundColor(Color.YELLOW);
tvTextfourth.setTextColor(Color.WHITE);
layout.addView(tvTextsecond);
layout.addView(tvTextthird);
layout.addView(tvTextfourth);
}
private void findViewById() {
layout = (LinearLayout) findViewById(R.id.flowLayout);
}
}
Linear Layout doesn't behave that way. If there is no space, it would go out of your display area. So, for your requirement, you can't use Linear Layout here.

When Extending LinearLayout Child Views are not visible

Alright. So I start my activity in Main, grab the FragmentManager and instantiate a Fragment which needs to return a View. OK. So I extended a LinearLayout in order to have something to return. My Activity and Fragment are happy but I am not.
Three LinearLayouts which I create in the parent ViewGroup are there (code below). I have verified this by counting children and by setting the background colors to contrast one another. The parent also changes size depending on how tall I make the children (when I don't declare any LayoutParams on the parent).
public class Mainmenu extends LinearLayout {
private ArrayList<LinearLayout> panes = new ArrayList<LinearLayout>();
private Context context;
private final int
LEFT = 0, CENTER = 1, RIGHT = 2;
public Mainmenu(Context c) {
super(c);
context = c;
setBackgroundColor(Color.WHITE);
setOrientation(HORIZONTAL);
setLayoutParams(
new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.FILL_PARENT,
LinearLayout.LayoutParams.FILL_PARENT));
for(int i=0;i<=RIGHT;i++){ //Create the (3) Panes
LinearLayout ll = new LinearLayout(context);
ll.setLayoutParams(
new LinearLayout.LayoutParams(300,
LinearLayout.LayoutParams.FILL_PARENT));
switch(i){
case LEFT | RIGHT:
ll.setBackgroundColor(Color.DKGRAY);
default:
ll.setBackgroundColor(Color.BLACK);
}
ll.setOrientation(LinearLayout.VERTICAL);
ll.setVisibility(VISIBLE);
ll.setWillNotDraw(false);
panes.add(i, ll);
addView(ll);
}
LinearLayout.LayoutParams buttons =
new LinearLayout.LayoutParams(100, 50);
buttons.setMargins(15, 5, 5, 0);
TextView tv1 = new TextView(context);
tv1.setText("hello");
tv1.setTextColor(Color.RED);
panes.get(LEFT).addView(tv1, buttons);
Button button = new Button(context);
button.setText("Launch Editor");
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v){
}
});
panes.get(CENTER).addView(button);
}
#Override
protected void onLayout(boolean changed, int l, int t, int r, int b) {
// TODO Auto-generated method stub
}
}
Nothing is null, all my elements (3 ViewGroups and 2 Views) are present in the tree but not visible. I've tried bringing children to the front through the parent and the children, creating them in different super.methods and invalidating the view in a similarly shotgunned fashion. What's going on? Is it as simple as not having any idea what I'm doing?
The problem is simply because you are overriding onLayout and doing nothing with it. You only need to override this if you want to layout the children yourself (ie, you were designing some unique custom layout). In this case just remove that method, or call super.onLayout.

Categories

Resources