I have a layout which is mainly a linearlayout with a Button and another layout which is is a linearlayout again with an EditText and two buttons : Cancel and Submit.
What I want is initially the first layout to be displayed. When user clicks the button this layout is being replaced by the 2nd layout(the one with the two buttons).
I have already tried ViewFlipper but the problem is that it takes the height of the one with bigger height. So when first layout is displayed
it has a big white space between the button and the content below it.
If you want to keep using a ViewFlipper you can fix the height issue by adding this to your ViewFlipper in XML:
android:measureAllChildren="false"
Layout1.setVisibility(INVISIBLE)
Layout2.setVisibility(VISIBLE)
Try use only one layout and set VISIBLE or GONE:
View b = findViewById(R.id.button);
b.setVisibility(View.GONE);
Or If you Have two layouts, use Intent:
Intent i = new Intent(this, ActivityTwo.class);
startActivity(i)
Related
I am creating an android layout in which I will place 3 buttons vertically with some space between them. But based on the requirements, the button count can come down to 2, and I need the buttons to repositioned based on the available screen space.See the screens below.
now the second case with two buttons should be like
How should I go about doing this ?
You can just set visibility of buttons depending upon your condition just like the following:
button.setVisibility(View.GONE);
When you set visibility to gone then the space will considered available for other layouts and your will be set as per your desire.
When you set visibility to invisible then the space will considered as allocated.
You can try below code for achieving the same based on your condition for adding buttons to your layout
//the layout on which you are working
LinearLayout layout = (LinearLayout) findViewById(R.id.linear_layout_tags);
//set the properties for button
Button btnTag = new Button(this);
btnTag.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));
btnTag.setText("Button");
btnTag.setId(some_random_id);
//add button to the layout
layout.addView(btnTag);
You can define all three buttons in the layout XML and then :
if you want to show the third button
thirdButton.setVisibility(View.VISIBLE);
if you want to hide
thirdButton.setVisibility(View.GONE);
at least if using a Linear or Relative Layout it should repositione by itself.
I've been making all of my views dynamically, and now I've come to the point where I want to add an EditText for people to write in.
I've been able to accomplish this for the most part, but it doesn't look right. I have a linear layout that I'm adding a relative layout to. I'm making the relative layout have a white background, then adding the EditText. Problem is, it always adds it to the direct center of the relative layout, and options to align it vertically to the top have so far failed.
I also need to be able to pull the text from it later when a separate button was pressed (I know how to make the button work, it's the pulling text from it part I'm a bit iffy on). Here's my code so far:
public void addEditText(LinearLayout L){
EditText myEditText = new EditText(c);
myEditText.setSingleLine(false);
RelativeLayout l1 = new RelativeLayout(c);
LinearLayout.LayoutParams lp=new LinearLayout.LayoutParams(scWidth1, 300);
lp.gravity=Gravity.CENTER;
l1.setLayoutParams(lp);
l1.setBackgroundColor(Color.WHITE);
l1.setGravity(Gravity.CENTER_VERTICAL);
l1.addView(myEditText);
L.addView(l1);
}
l1.setGravity(Gravity.CENTER_VERTICAL); places the EditText in center vertical of the parent container i.e RelativeLayout, remove that line.
I have 4 buttons in my linear layout and i need to bring to front first button.
Normal order is
Button 1 | Button 2 | Button 3 | Button 4
But when I call button1.bringToFront() function , button1 is going to end like
Button 2 | Button 3 | Button 4 | Button 1
How can I solve this problem. Relative layout doesn't causes this problem but I have to use LinearLayout because buttons will order vertically and I'm deleting a button in some conditions.
Thanks
LinearLayout doesn't work with the z-axis, hence, it's name linear. Try using a RelativeLayout and then call bringToFront() to get the desired effect. With a RelativeLayout you can call layout_alignBollow to order the views vertically. Or you can nest views and layouts, for instance, within your LinearLayout nest three RelativeLayout within those you can place your Buttons (be careful with this approach as adding too many views can be a bad thing).
If you have to work with z-axis in a LinearLayout, you may use setTranslationZ function.
Example:
yourView.setTranslationZ(100);
Since bringToFront messes up the LinearLayout order, I decided to use a RelativeLayout and put the "top" view (the view I want on top) last in the XML.
Example:
<RelativeLayout ...>
<ViewBelow
android:layout_below="#+id/view_on_top"
...
/>
<!-- Last view in XML appears above other views on screen -->
<ViewOnTop
android:id="#+id/view_on_top"
...
/>
</RelativeLayout>
In the specific case of this question, the ViewOnTop would be the Button 1, and the ViewBelow would be a LinearLayout containing the other buttons.
make your layout call the forceLayout() to disable re-arrangement of the layout.
hi I have 2 layout inside a framelayout and i want one linearlayout at top and another at bottom where i will specify height for both the layouts
and how to set this parameter for the linearLayout "layout_gravity " by code not by XML
Use a surfaceView , the SurfaceView allows you to stack in the Z order.
Docs
make sure your surfaceView is under the views of the views you want to stack.
Hope this helps
first fetch the layout from id.
LinearLayout layout = (LinearLayout)findViewById(R.id.IDOFLAYOUT);
//the set the gravity by this method
layout.setGravity(Gravity.CENTER);
How do I go about implementing a button bar with buttons of different shapes and heights? As an example (please excuse my poor drawing skills for this quick mockup):
The example bar has 3 buttons, with the middle button (3) a different shape and height than the other 2 buttons (1,2). The button bar will be a view that is included and merged into other views so as to seem to float on top of the parent view.
I was thinking of implementing buttons 1 and 2 into a layout, and then button 3 as another layout that I then merge with the first two button's layout.
like my previous comrades said, you need some kind of layout or container that can have a background (if you wish for button #3 to hoover above it) then use relative layout for mixing the two the disadvantage of this other than complexity is that youcannot relate to the other two buttons since they reside in a different layout.
More elegant solution may be to have a special background drawable that can:
have a method setCurrentHeight() that will specify the height the actual viewable section should have the rest will be filled with transparent color.
override it's own draw so just before it's drawing it will have a callback called, call back you can register yourself to.
then you can register the callback in your activity to take the current position of the #3 button and set the height accordingly, this way you are still with one layout with special drawable as background.
A customized LevelDrawable might do the trick.
I would layout this bar as follows:
A RelativeLayout as a container for the rest, with height set to wrap_content and alignparentbottom = true
An ImageView for the bar
2 Buttons with a transparent background (Button 1 and 2)
Button 3 is a custom Button with a custom Image of a trapezoid as background
So you will have a Layout similar to this:
<RelativeLayout
...>
<ImageView
.../>
<Button
... Button 1 />
<Button
... Button 2 />
<Button
... Button 3 />
</RelativeLayout>
I don't exactly know that this will work, and I can't test it, but you might give something like this a try; I believe it can all be done elegantly through XML.
Have a RelativeLayout (id:mainLayout) that will contain all of your views, wrap_content for both dimensions.
Have a blank View as your first child that will serve as your background bar
Set the View's background color/image to what you want; layout_width to fill_parent; layout_height to wrap_content; layout_alignTop="#id/leftButton"; layout_alignBottom="#id/leftButton".
Add an ImageButton for your center button (id:bigButton), wrap_content for both dimensions; layout_centerInParent="true".
Add an ImageButton for your left button (id:leftButton), wrap_content for both dimensions; layout_toLeftOf="#id/bigButton"; layout_centerInParent="true".
Add an ImageButton for your right button (id:rightButton), wrap_content for both dimensions; layout_toRightOf="#id/bigButton"; layout_centerInParent="true".
In my head, I believe this works, but I could be off. Regardless, something to think about, and I hope it helps you find a solution. :)
Better you can tablelayout with different button styles or relative layout for button "3"