GridLayout won't display in a grid - java

I'm using a gridLayout to simply display a 2x2 layout. I'll increase this later, but I'm just trying to get a sample to work. As of right now they line up in a single horizontal row. By just looking at my xml, does anyone see anything blatantly obvious that I'm doing wrong? My code compiles, and my xml "validates". Thanks
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:grid="http://schemas.android.com/apk/res-auto"
xmlns:app="http://schemas.android.com/apk/res/com.test.example"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<android.support.v7.widget.GridLayout
android:id="#+id/GridLayout1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:columnCount="2"
android:rowCount="2" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</android.support.v7.widget.GridLayout>
<EditText
android:id="#+id/editText1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10" />
</RelativeLayout>

The documentation for GridLayout specifies that You should not need to use either of the special size values: WRAP_CONTENT or MATCH_PARENT when configuring the children of a GridLayout. Specified in the summary here

I've never used GridLayout before, but from the example here, it appears that you need to specify a android:layout_column="0" attribute when you want a new row to start. I recommend that you read the full article.

Related

How can i hide the elements when my keyboard pops up?

I have seen a few similar questions, but they want the things to show up, I want to hide them.
Basically I have a bunch of elements in my RelativeLayout and when the activity shows, the keyboard pops up, since I have an editText, and the elements that are aligned in the Bottom go up along the keyboard, I don't want that, I want them to stay hidden, any idea how can I achieve this?
This is the xml of my Activity:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:background="#drawable/bg"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:orientation="vertical"
android:id="#+id/invoices_layout"
tools:context=".NavManager">
<RelativeLayout
android:id="#+id/mcr_bar_progress"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true"
android:visibility="visible">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerInParent="true"
android:layout_marginTop="15dp"
android:text="Agregar Productos"
android:textColor="#android:color/white"
android:textSize="34sp" />
<TextView
android:id="#+id/textGrid2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_centerHorizontal="true"
android:layout_marginTop="0dp"
android:text="Grupo MkTech Solutions"
android:textColor="#android:color/white"
android:textSize="15sp" />
<EditText
android:id="#+id/txtproducto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="0dp"
android:layout_marginTop="92dp"
android:layout_marginEnd="0dp"
android:hint="Ej: Refresco Natural"
android:inputType="text"
android:textColor="#color/white"
android:textSize="15sp"
app:backgroundTint="#color/white" />
<ImageButton
android:id="#+id/btnbuscar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/txtproducto"
android:layout_alignEnd="#+id/txtproducto"
android:layout_marginTop="4dp"
android:layout_marginEnd="0dp"
android:onClick="onClickSwitch"
app:srcCompat="#drawable/research" />
<TextView
android:id="#+id/txttotal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:padding="10dp"
android:textColor="#color/WhiteBgText"
android:textSize="15sp" />
<ImageButton
android:id="#+id/btnlimpiarcarro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:onClick="onClickCleanCart"
android:padding="10dp"
app:srcCompat="#drawable/quit" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="0dp"
android:layout_marginTop="150dp"
android:layout_marginBottom="50dp"
android:fillViewport="true">
<TableLayout
android:id="#+id/tableInvoices"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="0dp"
android:stretchColumns="*"></TableLayout>
</ScrollView>
</RelativeLayout>
</LinearLayout>
The last two elements before the scrollview are the ones that need to stay hidden.
add the following to your activity in the manifest:
android:windowSoftInputMode="adjustPan"
from the API:
The activity's main window is not resized to make room for the soft keyboard. Rather, the contents of the window are automatically panned so that the current focus is never obscured by the keyboard and users can always see what they are typing. This is generally less desirable than resizing, because the user may need to close the soft keyboard to get at and interact with obscured parts of the window.
By my understanding of your question you do not want the keyboard to push the UI elements at the bottom of your layout.
To achieve this all you need to do is put the following line into your manifest activity tag:
android:windowSoftInputMode="adjustNothing"

Android weightSum does not work

i have coded a ListViewAdapter to show rows in an activity with special Items. But my problem it isn't working as it should. My goal is, that the elements in a row have a fixed size and therefore i used weightSum. They should have a percentage.
<?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:layout_height="match_parent"
android:weightSum="100.0">
<TextView
android:id="#+id/txtID"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="8.0"
android:text="TextView" />
<TextView
android:id="#+id/txtVis1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="36.0"
android:text="TextView" />
<View
android:layout_width="2dp"
android:layout_height="match_parent"
android:layout_marginRight="5dp"
android:background="#android:color/darker_gray" />
<TextView
android:id="#+id/txtVis2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="36.0"
android:text="TextView" />
<EditText
android:id="#+id/editVis1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10.0"
android:inputType="number|numberSigned|numberDecimal|numberPassword|phone"
android:text="" />
<EditText
android:id="#+id/editVis2"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="10.0"
android:inputType="number|numberSigned|numberDecimal|numberPassword|phone"
android:text="" />
</LinearLayout>
And here is, how it looks like. Its obvious wrong. It should be alle the same size and in the middle should be the black line on every row at the same position
The strange think is that it worked in a very early build and now it does not work anymore. What can i do?
If you need other code just tell me ;-) Thanks.

GridLayout with equal size for each element

I'm trying to make a layout that is laid out like this.
But I need the elements (the images) to have equal and full size to create the spacing between them and not use margins to space them out.
You can achieve the required result with android TableLayout.
Here's the developer link for TableLayout
some times grid layout is not an answer. try 3 linear layout over each other like this its simple
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical"
android:background="#000000">
<Button
android:layout_height="60dp"
android:layout_width="match_parent"
android:background="#drawable/icon_you_should_know"
android:maxWidth="360dp"/>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_wounded_demon"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_maze_desert"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_working_with_darkness"/>
</LinearLayout>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_weak_spot"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
</LinearLayout>
<LinearLayout
android:layout_height="120dp"
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="top|center">
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
<Button
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:layout_weight="1.0"
android:maxWidth="120dp"
android:background="#drawable/icon_none"/>
</LinearLayout>
also u can remove or add a button to each line using max width and height and dont forget to add layout_weight="1" its works me hope it work for u.
and for margins u could put picture in a bigger picture without background in PNG format.

Android hide and show components

I am making a sqlite android application which is to list some data , show graphs , visit a web site by starting default browser(not in application by using webview) , send emails by using a form . I will have a couple of forms . So what I was thinking is that instead of using several activities and xml layouts I put it all in one xml layout and simply on button click show and hide components as it is done with javascript
These are my components for now ...
<ScrollView 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="432dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".MainActivity" >
<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_marginTop="32dp"
android:text="ENTER COMPANY NAME " />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/textView1"
android:layout_centerHorizontal="true"
android:ems="10">
<requestFocus />
</EditText>
<Spinner
android:id="#+id/spinner1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/editText1"
android:layout_centerHorizontal="true"
android:layout_marginTop="17dp"
android:entries="#array/country_arrays"
android:prompt="#string/country_prompt"
/>
<Spinner
android:id="#+id/spinner2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/spinner1"
android:layout_below="#+id/spinner1"
android:entries="#array/limit_arrays"
android:prompt="#string/limit_prompt"
/>
<Button
android:id="#+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/spinner2"
android:layout_centerHorizontal="true"
android:text="Hide" />
<Button
android:id="#+id/button2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button1"
android:layout_centerHorizontal="true"
android:text="Show" />
</RelativeLayout>
</ScrollView>
Then I am to show or hide components by using for example
spinner1.setVisibility(View.VISIBLE);
spinner1.setVisibility(View.GONE);
This seams to be working fine , but I am getting a kind of low on space on my xml screen. How do I put more components in the bottom and make my View scroll down although I have put
<ScrollView 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">
on the top of my xml layout . And is this a good idea for a menu organisation ?

Android Custom ListViewItem Layout

I dont quite get the layout of my List View Item doesnt show like I want it to.
I hope somone would give me some hints since i cannot make it work for quite some time now.
What i want is as follows:
Here is my approach:
<?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="20dip"
android:padding="6dip">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="3dip">
<TextView
android:id="#+id/nametext"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
/>
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="7dip">
<ImageView
android:id="#+id/photo"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginRight="6dip"
android:src="#drawable/icon" />
<LinearLayout
android:orientation="vertical"
android:layout_width="0dip"
android:layout_weight="1"
android:layout_height="fill_parent">
<TextView
android:id="#+id/toptext"
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:gravity="center_vertical"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/middletext"
android:singleLine="true"
android:ellipsize="marquee"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="0dip"
android:layout_weight="1"
android:id="#+id/bottomtext"
android:singleLine="true"
android:ellipsize="marquee"
/>
</LinearLayout>
<ImageView
android:id="#+id/stars"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_marginLeft="6dip"
android:src="#drawable/icon" />
</LinearLayout>
Any help would be appreciated.
I already tried different sizes and a RelativeLayout. No change.
It doesnt look anythik like i thought it should.
edit three blanks missing
edit2 added pic
You could do this with LinearLayouts, but you'll need three nested layouts:
A vertical LinearLayout containing everything.
A horizontal LinearLayout containing everything aside from the heading.
A vertical LinearLayout containing the non-heading TextViews.
The inner vertical LinearLayout will probably need android:layout_weight="1" so it stretches to fill the available space but doesn't squeeze out the right-hand ImageView.
That's all very expensive, particularly when multiplied by the numerous views in a ListView. A RelativeLayout can get this done with only one layout.
<?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="wrap_content"
android:padding="6dip" >
<TextView
android:id="#+id/textView1"
android:layout_alignParentTop="true"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_vertical|center_horizontal"
android:textSize="40sp">
</TextView>
<ImageView
android:id="#+id/imageView1"
android:layout_below="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" >
</ImageView>
<ImageView
android:id="#+id/imageView2"
android:layout_below="#+id/textView1"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/icon" >
</ImageView>
<TextView
android:id="#+id/textView2"
android:layout_below="#+id/textView1"
android:layout_toRightOf="#+id/imageView1"
android:layout_toLeftOf="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/textView3"
android:layout_below="#+id/textView2"
android:layout_toRightOf="#+id/imageView1"
android:layout_toLeftOf="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView" >
</TextView>
<TextView
android:id="#+id/textView4"
android:layout_below="#+id/textView3"
android:layout_toRightOf="#+id/imageView1"
android:layout_toLeftOf="#+id/imageView2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="TextView" >
</TextView>
</RelativeLayout>
Edited for formatting and to add:
This is the result. Of course, text and image sizes need adjusted, but the basic result is what you've described.
Im not sure if you really want to have a 3dip headline and 7dip content...
if you want the stuff inside the outer linearlayout vertical arranged, use
android:orientation="vertical"
in the parent LinearLayout. to arrange the three items in the content part of your layout, you might use android:weight or android:alignParentLeft / Right.
I fear im not sure waht you want to do, so i can not edit your layout :(

Categories

Resources