I am creating a dynamic interface based on a string from sharedpreferences.
Heres my code
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
CreateInterface();
}//End-OnCreate
public void CreateInterface()
{
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
TableLayout tl = new TableLayout(this);
TableRow[] tr = null;
// Here is a loop that creates tablerow, create button in that, and add tablerow to tablelayout tl. This part is irrelevant couse it works perfectly.
ll.addView(tl);
sv.addView(ll);
setContentView(sv);
}
Now, i want to change the background color of the screen. How would i go about doing that?
ll.setBackgroundColor() should does the job.
Set ScrollView or LinearLayout Background Color using below method.
sv.setBackgroundColor(); or ll.setBackgroundColor();
Get Color string using getResources().getColor(R.color.yourColorID);
Related
The app I am trying to make, has got a lot of similar LinearLayouts and textViews that need to be created programmatically and placed on the screen in a specific order.
So I decided to define a method which returns one element, and for the furthur uses,I will put the method in some loop to produce the others. but when I create a view or layout this way, nothing shows up or sometimes the app crashes, as if it's been sent null to addView(). It only works when I create the View/Layout in onCreate() and then I use it right there afterwards.So , any ideas that I can use the method to creat my Layout/View? Because they are too many and it's not possible to create them one by one in onCreate()
Here's the method:
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
tv_day.setWidth(LinearLayout.LayoutParams.MATCH_PARENT);
tv_day.setHeight(LinearLayout.LayoutParams.WRAP_CONTENT);
tv_day.setGravity(Gravity.END);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
and this is onCreate() which doesn't add any linear layouts with a textView in it :
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_month_view);
LinearLayout ll= createLinearLayout();
LinearLayout mainLayout = (LinearLayout) findViewById(R.id.activity_month_view);
mainLayout.addView(ll);
}
I think this should help
- add an empty linear layout in XML with some id.
- reference that layout in code
- add elements to that layout dynamically
Hey was just checking your code. Its working perfectly now just try this method.
public LinearLayout createLinearLayout(){
TextView tv_day = new TextView(this);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
tv_day.setLayoutParams(layoutParams);
tv_day.setGravity(Gravity.CENTER);
tv_day.setText("27");
LinearLayout ll_horizontal = new LinearLayout(getBaseContext());
LinearLayout.LayoutParams ll_horizontal_params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT);
ll_horizontal.setLayoutParams(ll_horizontal_params);
ll_horizontal.setOrientation(LinearLayout.HORIZONTAL);
ll_horizontal.addView(tv_day);
return ll_horizontal;
}
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
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.
I currently have a SurfaceView (named BoardView) that is being stored in a FrameLayout. There is another LinearLayout (l1) that is stored in this FrameLayout(f1) which contains an EditText. I want to be able to bring the EditText to the front from within my BoardView. Is this possible? I tried using getParent().bringChildToFront(); but it didn't work. Any ideas?
public class Board extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
//use a frame layout so you can also display a dialog box
// allows more than one view to be used
FrameLayout f1 = new FrameLayout(this);
LinearLayout l1 = new LinearLayout(this);
EditText edit = new EditText(this);
l1.setOrientation(LinearLayout.VERTICAL);
l1.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
edit.setText("Enter your name!");
l1.addView(edit);
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
f1.addView(l1);
f1.addView(new BoardView(this));
setContentView(f1);
//setContentView(new BoardView(this));
}
}
Sounds rather silly, but try l1.bringToFront() and see if that works? Alternatively just add l1 second:
f1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,
LayoutParams.FILL_PARENT));
f1.addView(new BoardView(this));
f1.addView(l1);
and the edit text will be on top.
Let me know if it works.
I found this example
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
TableLayout layout = new TableLayout (this);
layout.setLayoutParams( new TableLayout.LayoutParams(20,20) );
layout.setPadding(1,1,1,1);
for (int f=0; f<=3; f++) {
TableRow tr = new TableRow(this);
tr.setPadding(10,10,10,10);
for (int c=0; c<=3; c++) {
Button b = new Button (this);
b.setText(""+f+c);
b.setTextSize(10.0f);
b.setPadding(10, 10, 10, 10);
b.setTextColor(Color.rgb( 100, 200, 200));
b.setBackgroundColor(Color.BLUE);
b.setOnClickListener(this);
b.setWidth(24);
b.setHeight(24);
tr.addView(b);
} // for
layout.addView(tr);
} // for
super.setContentView(layout);
} // ()
I need to have matrix of buttons ( something like GridLayout in Java ). The problem in this code is that I don't have any space between columns in same row. How to add space between buttons in same row ?
I think you need to set margins for your buttons, because padding may only size down the top layer of the button and not its background. Here is an example how to do something similar in code: Programmatically set margin for TableRow
However in this example you need to change the parent container to TableRow, because layout parameters always refer to its immediate parent, which for your buttons is TableRow.