i'm adding programatically 3 textviews into a framelayout that haves a camera view.
The three textviews are writting in the same position, but i want to put each textview bottom to another (using framelayout)
I dont know how to do it, i can't find any examples or info about doing this with framelayout programatically, and also i didnt find the way to do it with setlayoutparams, because that method doesn't have x/y parameters or something like that.
here is the code:
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
cv = new CustomCameraView(this.getApplicationContext());
FrameLayout rl = new FrameLayout(this.getApplicationContext());
setContentView(rl);
rl.addView(cv);
tv1=new TextView(getApplicationContext());
tv2=new TextView(getApplicationContext());
tv3=new TextView(getApplicationContext());
rl.addView(tv1);
rl.addView(tv2);
rl.addView(tv3);
tv1.setText("Test1");
tv2.setText("Test2");
tv3.setText("Test3");
}
Make a linearLayout, add the textViews to this LinearLayout , and add this linearLayout to your FrameLayout. Use the orientation of LinearLayout vertical.
use LinearLayout.setOrientation(LinearLayout.VERTICAL), method for setting orientation to vertical.
Related
I have an issue with the creation of a new RelativeLayout above an already existing TextView in my app.
My main layout is called contentLayout and inside it I have a TextView called addRoomButton :
RelativeLayout contentLayout = (RelativeLayout) findViewById(R.id.contentLayout);
TextView addRoomButton = (TextView) findViewById(R.id.addRoomText);
I just want to add a new RelativeLayout above my TextView so I wrote this :
RelativeLayout relativeLayout = new RelativeLayout(this);
relativeLayout.setId(R.id.roomRelativeLayout);
RelativeLayout.LayoutParams rlp = new RelativeLayout.LayoutParams(
RelativeLayout.LayoutParams.MATCH_PARENT,
100
);
rlp.addRule(RelativeLayout.ALIGN_PARENT_TOP);
rlp.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
rlp.addRule(RelativeLayout.ABOVE, addRoomButton.getId());
contentLayout.addView(relativeLayout, rlp);
The RelativeLayout is well created on the top of my contentLayout, but my TextView is not visible. I think it's just behind relativeLayout but I'm not sure...
Is there any solutons to put the TextView below the new RelativeLayout without creating LinearLayout or other Layouts?
Why wouldn't you do it by XML?
layout_below=""
try out your idea with xml. put relativelayout with relativelayout(A) in it and button(B). set A to layout_above B and see there is no success. Set B bellow A and you get the effect you want. So you need to getLayoutParams() of your button and adjust it to be below your newly created layout.
Could some help me with Layouts? I'm having a problem getting it to display the way I'd like.
I have two relativelayouts in a linearlayout. RelativeLayout 1 is used to accomodate a fragment, RelativeLayout 2 contains the 'main' layout that should fill the screen when there is no fragment, but resize when the fragment is added.
I create the layouts dynamically like so:
LinearLayout mainLayout = new LinearLayout(this);
mainLayout.setLayoutDirection(LinearLayout.VERTICAL);
unityPlayerLayout = new RelativeLayout(this);
youtubeLayout = new RelativeLayout(this);
LinearLayout.LayoutParams mainParams = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT);
mainLayout.setLayoutParams(mainParams);
RelativeLayout.LayoutParams youtubeLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,600);
RelativeLayout.LayoutParams unityPlayerLayoutParams = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.MATCH_PARENT);
unityPlayerLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
mainLayout.addView(youtubeLayout,0,youtubeLayoutParams);
mainLayout.addView(unityPlayerLayout);
unityPlayerLayout.addView(playerView,0,unityPlayerLayoutParams);
Upon adding the fragment, unityPlayerLayout does not resize and aligned to the bottom though. It get's pushed to the right, I can see a sliver of a couple of pixels, which is weird, since youtubeLayout and mainLayout should match the screen.
So, to summarize: Upon adding a fragment to youtubeLayout, I need unityPlayerLayout to resize it's height and drop to the bottom, but in practice unityPlayerLayout gets pushed to the right, and does not resize it's height.
Anyone any idea? Much appreciated!
You nee to set Layout orientation for the main LinearLayout, not direction
Change
mainLayout.setLayoutDirection(LinearLayout.VERTICAL);
to
mainLayout.setOrientation(LinearLayout.VERTICAL);
// try this way,hope this will help you...
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
LinearLayout mainLayout = new LinearLayout(this);
RelativeLayout unityPlayerLayout = new RelativeLayout(this);
unityPlayerLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_dark));
RelativeLayout youtubeLayout = new RelativeLayout(this);
youtubeLayout.setBackgroundColor(getResources().getColor(android.R.color.holo_red_light));
mainLayout.setOrientation(LinearLayout.VERTICAL);
mainLayout.addView(youtubeLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,600));
mainLayout.addView(unityPlayerLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,0,1f));
setContentView(mainLayout,new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.MATCH_PARENT));
}
I want to create a layout (see class RosterPlayerView below) that comprises an image with text below it and then instantiate that view multiple times in a relative layout. I used relative layout instead of linear as the layout will become more complex.
When I first ran the code below (but without the setId calls) the text appeared above the image. Thanks to this stack overflow article I discovered that relative layout needs unique widget ids to work. But when I added the setId() calls the text view is not displayed at all.
What am I doing wrong?
public class RosterPlayerView extends RelativeLayout {
ImageView imageView;
TextView textView;
static int layoutId = 100;
public RosterPlayerView(Context context, int playerId, Drawable photo) {
super(context);
imageView = new ImageView(context);
textView = new TextView(context);
addView(imageView, new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
imageView.setId(layoutId++);
RelativeLayout.LayoutParams timeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
timeLayoutParams.addRule(RelativeLayout.BELOW, imageView.getId());
addView(textView, timeLayoutParams);
imageView.setImageDrawable(photo);
textView.setId(layoutId++);
textView.setText("0:00");
}
}
a LinearLayout would be an awful lot simpler for what you are trying to do. So would inflating an XML layout, for that matter.
Try to set the Id of you imageView before adding it to the layout.
You can also create a LinearLayout with the imageView and textView inside before adding it to the RelativeLayout
I am using the following layout and want to add textviews one below another dynamically(depending on the data I want to display)
main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/contain"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
</LinearLayout>
</ScrollView>
So I tried this
public class Feeds extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent=getIntent();
Bundle b=intent.getExtras();
String s= b.getString("datapack");
Log.w("String",s);
String data[]=s.split("#");
String temp="";
LinearLayout l=(LinearLayout)findViewById(R.id.contain);
LinearLayout.LayoutParams lp = new LinearLayout.LayoutParams(
(LayoutParams.WRAP_CONTENT), (LayoutParams.WRAP_CONTENT));
RelativeLayout relative = new RelativeLayout(getApplicationContext());
relative.setLayoutParams(lp);
for(int i=1;i<data.length;i++)
{Log.w("i data",data[i]);
temp=temp + data[i] + ",";
Log.w("tag",temp);
if(i%5==0)
{Log.w("data",temp);
TextView tv = new TextView(getApplicationContext());
tv.setLayoutParams(lp);
tv.setId(i);
tv.setText(temp);
relative.addView(tv, lp);
temp=null;
}
}
l.addView(relative);
}
}
I am using Scrollview for the first time so that is crating a bit of problem but the main problem is that the textviews in the relative layout overlap one another.How can I specify the gap between each view??
If you're adding views into a RelativeLayout you have to set where the view will be placed, different than LinearLayout that you only need to add the views inside there.
Trye to change to this
LinearLayout layout= new LinearLayout (getApplicationContext());
layout.setLayoutParams(lp);
And here you go a little tip. Do not do this
if(i%5==0)
{Log.w("data",temp);
try to do this
if(i%5==0){
Log.w("data",temp);
it's better to read and understand
I don't see where you are defining the positions for each textview, ie layout_below, layout_toLeftOf... If no positions are defined the relative layout stacks each view in the upper left corner.
Addition
It seems digulino and I agree. I would like to add that you should be able to get the layout scheme you want with only a RelativeLayout inside a ScrollView, maybe a HorizontalScrollView too if you want horizontal scrolling with fling gesture support.
Im posting from my phone so please excuse stupid typos and formatting issues.
I have an activity which lists saved games that the player can load.
I created a simple layout xml file which defines a ScrollView. On load, I grab all the saved games and programatically add a view for each saved game to a vertically oriented LinearLayout child of the ScrollView.
The view for each game consists of a Horizontally oriented LinearLayout which in turn contains a Button and a vertically oriented LinearLayout. That LinearLayout in turn contains some TextViews and ImageViews (and one more LinearLayout which I'm ommitting here for the sake of clarity).
The hierarchy looks something like this (some details omitted).
ScrollView
LinearLayout - vertical
Each saved game:
LinearLayout - horizontal
Button - load game
LinearLayout - vertical
TextView - game name
TextView - date string
My problem:
I would like the top of the button and the "game name" texview to be vertically aligned but the TextView (or maybe it's LinearLayout parent) has some rogue padding on top that I can't get rid of. See screenshot for details.
LoadSaved class:
Note: mScrollView is badly named. It refers to the ScrollView's child LinearLayout.
public class LoadSaved extends Activity {
public LinearLayout mScrollView;
private MinerDb mDb;
public void onCreate(Bundle b) {
super.onCreate(b);
setContentView(R.layout.loadsaved);
mDb = new MinerDb(this);
mScrollView = (LinearLayout) findViewById(R.id.load_scroll_view);
Bundle[] savedGames = mDb.getSavedGames();
for (int i = 0; i < savedGames.length; i++) {
Bundle game = savedGames[i];
final int gameId = game.getInt("gameId");
String name = game.getString("name");
String date = game.getString("date");
Bundle player = game.getBundle("player");
int playerMoney = player.getInt("money");
int playerHealth = player.getInt("health");
LinearLayout gameContainer = new LinearLayout(getApplicationContext());
gameContainer.setPadding(5, 5, 5, 5);
gameContainer.setGravity(Gravity.TOP);
gameContainer.setOrientation(LinearLayout.HORIZONTAL);
gameContainer.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
Button loadButton = new Button(getApplicationContext());
loadButton.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
loadButton.setText("Load");
LinearLayout gameInfo = new LinearLayout(getApplicationContext());
gameInfo.setOrientation(LinearLayout.VERTICAL);
gameInfo.setPadding(10,0,10,10);
gameInfo.setGravity(Gravity.TOP);
gameInfo.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
TextView nameView = new TextView(getApplicationContext());
nameView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
nameView.setGravity(Gravity.TOP);
nameView.setText(name);
TextView dateView = new TextView(getApplicationContext());
dateView.setPadding(5,0,0,0);
dateView.setGravity(Gravity.TOP);
dateView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
dateView.setText(date);
LinearLayout playerView = new LinearLayout(getApplicationContext());
playerView.setOrientation(LinearLayout.HORIZONTAL);
playerView.setPadding(5,0,0,0);
playerView.setGravity(Gravity.TOP);
playerView.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT));
TextView playerMoneyView = new TextView(getApplicationContext());
playerMoneyView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
playerMoneyView.setPadding(0,0,10,0);
playerMoneyView.setTextColor(Color.GREEN);
playerMoneyView.setText("$" + playerMoney);
TextView playerHealthView = new TextView(getApplicationContext());
playerHealthView.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
playerHealthView.setPadding(0,0,10,0);
playerHealthView.setTextColor(Color.RED);
playerHealthView.setText(playerHealth + "%");
playerView.addView(playerMoneyView);
playerView.addView(playerHealthView);
gameInfo.addView(nameView);
gameInfo.addView(dateView);
gameInfo.addView(playerView);
gameContainer.addView(loadButton);
gameContainer.addView(gameInfo);
mScrollView.addView(gameContainer);
loadButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
Log.e("LoadSaved", "LoadSaved::onCreate: Clicking: " + gameId);
Intent loadGameIntent = new Intent(LoadSaved.this, Miner.class);
loadGameIntent.putExtra("load_game", gameId);
startActivity(loadGameIntent);
finish();
}
});
}
}
}
loadsaved.xml
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/load_scroll_view" />
</ScrollView>
</LinearLayout>
If you want any kind of alignment, why don't you use a RelativeLayout? That's basically designed to align one view with another. android:layout_alignTop sounds like something you want.
(And, of course, verify that the padding values are the same in all controls, but I'm sure you did that.)
Why don't you try using a ListView for that kind of gui.
You will still need to define a row xml.
+1 to the answers suggesting ListView and RelativeLayout. For this type of situation you probably want a ListView with an item layout using RelativeLayout. (ListView will scale much better if there are many items, and if this is for a list of saved games it seems like this could grow quite a bit.) For this type of UI it's recommended to have the whole row/list item clickable rather than use a small Load button, but that's a design issue and ultimately up to you.
Don't use getApplicationContext for creating your views. Activity is a Context, just pass this in your case.
By default LinearLayouts try to align child views by their text baseline if present. Note that the bottom of your button's Load text aligns perfectly with the CURRENT_GAME text in your screenshot. Try gameContainer.setBaselineAligned(false).
Normally your gameInfo layout would only report the baseline of one of its children if you set a baselineAlignedChildIndex, but it looks like this behavior changed between cupcake and eclair when creating LinearLayouts programmatically. (Link to the commit that changed it in AOSP here.)