I have created a listview called popuplistfragment.java and list_view.xml.I have added textview in xml file but I dont know where to add code to change the textview in the listview.
import android.app.ListFragment;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.TextView;
import android.widget.Toast;
public class PopupListFragment extends ListFragment implements OnItemClickListener {
TextView textview;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.list_item, container, false);
return view;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter adapter = ArrayAdapter.createFromResource(getActivity(), R.array.quotes, android.R.layout.simple_list_item_1);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,long id) {
//Toast.makeText(getActivity(), "Item: " + position, Toast.LENGTH_SHORT).show();
String item = (String) parent.getItemAtPosition(position);
Log.v("check", "item is clicked");
Intent myIntent = new Intent(getActivity(), SwipeViews.class);
myIntent.putExtra("key", item);
myIntent.putExtra("index", position);
startActivity(myIntent);
}
}
And this is the list_view.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="?attr/listPreferredItemHeight"
android:background="#drawable/back_listview">
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:longClickable="false"
>
</ListView>
<TextView
android:id="#+id/text1"
android:textColor="#e15258"
android:layout_height="match_parent"
android:layout_width="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:maxLines="1"
android:ellipsize="end"
android:textAppearance="?android:attr/textAppearanceMedium" />
</LinearLayout>
Firstly you need to extend Adapter class
public class ListAdapter extends ArrayAdapter<String> {
String [] mQuotes;
public ListAdapter(String [] quotes){
mQuotes = quotes;
}
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater layoutInflater = LayoutInflater.from(getContext());
v = layoutInflater.inflate(R.layout.itemlistrow, null);
}
TextView quoteTextView = v.findViewById(R.id.quote_textView);
quoteTextView.setText(mQuotes[position])
return v;
}
#Override
public int getCount() {
return quotes.length;
}
}
Then implement other methods.
The next step is to change next code
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
String [] QuoteArray = getResources().getStringArray(R.array.quotes);
ListAdapter adapter = new ListAdapter(QuoteArray);
setListAdapter(adapter);
getListView().setOnItemClickListener(this);
}
Then add itemlistrow.xml to Layout folder and change it so:
<?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">
<TextView
android:id="#+id/quote_textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</LinearLayout>
You can customize itemlistrow.xml as you want
Related
I have an array of images, and wanted to display image from it, to alert dialog when we click on grid item.
Here is my class:
mainactivity.java
package glassfibre.hospital2;
import android.os.Bundle;
import android.app.Activity;
import android.support.v4.app.FragmentActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.GridView;
import android.widget.Toast;
public class MainActivity extends FragmentActivity
{
GridView grid;
String[] web = {
"Food",
"Doctor",
"Water",
"Feel Cold",
"Adjust Bed",
"Toilet"
} ;
int[] imageId = {
R.drawable.food,
R.drawable.doctor,
R.drawable.water2,
R.drawable.lowtemp,
R.drawable.bed,
R.drawable.toilet
};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
CustomGrid adapter = new CustomGrid(MainActivity.this, web, imageId);
grid=(GridView)findViewById(R.id.grid);
grid.setAdapter(adapter);
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});
}
}
CustomGrid.java
package glassfibre.hospital2;
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;
public class CustomGrid extends BaseAdapter{
private Context mContext;
private final String[] web;
final int[] Imageid;
public CustomGrid(Context c,String[] web,int[] Imageid ) {
mContext = c;
this.Imageid = Imageid;
this.web = web;
}
#Override
public int getCount() {
return web.length;
}
#Override
public Object getItem(int position) {
return null;
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, 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.grid_single, null);
TextView textView = (TextView) grid.findViewById(R.id.grid_text);
ImageView imageView = (ImageView)grid.findViewById(R.id.grid_image);
textView.setText(web[position]);
imageView.setImageResource(Imageid[position]);
} else {
grid = (View) convertView;
}
return grid;
}
}
grid_single.xml
<RelativeLayout 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="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp" >
<ImageView
android:id="#+id/grid_image"
android:src="#drawable/food"
android:layout_width="150dp"
android:layout_height="150dp">
</ImageView>
<TextView
android:id="#+id/grid_text"
android:text="#string/food"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:layout_below="#id/grid_image"
android:gravity="center"
android:textSize="20sp" >
</TextView>
</RelativeLayout>
mydialog.java
package glassfibre.hospital2;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.DialogInterface;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.DialogFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
import java.util.zip.Inflater;
import static android.R.attr.gridViewStyle;
import static android.R.attr.id;
import static android.R.attr.thumbPosition;
import static glassfibre.hospital2.R.layout.grid_single;
public class mydialog extends DialogFragment implements View.OnClickListener
{
Button bt;
ImageView iv;
CustomGrid cg;
#Nullable
#Override
public View onCreateView(LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
View view=inflater.inflate(R.layout.my_dialog,null);
bt=(Button)view.findViewById(R.id.button);
bt.setOnClickListener(this);
iv= (ImageView) view.findViewById(R.id.imageView);
setCancelable(false);
AlertDialog.Builder builder=new AlertDialog.Builder(getActivity());
// > here i wanted to set image which i click on grid view
return view;
}
#Override
public void onClick(View view)
{
if(view.getId()==R.id.button)
{
dismiss();
}
}
}
my_dialog.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="I want"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="20dp"
android:id="#+id/textView3"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:gravity="center"
android:textSize="20dp"/>
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
app:srcCompat="#drawable/food"
android:id="#+id/imageView"
android:layout_marginTop="19dp"
android:layout_below="#+id/textView3"
android:layout_centerHorizontal="true" />
<Button
android:text="OK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="12dp"
android:id="#+id/button" />
</RelativeLayout>
I have an array of images, and wanted to display image from it, to alert dialog when we click on grid item.
Do something like this:
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
int imageResource = imageId[position];
dialog.setImageResource(imageResource);
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});
And in your mydialog class declare int variable
private int imageResource;
And create a method:
public void setImageResource(int imageResource) {
this.imageResource = imageResource;
}
now you can set image resource to imageview in onCreateView method of mydialog class
Inside your myDialog class,just have a setter method which stores the Drawable Resource id , like shown below
public void setImageIdToDisplay(int aDrawableId) {
mDrawableId = aDrawableId;
}
and in your oncreateview method of myDialog class, just set the image resource id to the imageview
iv.setImageResource(mDrawableId);
and in your MainActivity's onItemClick listner of gridview, pass the drawable resource id as shown below
grid.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Toast.makeText(MainActivity.this, "You Clicked at " +web[+ position], Toast.LENGTH_SHORT).show();
mydialog dialog = new mydialog();
dialog.setImageIdToDisplay(imageId[position]);
dialog.show(getSupportFragmentManager(),"my_dialog");
}
});
I have to receive clicked list number. When I use getcheckeditemposition() method of ListView, return number is always 1. I don't know why. I have to solve this problem.
This is my code:
ChoiceDialogFragment.java
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentTransaction;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Adapter;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
import java.util.ArrayList;
import aa.SendFragment;
public class ChoiceEmotionDialogFragment extends DialogFragment {
private ListView listView;
private ArrayAdapter<String> adapter;
private ArrayList<String> data = new ArrayList<>();
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.Dialog);
data.add("a");
data.add("b");
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);
final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dismiss();
}
});
final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Log.d("checkedNum", listView.getCheckedItemCount()+"");
}
});
listView = (ListView) view.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
listView.setAdapter(adapter);
return view;
}
}
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"
android:background="#android:color/white"
android:orientation="vertical">
<TextView
android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:layout_marginTop="15dp"
android:text="choice"
android:textColor="#000000"
android:textSize="20sp"
android:textStyle="bold" />
<TableLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/dialog_title">
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:choiceMode="singleChoice" />
<TableRow>
<Button
android:id="#+id/dialog_button_cancel"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#null"
android:text="cancel" />
<View
android:layout_width="1dp"
android:layout_height="match_parent"
android:background="#90909090" />
<Button
android:id="#+id/dialog_button_confirm"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:background="#null"
android:text="confirm" />
</TableRow>
</TableLayout>
</RelativeLayout>
you have to set the item as checked on clicking the item
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
listView.setItemChecked(position, true);
}
});
Existing questions
You can use setOnItemClickListener to get selected item first, and store it to a global variable;
int selectedItemPosition;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_choice_emotion_dialog, container, false);
final Button cancel = (Button) view.findViewById(R.id.dialog_button_cancel);
cancel.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
dismiss();
}
});
final Button confirm = (Button) view.findViewById(R.id.dialog_button_confirm);
confirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// Access the selected item here
Log.d("checkedNum", selectedItemPosition+"");
}
});
listView = (ListView) view.findViewById(R.id.listview);
adapter = new ArrayAdapter(getActivity(), android.R.layout.simple_list_item_single_choice, data);
listView.setAdapter(adapter);
listView.setOnItemClickListener(new OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
// Store the position
selectedItemPosition = position;
}
});
return view;
}
I am a newbie in Android, so I am really sorry if the error that you will encounter is a blunder. I tried making a Custom ListView (An ImageView and a TextView in each row) by using custom_row.xml as the layout file and CustomAdapter.java as the customized adapter.
I do not understand why this application crashes every time I run the same. The exception (As shown in Android Monitor) is caused at this very line in the CustomAdapter.java file.
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
I am attaching all of the Java and Layout codes for the reference. Thanks.
CustomAdapter.java
package com.abhinavankur.listexample;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.ArrayAdapter;
class CustomAdapter extends ArrayAdapter<String> {
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater myInflater = LayoutInflater.from(getContext());
View customView = myInflater.inflate(R.layout.custom_row, parent, false);
String singleTeam = getItem(position);
TextView myText = (TextView) customView.findViewById(R.id.myText);
ImageView myImage= (ImageView) customView.findViewById(R.id.myImage);
myText.setText(singleTeam);
myImage.setImageResource(R.drawable.mine);
return customView;
}
}
custom_row.xml
<?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">
<ImageView
android:layout_width="80dp"
android:layout_height="80dp"
android:id="#+id/myImage"
android:src="#drawable/mine"
android:layout_margin="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Large Text"
android:id="#+id/myText"
android:layout_margin="5dp" />
</LinearLayout>
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<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.abhinavankur.listexample.MainActivity">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/myListView">
</ListView>
</RelativeLayout>
MainActivity.java
package com.abhinavankur.listexample;
import android.os.Bundle;
import android.app.Activity;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
String teams[] = {"Real Madrid","Manchester United","Barcelona","Chelsea","Bayern Munich","PSG","Borussia Dortmund","Liverpool","Arsenal","Valencia","Villareal","Leicester City","AS Roma"};
ListAdapter myAdapter = new CustomAdapter(this, teams);
ListView myList = (ListView) findViewById(R.id.myListView);
myList.setAdapter(myAdapter);
Toast.makeText(this,"List Shown",Toast.LENGTH_LONG).show();
myList.setOnItemClickListener(
new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
String team = String.valueOf(parent.getItemAtPosition(position));
Toast.makeText(MainActivity.this,team,Toast.LENGTH_SHORT).show();
}
}
);
}
}
Change your CustomAdapter as below. You were inflating the layout in a wrong way.
class CustomAdapter extends ArrayAdapter<String> {
Context context;
CustomAdapter(Context context, String[] teams) {
super(context,R.layout.custom_row,teams);
this.context = context
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (convertView == null) {
LayoutInflater layoutInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = layoutInflater.inflate(R.layout.custom_row, null);
}
....
....
}
I have a list view inside a DialogFragment and onClick for the list item doesnt work. Dont know where Im going wrong here. Implemented onItemClick but no use. Code below.
DialogFragment
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.DialogFragment;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class DFragment extends DialogFragment {
ListView mylist;
ArrayList dateitems ;
ArrayList descitems ;
ArrayList locationitems ;
ArrayList listitems ;
public void setList(ArrayList list){
listitems = list;
}
public void setDateList(ArrayList list){
dateitems = list;
}
public void setLocList(ArrayList list){
locationitems = list;
}
public void setDescList(ArrayList list){
descitems = list;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.popup_content, container,
false);
mylist = (ListView) rootView.findViewById(R.id.poplist);
TextView title = (TextView) rootView.findViewById(R.id.dialog_title);
title.setText("Today's Events");
getDialog().getWindow().requestFeature(Window.FEATURE_NO_TITLE);
mylist.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
Intent intent = new Intent(getActivity(), SupportEventActivity.class);
intent.putExtra("rvEventTitle", listitems.get(position).toString());
intent.putExtra("rvEventDescription", descitems.get(position).toString());
intent.putExtra("rvEventLocation", locationitems.get(position).toString());
intent.putExtra("rvEventTimestamp", dateitems.get(position).toString());
}
});
// Do something else
return rootView;
}
#Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
ArrayAdapter<String> adapter = new ArrayAdapter<String>(getActivity(),
R.layout.popup_content_row, R.id.poprow_text, listitems);
mylist.setAdapter(adapter); }
Layout file. popup_content.xml
<?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="wrap_content"
android:orientation="vertical"
android:background="#drawable/back"
>
<TextView
android:id="#+id/dialog_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="25dp"/>
<ListView
android:id="#+id/poplist"
android:layout_width="wrap_content"
android:layout_height="200dp"
android:layout_centerHorizontal="true"
android:layout_centerVertical="true"
android:divider="#8F8F8F"
android:dividerHeight="2px"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" /> </LinearLayout>
You didn't add startActivity(intent) to your OnClickListener.
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:padding="10dp"
android:src="#drawable/arteta"/>
<TextView
android:id="#+id/nameTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#id/imageView1"
android:text="name"/>
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageView
android:layout_width="150dp"
android:layout_height="100dp"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:padding="10dp"
android:id="#+id/imageView1"
android:src="#drawable/arteta"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="27dp"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/imageView1"
android:text="Name"
android:id="#+id/nameTv" />
</RelativeLayout>
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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MainActivity">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="setContentView" />
</RelativeLayout>
I have made a ListView with a DialogFragment and I am trying to display this in full screen.
However, when I run it on a device, there is always a border around the edges of the ListView, like a box within a box and cant seem to set it to fill the screen!
I have tried many routes to make this ListView cover the entire screen to no avail.
I would really like and appreciate some help with this.
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="match_parent"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
></ListView>
</RelativeLayout>
MainActivity
import android.support.v4.app.FragmentActivity;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.app.FragmentManager;
public class MainActivity extends FragmentActivity {
Button showBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
final PlayersFragment p = new PlayersFragment();
showBtn = (Button) findViewById(R.id.button1);
showBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
p.show(fm, "Players Fragment");
}
});
}
}
PlayersFragment
import android.app.DialogFragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class PlayersFragment extends android.support.v4.app.DialogFragment {
ListView lv;
String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};
int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
R.drawable.rooney, R.drawable.terry};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//set dialog title
getDialog().setTitle("Soccer SuperStars");
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
Adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Adapter extends ArrayAdapter<String>{
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public Adapter(Context context, String[] players) {
super(context, R.layout.rowmodel,players);
this.c=context;
this.players = players;
this.images = images;
}
#Override
public View getView(int position,View convertView, ViewGroup parent){
if(convertView==null)
{
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.rowmodel,null);
}
TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);
nameTv.setText(players[position]);
img.setImageResource(images[position]);
return convertView;
}
}
I think that you should use a FrameLayout in your main_activity layout. Something like this:
<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">
<FrameLayout
android:id="#+id/fragment_container"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Show"
android:id="#+id/button1"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:onClick="setContentView" />
</FrameLayout>
</RelativeLayout>
Then, in your MainActivity, try this:
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class MainActivity extends FragmentActivity {
Button showBtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final android.support.v4.app.FragmentManager fm = getSupportFragmentManager();
final PlayersFragment p = new PlayersFragment();
showBtn = (Button) findViewById(R.id.button1);
showBtn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
final FragmentTransaction transaction = getSupportFragmentManager().beginTransaction();
showBtn.setVisibility(View.GONE);
transaction.replace(R.id.fragment_container, p);
transaction.addToBackStack(null);
transaction.commit();
}
});
}
#Override
public void onBackPressed() {
if (showBtn != null) {
showBtn.setVisibility(View.VISIBLE);
}
super.onBackPressed();
}
}
Your adapter:
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class Adapter extends ArrayAdapter<String>{
Context c;
String[] players;
int[] images;
LayoutInflater inflater;
public Adapter(Context context, String[] players, int[] images) {
super(context, R.layout.simplerow,players);
this.c=context;
this.players = players;
this.images = images;
}
#Override
public View getView(int position,View convertView, ViewGroup parent){
if(convertView==null)
{
inflater= (LayoutInflater) c.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = inflater.inflate(R.layout.simplerow,null);
}
TextView nameTv = (TextView) convertView.findViewById(R.id.nameTv);
ImageView img = (ImageView) convertView.findViewById(R.id.imageView1);
nameTv.setText(players[position]);
img.setImageResource(images[position]);
return convertView;
}
}
Your dialog 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="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer SuperStars"
android:layout_gravity="center"/>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#android:color/black"/>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/listView1"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
></ListView>
</LinearLayout>
And your PlayerFragment:
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.Toast;
public class PlayersFragment extends android.support.v4.app.DialogFragment {
ListView lv;
String[] players = {"micheal arteta", "deago costa", "andy reid", "scum degea", "scum rooney", "john terry"};
int[] images = {R.drawable.arteta, R.drawable.costa, R.drawable.reid, R.drawable.degea,
R.drawable.rooney, R.drawable.terry};
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.dialog, container, false);
//initialize listview
lv = (ListView) rootView.findViewById(R.id.listView1);
//create adapter obj and set list view to it
Adapter adapter = new Adapter(getActivity(), players, images);
lv.setAdapter(adapter);
lv.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> a, View v, int pos, long id) {
Toast.makeText(getActivity(), players[pos], Toast.LENGTH_SHORT).show();
}
});
return rootView;
}
}
I removed some come (paddings) from your main_activity layout and included some code show the "dialog" (that is now a fragment) full screen.
1) First method
public class PlayersFragment extends DialogFragment {
#Override
public void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, android.R.style.Theme_NoTitleBar_Fullscreen);
}
}
2) Second method
public class PlayersFragment extends DialogFragment {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NO_TITLE, R.style.myCustomStyle);
}
}
in values add style
<?xml version="1.0" encoding="utf-8"?>
<resources>
<style name="AppBaseTheme" parent="Theme.AppCompat.Light.NoActionBar">
</style>
<!-- Application theme. -->
<style name="AppTheme" parent="AppBaseTheme">
</style>
<style name="GalleryDialogStyle" parent="AppTheme">
<item name="android:windowActionBar">false</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowFullscreen">true</item>
<item name="android:windowContentOverlay">#null</item>
</style>
</resources>