android gridlayout implementation [closed] - android

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Closed 9 years ago.
Improve this question
I'm trying to develop one new project. My client wants me to design like current GooglePlay design (block design per attach). That's why I'm confused whether <android.support.v7.widget.GridLayout> will be useful or not for implement such block design featured. If not, please let me know how to do it?

You might want to take a look at http://developer.android.com/design/building-blocks/grid-lists.html. Not exactly sure what you are trying to do, but I hope this helps. It uses GridView instead of GridLayout. A good post about that can be found GridView VS GridLayout in Android Apps
Edit: Here is my example
Note: Most of this is taken from http://developer.android.com/guide/topics/ui/layout/gridview.html. The ImageAdapter used in GridViewExample is taken directly from there as are the pictures used.
Here is my layout
<?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" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:textColor="#android:color/black"
android:textStyle="bold"
android:textSize="50sp"
android:gravity="center_horizontal"
android:text="Page Title" />
<!-- Insert some sort of scrolling if desired -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textSize="25sp"
android:gravity="center_horizontal"
android:text="Grid 1 Title" />
<GridView
android:id="#+id/gridView1"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:columnWidth="50dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:textColor="#android:color/white"
android:textSize="25sp"
android:gravity="center_horizontal"
android:text="Grid 2 Title" />
<GridView
android:id="#+id/gridView2"
android:layout_width="fill_parent"
android:layout_height="100dp"
android:columnWidth="50dp"
android:numColumns="auto_fit"
android:verticalSpacing="5dp"
android:horizontalSpacing="5dp"
android:stretchMode="columnWidth"
android:gravity="center" />
</LinearLayout>
</LinearLayout>
Here is my Main Activity:
package com.example.gridviewdemo;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.GridView;
import android.widget.Toast;
public class GridViewExample extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gridview_example);
GridView gridView1 = (GridView) findViewById(R.id.gridView1);
gridView1.setAdapter(new ImageAdapter(this));
gridView1.setOnItemClickListener(new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
Toast.makeText(GridViewExample.this, "1: " + position, Toast.LENGTH_SHORT).show();
}
});
GridView gridView2 = (GridView)findViewById(R.id.gridView2);
gridView2.setAdapter(new ImageAdapter(this));
gridView2.setOnItemClickListener(new OnItemClickListener(){
public void onItemClick(AdapterView<?> parent, View v, int position, long id){
Toast.makeText(GridViewExample.this, "2: " + position, Toast.LENGTH_SHORT).show();
}
});
}
}
Produces:
It looks like the layout is a little off on a tablet, but with some work I am sure that can be fixed

Related

ListView using custom Adapter is overwritten when also adding buttons to the xml file

I am attempting to show a list of items (each entry containing 3 textviews) followed by some buttons that will switch to other activities on click, where similar setups exist. While the ListView is populated perfectly when buttons aren't involved, as soon as I try to add buttons it disappears. I think this may be to do with my having set up the music_list.xml file as exclusively for the list, but from looking at other sources it should be possible to have a ListView followed by other Views. Currently the text button doesn't do anything; I'm trying to get it to show up with the ListView before writing any logic out.
My music_list.xml file, where there is a list and hopefully buttons:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ListView
android:id="#+id/music_list"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_alignParentTop="true"
android:layout_weight="1.0"/>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_weight="0"
android:text="Test 1"
android:layout_marginTop="32dp"
/>
</LinearLayout>
My music_item file, which holds the 3 TextViews that are used to populate the list;
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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/artist_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="left"
tools:text="lutti" />
<TextView
android:id="#+id/title_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="center"
tools:text="one" />
<TextView
android:id="#+id/album_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="8dp"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:gravity="right"
tools:text="one" />
</LinearLayout>
And the current logic for an activity page, which populates the list but doesn't yet do anything to the buttons that are meant to be underneath it:
package com.example.android.musicapp;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ListView;
import java.util.ArrayList;
/**
* Created by ~ Adam ~ on 1/2/2018.
*/
public class MyMusicActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.music_list);
ArrayList<Music> music = new ArrayList<Music>();
music.add(new Music("The Who", "Won't Get Fooled Again", "Who's Next"));
music.add(new Music("A Great Big World", "There's an Answer", "Is there Anybody Out There?"));
music.add(new Music("James Blake", "Retrograde", "Overgrown"));
music.add(new Music("Chopin", "Ballade No.3 in A Flat, Op.47", "Chopin: Ballades"));
music.add(new Music("Kendrick Lamar", "Loyalty (Feat. Rihanna)", "DAMN."));
music.add(new Music("Schel", "When the Dragon Came Down", "Schel"));
music.add(new Music("Man man", "Loot My Body", "On Oni Pond"));
music.add(new Music("Sarah Jarosz", "Dark Road", "Build Me Up From Bones"));
music.add(new Music("Jimmy Buffett", "Come Monday", "Songs You Know By Heart"));
MusicAdapter adapter = new MusicAdapter(this, music);
ListView listView = (ListView) findViewById(R.id.music_list);
listView.setAdapter(adapter);
}
}
When you add buttons in your first layout that layout pushes your listview layout to the left so that's why you are unable to see your list. Try using relativeLayout or put your linearLayout correctly. I have used RelativeLayout and it is working fine now:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:orientation="vertical"
android:layout_height="fill_parent">
<ListView
android:id="#+id/music_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true" />
<RelativeLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:gravity="bottom|center_horizontal"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="24dp"
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:text="Test 1"
android:layout_marginTop="32dp"
/>
</RelativeLayout>

ListView using array adapter and customizing xml file

I am trying to show a simple listView with an image, a large text and a small text in each list item. I have coded the following but when I run the app, it gives a NullPointerException. For the method i have used, I do not need to findviewbyid so I'm not sure what I am doing to give this error. Anyone know?
This is my MenuPage.java file
import android.app.Activity;
import android.app.ListActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
public class MenuPage extends ListActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
String[]values=new String[]{"Coffee","Steak","Ice Cream"};
String[]prices=new String[]{"$4.99","$21.99","$12.99"};
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.values, values));
setListAdapter(new ArrayAdapter<String>(this,R.layout.activity_main,R.id.prices, prices));
}
}
this is my menu.xml for the java file above:
<?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">
<ImageView
android:id="#+id/ic_launcher_movies"
android:layout_width="50px"
android:layout_height="50px"
android:layout_marginLeft="4dp"
android:layout_marginRight="10px"
android:layout_marginTop="2dp"
android:src="#drawable/items"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
</ImageView>
<TextView
android:id="#+id/values"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/values"
android:textSize="25sp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="64dp"
android:layout_alignParentTop="true"
android:layout_marginTop="0dp"
android:minHeight="120dp">
</TextView>
<TextView
android:id="#+id/prices"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#+id/prices"
android:textSize="10sp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="120dp"
android:layout_marginLeft="65dp">
</TextView>
also, I created an xml with the drawables for the images i want to use;
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:duration="30"
android:drawable="#drawable/coffee"/>
<item
android:duration="30"
android:drawable="#drawable/steak"/>
<item
android:duration="30"
android:drawable="#drawable/icecream"/>
and my logcat gives this error
any help would be appreciated! thank you
I think the problem in the menu layout file
android:text="#+id/values"
try to write some text or specify link to string resource like
android:text="#string/my_button_text"
And when you call setListAdpate two times you ovirwrite first adapter with new. Have a look to example here
ListActivity

How to set scrolling performance for webview?

I load url, for example mail.google.com, into webview in my app, and scrolling on web-page so slow! Tested on Samsung GalaxyTab 10.1, Motorola Droid X, Acer Liquid, etc.
Why?
Parameters that I try use:
webView.getSettings().setCacheMode(WebSettings.LOAD_NO_CACHE);
webView.setRenderPriority(RenderPriority.HIGH);
add android:hardwareAccelerated="true" to the tag in your manifest.
Remove background image of the webview if you put it and also see if you used heavy size images in progress or loading or other images of heavy size reduce their sizes or remove it
This is a main.xml file
This code is a scrolling only, any where u can use
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent" android:fillViewport="true">
<LinearLayout android:id="#+id/LinearLayout01"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<TextView android:id="#+id/TextView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="This is a header"
android:textAppearance="?android:attr/textAppearanceLarge"
android:paddingLeft="8dip"
android:paddingRight="8dip"
android:paddingTop="8dip"></TextView>
<TextView android:text="#+id/TextView02"
android:id="#+id/TextView02"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_weight="1.0"></TextView>
<LinearLayout android:id="#+id/LinearLayout02"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button android:id="#+id/Button01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:layout_weight="1.0"></Button>
<Button android:id="#+id/Button02"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cancel"
android:layout_weight="1.0"></Button>
</LinearLayout>
</LinearLayout>
</ScrollView>
This is .java file
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.TextView;
public class ScrollViewActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
TextView view = (TextView) findViewById(R.id.TextView02);
String s="";
for (int i=0; i < 200; i++) {
s += "Android Market ";
}
view.setText(s);
}
}

Android XML Layout Problems

I was trying to create a row layout where each of my list item will have a template to follow. Currently I have this, which allows me to display a single line text in each list.
<?xml version="1.0" encoding="utf-8"?>
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:textSize="20sp"
android:padding="100dp" >
</TextView>
However, when I try to change it to allow me to add more items such as image buttons and more text field, it always doesn't allow me to compile.
I tried taking a piece of xml from a tutorial on developer.android's website,
<?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="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
But it throws me an error stating - The processing instruction target matching "[xX][mM][lL]" is not
allowed.
- error: Error parsing XML: XML or text declaration not at start of entity
Can someone help me with this? I am not too sure what this error means.
UPDATE:
what i am trying to do is to have a fragment like a side bar on the right which shows a list of items, with the first xml that i posted, i'm able to get the results i want, but i couldn't make any changes to it.
package com.project.test;
import android.app.ListFragment;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class TestListFragment extends ListFragment {
String [] Items = {"Item A", "Item B", "Item C"};
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
setListAdapter(new ArrayAdapter<String>(getActivity(),
R.layout.listtemplate, Items));
}
#Override
public void onListItemClick(ListView l, View v, int position, long id) {
Log.i("FragmentList", "Item clicked: " + id);
}
}
You would not believe the solution to this... I had the same issue and the answer was "remove all the white spaces before start the xml first line"
Might be because you are missing the starting tag of the LinearLayout?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<TextView android:id="#+id/text2"
android:textSize="16sp"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>

Android dynamically change checkbox problem

I'm trying to change my checkbox dynamically.
for some reason the app throw my an exception.
what am I doing wrong?
this is my java code:
package com.school.commtest;
import android.app.Activity;
import android.os.Bundle;
import android.widget.CheckBox;
import android.widget.TableLayout;
import android.widget.TableRow;
public class OtherPlacesMenu extends Activity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
CheckBox c1 = (CheckBox)this.findViewById(R.id.schools);
c1.setChecked(true);
setContentView(R.layout.otherplacesmenu);
}
}
and this is the xml:
<?xml version="1.0" encoding="utf-8"?>
<TableLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/table">
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow1">
<CheckBox android:text="School" android:id="#+id/schools" android:layout_width="wrap_content" android:layout_height="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow2">
<CheckBox android:text="Supermaker" android:layout_height="wrap_content" android:id="#+id/supermarkets" android:layout_width="wrap_content"></CheckBox>
</TableRow>
<TableRow android:layout_height="fill_parent" android:layout_width="wrap_content" android:id="#+id/tableRow3">
<CheckBox android:text="Gardens" android:layout_height="wrap_content" android:id="#+id/gardens" android:layout_width="wrap_content"></CheckBox>
</TableRow>
</TableLayout>
I'm sure its a simple question, but I cannot find the answer anywhere.
p.s.
I can't set the value of the checkbox to be true at the xml because I would later want to change the checkbox from the user setting.
10x!
Usually you should post the exception message when asking a question like this, but it looks like your problem is that you're trying to findViewById before you setContentView. Your view doesn't exist to be found when you look for it.

Categories

Resources