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.
Related
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
When creating dynamic buttons I would like them to stack one under the other vertically. I am not sure how to create this effect.
for(int i = 0; i <notificationArrayList.size(); i++)
{
if(i == 0)
{lp.addRule(RelativeLayout.BELOW, R.id.searchButton);}
else
{} //maybe tell the code here to stack under the lastID?
Notification oNote = notificationArrayList.get(i);
Button btn = new Button(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText(oNote.NotificationText);
btn.setBackgroundColor(Color.rgb(70, 80, 90));
rl.setLayoutParams(lp);
rl.addView(btn, lp);
}
Maybe in the else statement have it get the last id and add RelativeLayout that way?
The easiest way would be to put all the buttons in a LinearLayout and just add the LinearLayout beneath the search button. This produces easier code, but slightly worse drawing performance. Pseudocode would be like:
LinearLayout ll = new LinearLayout(context);
for(i=0; i<numButtons; i++) {
ll.addView(new Button(context));
}
RelativeLayout.LayoutParam lp = new RelativeLayout.LayoutParam();
lp.addRule(RelativeLayout.BELOW, R.id.searchButton);
relativeLayout.addView(ll,lp);
This example should give you an idea:
public class MyActivity extends Activity {
private RelativeLayout rel;
private EditText editText;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_mine);
rel = (RelativeLayout) findViewById(R.id.main_rel);
editText = (EditText) findViewById(R.id.pref_edit_text);
Button button = new Button(this);
button.setText("Delete");
// create the layout params that will be used to define how your
// button will be displayed
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
// add the rule that places your button below your object (here a editText)
params.addRule(RelativeLayout.BELOW, editText.getId());
// set the layoutParams on the button
button.setLayoutParams(params);
// add button to your RelativeLayout
rel.addView(button);
}
}
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);
I have a linearlayout, which the first element is a imageview header, and the second element is a gridview.
It works fine, but I have a erroneal black space of 50px (more or less) between the android title bar and the header of the app, which is the first element of the linearlayout
Why do I have that space? The only way I find to remove it is to put this line: ll.setPadding(0, -50, 0, 0);
This is the full code:
public class MainGrid extends Activity {
private GridView myGridView;
private ImageAdapter myImageAdapter;
private ImageView header;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);//turn off the window's title bar
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);//fullscreen
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setGravity(Gravity.CENTER);
//ll.setPadding(0, -50, 0, 0);
header = new ImageView(getApplicationContext());
header.setImageResource(R.drawable.header_acc);
myGridView = new GridView(this);
myImageAdapter=new ImageAdapter(this);
myGridView.setAdapter(myImageAdapter);
ll.addView(header);
ll.addView(myGridView);
setContentView(ll);
}
The snapshot:
Update: This should work just fine if you set android:theme="#android:style/Theme.NoTitleBar.Fullscreen" in the Manifest.
public class MainGrid extends Activity {
private GridView myGridView;
private ImageAdapter myImageAdapter;
private ImageView header;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
ll.setGravity(Gravity.TOP); // SET THIS TO TOP
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT);
ll.setLayoutParams(lp);
// I'VE ADDED LAYOUTPARAMS TOO
header = new ImageView(getApplicationContext());
header.setImageResource(R.drawable.header_acc);
myGridView = new GridView(this);
myImageAdapter=new ImageAdapter(this);
myGridView.setAdapter(myImageAdapter);
ll.addView(header);
ll.addView(myGridView);
setContentView(ll);
}
If you're not using the title bar, you can always get rid of it:
http://elliotth.blogspot.com/2009/07/removing-title-bar-from-your-android.html
Using this in for activity tag in manifest solves your problem
android:theme="#android:style/Theme.NoTitleBar"
For Full screen you should use
android:theme="#android:style/Theme.NoTitleBar.Fullscreen"
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.