Flat buttons, listview android - android

I have a hard time pointing out what UI elements are used here? Is it buttons or listviews?

This view is not seems as a list view. They are either Buttons or ImageButtons fitted in some layout.

If you want to make this in a ListView, you are gonna have a hard time. The best would be to make it in a GridLayout, if you are not that confident in working with that then the easiest solution would be a LinearLayout in the root with vertical orientation, and then add a LinearLayout with horizontal orientation where the childs have `weight="1"
Something like this (Just for the first two buttons):
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_marginTop="20dp">
<FrameLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="true"
android:layout_marginRight="10dp"
android:onClick="onChildOneClick">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="Button 1"
android:background="android:color/green" />
</FrameLayout>
<FrameLayout
android:layout_width="0dp"
android:layout_height="50dp"
android:layout_weight="1"
android:clickable="true"
android:layout_marginLeft="10dp"
android:onClick="onChildTwoClick" />
</LinearLayout>
</LinearLayout>
Inside your framelayout you can put a ImageView + TextView to make it look like the sketch

Related

Android- How to create 4 square buttons in one row with icons on top of them?

Im trying to create an app which will have 4 square buttons in one row with an icon on top of these buttons. Which is the best way to approach this? I'm trying to do something like this as shown in the image
Thank You
Set your drawable image on place of #mipmap/ic_launcher_round
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginRight="4sp"
android:orientation="horizontal"
android:weightSum="4">
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4sp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher_round" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4sp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher_round" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4sp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher_round" />
<ImageButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="4sp"
android:layout_weight="1"
android:src="#mipmap/ic_launcher_round" />
</LinearLayout>
</LinearLayout>
Have you Tried Table Layout? Although you can use any Layout, I suggest you stick to one Parent layout like the relative layout and then multiple child layouts to manage to UI elements.
Thanks for the solutions everyone. The image i posted was edited on photoshop. Now I did it in android studio using linear layout

My scroll view doesn't scroll

I have a xml view like this, and as you see I have scroll view in it, but scroll view doesn't scroll and just match screen size, and I'm not able to see data under the screen.
This is my code:
CODE HAS BEEN UPDATED
<ScrollView
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"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/white"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/aboveRelative"
android:background="#color/darkOrange">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/category_text"
android:textSize="23sp"
android:textColor="#color/white"
android:text="Category"
android:paddingLeft="10sp" />
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="200dp"
android:verticalSpacing="0dp"
android:horizontalSpacing="10dp"
android:numColumns="2"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/gray"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/topVoices"
android:textSize="23sp"
android:text="#string/topVoices"
android:paddingLeft="10sp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/trendlist"
android:elevation="2dp"
android:background="#color/white"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/moreTrend"
android:layout_gravity="right"
android:textSize="23sp"
android:text="More"
android:paddingLeft="10sp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/newVoices"
android:textSize="23sp"
android:text="#string/newVoices"
android:paddingLeft="10sp" />
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/soundlist"
android:background="#color/white"
android:layout_below="#+id/newVoices"
android:elevation="5dp"
android:layout_margin="10dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/soundlist"
android:id="#+id/moreNew"
android:layout_gravity="right"
android:textSize="23sp"
android:text="#string/more"/>
<LinearLayout
android:id="#+id/yoursongs"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center"
android:background="#color/white"
android:padding="10dp">
</LinearLayout>
</LinearLayout>
</LinearLayout>
I'm sure that there are some data under the screen but scroll view doesn't let me scroll it and see them.
Scrollview layout can only have ONE child layout. You still have TWO linear layout children in the XML you posted.
This is explained in the first paragraph of the Android developer documentation for ScrollView:
http://developer.android.com/reference/android/widget/ScrollView.html
Your layout is wrong. Here's what you gotta do:
Change your ScrollView layout_height attribute to match_parent.
The ScrollView will take the whole screen and make itself scrollable, that's why you need it as match_parent. The scrolling happens inside the View.
Put everything inside the ScrollView in a Linear or RelativeLayout. (see below)
The ScrollView extends FrameLayout, so it can only have a single child view.
IMPORTANT: this layout must have the attribute layout_height set to wrap_content, otherwise the ScrollView won't have anything larger than itself to scroll.
I'd also remove the root layout from the XML and leave the ScrollView as root.
Simply because it's pointless and it requires extra work from the LayoutInflater.
EDIT: your layout is also way more complex that it needs to be.
It lead me to believe you did not have a single child inside the ScrollView (which you do).
Still, item #1 should fix your problem.
Finally, I found answer. The problem was from fillviewport in scroll view. if you remove fillviewport and set fix size for listview, you can scroll.

Scrollable Perfectly Symmetrical ImageButtons in XML Android

In my Android app, I want to create a scrollable grid of images that have the same width/height. I don't want to use GridView because there will not be that many images. So far, I have:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
android:background="#null"
android:src="#drawable/food"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
android:background="#null"
android:src="#drawable/music" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="2">
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
android:background="#null"
android:src="#drawable/offcampus"/>
<ImageButton
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitCenter"
android:background="#null"
android:src="#drawable/sports" />
</LinearLayout>
</LinearLayout>
</ScrollView>
It is very close to what I want to achieve, but the second LinearLayout (the one with the layout_height of 0dp) causes the first two ImageButtons to take up the majority of the screen, so that the user has to scroll a lot to see the next two ImageButtons. In other words, there is way too much space between "rows" of images. Is there any way I can fix this?
I attached a picture in case there was confusion.
Thanks for your help.
Check if the extra space is due to the LinearLayout being too tall or if the ImageButtons themselves are. You can use "Dump View Hierarchy" button in the ADT in Eclipse's Devices view.
I suspect it's the ImageButtons using the images' original height. See if setting android:adjustViewBounds on the ImageButtons help with the issue. See reference: http://developer.android.com/reference/android/widget/ImageView.html#attr_android:adjustViewBounds

RelativeLayout in a LinearLayout won't wrap content correctly

I'm trying to make a simple clock/stopwatch app. In my overall vertical linear layout, I have a relative layout followed by a button. The relative layout has several imageviews stacked on each other in order to produce the clock.
I noticed, however, that the relative layout, as well as the imageviews themselves, take up way too much space in the linear layout. The clock pieces are SQUARE, so why is Eclipse insisting that it is a long vertical rectangle? My button at the bottom doesn't even show if I don't use weights. (But strangely enough, it shows if it is above the relative layout.)
I've tried everything I could: Set the height of items in relative layout to wrap_content, as well as the relative layout itself. I tried using weights, by giving the relativelayout a weight of 0 and the button 1, and then setting their layout_heights respectively to 0dp as needed. Still no go. There is a lot of room left for other things, and I'd like for the clock's parent layout to wrap itself around just the content.
What is going on here? Please see attached image for details. Code is attached below.
<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:background="#drawable/tiling_rules"
android:orientation="vertical" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
tools:ignore="ContentDescription" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/clock" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/hour" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/min" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/sec" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bell" />
</RelativeLayout>
<Button
android:layout_width="match_parent"
android:layout_height="0dip"
android:layout_gravity="center_horizontal"
android:layout_weight="1"
android:text="Test" />
It turns out that all I needed was to add:
android:adjustViewBounds="true"
to every ImageView! Now the clock pieces' bounds are "wrapped" around the content, even though setting wrap_content in its layout didn't work. Thanks for the suggestions though!
Try using a RelativeLayout as your main layout. This way you can make the button show at the bottom of the screen. Then if you need, use a separate layout for the clock pieces within the main layout
Kinda like this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="ContentDescription" >
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/clock" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/hour" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/min" />
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/sec" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/bell" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Button" />

layout_centerHorizontal is not centering

I have a relative layout, and inside it i place 3 items, 2 imageviews and one scrollview.
My layout is like this...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:gravity="center"
android:orientation="horizontal"
android:baselineAligned="true"
android:background="#drawable/background">
<RelativeLayout
android:id="#+id/logosLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:layout_weight="1"
android:layout_margin="16dp"
android:gravity="center">
<!-- An image will be placed here -->
</RelativeLayout>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_weight="1"
android:background="#33ffffff"
android:gravity="center"
android:orientation="vertical">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/up" />
<ScrollView
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_margin="8dp"
android:fadingEdgeLength="32dp"
android:scrollbars="none" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical" >
<ImageButton
android:id="#+id/hotelBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:background="#drawable/button_background"
android:contentDescription="#string/hotelBtnDesc"
android:gravity="center"
android:src="#drawable/icon1" />
<TextView
android:id="#+id/hotelBtnDescTxtVw"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:gravity="center"
android:text="#string/hotelBtnDesc"
android:textColor="#ffffff"
android:textSize="14sp" />
<!-- more scrollview items -->
</LinearLayout>
</ScrollView>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:src="#drawable/down" />
</RelativeLayout>
</LinearLayout>
The above code produces the view shown here:
You may note that the arrows are not aligned in the center, but are slightly moved to the right. Why is that, and how can I fix it ? Note that i have already used android:layout_centerHorizontal="true" to my imageviews.
Thank you in advance
Its problem with your
android:layout_margin="8dp"
remove it from both scrollview and imageView
and pass it to RelativeLayout direct.
or add
android:layout_marginTop="8dip"
android:layout_marginBottom="8dip"
in your RelativeLayout.
and pass
android:layout_centerHorizontal="true"
to your scrollview
they are aligned to the center if you put the marginRight into consideration, try adding android:layout_margin="8dp" to the arrows.
u need to set image view that is under the Relative layout set this image view width fill parent.
Try removing android:layout_weight="1" from your RelativeLayout and see if it works.
In your LinearLayout, the attribute android:orientation="horizontal" causing your display to be aligned horizontaly. Therefore your RelativeLayout is in center but since its shared with another layout thats why you cant note the difference. If you completely remove the first RelativeLayout (the one with id logosLayout) then i hope you'll see the second layout in center.
So, first you need to define the hierarchy of layout you require and then adjust your views accordingly.
It seems problem with your android:layout_margin="8dp" for your scrollview only and pass it to RelativeLayout direct but instead of using margin use padding = "8dp".

Categories

Resources