I'm fairly new to Android programming and tried everything I could I found on SO, but it still doesn't work.
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/ScrollView01"
android:layout_width="fill_parent"
android:fillViewport="true"
android:layout_height="fill_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context="com.northcityproductions.androidiosapptransfertest1.MainActivity">
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="18dp"
android:headerDividersEnabled="false"
android:footerDividersEnabled="false" />
<ImageView
android:layout_width="48dp"
android:layout_height="48dp"
android:id="#+id/imageView1"
android:layout_gravity="center_vertical"
android:clickable="false"
android:padding="5dp" />
<TextView
android:id="#+id/textView1"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="10dip"
android:paddingStart="50dip"
android:textSize="20dip"
android:layout_gravity="center_vertical"
android:textStyle="bold"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:clickable="false"
android:layout_alignParentStart="true">
</TextView>
</RelativeLayout>
AndroidListAdapter.java
package com.northcityproductions.androidiosapptransfertest1;
import android.app.Activity;
import android.content.Context;
import android.graphics.drawable.Drawable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
import java.util.List;
public class AndroidListAdapter extends ArrayAdapter {
List<String> androidListViewStrings = new ArrayList<String>();
List<Drawable> imagesId = new ArrayList<Drawable>();
Context context;
public AndroidListAdapter(Activity context, List<Drawable> imagesId, List<String> textListView) {
super(context, R.layout.activity_main, textListView);
this.androidListViewStrings = textListView;
this.imagesId = imagesId;
this.context = context;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
LayoutInflater layoutInflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View viewRow = layoutInflater.inflate(R.layout.activity_main, null,
true);
TextView mtextView = (TextView) viewRow.findViewById(R.id.textView1);
ImageView mimageView = (ImageView) viewRow.findViewById(R.id.imageView1);
mtextView.setText(androidListViewStrings.get(i));
mimageView.setImageDrawable(imagesId.get(i));
return viewRow;
}
}
Finally, MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
//get list of apps
int flags = PackageManager.GET_META_DATA |
PackageManager.GET_SHARED_LIBRARY_FILES |
PackageManager.GET_UNINSTALLED_PACKAGES;
PackageManager pm = getPackageManager();
List<ApplicationInfo> applications = pm.getInstalledApplications(flags);
List<String> applicationsInstalled = new ArrayList<String>();
List<Drawable> applicationIcons = new ArrayList<Drawable>();
//Create map and sort alphabetically
Map applicationsList = new HashMap();
for (ApplicationInfo appInfo : applications) {
if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 1) {
// System application
} else {
// Installed by user
String appName = (String) pm.getApplicationLabel((appInfo));
appName = appName.substring(0, 1).toUpperCase() + appName.substring(1);
applicationsList.put(appName, pm.getApplicationIcon(appInfo));
}
}
Map<String, Drawable> treeApps = new TreeMap<String, Drawable>(applicationsList);
for (Map.Entry<String, Drawable> appMap : treeApps.entrySet()) {
applicationsInstalled.add(appMap.getKey());
applicationIcons.add(appMap.getValue());
}
//Arrange them in listview
final AndroidListAdapter androidListAdapter = new AndroidListAdapter(this, applicationIcons, applicationsInstalled);
ListView lv1 = (ListView) findViewById(R.id.listView1);
lv1.setAdapter(androidListAdapter);
lv1.setOnItemClickListener(new OnItemClickListener(){
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long rowId) {
AlertDialog.Builder adb = new AlertDialog.Builder(MainActivity.this);
adb.setTitle("List");
adb.setMessage(" selected Item is="+parent.getItemAtPosition(position));
adb.setPositiveButton("Ok", null);
adb.show();
}
});
}
}
I have tried adding the blocksDescendants code in my activity_main.xml under Listview, and Relativelayout, but that doesn't seem to fix it. I also tried setting the focusable property to false for the imageview and textview but that didn't fix it either.
Related
The following project was copied verbatim from an online lesson. It is supposed to display a ListView but nothing shows up. I am brand new to Android Studio and this is only my second project in it. There are 5 files and I will display the contents of each of them below. I just need another pair of eyes or two to find the problem, because I'm "bug blind". Any help will be greatly appreciated.
activity_main.xml
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/listView"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_marginStart="16dp"
android:layout_marginTop="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="16dp"
android:visibility="visible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" /> </androidx.constraintlayout.widget.ConstraintLayout>
MainActivity.java
package com.example.tadhg.uiuxadapterpractical;
import androidx.appcompat.app.AppCompatActivity;
import android.content.Context;
import android.content.res.TypedArray;
import android.os.Bundle;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
ListView lv;
Context context;
TypedArray images;
String [] titleList;
String [] descList;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
lv = (ListView) findViewById(R.id.listView);
}
}
OurAdapter.java
package com.example.tadhg.uiuxadapterpractical;
import android.content.Context; import android.content.res.TypedArray;
import android.view.LayoutInflater; import android.view.View; import
android.view.ViewGroup; import android.widget.BaseAdapter; import
android.widget.ImageView; import android.widget.TextView;
public class OurAdapter extends BaseAdapter {
private String [] titles;
private String [] desc;
private TypedArray imageID;
private Context context;
private static LayoutInflater layoutInflater;
//constructor
OurAdapter(Context adapterContext, String[] titleList, String[] descList, TypedArray images){
titles = titleList;
desc = descList;
imageID = images;
context = adapterContext;
layoutInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
}
#Override
public int getCount() {
return titles.length;
}
#Override
public Object getItem(int i) {
return i;
}
#Override
public long getItemId(int i) {
return i;
}
private class ViewHolder {
TextView tv1, tv2;
ImageView img;
}
#Override
public View getView(int position, View view, ViewGroup viewGroup) {
ViewHolder viewHolder = new ViewHolder();
if (view == null) {
view = layoutInflater.inflate(R.layout.list_layout, viewGroup, false);
viewHolder.tv1 = (TextView)view.findViewById(R.id.textView7);
viewHolder.tv2 = (TextView)view.findViewById(R.id.textView8);
viewHolder.img = (ImageView) view.findViewById(R.id.imageView2);
view.setTag(viewHolder);
} else {
viewHolder = (ViewHolder) view.getTag();
}
viewHolder.tv1.setText(titles[position]);
viewHolder.tv2.setText(desc[position]);
viewHolder.img.setImageResource(imageID.getResourceId(position, 0));
return view;
} }
List_layout.xml
<androidx.constraintlayout.widget.ConstraintLayout
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:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:srcCompat="#drawable/cplus" />
<TextView
android:id="#+id/textView7"
android:layout_width="0dp"
android:layout_height="32dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="24sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toTopOf="parent" />
<TextView
android:id="#+id/textView8"
android:layout_width="0dp"
android:layout_height="24dp"
android:layout_marginStart="16dp"
android:layout_marginTop="8dp"
android:layout_marginEnd="8dp"
android:text="TextView"
android:textSize="18sp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/imageView2"
app:layout_constraintTop_toBottomOf="#+id/textView7"
tools:text="TextView" />
</androidx.constraintlayout.widget.ConstraintLayout>
strings.xml
UIUXAdapterPractical
<string-array name="Languages">
<item>Java</item>
<item>Swift</item>
<item>C#</item>
<item>SQL</item>
<item>Javascript</item>
<item>Jquery</item>
<item>C++</item>
</string-array>
<string-array name="LanguageDesc">
<item>Desc. of Java</item>
<item>Desc. of Swift</item>
<item>Desc. of C#</item>
<item>Desc. of SQL</item>
<item>Desc. of Javascript</item>
<item>Desc. of Jquery</item>
<item>Desc. of C++</item>
</string-array>
<array name="LanguageImg">
<item>#drawable/java</item>
<item>#drawable/swift</item>
<item>#drawable/csharp</item>
<item>#drawable/sql</item>
<item>#drawable/js</item>
<item>#drawable/jquery</item>
<item>#drawable/cplus</item>
</array> </resources>
you have missed attaching your adapter to listview...you must set your adapter to your listview then only you will see your listview
change your code with following code
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
context = this;
titleList = getResources().getStringArray(R.array.Languages);
descList =
getResources().getStringArray(R.array.LanguageDesc);
images =
getResources().obtainTypedArray(R.array.LanguageImg);
final OurAdapter adapter = new OurAdapter(getApplicationContext(),titleList,descList,images);
lv = (ListView) findViewById(R.id.listView);
lv.setAdapter(adapter);
}
hope it will solve your problem :)
In your listview you have:
android:layout_width="0dp"
android:layout_height="0dp"
instead put
android:layout_width="match_parent"
android:layout_height="match_parent"
You're forcing the size to be 0 so you can't see it. Tell me if it works
I have following code that should:
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
This is what I load into the ListView:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="true">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/github_icon"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
Here is the Adapter :
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry>{
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry> githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
This is not working for me. Im no quit sure where I should place this code. Can I place this in the onCreate? If not where should I move it? I completly new in Android and I have also not much experience in Java.
Here is what i did for you..
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
private ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.listview_github_entries);
listView.setAdapter(new GithubEntryAdapter(MainActivity.this, getList()));
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
TextView github_url_tv = view.findViewById(R.id.github_url);
String url_text= github_url_tv.getText().toString();
Toast.makeText(MainActivity.this, url_text", Toast.LENGTH_LONG).show();
}
});
}
private ArrayList<GithubEntry> getList() {
ArrayList<GithubEntry> githubEntries = new ArrayList<>();
GithubEntry githubEntry = new GithubEntry();
githubEntry.setGithub_name("Name");
githubEntry.setGithub_url("url");
GithubEntry githubEntry1 = new GithubEntry();
githubEntry1.setGithub_name("Name");
githubEntry1.setGithub_url("url");
GithubEntry githubEntry2 = new GithubEntry();
githubEntry2.setGithub_name("Name");
githubEntry2.setGithub_url("url");
githubEntries.add(githubEntry);
githubEntries.add(githubEntry1);
githubEntries.add(githubEntry2);
return githubEntries;
}
}
Here is adapter
import android.app.Activity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import java.util.ArrayList;
public class GithubEntryAdapter extends ArrayAdapter<GithubEntry> {
public GithubEntryAdapter(Activity context, ArrayList<GithubEntry>
githubEntries){
super(context, 0, githubEntries);
}
public View getView(int position, View convertView, ViewGroup parent){
View listItemView = convertView;
if (listItemView == null){
listItemView = LayoutInflater.from(getContext()).inflate(
R.layout.list_item, parent, false);
}
GithubEntry currentGithubEntry = getItem(position);
TextView github_url = (TextView) listItemView.findViewById(R.id.github_url);
github_url.setText(currentGithubEntry.getGithub_url());
TextView github_name = (TextView) listItemView.findViewById(R.id.github_name);
github_name.setText(currentGithubEntry.getGithub_name());
return listItemView;
}
}
here is POJO(plan java object) class
class GithubEntry {
private String Github_url;
private String Github_name;
public String getGithub_url() {
return Github_url;
}
public void setGithub_url(String github_url) {
Github_url = github_url;
}
public String getGithub_name() {
return Github_name;
}
public void setGithub_name(String github_name) {
Github_name = github_name;
}
}
and here is list_item
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/list_item"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="10dp"
android:orientation="horizontal"
android:clickable="false">
<ImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#mipmap/ic_launcher_round"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/github_name"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:id="#+id/github_url"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
and here is activity layout
<?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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.kaimeramedia.githubentry.MainActivity">
<ListView
android:id="#+id/listview_github_entries"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
If mean that the OnItemClickListeneter not working, then you need to implement a custom adapter by extending ArrayAdater to serve you custom row, And in the custom adapter you can use a callback interface or implement a listener on the view it self See the example.
I think you want to display the list of values here. You should write it inside onCreate because onCreate is a method where you method starts and run. So, Put it inside onCreate. For more correct answer please explain everything.
I am new to android. I have created material design Grid View. Now what I want to do, when I click on each grid view it should take to me each new activity. When i click on ALPHABETS it will go to ListAlphabet.java. Thank you for help.
My activity_main.xml is :
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/android_coordinator_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<android.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:id="#+id/appbar_layout"
android:layout_height="#dimen/app_bar_height"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar_android_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:expandedTitleMarginStart="#dimen/expanded_toolbar_title_margin_start"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<ImageView
android:id="#+id/image_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerInside"
android:src="#drawable/code"
app:layout_collapseMode="parallax"
app:layout_collapseParallaxMultiplier="0.7" />
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin"
app:popupTheme="#style/ThemeOverlay.AppCompat.Light"
app:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:id="#+id/nestedscrollview"
android:layout_height="match_parent"
android:fillViewport="true"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<GridView
android:id="#+id/grid"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:columnWidth="100dp"
android:gravity="center"
android:listSelector="#00000000"
android:numColumns="auto_fit"
android:stretchMode="columnWidth" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
gridview_custom_layout.xml is :
<?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"
android:id="#+id/android_gridview_custom_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<com.andexert.library.RippleView
android:id="#+id/more"
rv_centered="true"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
app:rv_color="#fff"
app:rv_rippleDuration="200">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/linearLayout"
android:orientation="vertical">
<ImageView
android:id="#+id/gridview_image"
android:layout_width="80dp"
android:layout_height="80dp"
android:src="#mipmap/ic_launcher" />
<TextView
android:id="#+id/gridview_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/grid_image"
android:layout_marginTop="10dp"
android:gravity="center"
android:text="Grid View Item"
android:textColor="#444"
android:textSize="12sp"
android:textStyle="bold" />
</LinearLayout>
</com.andexert.library.RippleView>
</LinearLayout>
MainActivity.java is :
package com.affinityapp.sj.alphabet;
import android.content.Context;
import android.os.Bundle;
import android.support.design.widget.CollapsingToolbarLayout;
import android.support.design.widget.CoordinatorLayout;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.widget.GridView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
CustomAndroidGridViewAdapter.java is :
package com.affinityapp.sj.alphabet;
/**
* Created by SJ on 23-01-2017.
*/
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by HP on 5/11/2016.
*/
public class CustomAndroidGridViewAdapter extends BaseAdapter {
private Context mContext;
private final String[] string;
private final int[] Imageid;
public CustomAndroidGridViewAdapter(Context c,String[] string,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.string = string;
}
#Override
public int getCount() {
return string.length;
}
#Override
public Object getItem(int p) {
return null;
}
#Override
public long getItemId(int p) {
return 0;
}
#Override
public View getView(int p, View convertView, ViewGroup parent) {
View grid;
LayoutInflater inflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
grid = new View(mContext);
grid = inflater.inflate(R.layout.gridview_custom_layout, null);
TextView textView = (TextView) grid.findViewById(R.id.gridview_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.gridview_image);
textView.setText(string[p]);
imageView.setImageResource(Imageid[p]);
} else {
grid = (View) convertView;
}
return grid;
}
}
So, you can use this way to click grid view:
public class MainActivity extends AppCompatActivity {
Toolbar toolbar;
CollapsingToolbarLayout collapsingToolbarLayoutAndroid;
CoordinatorLayout rootLayoutAndroid;
GridView gridView;
Context context;
ArrayList arrayList;
public static String[] gridViewStrings = {
"ALPHABETS",
"NUMBERS",
"MONTH",
"DAYS",
"ANIMALS",
"CALL US",
};
public static int[] gridViewImages = {
R.drawable.icon_alphabet,
R.drawable.icon_number,
R.drawable.icon_calendar,
R.drawable.icon_days,
R.drawable.icon_animal,
R.drawable.icon_call
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
gridView = (GridView) findViewById(R.id.grid);
gridView.setAdapter(new CustomAndroidGridViewAdapter(this, gridViewStrings, gridViewImages));
initInstances();
gridView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
// Do whatever you want, like start new Activity or display new view or display dialog box or display just message
Toast.makeText(MainActivity.this, "" + position, Toast.LENGTH_SHORT).show();
}
});
}
private void initInstances() {
rootLayoutAndroid = (CoordinatorLayout) findViewById(R.id.android_coordinator_layout);
collapsingToolbarLayoutAndroid = (CollapsingToolbarLayout) findViewById(R.id.collapsing_toolbar_android_layout);
collapsingToolbarLayoutAndroid.setTitle("e-Learning");
}
}
Hope, this will help you.
Implement onItemClickListener method in you activity:
gridview.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View v, int position, long id) {
final Intent intent;
switch(position)
{
case 0:
intent = new Intent(context, FirstActivity.class);
break;
case 1:
intent = new Intent(context, SecondActivity.class);
break;
...
default:
intent = new Intent(context, DefaultActivity.class);
break;
}
startActivity(intent);
}
});
Possibly duplicate question...
Research thoroughly before posting a question please.
Answer can be found in this thread: How can I give an imageview click effect like a button on Android?
Edit:
In your current activity, you need to create a variable ImageButton.
ImageButton myButton = (ImageButton) findViewById(R.id.the_button_you_created_on_the_layout.xml);
Then you need to set a click listener to this button
myButton.setOnClickListener(new View.OnClickListner(){
// When the button is pressed/clicked, it will run the code below
#Override
public void onClick()
// Intent is what you use to start another activity
Intent myIntent = new Intent(this, YourActivity.class);
startActivity(intent);
}
});
So i want to make my CustomArrayAdapter class generic.
CustomDialogFragmentNotGeneric:
public class CustomDialogFragment extends DialogFragment
{
TextView listViewItemTextView;
ArrayAdapter<String> arrayAdapter;
ListView dialogListView;
String[] items = {"Hello","Hello there","Hi","Hi there"};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,Bundle savedInstanceState)
{
View rootView = inflater.inflate(R.layout.dialog, container,false);
getDialog().setTitle("Choose an option"); // Set dialog title
listViewItemTextView = (TextView) rootView.findViewById(R.id.list_view_item_text_view_id);
dialogListView = (ListView) rootView.findViewById(R.id.dialog_list_view_id);
getDialog().setTitle("Opening Words"); // Setting dialog title
CustomArrayAdapter customArrayAdapter = new CustomArrayAdapter(getActivity(), items);
dialogListView.setAdapter(customArrayAdapter);
dialogListView.setOnItemClickListener(new OnItemClickListener()
{
#Override
public void onItemClick(AdapterView<?> parent, View view,int position, long id)
{
Toast.makeText(getActivity(), items[position], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
It's not generic because of the String[] itemm = line.
So instead of building a lot of classes that in each there will be a different String[] item, how do i make that generic?
dialog.xml:
<?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" >
<ListView
android:id="#+id/dialog_list_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
MyCustomArrayAdapter class:
package com.example.predesignedmails;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomArrayAdapter extends ArrayAdapter<String>
{
Context context;
String[] items;
LayoutInflater layoutInflater;
public CustomArrayAdapter(Context context, String[] items)
{
super(context, R.layout.list_view_row,items);
this.context = context;
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
if (convertView == null)
{
this.layoutInflater = (LayoutInflater) this.context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = this.layoutInflater.inflate(R.layout.list_view_row,parent,false);
}
TextView rowTextView = (TextView) convertView.findViewById(R.id.row_text_view_id);
rowTextView.setText(this.items[position]);
return convertView;
}
}
list_view_row.xml:
<?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:id="#+id/row_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
/>
</LinearLayout>
I have put a lot of effort into this post. Please help me guys. Thanks.
EDIT
my activity layout:
<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"
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="com.example.predesignedmails.LoveMailsActivity" >
<TextView
android:id="#+id/love_email_emai_to_send_to_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/emai_to_send_to_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#color/opening_words_list_view_header_color"
/>
<EditText
android:id="#+id/love_email_email_to_send_to_edit_text_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:hint="#string/emai_to_send_to_edit_text_hint"
android:inputType="textEmailAddress"
android:textSize="18sp"
android:textColor="#color/selection_text_color"
android:layout_below="#id/love_email_emai_to_send_to_text_view_id"
/>
<TextView
android:id="#+id/love_email_opening_words_header_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/love_email_opening_words_text_view_text"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#color/opening_words_list_view_header_color"
android:layout_below="#id/love_email_email_to_send_to_edit_text_id"
/>
<TextView
android:id="#+id/love_email_opening_words_text_view_id"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="#string/initial_text"
android:textSize="18sp"
android:textColor="#color/selection_text_color"
android:layout_below="#id/love_email_opening_words_header_text_view_id"
/>
</RelativeLayout>
ArrayAdapter is already generic. Based on your current code, you can get rid of your entire CustomArrayAdapter class and simply use this constructor (replacing String with whatever type your items are):
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.list_view_row, R.id.row_text_view_id, items);
If you plan to keep CustomArrayAdapter, you can declare it like this to keep the generics:
public class CustomArrayAdapter<T> extends ArrayAdapter<T> {
...
}
Naturally you would use T instead of String wherever necessary.
I am setting a listView dynamically. I will have two things shown on each line that I will be receiving from parse.com. I want the first thing to be left aligned and blue, with the second thing right aligned and red. I will have two arrays, firstArray, and secondArray. I found lots of ways to do this, but none dynamically. How should I go about doing this, or I can do it in xml?
Updated
This is my xml after that tutorial posted by #t0s
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:padding="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<TextView
android:id="#+id/column1"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#0000FF"
android:gravity="left"
/>
<TextView
android:id="#+id/center"
android:text="VS"
android:textSize="15sp"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FFFFFF"
android:gravity="center"
/>
<TextView
android:id="#+id/column2"
android:layout_width="50dip"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textColor="#FF0000"
android:gravity="right"
/>
</LinearLayout>
and my java
ListView list = (ListView) findViewById(R.id.mylist);
ArrayAdapter<String> listAdapter = new ArrayAdapter<String>(MainActivity.this, R.layout.mylist, namesArray);
list.setAdapter(listAdapter);
//find my objects off parse, and call this method
private String display(Object name, Object record,
ArrayAdapter listAdapter) {
String fightCard = (name).toString();
namesArray.add((name).toString() +" " + (record).toString());
return fightCard;
}
Well my code is a mess.
Check the tutorial here is [very simple]
Ok Chad check here the code :
Main Activity :
package org.example.chad;
import java.util.ArrayList;
import android.app.ListActivity;
import android.os.Bundle;
public class MainActivity extends ListActivity {
private ArrayList<DataItem> data = new ArrayList<DataItem>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
//create Objects
DataItem obj1 = new DataItem("name1", "record1");
DataItem obj2 = new DataItem("name2", "record2");
DataItem obj3 = new DataItem("name3", "record3");
DataItem obj4 = new DataItem("name4", "record4");
DataItem obj5 = new DataItem("name5", "record5");
//add the to ArrayList
data.add(obj1);
data.add(obj2);
data.add(obj3);
data.add(obj4);
data.add(obj5);
CustomAdapter adapter = new CustomAdapter(this, R.layout.row, data);
setListAdapter(adapter);
}
}
main.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"
tools:context=".MainActivity" >
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent" />
</RelativeLayout>
DataItem :
package org.example.chad;
public class DataItem {
private String name;
private String record;
public DataItem(){
}
public DataItem(String n, String r ){
this.name = n;
this.record = r;
}
public String getname() {
return name;
}
public void setname(String name) {
this.name = name;
}
public String getrecord() {
return record;
}
public void setrecord(String record) {
this.record = record;
}
}
CustomAdapter :
package org.example.chad;
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<DataItem> {
private ArrayList<DataItem> objects;
public CustomAdapter(Context context, int textViewResourceId, ArrayList<DataItem> objects) {
super(context, textViewResourceId, objects);
this.objects = objects;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.row, null);
}
DataItem i = objects.get(position);
if (i != null) {
TextView nameView = (TextView) v.findViewById(R.id.name);
TextView recordView = (TextView) v.findViewById(R.id.record);
if (nameView != null){
nameView.setText(i.getname());
}
if (recordView != null){
recordView.setText(i.getrecord());
}
}
return v;
}
}
and row.xml :
<?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="horizontal" >
<TextView
android:id="#+id/name"
android:layout_width="100sp"
android:layout_height="20sp"
android:textSize="16sp"
android:layout_gravity="left|center_vertical"
android:layout_marginLeft="10sp"/>
<TextView
android:id="#+id/record"
android:layout_width="100sp"
android:layout_height="wrap_content"
android:layout_marginLeft="10sp"
android:layout_gravity="right"
android:textSize="16sp" />
</LinearLayout>
In a couple of hours I will add some comments.
The result is here
If im not mistaken you have to extend BaseAdapter class so you have a custom Adapter for your ListView, it is not possible to add two array items to a ListView without creating a custom adapter.
here is a tutorial.
is not as hard as it looks like. Hope it helps.