I have this code for initial/default layout
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Retrieve the shared preferences
mGameSettings = getSharedPreferences(GAME_PREFERENCES, Context.MODE_PRIVATE);
glView = new GLSurfaceView(this);
glView.setRenderer(this);
glView.setZOrderMediaOverlay(false);
glView.setId(123); // set id
layout = new RelativeLayout(this);
// create a fake view with zero size and place it to center of RelativeLayout
fakeView = new View(this);
fakeView.setId(24736);
fakeparams = new RelativeLayout.LayoutParams(0, 0);
fakeparams.addRule(RelativeLayout.CENTER_IN_PARENT, RelativeLayout.TRUE);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
params.addRule(RelativeLayout.ABOVE, fakeView.getId()); // set position is above fakeView
layout.addView(glView, params);
setContentView(layout);
Say we are in main menu and in some menu when user click it, i'll inflate new layout which I prepared in res/layout folder
myStoryView = getLayoutInflater().inflate(R.layout.story2, layout, false);
layout.addView(myStoryView);
This goes well, I get the result, new layout appear as it should.
later I call this code when user want to close story screen and return to main menu:
layout.removeView(myStoryView);
But what happened is I lose all my layout. It exits entire app, though in my galaxy tab when I hold center button, I can see my app is there, maybe it's not entirely closed. I only have no layout to display.
sorry, i just need to
setContentView(layout)
Related
I have a piece of code which I use to create a new LinearLayout. Within the layout I wish to add a TextView which contains both a label and a value. Then next to it on the right I want to display the button. I want the button to be located toward the end of the screen, without stretching the button. I am happy with the button width and height as WARP_CONTENT.
How can I achieve this in code? I have barely any XML so using XML is not an option. I am trying to make the app as dynamic as possible, so I decided to steer clear of XML.
Please see the code below:
// Build a button
final Button addButton = new Button(task.getParent());
addButton.setText("Add New");
addButton.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
// Open a file picker here to let the user pick a file
}
});
// Build a new layout to hold all the elements
LinearLayout verticalLayout = new LinearLayout(task.getParent());
verticalLayout.setOrientation(LinearLayout.HORIZONTAL);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(addButton);
Thank you guys in advance.
Try this: Add Space (View) between TextView & Button.
// View space = new View(parent_context);
View space = new View(task.getParent());
// Width:0dp, Height:1 & Weight: 1.0
LinearLayout.LayoutParams spaceLP = new LinearLayout.LayoutParams(0, 1, 1.0f);
space.setLayoutParams(spaceLP);
verticalLayout.addView(sizeTextView);
verticalLayout.addView(space);
verticalLayout.addView(addButton);
Add textview with size and gravity,like this:
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,LinearLayout.LayoutParams.WRAP_CONTENT);
params.setLayoutDirection(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
verticalLayout.addView(sizeTextView,params);
To achieve this you should use Relative layout and RelativeLayout.LayoutParams. By using LayoutParams you can set the rule to align your views as per your requirements.
for example
RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams)button.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
params.addRule(RelativeLayout.LEFT_OF, R.id.id_to_be_left_of);
button.setLayoutParams(params);
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));
}
Hi I am having troubles trying to get this Relative Layout placed below another Relative Layout. In my Activity I have a NavBar (RelativeLayout) that is aligned to the top of the activity. I would like to place my TitleBar (RelativeLayout) below this programmatically.
Here is my onCreate method where I allocate both the NavBar and TitleBar and add them to my Activity. The NavBar is correctly aligned at the top of the activity, however, the TitleBar is aligned with the top of the activity as well. I'd like the top of the TitleBar to align with the bottom of the NavBar so it is placed below the NavBar.
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, R.id.BINavBar_layout);
this.titleBar.setLayoutParams(params);
I don't think you are actually setting the id of the nav bar. Try the following:
super.onCreate(bundle);
Context context = getApplicationContext();
RelativeLayout.LayoutParams params;
//navbar
this.navBar = new BINavBar(context);
relativeLayout.addView(this.navBar);
params = (LayoutParams)this.navBar.getLayoutParams();
params.addRule(RelativeLayout.ALIGN_PARENT_TOP);
this.navBar.setLayoutParams(params);
this.navBar.setId(R.id.BINavBar_layout); // this is the change
//titleBar
this.titleBar = new BITitleBar(context);
relativeLayout.addView(this.titleBar);
params = (LayoutParams)this.titleBar.getLayoutParams();
params.addRule(RelativeLayout.BELOW, this.navBar.getId()); // this should use navBar's id
this.titleBar.setLayoutParams(params);
Note that you may need to set the id yourself. Are you sure that R.id.BINavBar_layout exists? If not, try using the following function: View.generateViewId()
I have a feeling the ID of the view you're creating isn't using the ID you're trying to use. Try this:
//params.addRule(RelativeLayout.BELOW, R.id.BINavBar_layout);
params.addRule(RelativeLayout.BELOW, this.navBar.getId());
I'm trying to draw an image inside a do while in the OnCreate method but it fails to show.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
drawElements();
}
public void drawElements(){
do{
RelativeLayout layout = new RelativeLayout(this);
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(80, 60);
params.setMargins(0, 0, 0, 0);
ImageView image = new ImageView(this);
personaje.setImageResource(R.drawable.image);
layout.addView(image, params);
setContentView(layout);
Log.i("MainActivity","here comes");
}while(!esc)
}
It's strange because it comes in the do while, because the log shows constantly on the LogCat but the image does not show.
In fact, it fails to remove the status bar or the title of the application as specified in the onCreate, but if I remove the do while it shows everything correctly.
Any suggestions?
You need to rethink your whole design. What you are doing here does not make any sense.
Your while loop is going to loop forever, blocking the UI thread. Thus, nothing will ever be displayed.
Instead, you should display a single image in onCreate. Add a key listener or button listener that will change the image within the listener.
I want to create a relative Layout dynamically through code with 2 Textviews one below the other.How to implement android:layout_below property through code in Android.
can anyone help me in sorting out this issue.
Thanks in Advance,
final TextView upperTxt = (...)
upperTxt.setId(12345);
final TextView lowerTxt = (...);
final RelativeLayout.LayoutParams params = RelativeLayout.LayoutParams(this, null);
params.addRule(RelativeLayout.BELOW, 12345);
lowerTxt.setLayoutParams(params);
Here is my solution for my special Problem.
In case the username wouldn't be found in the db i had to create a RelativeLayout that looks like the xml-generated one.
// text view appears on top of the edit text
enterNameRequest = new TextView(mainActivity.getApplicationContext());
// fill the view with a string from strings.xml
enterNameRequest.setText(mainActivity.getResources().getString(R.string.enterNameRequest));
// edit text appears below text view and above button
enterName = new EditText(mainActivity.getApplicationContext());
enterName.setId(667);
// button appears at the bottom of the relative layout
saveUserName = new Button(mainActivity.getApplicationContext());
saveUserName.setText(mainActivity.getResources().getString(R.string.useUserName));
saveUserName.setId(666);
// generate the relative layout
RelativeLayout layout = new RelativeLayout(mainActivity.getApplicationContext());
layout.setId(668);
// set a background graphic by its id
layout.setBackgroundDrawable(mainActivity.getApplicationContext().getResources().getDrawable(R.drawable.background_head_neutral));
// runtime told me that i MUST use width and height parameters!
LayoutParams params2 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ABOVE, 666);
enterName.setLayoutParams(params2);
LayoutParams params3 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params3.addRule(RelativeLayout.ABOVE, 667);
enterNameRequest.setLayoutParams(params3);
LayoutParams params4 = new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT);
params4.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM, 668);
saveUserName.setLayoutParams(params4);
// add views
layout.addView(enterNameRequest);
layout.addView(enterName);
layout.addView(saveUserName);
/* todo: set button action */
mainActivity.setContentView(layout);
What i found out additionally:
It is not so good to manipulate the layout manually from within java!
You should better use a new Activity and set a new layout in it.
This way, the application-code is readable a lot better!
I even tried to set several layouts (not manually, but wit setContentView) in one activity, and it turned out that i didn't know where what was accessing what else... Also, i had a great problem in adding onClickListeners... so you better use -- android:onClick="myButtonMethod" -- in your button tag in the xml and have a method in your according activity, which uses the layout, like this:
public void myButtonMethod(View v){
// do stuff
}
This improves performance because you are not using additional Listeners - but you use the already available Listener that is bound to your activity in every case.
u can try this
LinearLayout.LayoutParams leftMarginParams = new LinearLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);``
leftMarginParams.leftMargin = 50;
Button btn1 = new Button(this);
btn1.setText("Button1");
linLayout.addView(btn1, leftMarginParams)