How To Set Layout For Android Grid View Custom Adapter - android

import android.app.Activity;
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.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import com.android.volley.toolbox.ImageLoader;
import com.android.volley.toolbox.NetworkImageView;
import com.nusecond.suredeal.app.R;
import com.nusecond.suredeal.app.suredeal.activity.MainActivity;
import com.nusecond.suredeal.app.suredeal.activity.Promo;
import com.nusecond.suredeal.app.suredeal.pojo.Campaigns;
import java.util.ArrayList;
import java.util.List;
/**
* Created by ns2 on 2/4/16.
*/
public class CustomPromoAdapter extends BaseAdapter{
private Activity activity;
private LayoutInflater inflater;
private List<Campaigns> campaigns;
ImageLoader imageLoader = AppController.getInstance().getImageLoader();
public CustomPromoAdapter(Activity activity, List<Campaigns> campaigns){
this.activity=activity;
this.campaigns=campaigns;
}
#Override
public int getCount() {
return campaigns.size();
}
#Override
public Object getItem(int location) {
return campaigns.get(location);
}
#Override
public long getItemId(int position) {
return position;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if (inflater==null)
inflater=(LayoutInflater) activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView==null)
convertView=inflater.inflate(R.layout.promo_gridlist,null);
if (imageLoader == null)
imageLoader = AppController.getInstance().getImageLoader();
NetworkImageView thumbNail = (NetworkImageView) convertView
.findViewById(R.id.thumbnail1);
TextView tView = (TextView)convertView.findViewById(R.id.textView1);
// tView.setLayoutParams(new LinearLayout.LayoutParams(5, 5));
Campaigns ca=campaigns.get(position);
tView.setText(ca.getName());
ImageView imgView = (ImageView)convertView.findViewById(R.id.imageView1);
thumbNail.setImageUrl(ca.getImage(), imageLoader);
return convertView;
}
}
This is My Custom Adapter To Display Images In Grid View.
Now I'm Displaying only 4 Images But I need 8.
The Text View Also Showing Not Properly.
I Don't Know Where I Need To Change I mean In My Adapter Or In My Layout File.
I Tried To Set LayoutParms But It Won't Work properly.
I'm new To Android Can Any One Help me...
Below Is My Layout....
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<com.android.volley.toolbox.NetworkImageView
android:id="#+id/thumbnail1"
android:layout_width="88dp"
android:layout_gravity="center"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"/>
<ImageView
android:id="#+id/imageView1"
android:layout_gravity="center"
android:layout_width="88dp"
android:layout_height="88dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
/>
<TextView
android:id="#+id/textView1"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="15dp"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"
android:text="TextView" />
</LinearLayout>
I'm Getting The Images And Text From Rest Setver..
Below is My Main Layout....
<?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:orientation="vertical"
tools:context=".suredeal.activity.Promo" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:numColumns="2" >
</GridView>
</RelativeLayout>
This is My Main Layout..
This is What I'm Displaying With The Above Code..
This is What I'm Trying To Do...
Any Help Thankful To Them.......

Related

Android Relative layout spacing between elements

I have this Design in Relative Layout. I have to do some more things.
How can I remove that straight line between two elements ?
How can I increase space between two elements ?
How to display product side by side ?
I have tried several solutions from stackoverflow. Like I have used margin bottom but got no luck.
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:background="#drawable/round_corner"
android:layout_marginRight="20dp"
>
<RelativeLayout
android:layout_width="200dp"
android:layout_height="250dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:paddingRight="20dp"
android:paddingLeft="20dp"
>
<de.hdodenhof.circleimageview.CircleImageView xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/product_image"
android:layout_width="match_parent"
android:layout_height="150dp"
android:layout_marginLeft="15dp"
android:layout_marginTop="20dp"
android:foregroundGravity="center"
app:civ_border_color="#d1b1b1"
/>
<View
android:layout_width="match_parent"
android:layout_height="180dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginStart="1dp"
android:layout_marginTop="0dp"
android:background="#drawable/gradient" />
<TextView
android:id="#+id/product_name_english"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#id/product_image"
android:layout_alignTop="#id/product_image"
android:layout_alignRight="#id/product_image"
android:layout_alignBottom="#id/product_image"
android:layout_margin="1dp"
android:gravity="center"
android:text="Hello"
android:textColor="#FFFF"
android:fontFamily="#font/aerial"/>
<TextView
android:id="#+id/price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/product_name_english"
android:layout_centerHorizontal="true"
android:layout_marginTop="-51dp"
android:layout_marginBottom="1dp"
android:fontFamily="#font/aerial"
android:text="Hello"
android:textColor="#FFFF" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="194dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="Add to cart"
android:textColor="#FFFFFF"
android:textSize="10sp" />
<Button
android:layout_width="250dp"
android:layout_height="20dp"
android:layout_alignParentTop="true"
android:layout_marginTop="224dp"
android:background="#drawable/buttonshape"
android:shadowColor="#A8A8A8"
android:shadowDx="0"
android:shadowDy="0"
android:shadowRadius="5"
android:text="View Product"
android:textColor="#FFFFFF"
android:textSize="10sp" />
</RelativeLayout>
</FrameLayout>
Here is the listview I am sending to my Adapter
listView = (ListView) findViewById(R.id.product_list);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
Here is my ArrayAdapter Class :
package com.example.yunus.ybazar_android;
import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.annotation.LayoutRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import java.io.InputStream;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import android.support.annotation.NonNull;
import android.widget.Toast;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.engine.DiskCacheStrategy;
public class all_product_list_ArrayAdapter extends ArrayAdapter<all_product_list_android_model> {
private final String Tag = "Filter" ;
private Context mContext;
private ArrayList<all_product_list_android_model> product_list ;
private final String urlMain = "http://118.179.70.235:28965/media/" ;
public all_product_list_ArrayAdapter(#NonNull Context context, ArrayList<all_product_list_android_model> list) {
super(context, 0, list);
mContext = context ;
product_list = list ;
}
#NonNull
#Override
public View getView(int position, #Nullable View convertView, #NonNull ViewGroup parent) {
View listItem = convertView;
if(listItem == null)
listItem = LayoutInflater.from(mContext).inflate(R.layout.all_product_list_arrayadapter_connection,parent,false);
all_product_list_android_model product_list_to_show = product_list.get(position);
ImageView image = (ImageView)listItem.findViewById(R.id.product_image);
new DownLoadImage(image).execute(urlMain.concat(product_list_to_show.product_main_image));
TextView name = (TextView) listItem.findViewById(R.id.product_name_english);
name.setText(product_list_to_show.product_name_english);
TextView price = (TextView) listItem.findViewById(R.id.price);
price.setText(("Taka ")
.concat(product_list_to_show.product_unit_price)
.concat("/")
.concat(product_list_to_show.product_unit));
final Button bt = (Button) listItem.findViewById(R.id.add_to_cart);
bt.setTag(product_list_to_show.id);
final Button bt2 = (Button) listItem.findViewById(R.id.view_product);
bt2.setTag(product_list_to_show.id);
bt.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
}
});
bt2.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
Intent intent = new Intent(mContext, indi_product_show.class);
intent.putExtra("product_id",String.valueOf(bt2.getTag()));
mContext.startActivity(intent);
}
});
return listItem;
}
}
Set your ListView's divider to null and its Divider Height to 0 pixels:
listView = (ListView) findViewById(R.id.product_list);
listView.setDivider(null);
listView.setDividerHeight(0);
mAdapter = new all_product_list_ArrayAdapter(all_product.this,data);
listView.setAdapter(mAdapter);
more info can be found in the docs for ListView here :
ListView#setDivider(android.graphics.drawable.Drawable)
ListView#setDividerHeight(int)
use divider for ListView in this way:
<ListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:divider="#null"
android:dividerHeight="0dp"/>

getting abrupt height for a horizontal recyclerview lists

I am creating a horizontal recyclerView containing cardView which in turn have a list of information.
If a name is of more than 3 lines the overall height of the recyclerview is disturbed.
Let me explain with the example:
Below is the normal layout when no text is more than 1
line
Now below is the layout when there is more than 1 line
Below are all the relevant files that are used
single_cast_layout.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="wrap_content"
android:minHeight="250dp"
android:layout_height="wrap_content">
<android.support.v7.widget.CardView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:minWidth="100dp"
android:layout_margin="#dimen/card_dimen"
android:elevation="3dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/castImage"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clickable="true"
android:scaleType="fitXY"
android:foregroundGravity="center"
android:contentDescription="#string/castContentDesc"
android:focusable="true"
tools:src="#drawable/ic_launcher_background" />
<TextView
android:id="#+id/castName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/castImage"
android:lines="2"
android:paddingLeft="#dimen/cast_name_padding"
android:paddingRight="#dimen/cast_name_padding"
android:paddingTop="#dimen/cast_name_padding"
android:textAlignment="center"
android:textColor="#color/movieNameBackground"
android:textSize="#dimen/cast_name_size"
tools:text="Actor Name" />
<TextView
android:id="#+id/castMovieName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/castName"
android:layout_centerHorizontal="true"
android:textAlignment="center"
android:paddingBottom="#dimen/cast_name_padding"
tools:text="character name" />
</RelativeLayout>
</android.support.v7.widget.CardView>
</LinearLayout>
castListAdapter.java
package com.example.ashish.moviesnow.adapters;
import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.ashish.moviesnow.Model.CastDetails;
import com.example.ashish.moviesnow.R;
import com.squareup.picasso.Picasso;
import java.util.List;
public class CastListAdapter extends RecyclerView.Adapter<CastListAdapter.MyViewHolder> {
private Context mContext;
private List<CastDetails> mList;
public CastListAdapter(Context mContext, List<CastDetails> mList) {
this.mContext = mContext;
this.mList = mList;
}
#Override
public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View view = LayoutInflater.from(mContext).inflate(R.layout.cast_layout,parent,false);
return new MyViewHolder(view);
}
#Override
public void onBindViewHolder(MyViewHolder holder, int position) {
CastDetails cd = mList.get(position);
String replacedString = cd.getmCastMovieName().replace("/","/\n");
if (cd.getmCastMovieName().length()>20){
}
holder.mCastText.setText(cd.getmCastRealName());
holder.mCastMovieName.setText(replacedString);
Picasso.with(mContext).load(cd.getmCastPoster())
.placeholder(R.drawable.ic_launcher_foreground)
.into(holder.mCastImage);
}
#Override
public int getItemCount() {
return mList.size();
}
public class MyViewHolder extends RecyclerView.ViewHolder {
private TextView mCastText,mCastMovieName;
private ImageView mCastImage;
public MyViewHolder(View itemView) {
super(itemView);
mCastText=(TextView)itemView.findViewById(R.id.castName);
mCastImage=(ImageView) itemView.findViewById(R.id.castImage);
mCastMovieName=(TextView)itemView.findViewById(R.id.castMovieName);
}
}
}
You can use android:ellipsize = "end" so if the sentence size larger than your line it will show dots (...) at the end you can also show it as you want.
Use android:lines="2" and android:ellipsize = "end" it will be fine for you . Or increase android:lines="3" and fix the layout_height as it. So all the layouts row will be show as same height.
<TextView
....
android:text="Hi I am Abhishek, How are you ?"
android:ellipsize = "end"
/>
Output will be as below :
Hi I am Abhi.....
Start's output will be : ...am Abhishek
End's output will be : Hi I am Ab...
Middle's output will be : Hi...Abhishek

showPopupMenu method not working when clickng on ImageView

I have a popup menu inside every item on a listview. When you click the imageview to the left (settings img with 3 dots) a popup menu should showup. however, I'm getting error
Could not find a method showPopupMenu(View) in the activity class android.app.Application for onClick handler on view class android.widget.ImageView with id 'settings_img'
does anyone know what this error is. it is obvious that can't find the method, but is it because it can't find the class/view? if so how can I fix it? thanks
main_activity
package com.example.george.hostscanmenus;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.ArrayAdapter;
import android.widget.ListView;
import android.widget.PopupMenu;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class MainActivity extends AppCompatActivity {
List hostList;
ArrayAdapter<String> hostAdapter;
PopupMenu popup;
String subnet = "192.168.10.";
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
hostList = new ArrayList<String>();
//populating host addresses
for(int i = 0; i < 255; i++) {
hostList.add(subnet+i +"-aa:bb:00:cc:33:ee");
}
//inflating adapter
listView = (ListView) findViewById(R.id.scan_list);
hostAdapter = new ArrayAdapter<String>(getApplicationContext(), R.layout.host_item, R.id.ip_address, hostList);
listView.setAdapter(hostAdapter);
}
/* code for popup menu in listview */
public void showPopupMenu(View v) {
popup = new PopupMenu(this, v);
popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() {
#Override
public boolean onMenuItemClick(MenuItem menuItem) {
switch (menuItem.getItemId()) {
case R.id.change_icon:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
case R.id.notifications:
Toast.makeText(getApplicationContext(), menuItem.getTitle(), Toast.LENGTH_SHORT).show();
return true;
default:
return MainActivity.super.onContextItemSelected(menuItem);
}
}
});
MenuInflater inflater = popup.getMenuInflater();
inflater.inflate(R.menu.host_menu, popup.getMenu());
popup.show();
}
}
array adapter
package com.example.george.hostscanmenus;
import android.content.Context;
import android.support.annotation.NonNull;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.TextView;
import java.util.ArrayList;
/**
* Created by george on 8/11/17.
*/
public class HostAdapter extends ArrayAdapter<String> {
public HostAdapter(Context context, int num, ArrayList<String> allHost) {
super(context, 0, allHost);
}
#NonNull
#Override
public View getView(int position, View convertView, ViewGroup parent) {
// Get the data item for this position
String host = getItem(position);
// Check if an existing view is being reused, otherwise inflate the view
if (convertView == null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.activity_main, parent, false);
}
// Lookup view for data population
ImageView nodeHostImgView = (ImageView) convertView.findViewById(R.id.host_icon);
TextView nodeIpTxtView = (TextView) convertView.findViewById(R.id.ip_address);
TextView nodeMacTxtView = (TextView) convertView.findViewById(R.id.mac_address);
ImageView nodeArrowImgView = (ImageView) convertView.findViewById(R.id.port_scan_arrow);
// Populate the data into the template view using the data object
nodeHostImgView.setImageResource(R.drawable.ic_computer_white_24dp);
nodeIpTxtView.setText(host.substring(0,host.indexOf("-")));
nodeMacTxtView.setText(host.substring(host.indexOf("-")));
nodeArrowImgView.setImageResource(R.drawable.ic_keyboard_arrow_right_white_24dp);
// Return the completed view to render on screen
return convertView;
}
}
main xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:ads="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/scan_list_linear"
tools:context=".MainActivity">
<!-- labels for ip / mac addres list -->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/list_label">
<TextView
android:id="#+id/host_port_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Host"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/ip_open_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.80"
android:textSize="20dp"
tools:text="IP / MAC"
android:paddingLeft="10dp"
android:textColor="#color/colorAccent"/>
<TextView
android:id="#+id/scan_label"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.20"
android:text="Scan"
android:textSize="20dp"
android:textAlignment="center"
android:textColor="#color/colorAccent"/>
</LinearLayout>
<!--Listview to display scan output-->
<ListView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/scan_list"/>
</LinearLayout>
list item 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="wrap_content"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/text_container"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/settings_img"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.10"
android:padding="3dp"
android:src="#drawable/ic_action_more_vert"
android:onClick="showPopupMenu"/>
<ImageView
android:id="#+id/host_icon"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_computer_white_24dp"/>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.50"
android:orientation="vertical"
android:paddingLeft="0dp"
>
<TextView
android:id="#+id/ip_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="18dp"
tools:text="192.168.10.100"
/>
<TextView
android:id="#+id/mac_address"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16dp"
tools:text="aa:bb:cc:00:11:22"
/>
</LinearLayout>
<ImageView
android:id="#+id/port_scan_arrow"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="0.20"
android:padding="5dp"
android:src="#drawable/ic_keyboard_arrow_right_white_24dp"/>
</LinearLayout>
</RelativeLayout>
menu xml
<!-- listview menu for host scan options eg:
change hostname, icon, notification etc -->
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<!-- Settings, should always be in the overflow -->
<item android:id="#+id/change_icon"
android:title="change icon"
/>
<item android:id="#+id/notifications"
android:title="set notification"
/>
</menu>
You should define your settings_img on your adapter getView and add a click listener for each element.
example:
ImageView settingsImgView = (ImageView) convertView.findViewById(R.id.settings_img);
settingsImgView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
context.showPopupMenu() //or popupmenu logic in here if you want
}
});
Usually, when it comes to the adapter, I pass the activity itself instead of the context. Then I pass the activity's context to super() and keep the activity in the field variable. That way I'm able to use all public methods of the parent activity.

RecyclerView Margin

i am developing application for home security. One of the feature, that must be implemented is the ability to see connected devices (for sending notifications, blocking access, etc). So far, i've been able to create RecyclerView list of the devices, everything is perfect (for me), except that cards in this list have no spacing between them.
Screenshot of how it looks now
device_data_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:cardview="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="80dp"
android:layout_margin="8dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="80dp">
<ImageView
android:id="#+id/deviceIcon"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:layout_marginLeft="8dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_alignBottom="#+id/deviceIcon"
android:layout_toEndOf="#+id/deviceIcon">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Username"
android:id="#+id/deviceUsername"
android:layout_gravity="center_vertical"
android:textColor="#000000"
android:layout_marginLeft="5dp" />
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_gravity="center_vertical"
android:gravity="center_vertical|center_horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Model"
android:gravity="center_vertical|right"
android:textColor="#000000"
android:id="#+id/deviceModel"
android:layout_marginLeft="5dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="25dp"
android:textColor="#000000"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Device Version"
android:gravity="center_vertical|right"
android:id="#+id/deviceVersion"
android:layout_marginLeft="5dp" />
</LinearLayout>
</LinearLayout>
devices_list.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:padding="#dimen/activity_vertical_margin">
<view
android:id="#+id/connectedDevicesList"
class="android.support.v7.widget.RecyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_centerInParent="true" />
</RelativeLayout>
View Holder Code
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import com.exampleapp.R;
public class DeviceViewHolder extends RecyclerView.ViewHolder {
protected ImageView deviceIcon;
protected TextView deviceUsername;
protected TextView deviceModel;
protected TextView deviceVersion;
public DeviceViewHolder(View view) {
super(view);
this.deviceIcon = (ImageView) view.findViewById(R.id.deviceIcon);
this.deviceUsername = (TextView) view.findViewById(R.id.deviceUsername);
this.deviceModel = (TextView) view.findViewById(R.id.deviceModel);
this.deviceVersion = (TextView) view.findViewById(R.id.deviceVersion);
}
}
RecyclerView Adapter
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import java.util.List;
import com.exampleapp.API.AuthorizedDevice;
import com.exampleapp.R;
public class ConnectedDeviceAdapter extends RecyclerView.Adapter<DeviceViewHolder> {
private List<AuthorizedDevice> devices;
private Context mContext;
public ConnectedDeviceAdapter(Context context, List<AuthorizedDevice> devices) {
this.devices = devices;
this.mContext = context;
}
#Override
public DeviceViewHolder onCreateViewHolder(ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
DeviceViewHolder viewHolder = new DeviceViewHolder(view);
return viewHolder;
}
#Override
public void onBindViewHolder(DeviceViewHolder deviceViewHolder, int i) {
AuthorizedDevice device = devices.get(i);
if (device.getDeviceId().equals("0")) {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.android_icon));
} else {
deviceViewHolder.deviceIcon.setBackground(ContextCompat.getDrawable(mContext, R.drawable.apple_icon));
}
deviceViewHolder.deviceUsername.setText(device.getUsername());
deviceViewHolder.deviceModel.setText(device.getDeviceName());
deviceViewHolder.deviceVersion.setText(device.getDeviceVersion());
}
#Override
public int getItemCount() {
return (null != devices ? devices.size() : 0);
}
}
Can anyone please help me solve this problem? Thank you very much in advance!
In your RecyclerView Adapter onCreateViewHolder:
replace:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, null);
with:
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.connected_devices_layout, viewGroup, false);
Or you can use RecyclerView.addItemDecoration(ItemDecoration decor).

My Android Application Is Giving Fragment Not Attached To Activity Error.

//Here Is My Code
My target is to add a custom listview on fragment and fire it on activity creation
// FragmentSongs.java
import android.os.Bundle;
import android.support.v4.app.ListFragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import android.widget.Toast;
public class FragmentSongs extends ListFragment implements OnItemClickListener {
ListView listview;
String[] Songs=getResources().getStringArray(R.string.songs);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
MyAdapter adapter=new MyAdapter(getActivity(),Songs);
listview.setAdapter(adapter);
listview.setOnItemClickListener(this);
View myview=inflater.inflate(R.layout.fragment_songs, container,false);
return myview;
}
#Override
public void onItemClick(AdapterView<?> parent, View view, int position,
long id) {
Toast.makeText(getActivity(), Songs[position], Toast.LENGTH_SHORT).show();
}
}
// MyAdapter.java
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.TextView;
public class MyAdapter extends ArrayAdapter<String>{
Context context;
String[] songs;
LayoutInflater inflater;
public MyAdapter(Context context,String[] songs) {
super(context,R.layout.songlist_row);
this.context=context;
this.songs=songs;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
if(convertView==null)
{
inflater=(LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView=inflater.inflate(R.layout.songlist_row, null);
}
TextView tv1=(TextView) convertView.findViewById(R.id.textView1);
tv1.setText(songs[position]);
return convertView;
}
}
// MainActivity.java
import android.os.Bundle;
import android.support.v4.app.FragmentActivity;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
public class MainActivity extends FragmentActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
System.out.print("Reached");
FragmentSongs songs=new FragmentSongs();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
transaction.add(R.id.fragment1,songs);
transaction.commit();
}
}
// activitymain.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/activitylayout"
android:orientation="vertical">
<fragment
android:id="#+id/fragment1"
android:name="android.app.ListFragment"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
//
this is the name of my fragment which i want to attach to activitymain
fragment_songs.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:android1="http://schemas.android.com/apk/res/android"
android:id="#+id/fragment1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:baselineAligned="false" >
<ListView
android1:id="#+id/listView1"
android1:layout_width="match_parent"
android1:layout_height="0dp"
android1:layout_weight="1" >
</ListView>
</LinearLayout>
// songlist_row.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:orientation="vertical"
android:padding="10dp"
android:background="#1A237E" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:text="Large Text"
android:background="#C5CAE9"
android:textAppearance="?android:attr/textAppearanceLarge" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView1"
android:background="#C5CAE9"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
<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_alignRight="#+id/textView1"
android:layout_toRightOf="#+id/textView2"
android:background="#C5CAE9"
android:text="Medium Text"
android:textAppearance="?android:attr/textAppearanceMedium" />
</RelativeLayout>
There are two ways of inserting a fragment of an activity and you are using both at the same time.
The first way is:
Declare the fragment inside the activity's layout file.
But in this case the fragment will be fixed on the activity like a view.
The second form is:
Programmatically add the fragment to an existing ViewGroup.
In this case you can add the fragment at any time to the activity, replace it with another fragment, remove the fragment when you want.
You can see what I'm telling you watching this link to the Android developer and looking "Adding a fragment to an activity".
Fragments Android Developers
Note: If you decide to programmatically add the fragment do not have to reference it in the XML Activity. On the other hand these lines would thus:
FragmentSongs songs=new FragmentSongs();
FragmentManager fm=getSupportFragmentManager();
FragmentTransaction transaction=fm.beginTransaction();
transaction.add(R.id.content,songs);
transaction.commit();
R.id.content: It is the id of the container in which you add the fragment programmatically in your case on the activitymain.xml LinearLayout. As you can see you did not like this but you linked the id of the fragment which is a mistake.
Well do not forget that you should not use two ways of adding the fragments so pick one and hope that what prompted you find it useful.

Categories

Resources