Android: Image Button icon and text - android

I'm having trouble with the layout of my navigation bar. I was hoping to have an icon with text underneath it all centered in the button. Currently when the icon is drawn in it pushes the text off the page. The only way to get it back is by using android:drawablePadding="" (from here) using a negative value but it looks like this:
My xml looks like this:
<LinearLayout android:id="#+id/navbar"
android:layout_alignParentBottom="true" android:gravity="center"
android:layout_width="fill_parent" android:background="#001"
android:layout_height="60dip" android:layout_weight="1"
android:orientation="horizontal">
<Button android:layout_height="fill_parent" android:id="#+id/navhomebtn" android:enabled="false"
android:textColor="#FFF" android:background="#drawable/navbuttonselected" android:drawableTop="#drawable/homeicon"
android:drawablePadding="-40dip"
android:text="Home" android:layout_weight=".25" android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="#+id/navsearchbtn"
android:textColor="#FFF" android:background="#drawable/navbuttons"
android:text="Search" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="#+id/navfavbtn"
android:textColor="#FFF" android:background="#drawable/navbuttons"
android:text="Favorites" android:layout_weight=".25"
android:layout_width="0dip" />
<Button android:layout_height="fill_parent" android:id="#+id/navlistbtn"
android:textColor="#FFF" android:background="#drawable/navbuttons"
android:text="Loans" android:layout_weight=".25"
android:layout_width="0dip" />
</LinearLayout>

The way your buttons look you might as well use a relativelayout containing a textview and an imageview. Then set the relativelayouts clickable attribute to true and the background attribute to your navbuttons drawable.
Another way is to use the drawableLeft/Right/Top/Bottom attributes like this:
<Button
android:id="#+id/home_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableLeft="#drawable/home_icon"
android:background="#drawable/nav_background"
android:text="Home"/>

To set text and image for button you may do like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="Button"
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="#drawable/icon"></Button>
</LinearLayout>
Hope, it helps you!

Related

Use in android:layout_weight buttons Android

I try to put buttons on screen. I want that this screens be split in same space on whole screen. I read a lot of the answers about use in android:layout_weight but i didn't get success.
code:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<Button
android:id="#+id/p_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p_selector"
/>
<Button
android:id="#+id/p1_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p1_selector"
/>
<Button
android:id="#+id/p2_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p2_selector"
/>
<Button
android:id="#+id/p3_data_button"
android:layout_width="wrap_content"
android:layout_height="0dip"
android:layout_weight="1"
android:layout_gravity="center"
android:background="#drawable/p3_selector"/>
</LinearLayout>
EDIT:
This background work with selector. it is affected?
I add Answer.
Here's my code which split screen textviews in 5 parts on screen.. in your code you havent provided android:weightSum="5" property..
<LinearLayout
android:id="#+id/l"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5" >
<TextView
android:id="#+id/txtUHID"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="UHID: " />
<TextView
android:id="#+id/txtBedID"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Bed ID: "
android:visibility="invisible" />
<TextView
android:id="#+id/txtDOB"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="DOB: " />
<TextView
android:id="#+id/txtSex"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:text="Sex: " />
<TextView
android:id="#+id/txtAge"
style="#style/pateint_context_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="right"
android:text="Age: " />
</LinearLayout>
Use android:layout_height="match_parent" if you are using weights and you want for the elements to cover the entire space.
As the Button are of size "wrap_content". They aren't showing.
Just place some text android:text="test" in the all button. It would work. Else use image in background selector.
Your xml code is absolutely fine, and it should display what you want it to display.
There must be something wrong with the drawable background you have added to all the buttons. I suggest that you remove the drawable and apply some basic color like
android:background="#color/black"
to make sure that it's problem with the drawable. Good luck!
I found the problem . You all right. The code work fine. In the old version of this application they do function that in some case put all content in relative layout. I understood that when content is in relative layout its need to be put on linear layout. but it's Ddon't working..
I convinced after i replace this function that this was what cause to this problem.

Button's text is only half visible

I am having troulbe fixing the following issue. I have a button with text OK. The button is nested inside a LinearLayout. This layout is nested inside two RelativeLayout. The second/inner RelativeLayout has `android:layout_margin="15dp". This is causing the text inside the button to be only half visible.
The XML is below. I am using it because it gives me the "popup" dialog with the (X) button on the top right corner.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
>
<RelativeLayout
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:padding="2dp"
android:background="#drawable/xml_round_corners_background"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="15dp"
android:background="#drawable/xml_round_corners_background"
android:gravity="center_vertical|center_horizontal"
android:layout_weight="1"
>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_weight="1"
>
<TextView
android:id="#+id/exitTV"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:textAlignment="center"
android:text="TEXT....\n\n\n"
android:textColor="#color/white"
/>
</LinearLayout>
<LinearLayout
android:layout_weight="1"
android:id="#+id/bluetooth_error_button_linearlayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal"
android:layout_below="#+id/exitTV"
>
<Button
android:layout_margin="15dp"
android:id="#+id/bt_error_message_ok_button"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/xml_botton1"
android:text="OK"
android:textColor="#color/DarkMagenta" />
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<Button
android:id="#+id/bt_error_message_x_button"
android:layout_alignParentRight="true"
android:text="X"
android:textColor="#FFF"
android:background="#drawable/xml_round_dismiss_button"
android:gravity="center_vertical|center_horizontal"
android:layout_margin="7sp"
android:layout_height="30sp"
android:layout_width="30sp"
android:textSize="15sp"
android:textStyle="bold"
android:onClick="cancelActivity" />
</RelativeLayout>
Could anyone please tell me what can i do to make the text fully visible?
I have changed/addded/removed attributes to the LinearLayout and the RelativeLayout the TextView, and the Button as well. but I could not make it work.
Thanks
EDIT:
This photo is how Eclipse shows the window, and its actually how I want it to be.
This is a partial screen shot from my phone. Samsumg Galaxy S III mini (4.0" screen)
I jsut realized that I was using another XML file as the background for each button. This XML was causing those issues.
rather than try to "text" with "background" in your XML, try using drawableTop (or ~Left ~Right or ~Bottom). These set the text so that the drawable is above "drawableTop" your text or where ever you want it

Android text will not align to center

I have a TextView and 3 Buttons centered in a RelativeLayout, and the text in all three of them is left-justified with the parent center as the leftmost line.
I've tried changing the gravity of the TextView, Buttons, and layout but nothing is working and I'm baffled because I've never seen Android align text like this before.
I want the Button text to be centered within each Button and the top text to be center-justified.
An image of what it looks like is here: http://i.stack.imgur.com/9lHEd.png
Here's my xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<TextView
android:id="#+id/textView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_margin="20dp"
android:gravity="center_horizontal"
android:text="#string/greeting"
android:textAppearance="?android:attr/textAppearanceLarge" />
<Button
android:id="#+id/startQuizButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:onClick="#string/startQuiz"
android:text="#string/start_quiz" />
<Button
android:id="#+id/lessonsButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/startQuizButton"
android:layout_centerHorizontal="true"
android:onClick="#string/startLesson"
android:text="#string/lessonsString" />
<Button
android:id="#+id/viewAllButton"
android:layout_width="280dp"
android:layout_height="wrap_content"
android:layout_below="#+id/lessonsButton"
android:layout_centerHorizontal="true"
android:onClick="#string/showAll"
android:text="#string/view_all" />
I included a RelativeLayout because you have it as your parent layout. However, i am not sure that it is needed. See if the following code is what you are looking for.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="Greetings" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Start Quiz" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Lessons" />
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="View all questions" />
</LinearLayout>
</RelativeLayout>
add in button tab android:gravity="center"...see if it works..
u need to set layout width and height to fill_parent and everything will work fine
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:text="your text" />
if you want to centre some text, then you could also do it this way:
<TextView
android:text="Quantity"
android:textSize="34sp"
android:textColor="#ffffff"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-light"
android:layout_centerHorizontal="true"
android:paddingTop="10dp"/>
here i have changed made it horozontal by using android:layout_centerHorizontal="true"which takes a boolean value of true or false, by making it true, it will automatically be centred on all devices
hope that helps

Alignment in android

I am new to android, currently designing a User Home Page. please let me know how to align UI controls like - Button , EditText, etc to left/right/center in Linear and List Layout. button code eg.
you can achieve this by using set gravity of layout . here i have one xml file in which i have two image button and one text view . Here i set text view in center . So its fits to all devices.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<ImageButton
android:id="#+id/Back"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:src="#drawable/back"
android:layout_gravity="center_vertical"
android:layout_marginLeft="5dp"
android:layout_marginTop="2dp"/>
<LinearLayout
android:layout_height="fill_parent"
android:layout_width="wrap_content"
android:layout_weight="1"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/txtName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/white"
android:textSize="18sp"
android:textStyle="bold"
android:singleLine="true"
android:text="Top 100"/>
</LinearLayout>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
android:id="#+id/Playing"
android:visibility="invisible"
android:src="#drawable/nowplaying"
android:layout_gravity="center_vertical"
android:paddingTop="3dp"/>
</LinearLayout>
</LinearLayout>
Checkout the android:layout* properties and the android:gravity properties and this example.

Android: How to make all elements inside LinearLayout same size?

I would like to create a dialog to display a video title and tags. Below text I would like to add buttons View, Edit and Delete and make these elements same size. Does anyone know how to modify .xml layout file in order to make elements inside LinearView same size?
The current layout file looks like this:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTitle" android:text="[Title]" >
</TextView>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/txtTags"
android:text="[Tags]" >
</TextView>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnPlay"
android:text="View">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnEdit"
android:text="Edit">
</Button>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/btnDelete"
android:text="Delete">
</Button>
</LinearLayout>
</LinearLayout>
I would appreciate if anyone could show the solution by modifying the pasted file content.
Thanks!
Use android:layout_width="0px" and android:layout_weight="1" on the three Buttons. That says the buttons should take up no more than 0 pixels, but the three of them should split any extra space between them. That should give you the visual effect you wish.
Another way is to make android:layout_width="fill_parent" and android:layout_weight="1" this will also works fine!!!
Use LinearLayout with your desired weightSum and create elements with equal layout_weight. Here is an example ...
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:weightSum="5">
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_share_white_36dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_search_white_36dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_event_note_white_36dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_brush_white_36dp"/>
<ImageView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:src="#drawable/ic_menu_white_36dp"/>
</LinearLayout>
So, the weight sum of all elements is 5. Here is the screenshot ...
Note that, Google Material Design icons are used. Hope this is helpful.

Categories

Resources