Trouble setting a ListView / DialogFragment to full screen - android

<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>

Related

Adding Search Box in Custom List

I want to add search box action in Main activity and List data activity,but I'm having trouble with getting filter() and other things,Please suggest me the ode.
package com.souravapplication.fruit;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.SearchView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.Locale;
public class MainActivity extends AppCompatActivity {
SearchView editsearch;
ListView listView;
String[] fruitNames = {"Apple","Oranges","Kiwi","Passion","Banana"};
int[] fruitImages = {R.drawable.apple,R.drawable.oranges,R.drawable.kiwi,R.drawable.passion,R.drawable.banana};
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = findViewById(R.id.listview);
final CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
editsearch = findViewById(R.id.search);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(final AdapterView adapterView, View view, int i, long l) {
Toast.makeText(getApplicationContext(),fruitNames[i],Toast.LENGTH_LONG).show();
Intent intent = new Intent(getApplicationContext(),Listdata.class);
intent.putExtra("name",fruitNames[i]);
intent.putExtra("image",fruitImages[i]);
startActivity(intent);
}
});
}
private class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return fruitImages.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
View view1 = getLayoutInflater().inflate(R.layout.row_data, null);
//getting view in row_data
TextView name = view1.findViewById(R.id.fruits);
ImageView image = view1.findViewById(R.id.images);
name.setText(fruitNames[i]);
image.setImageResource(fruitImages[i]);
return view1;
}
}
}
My List Data Activity class.
package com.souravapplication.fruits;
import android.content.Intent;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuItem;
import android.widget.ImageView;
import android.widget.TextView;
public class Listdata extends AppCompatActivity {
TextView listdata;
ImageView imageView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_listdata);
listdata = findViewById(R.id.listdata);
imageView = findViewById(R.id.imageView);
Intent intent = getIntent();
String receivedName = intent.getStringExtra("name");
int receivedImage = intent.getIntExtra("image",0);
listdata.setText(receivedName);
imageView.setImageResource(receivedImage);
//enable back Button
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (item.getItemId() == android.R.id.home) {
onBackPressed();
}
return super.onOptionsItemSelected(item);
}
#Override
public void onBackPressed() {
super.onBackPressed();
}
}
I have defined the SearchBox in android XML and it's working, the only problem is putting action in the search box.
My activity_main.xml file
<?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=".MainActivity">
<SearchView
android:id="#+id/search"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintStart_toStartOf="parent">
</SearchView>
<ListView
android:id="#+id/listview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:layout_marginRight="16dp"
app:layout_constraintEnd_toEndOf="parent"
tools:ignore="MissingConstraints"
tools:layout_editor_absoluteY="-699dp">
</ListView>
</android.support.constraint.ConstraintLayout>
My row_data.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">
<RelativeLayout
android:id="#+id/listviewdata"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp"
>
<TextView
android:id="#+id/fruits"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginEnd="44dp"
android:layout_marginRight="44dp"
android:layout_toStartOf="#+id/images"
android:layout_toLeftOf="#+id/images"
android:text="Apple"
android:textColor="#000"
android:textSize="35sp"
/>
<ImageView
android:id="#+id/images"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginEnd="27dp"
android:layout_marginRight="27dp"
android:src="#drawable/apple"/>
</RelativeLayout>

ArrayAdapter not active in spinner

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;
}
}

Sorting Grid View using Spinner , XML array, and Custom adapter in android

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>

How to make multiple layout for one screen?

I want to make Screen in android with multiple layouts. Fox example On the top of the screen there is the header with image slider (this part is done) below that there will be a list view and below that there will be grid view.
Activitymain.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"
tools:context="com.example.root.multipleview.MainActivity">
<!--Header image-->
<android.support.v4.view.ViewPager
android:id="#+id/viewPager"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:autoStart="true"></android.support.v4.view.ViewPager>
<!--ListView Below header -->
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:height="10dp" />
</RelativeLayout>
CustomLayout.xml
<?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"
android:layout_alignParentTop="true"
android:background="#f1f3f7"
android:orientation="vertical">
<!--Header image with slider-->
<ImageView
android:id="#+id/headerimg"
android:layout_width="fill_parent"
android:layout_height="180dp"
android:scaleType="centerCrop"
android:layout_marginTop="4dp"
android:layout_marginLeft="4dp"
android:layout_marginRight="4dp"
android:layout_marginBottom="4dp"
android:src="#mipmap/ic_launcher" />
<!--ListView Below Header-->
<RelativeLayout
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
android:layout_marginTop="20dp"
android:paddingLeft="2dp">
<ImageView
android:id="#+id/imageView"
android:layout_width="100dp"
android:layout_height="90dp"
android:layout_weight="0.05"
app:srcCompat="#mipmap/ic_launcher"
android:paddingTop="10dp"
android:paddingBottom="10dp"
android:gravity="center"/>
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginLeft="40dp"
android:layout_marginStart="40dp"
android:layout_marginTop="11dp"
android:layout_toEndOf="#+id/imageView"
android:layout_toRightOf="#+id/imageView"
android:text="TextView" />
<TextView
android:id="#+id/textdesc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView"
android:layout_marginBottom="20dp"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/textName"
android:layout_alignStart="#+id/textName"/>
</RelativeLayout>
</LinearLayout>
ListView.java
package com.example.root.multipleview;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;
/**
* Created by root on 11/18/2017.
*/
public class ListView extends AppCompatActivity {
int[] Images = {R.drawable.salad,R.drawable.salami,R.drawable.flour,R.drawable.salt,
R.drawable.sandwich,R.drawable.sausage,R.drawable.seeds,R.drawable.cheese,R.drawable.shrimp,R.drawable.cupcake,R.drawable.cup,R.drawable.spices,R.drawable.cabbage
,R.drawable.spoon,R.drawable.apple,R.drawable.asparagus,R.drawable.cupcake,R.drawable.fish,R.drawable.corn,R.drawable.cupcake,R.drawable.spatula,R.drawable.olives
,R.drawable.garlic,R.drawable.taco,R.drawable.broccoli,R.drawable.cabbage};
String[] Names = {"a","B","c","d","e","f","g","h","i","j","k","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z"};
String[] Description = {"VVV","DDD","ddd","dddsa","ddsdsds","zsxx","wwwwq","ddwqK","EQWK","FgggFFF","FFFkklF","FghhFFF","FFhhFF","FFhhFF",
"FFmmmFF","FgggFFF","FFFFbbb","FFFFfeee","gfffFFFF","FFFFfff","FFFgffF","FFFFfgffg","FFFfgfgfF","FFFgffF","FFFgfggdF","FFFFdssa"};
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
android.widget.ListView listView= (android.widget.ListView) findViewById(R.id.listView);
CustomAdapter customAdapter = new CustomAdapter();
listView.setAdapter(customAdapter);
}
public class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return Images.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
ImageView imageView = (ImageView)view.findViewById(R.id.imageView);
TextView textView_N = (TextView)view.findViewById(R.id.textName);
TextView textView_D = (TextView)view.findViewById(R.id.textdesc);
imageView.setImageResource(Images[i]);
textView_N.setText(Names[i]);
textView_D.setText(Description[i]);
return view;
}
}
}
MainActivity.java
package com.example.root.multipleview;
import android.support.v4.view.ViewPager;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
ViewPager viewPager;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
viewPager = (ViewPager)findViewById(R.id.viewPager);
ViewPageAdapter viewPageAdapter = new ViewPageAdapter( this);
viewPager.setAdapter(viewPageAdapter);
}
}
ViewPageAdapter
package com.example.root.multipleview;
import android.content.Context;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
/**
* Created by root on 11/18/2017.
*/
public class ViewPageAdapter extends PagerAdapter{
private Context context;
private LayoutInflater layoutInflater;
private Integer[] images = {R.drawable.b,R.drawable.c,R.drawable.e,R.drawable.j,R.drawable.r,R.drawable.x,R.drawable.y};
public ViewPageAdapter(Context context){this.context = context;}
#Override
public int getCount() {
return images.length;
}
#Override
public boolean isViewFromObject(View view, Object object) {
return view == object;
}
#Override
public Object instantiateItem(ViewGroup container, int position) {
layoutInflater =(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = layoutInflater.inflate((R.layout.custom_layout), null);
ImageView imageView = (ImageView) view.findViewById(R.id.headerimg);
imageView.setImageResource((images[position]));
ViewPager vp = (ViewPager) container;
vp.addView(view, 0);
return view;
}
#Override
public void destroyItem(ViewGroup container, int position, Object object) {
ViewPager vp = (ViewPager) container;
View view = (View) object;
vp.removeView(view);
}
}
When I check this all layout separately it works but after adding in one layout it's not working. output screenshot is added
enter image description here
How to make multiple layout for one screen?
my approach for this goal is using Fragments
how?
i do make a single main_layout and set a FrameLayout in order to put fragments with different layouts.so i have a screen with different layouts.
this is my best answer for your question .

i want to display list of juice object.but here am getting only one object and displaying only one object

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);

Categories

Resources