i try to use spinner with array adapter and it will be stopped without any error can any one tell me the error in the code
i need to Appear image with text
i tried it by custom adapter and show same result
any one help me please i am new in android application developer
Main.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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="47dp"
android:spinnerMode="dropdown">
</Spinner>
</RelativeLayout>
spinner_item
<?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">
<ImageView
android:id="#+id/i1"
android:layout_width="100dp"
android:layout_height="100dp"
android:src="#drawable/image" />
<TextView
android:id="#+id/txt1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="15dp"
android:text="Orange"
android:textSize="30dp"
android:textStyle="bold" />
</LinearLayout>
Main.java
package com.safaa.user.spinner;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Spinner;
import android.widget.Toast;
public class MainActivity extends AppCompatActivity {
String[] names={"apple","panana","orange"};
int[]images={R.drawable.image,R.drawable.image3,R.drawable.image2
};
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner spinner=findViewById(R.id.spinner);
adapter=new CustomAdapter(this,names,images);
spinner.setAdapter(adapter);
spinner.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
Toast.makeText(getApplicationContext(),names[position],Toast.LENGTH_SHORT).show();
}
#Override
public void onNothingSelected(AdapterView<?> parent) {
}
});
}
}
CustomAdapter
package com.safaa.user.spinner;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
public class CustomAdapter extends ArrayAdapter<String>{
Context context;
String []names;
int image[];
public CustomAdapter(Context context, String[]names,int[]image) {
super(context, R.layout.spinner_item,names);
this.image=image;
this.names=names;
this.context=context;
}
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row =inflater.inflate(R.layout.spinner_item,null);
TextView t1=row.findViewById(R.id.txt1);
ImageView i1=row.findViewById(R.id.i1);
t1.setText(names[position]);
i1.setImageResource(image[position]);
return row;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater=(LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View row =inflater.inflate(R.layout.spinner_item,null);
TextView t1=row.findViewById(R.id.txt1);
ImageView i1=row.findViewById(R.id.i1);
t1.setText(names[position]);
i1.setImageResource(image[position]);
return row;
}
}
Related
Friends, I am a new developer in the field of android and I want to develop an application like that it will sort the Gridview by using a spinner, I had applied the XML array for Spinner and Custom adapter also I tried various solution but it didn't work so please help me
Moodel Class for adapter of GridAdapter
package braniacs.com.gridupdaterapp;
/**
* Created by grimReaper on 12/4/2017.
*/
public class GridModel {
String thingName;
public String getThingName() {
return thingName;
}
public void setThingName(String thingName) {
this.thingName = thingName;
}
}
MainActvityCode along adapter in it!
package braniacs.com.gridupdaterapp;
import android.content.Context;
import android.database.Cursor;
import android.provider.MediaStore;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
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.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
GridView mGridViewA;
Spinner mspinnerColony;
ArrayList<GridModel> ColonyArray;
String[] ColonyArraySpinnerWali = {"hie", "wasteage", "short", "Lovely", "RUSSIA", "ASIA", "KOREA"};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mGridViewA = findViewById(R.id.mgridViewApp);
mspinnerColony = findViewById(R.id.spnr_colony);
ColonyArray = new ArrayList<>();
ArrayAdapter mArrayAdapter = new ArrayAdapter(getApplicationContext(), android.R.layout.simple_list_item_1, ColonyArraySpinnerWali);
mspinnerColony.setAdapter(mArrayAdapter);
mspinnerColony.setSelection(0);
mspinnerColony.setOnItemSelectedListener(new AdapterView.OnItemSelectedListener() {
#Override
public void onItemSelected(AdapterView<?> adapterView, View view, int i, long l) {
if (i != 0) {
GridModel mgRIDmODEL = new GridModel();
TextView mTitle = (TextView) view;
mgRIDmODEL.setThingName(mTitle.getText().toString());
ColonyArray.add(mgRIDmODEL);
GridVuCustionAdapter adapter = new GridVuCustionAdapter(MainActivity.this,
R.layout.custom_gridvu_design, ColonyArray);
mGridViewA.setAdapter(adapter);
}
}
#Override
public void onNothingSelected(AdapterView<?> adapterView) {
}
});
mGridViewA.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
// CLICK LISTENER AREA
}
});
}
public static class GridVuCustionAdapter extends ArrayAdapter<GridModel> {
LayoutInflater mLayoutInflater;
ArrayList<GridModel> objects;
Context context;
int resource;
public GridVuCustionAdapter(#NonNull Context context, int resource, #NonNull ArrayList<GridModel> objects) {
super(context, resource, objects);
mLayoutInflater = (LayoutInflater) context.getSystemService(LAYOUT_INFLATER_SERVICE);
this.objects = objects;
this.context = context;
this.resource = resource;
}
#Override
public int getCount() {
return objects.size();
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
convertView = mLayoutInflater.inflate(resource, parent, false);
TextView txtVuTitle = convertView.findViewById(R.id.txt_vu_name);
ImageView imgVuCancel = convertView.findViewById(R.id.img_vu_cancel);
txtVuTitle.setText(objects.get(position).getThingName());
return convertView;
}
}
}
the xml file for custom design for the design is simple here
<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="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="2">
<TextView
android:id="#+id/txt_vu_name"
android:layout_weight="1.7"
android:layout_width="0dp"
android:layout_height="match_parent"
android:gravity="center"
android:text="TextView" />
<ImageView
android:layout_weight="0.2"
android:id="#+id/img_vu_cancel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#android:drawable/btn_dialog" />
</LinearLayout>
the xml file is here for the activity
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.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="braniacs.com.gridupdaterapp.MainActivity">
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mgridViewApp"
android:layout_width="0dp"
android:layout_height="200dp"
android:columnWidth="90dp"
android:gravity="center"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:stretchMode="columnWidth"
android:verticalSpacing="10dp"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.0"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.498" />
<Spinner
android:id="#+id/spnr_colony"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginEnd="8dp"
android:layout_marginStart="8dp"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
tools:layout_editor_absoluteY="45dp" />
</android.support.constraint.ConstraintLayout>
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 want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
i want to display list of juice object.but here am getting only one object and displaying only one object.getcount() is calling 5 times.but it is not displaying.please help me.
//FoodHome.java
package com.example.hotelmenucard;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ListView;
public class FoodHome extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_food_home);
populateListView();
registerClickCallBack();
}
private void populateListView() {
String[] items={"All Day BreakFast","Salads","Sandwiches","Hot Drinks","Juices"};
ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this, //Context for the activity
R.layout.text_view, //Layout to use(create)
items); //Items to be displayed
ListView list=(ListView)findViewById(R.id.listView1);
list.setAdapter(adapter);
}
private void registerClickCallBack() {
ListView list=(ListView)findViewById(R.id.listView1);
list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
/*TextView textview=(TextView)view;
String msg="hai"+textview.getText();
Toast.makeText(FoodHome.this, msg, Toast.LENGTH_LONG).show();*/
Intent i = new Intent(FoodHome.this, ItemList.class);
startActivity(i);
}
});
}
}
//ItemList.java
package com.example.hotelmenucard;
import java.util.ArrayList;
import java.util.List;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.RelativeLayout;
import android.widget.TextView;
public class ItemList extends Activity {
private List<Juice> juices = new ArrayList<Juice>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_item_list);
populateJuiceList();
populateJuiceListView();
Log.i(juices.toString(),"list of juice");
}
private void populateJuiceList() {
juices.add(new Juice("Lemon", 15, "lemon*****", R.drawable.lemon));
juices.add(new Juice("Strawberry", 15, "Strawberry*****",
R.drawable.strawberry));
juices.add(new Juice("Watermelon", 15, "Watermelon*****",
R.drawable.watermelon));
juices.add(new Juice("Pear", 15, "pear*****", R.drawable.pear));
juices.add(new Juice("Pomegranate", 15, "Pomegranate*****",
R.drawable.pomegranate));
System.out.println(juices+"juices in list");
}
private void populateJuiceListView() {
ArrayAdapter<Juice> adapter = new MyListAdapter(R.layout.activity_item_list);
ListView list = (ListView) findViewById(R.id.carsListV);
list.setAdapter(adapter);
}
private class MyListAdapter extends ArrayAdapter<Juice> {
public MyListAdapter(int activityItemList) {
super(ItemList.this, activityItemList, juices);
}
public int getCount() {
System.out.println(juices.size()+"in getcount");
return juices.size();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
RelativeLayout itemView =(RelativeLayout) convertView;
if (itemView == null) {
itemView = (RelativeLayout)getLayoutInflater().inflate(R.layout.item_list,
parent, false);
}
Juice juice = juices.get(position+1);
ImageView imageView = (ImageView)itemView.findViewById(R.id.imageView1);
imageView.setImageResource(juice.getId());
TextView txt=(TextView)itemView.findViewById(R.id.textView1);
txt.setText(juice.getName());
TextView text1=(TextView)itemView.findViewById(R.id.textView2);
text1.setText(juice.getUsing());
itemView.bringToFront();
TextView t=(TextView)itemView.findViewById(R.id.textView3);
t.setText(Integer.toString(juice.getRate()));
System.out.println(itemView.getVisibility());
return itemView;
// return super.getView(position, convertView, parent);
}
}
}
//activity_item_list.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"
android:background="#drawable/bg"
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.hotelmenucard.ItemList" >
<ListView
android:id="#+id/carsListV"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true" >
</ListView>
</RelativeLayout>
//item_list.xml (typo mistake changed from item_view.xml)
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/bg"
android:gravity="top"
android:textColor="#B2F8F8" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:src="#drawable/lemon" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView1"
android:layout_centerHorizontal="true"
android:text="TextView" />
<TextView
android:id="#+id/textView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/textView2"
android:layout_alignBottom="#+id/textView2"
android:layout_alignParentRight="true"
android:text="TextView" />
</RelativeLayout>
instead of ArrayAdapter<String> adapter=new ArrayAdapter<String>(
this,R.layout.text_view,items);
use this
ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1, items);
<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>
i tried many reference to this,but didn't get it working.
I have a simplecursoradapter with listview .
I populate textview and a button in each listitem.
I need to perform 2 operations:
1.Button Click
2.Listview Click.
Using onListItemClick my code is running perfect.but i cannot access the button inide the listview to perform(a call).
My code below:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:focusable="false"
android:layout_gravity="center_vertical"/>
<TextView
android:id="#+id/jobnumber"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:textStyle="bold"
android:textColor="#000000"
android:layout_marginRight="50dp"
android:height="30sp"
android:gravity="right" />
<TextView
android:id="#+id/complaintdate"
android:layout_width="257dp"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="#+id/jobnumber"
android:gravity="right" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_gravity="center_horizontal"
>
<TextView
android:id="#+id/lbltenantname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="left"
android:text="#string/Tenant">
</TextView>
<TextView
android:id="#+id/tenantname"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:gravity="center_vertical|center_horizontal">
</TextView>
</LinearLayout>
and other textviews....
list to hold the Listitem...
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/backgroundn"
android:descendantFocusability="blocksDescendants">
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="4dp"/>
</LinearLayout>
and my android code
import android.app.ActionBar;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.Context;
import android.content.Intent;
import android.database.Cursor;
import android.net.ConnectivityManager;
import android.net.NetworkInfo;
import android.os.Bundle;
import android.support.v4.widget.SimpleCursorAdapter;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;
public class JobList extends ListActivity
{
SimpleCursorAdapter mAdapter;
ListView lv;
private DatabaseAdapter dbHelper;
Menu menuitem;
#Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.joblist_item);
ActionBar actionBar = getActionBar();
actionBar.setDisplayHomeAsUpEnabled(true);
Intent intent=getIntent();
String BegeningUserid= intent.getStringExtra("USERID");
String SelfStatus= intent.getStringExtra("SELF");
Button call=(Button)findViewById(R.id.btn_call);
try
{
dbHelper=new DatabaseAdapter(this);
dbHelper.open();
lv=(ListView)findViewById(android.R.id.list);
Cursor c=dbHelper.fetchAllJobs(BegeningUserid,SelfStatus);
#SuppressWarnings("static-access")
String[] from = new String[] { dbHelper.COL_JOBNUMBER,dbHelper.COL_TENANT ,dbHelper.COL_DEVELOPMENT,dbHelper.COL_SHORTCODE,dbHelper.COL_PROPERTY,dbHelper.COL_COMPLAINTDATE,dbHelper.COL_COMPLAINTDESC,dbHelper.COL_MOBILE};
int[] to = new int[] {R.id.jobnumber, R.id.tenantname, R.id.location,R.id.ShortCode, R.id.Unit,R.id.complaintdate,R.id.complaintdesc,R.id.mobile};
#SuppressWarnings("deprecation")
SimpleCursorAdapter adapter = new SimpleCursorAdapter(
JobList.this, R.layout.list_item, c, from, to);
adapter.notifyDataSetChanged();
lv.setAdapter(adapter);
if(lv.getCount()<=0)
{
Toast.makeText(getApplicationContext(), "NO JOBS ASSIGNED", Toast.LENGTH_LONG).show();
}
lv.setOnItemClickListener(new OnItemClickListener()
{
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// When clicked, show a toast with the TextView text
Toast.makeText(getApplicationContext(), "Clicked",Toast.LENGTH_SHORT).show();
}
});
}
catch(Exception e)
{
System.out.println(e);
}
finally
{
dbHelper.close();
}
}
my listitemclick which is working perfect
public void onListItemClick(ListView parent, View view, int position, long id)
{
}
all i need is click button inside the listview and perform another activity depending on the data from the particular listview position.
set this custom Adapter to your list view...
Custom Adapter Class
public class CustomAdapter extends BaseAdapter implements OnClickListener {
//song list and layout
private ArrayList<from> from;
private LayoutInflater songInf;
//constructor
public CustomAdapter(Context c, ArrayList<from> arrfrom){
from=arrfrom;
songInf=LayoutInflater.from(c);
}
#Override
public int getCount() {
return from.size();
}
#Override
public Object getItem(int arg0) {
return null;
}
#Override
public long getItemId(int arg0) {
return 0;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LinearLayout songLay = (LinearLayout)songInf.inflate
(R.layout.list_item, parent, false);
TextView tv = (TextView)songLay.findViewById(R.id.from_text);
Button button = (Button)songLay.findViewById(R.id.from_button);
tv.setText(from.get(position));
button.setOnClickListner(this);
return songLay;
}
#Override
public void onClick(View v)
{
// Here is code after button click
}
}