In my single screen android project(using only one XML file) when i press button1 then linerlayout1 is open and when i press button2 then linearlayout2 is open.my button1 and button2 is placed in linearlayout3.can it works??if yes then how??
thanks in advance.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativeLayout1"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:id="#+id/linearLayout1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_above="#+id/linearLayout3"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout1" />
<RadioButton
android:id="#+id/radioButton4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout1" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/Button2" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearLayout2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:orientation="vertical" >
<RadioButton
android:id="#+id/radioButton13"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout2" />
<RadioButton
android:id="#+id/radioButton14"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/linearLayout2" />
</LinearLayout>
</RelativeLayout>
Yes, it's very simple:
Use the android:Visibility XML tag initially, and when each respective button is pressed, you'd obtain your reference to the LinearLayouts, and set their visibility to VISIBLE or GONE in code.
Note: you want to use GONE rather than INVISIBILE as INVISIBILE will still take up screen space, so there'll be a large chunk of blank space where it was before, whereas I assume you want them in place of each other
in the layout xml of linear layout 1 and 2, use
android:visibility="gone"
From code
LinearLayout l1 = (LinearLayout) findViewById(R.id.linearLayout2);
you can use the following to make it visible
l1.setVisibility(View.VISIBLE);
and to hide it
l1. setVisibility(View.GONE):
You can play around with the "setVisibility" property of your layout.
In the "onCreate()" method you can do something like this
LinearLayout layout1 = (LinearLayout) view.findViewById(R.id.linearLayout1);
layout1.setVisibility(View.GONE);
And then on your button press
layout1.setVisibility(View.VISIBLE);
Related
I can use String-array inside Spinner
But i don't know if i can put anything else ?
I want to put some Button's in a Spinner but when i put some in XML , the application crashed !!
<Spinner
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/spinner"
android:layout_below="#+id/textView"
android:layout_toRightOf="#+id/textView"
android:layout_toEndOf="#+id/textView"
android:layout_marginTop="125dp" >
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent" />
<Button
android:layout_width="wrap_content"
android:layout_height="match_parent" />
</Spinner>
Is this the Right way to do it
Or what ???
This is not going to work bgecause this is not the way that you place buttons in a spinner.
By Definition Spinner is a dropdown menu that can almost take any layout. First of all you have to decide how your spinner row will be.
e.g.:
If you would like to have 2 buttons you got to make a layout/spinner_row.xml file with:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="match_parent"
android:padding="20dp"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button" />
<Button
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button2" />
</LinearLayout>
and looks like:
Then you got to make an adapter that adapts each row of the spinner with the data that you would like.
I would like to place 2 small icons (with actions) over a big Button that take the 1/3 of the screen. The 2 icons and the button have to support actions when we click on them. I'm pretty sure I have to use imageButton for the icons. However I can't find any way to keep the icons (imageButton) on top of the button.
Here is my code:
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<ImageButton
android:id="#+id/icon1"
android:src="#drawable/ic1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/ic1"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action1"/>
<ImageButton
android:id="#+id/icon2"
android:src="#drawable/ic2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/ic2"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
<com.myapp.CustomViews.myButton
android:id="#+id/big_button1"
android:text="#string/text_big_button1"
android:background="#drawable/changing_button1"
android:textColor="#color/white"
android:layout_height="match_parent"
android:textSize="#dimen/font_size_buttons"
android:layout_width="match_parent"
android:gravity="center"
android:textAllCaps="false"
android:capitalize="none"
android:onClick="bigAction1"/>
</RelativeLayout>
The "#drawable/changing_button1" on the big Button allows me to put one color for the button and a different one when focused or pressed.
I tried so many different things and I couldn't find a way while I'm sure there is a simple solution. The icons remain hidden behind the big Button.
Thank you for your help!
You could use FrameLayout to overlay views. For example:
<FrameLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp">
<com.myapp.CustomViews.myButton
android:layout_gravitiy="center"
.../>
<ImageButton
android:layout_gravitiy="left|center_vertical"
.../>
<ImageButton
android:layout_gravitiy="right|center_vertical"
.../>
</FrameLayout>
Here is a slightly modified version of your layout, which works fine.
I replaced all your custom stuff with plain android and standard buttons, and moved the big button up in the layout, because if it is at the bottom, it has a higher z-index and overlays the two small buttons.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/big_button1"
android:text="BIG BUTTON"
android:background="#android:color/holo_blue_dark"
android:textColor="#color/white"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:gravity="center"
android:textAllCaps="false"
android:capitalize="none"
android:onClick="bigAction1"/>
<Button
android:id="#+id/icon1"
android:text="BT1"
android:background="#android:color/holo_red_dark"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action1"/>
<Button
android:id="#+id/icon2"
android:text="BT2"
android:background="#android:color/holo_green_dark"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
</RelativeLayout>
How about changing order in the layout ? Place your Button widget on top, then declare 2 icons after that ?
You should add android:translationZ="5dip" for the 2 imageButton. It works (you should run and see, it didn't appear so on the IDE)
I finally found a way to do it.
The solution is to use the RelativeLayout as a button and replace the Button by a simple TextView:
<RelativeLayout
android:layout_width="match_parent"
android:layout_weight="1"
android:layout_height="0dp"
android:background="#drawable/custom_button_changing_color"
android:clickable="true"
android:focusable="true"
android:id="#+id/button_layout"
android:onClick="bigAction1">
<TextView
android:id="#+id/text_button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/text"
android:gravity="center"
android:layout_centerVertical="true"
android:textColor="#color/white"
android:textSize="#dimen/font_size_buttons"
android:textAllCaps="false"
android:background="#color/transparent"/>
<ImageButton
android:id="#+id/icon1"
android:src="#drawable/ic1"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:contentDescription="#string/ic1"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action2"/>
<ImageButton
android:id="#+id/icon2"
android:src="#drawable/ic2"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:contentDescription="#string/ic2"
android:layout_width="48dp"
android:layout_height="48dp"
android:onClick="action3"/>
</RelativeLayout>
I hope this helps! :)
I'm trying to develop an Augmented Reality aplication for Android using Metaio. But actually I've a problem.
I want to make a little menu appear when the aplication recongize a marker.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:background="#android:color/transparent"
android:onClick="onButtonClick"
android:contentDescription="#null"
android:src="#drawable/cancel" />
<LinearLayout
android:id="#+id/buttonBar123"
style="#android:style/ButtonBar"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:visibility="invisible"
android:orientation="horizontal"
android:gravity="center">
<Button
android:id="#+id/TextoButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onTextoButtonClick"
android:text="Texto" />
<Button
android:id="#+id/ImageButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="onImagemButtonClick"
android:text="Imagem" />
</LinearLayout>
</RelativeLayout>
My layout is on a View and I don't know how to change the ButtonBar123 to visible in Java code. Can you help me? Thanks
You just need to get a reference to your Layout
LinearLayout buttonBar = (LinearLayout) findViewById(R.id.buttonBar123);
then set the visibility
buttonBar.setVisibility(View.VISIBLE);
Visibility
LinearLayout buttonBar = (LinearLayout)findViewById(R.id.buttonBar123);
buttonBar.setVisibility(View.VISIBLE);
I need to center layout dynamically, but the problem is I use inflate method to add the layout. And I want buttons to be center.
I tried adding xml attributes like gravity, layout_gravity. But it didn't work.
And I tried to add parameters, but since I inflate xml I can't add parameters. Because I can't use addRule method.
Then I tried to make every element center by adding gravity attribute to root layout, it didn't work out either.
Here is the my code.
actvity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/RelativeLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<SurfaceView
android:id="#+id/surfaceView1"
android:layout_width="match_parent"
android:layout_height="335dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:id="#+id/buttons_layout"
android:layout_below="#+id/surfaceView1"
>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="Button" />
</RelativeLayout>
</RelativeLayout>
three_buttons_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="29dp"
android:text="Button 1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="29dp"
android:text="Button 2" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
And here is my java code
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
inflater = (LayoutInflater) this.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
parent = (ViewGroup)findViewById(R.id.buttons_layout);
captureButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
camera.takePicture(null, null, photoCallback);
captureButton.setVisibility(View.GONE);
inflater.inflate(R.layout.three_buttons_layout, parent, true);
}
});
I want Button1, Button2, and Button3 to be center.
A horizontal LinearLayout will always left-align its subviews. You're going to have to use a RelativeLayout in order to get the positioning you want. I'd suggest giving button1 the android:layout_alignParentLeft attribute, button2 the android:layout_centerHorizontal attribute and button3 the android:layout_alignParentRight attribute, like this:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginBottom="29dp"
android:text="Button 1" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginBottom="29dp"
android:text="Button 2" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:text="Button 3" />
</RelativeLayout>
(I'm just editing your code in the comment editor, so there might be a syntax error in there somewhere, but you get the idea.)
Also, you should be able to set any attribute you want on those buttons after inflating the view. Just call findViewById(R.id.button1) and it should return you the view you want (as long as its after the inflater.inflate call). On that note, you've given all your buttons the same id, which probably isn't a good idea.
Hope that helps! Let me know if it's still not working.
Edit: I just realized you're going to need that RelativeLayout to have android:layout_width="match_parent". I've edited the above xml accordingly.
//try this
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/LinearLayout1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
>
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 2" />
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button 3" />
</LinearLayout>
//your can remove this below line from parent to make it as center horizontally
android:layout_gravity="center"
I am trying to display two texts in a layout such that they are displayed one after the other but the following code is pasting the strings right above each other. What should I do. I tried to exchange fill_parent & wrap_content between the two textviews but it is worthless
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".StartingPoint" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/testing"
tools:context=".StartingPoint" />
You have to put the property below in the second TextView and set on the below property the id of the first TextView
change your two TextView by this two:
<TextView
android:id="#+id/TextView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/hello_world"
tools:context=".StartingPoint" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/TextView1"
android:layout_centerHorizontal="false"
android:layout_centerVertical="false"
android:text="#string/testing"
tools:context=".StartingPoint" />
But you need to read the documentation at android developers first, because that is very basic.
Hope to help :)
Use LinearLayout as the parent layout and set its orientation to horizontal. This will work.
You have to tell the RelativeLayout how it has to place the different views.
Documentation: http://developer.android.com/guide/topics/ui/layout/relative.html
It could be simpler to use a LinearLayout.
If you need RelativeLayout menas,
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="FirstText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:layout_marginTop="30dp"
android:text="secondText"
android:textAppearance="?android:attr/textAppearanceLarge" />
</RelativeLayout>
Else you need LinearLayout means,
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="FirstText"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="30dp"
android:text="secondText"
android:textAppearance="?android:attr/textAppearanceLarge" />
</LinearLayout>
In the Case of Relative layout you need the textview is one by one means use this parameter, android:layout_below="#+id/textView1" else you are using LinearLayout means use this parameter android:orientation="vertical"