I have following Table in my Android app. Everything works, except the last button - about me. When I set background to transparent color - it is not beign display on my device (however, it is visible on the graphical view of the layout in the eclipse. By "not display" I mean, that it is not on the screen - on its place its "next". If I remove android:background from the last button - it comes back to device. Whats wrong?
<TableRow
android:id="#+id/tableRow1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageButton
android:id="#+id/buttonShare"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:onClick="shareIt"
android:src="#drawable/social_share" />
<ImageButton
android:id="#+id/buttonBack"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:onClick="prevImage"
android:src="#drawable/navigation_previous_item" />
<ImageButton
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:src="#drawable/navigation_accept" />
<ImageButton
android:id="#+id/buttonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:onClick="showAbout"
android:src="#drawable/action_about" />
<ImageButton
android:id="#+id/buttonForward"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:onClick="nextImage"
android:src="#drawable/navigation_next_item" />
<ImageButton
android:id="#+id/buttonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/transparent"
android:onClick="showAbout"
android:src="#drawable/action_about" />
</TableRow>
</TableLayout>
Part of my color resource is like below:
<color name="transparent">#00000000</color>
I just loaded overwrited action_about.png picture with new one. Now, the button appears - but it shows old png, about, not the new one. What is going on?
Why are you trying to set the background to transparent? Do you just want a button that does not have the device default button "frame" and instead has only your action_about drawable? If so you can do that with a normal Button instead of ImageButton like so:
<Button
android:id="#+id/buttonAbout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/action_about"
android:onClick="showAbout"/>
EDIT: Also note that you have the buttonAbout element twice in your layout, it is at the bottom, and 3 from the bottom.
EDIT 2: Do you not have a clean... option at all under the Project menu at the top? or is it just grayed out?
I downloaded Eclipse Indigo sr2 (I can't find link on the site for sr1) and It is there for me.
Related
I'm making an app in Android Studio, my problem is the position on the button. In the layout the position is correct, but in the simulator the button is in the left corner.
Example:
Design
Simulator
My code:
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F10000"
android:onClick="onClick"
android:text="INICIO"
android:textColor="#000000"
tools:layout_editor_absoluteX="161dp"
tools:layout_editor_absoluteY="622dp />
the tools keyword in your Button is just to show in layout and nothing more, for define the position of the button you should rely on your button parent,for example if its LinearLayout you can change the position of button by giving it android:layout_gravity
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/parent">
<androidx.appcompat.widget.AppCompatButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#F10000"
android:onClick="onClick"
android:text="INICIO"
android:textColor="#000000"
android:layout_gravity="center" />
</LinearLayout>
So I have this STRANGE issue happening on a galaxy tab (not sure if it's also happening on other tablets, but it doesn't on my nexus 5).
Here's a preview of my layout in android studio:
The XML for a row with white background is like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="horizontal"
>
<TextView
android:id="#+id/first_name_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="#dimen/content_spacing_normal"
android:textColor="#color/gray_dark"
android:textSize="#dimen/font_size_normal"
android:text="#string/form.label.first_name"
/>
<EditText
android:id="#+id/first_name_value"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:padding="#dimen/content_spacing_normal"
android:background="#null"
android:ellipsize="end"
android:textColor="#color/purple"
android:textSize="#dimen/font_size_small"
android:layout_weight="1"
android:gravity="right"
android:hint="#string/form.placeholder.first_name"
>
</EditText>
</LinearLayout>
On the Galaxy Tab, the layout looks like this:
What's even stranger is that it displayed properly a couple of times, randomly, but most of the time the white background color doesn't seem to propagate to it's children.
Thanks in advance.
I have a Button with its width as match_parent. And I want a image/icon with a text in its center. I have triedandroid:drawableStart attribute but its of no help. I don't know what I am missing. Is there any one who is also facing the same problem.
Note:This link is not solving my problem. I am attaching my button portion of xml
<Button
android:background="#drawable/blue_button"
android:id="#+id/btn_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:drawableStart="#drawable/ic_list"
android:text="#string/btn_schedule"
android:textColor="#android:color/white" />
After adding these attribute my text is coming on center but image/icon is still on left side of button. Can any body help!!!
You might want to check my answer to this question:
Android Button with text and image
Its an ugly solution but it you want the image and text to be centered as a group in the button rather than text center and drawable padded from the side then its the only way I have found.
100% working
<FrameLayout
style="?android:attr/buttonStyle"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
style="?android:attr/buttonStyle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#null"
android:clickable="false"
android:drawableLeft="#android:drawable/ic_delete"
android:focusable="false"
android:gravity="center"
android:minHeight="0dp"
android:minWidth="0dp"
android:text="Button Challenge" />
from here
This works for me. Basically play with the paddingLeft and PaddingRight of the button to get the text and image close together to the center.
Note: Might be an issue with rather long texts.
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="search button"
android:drawableStart="#drawable/ic_action_search"
android:paddingLeft="60dp"
android:paddingRight="60dp"/>
</LinearLayout>
I had the same problem and I hadn't found anywhere clear solution. BUT I got some information which also help me to provide following solutions (I have chosen the second one):
Create image which will contain your image and text
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:drawableTop="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
It is not so clear and you have to add more complicated changes to your layout but it is easier to modify the text later
<FrameLayout
android:gravity="center_horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<Button
android:layout_height="50dp"
android:layout_width="match_parent" />
<TextView
android:text="sample"
android:layout_height="50dp"
android:layout_width="wrap_content"
android:drawableLeft="#drawable/YOUR_IMAGE"
android:gravity="center_horizontal|center_vertical" />
</FrameLayout>
I tried android:drawableTop
The image shown in the center of the button
I'm currently learning android development, and before I get too stuck in to a large project, I decided I would need to learn about making an application accessible from as many devices as possible.
So I have a test application using RelativeLayout. The top-level activity has 6 large menu buttons on it. These buttons are square graphical images (not 9-patch buttons as they are - to my knowledge - too graphically primitive). On the device I'm using for testing, these buttons appear in a perfect 2x3 arrangement like such:
However, when I try and run this application on a larger device, the buttons will appear like so:
Is there a way to scale non-9-patch buttons based on the size of the screen, so that they will always appear like the first image? Is this recommended? If not, is there an alternative way of doing such a layout for different screen sizes?
My ideal layout would be scalable across different devices, like so:
I am using similar menu. And here is first row of it. The buttons in this menu has labels too.
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="20dip"
android:layout_marginRight="20dip"
android:gravity="center" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip" >
<Button
android:id="#+id/screen_home_btn_profile"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_ic_my_profile" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dip" >
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_my" />
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_profile"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="5dip" >
<Button
android:id="#+id/screen_home_btn_application"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/selector_ic_my_application" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:paddingBottom="5dip" >
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_my" />
<TextView
style="#style/label_text_style_home_screen"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/screen_home_label_application"
android:textStyle="bold" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
It seems you are giving outer margins to the buttons. Align them in center and give spaces between 2 buttons and not the screen border & buttons.
i am writing an application to control a robot
for controlling i need ( up, down , right, left ) keys
i used eclipse designer to create it but maybe it is not possible
i want to create something like this :
how can i create buttons like this in center of screen ?
is there a better way to create arrow keys on android ?
Here you go :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<View
android:id="#+id/view1"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/view1"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_above="#+id/view1"
android:layout_centerHorizontal="true"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/view1"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#+id/view1"
android:src="#drawable/ic_launcher" />
</RelativeLayout>
Of course, you can replace ImageView with anything else like an ImageButton.
Be sure, if you change the name of the first view, tu update the name in the 4 ImageView.
And be aware that the corners of each image are overlapping each other.
I suggest using ImageButtons and setting the background for each to a State List Drawable xml file. This will change the image between states(focused, pressed, etc).
The tricky part would be positioning each so that they orient themselves in this sort of "box" you have above.
Just create some PNGs of each button in their positions. Up button points up, left points left etc. Then use RelativeLayout to position them with respect to each other. You can also use Buttons as well