I got a problem with a custom dialog in an android application.
I got a Custom Dialog with a ListView inside it. The Dialog itself is 600dp width and the ListView is 600dp as well.
The problem is that the dialog is more than that width value:
This is the Dialog layout:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="vertical">
<ListView
android:id="#+id/promotionListView"
android:layout_width="600dp"
android:layout_height="400dp"
android:scrollbarStyle="insideOverlay"
android:dividerHeight="1dp" />
<LinearLayout
android:layout_width="600dp"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/promotion_dialog_abort"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/annulla"/>
<Button
android:id="#+id/promotion_dialog_apply"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/applica"/>
</LinearLayout>
</LinearLayout>
This is the layout of the single row: (note that both the width of the layout and the sum of single views is 600dp)
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="600dp" <-- I tried "wrap_content" and "match_parent" with the same result
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/promotionCheck"
android:layout_width="50dp"
android:layout_height="50dp"
android:gravity="center"
android:layout_gravity="center" />
<TextView
android:id="#+id/promotionText"
android:layout_width="350dp"
android:layout_height="50dp"
android:gravity="center_vertical"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
<TextView
android:id="#+id/promotionFactor"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
<TextView
android:id="#+id/promotionPrice"
android:layout_width="100dp"
android:layout_height="50dp"
android:gravity="center_vertical|right"
android:layout_gravity="center"
android:textColor="#android:color/white"
android:textSize="#dimen/customerdetail_headertextsize" />
</LinearLayout>
And finally the Custom Dialog Class
public class PromotionDialog extends Dialog {
private boolean authCode;
private Set<OrderDetailDiscount> promotions;
private List<OrderDetailDiscount> selectedPromotions;
private ListView mainListView;
public Response response = Response.CANCEL;
private Button buttonAbort;
private Button buttonAccept;
public PromotionDialog(Context context, boolean authCode, Set<OrderDetailDiscount> promotions) {
super(context);
this.authCode = authCode;
this.promotions = promotions;
selectedPromotions = new ArrayList<>();
setTitle("Promozioni");
setContentView(R.layout.isfa_promotiondialog);
buttonAbort = (Button) findViewById(R.id.promotion_dialog_abort);
buttonAccept = (Button) findViewById(R.id.promotion_dialog_apply);
}
}
I really can't understand what the problem is, I tried to manually resize the titlebar with no success... what could I possibbly try?
You should use wrap_content or match_parent instead of fixed width/height values in order to support different screen sizes and to simulate table rows you can apply layout_weight to each child (column) of a horizontal LinearLayout.
Moreover, I suggest you to use AlertDialog.Builder APIs.
Here's what I achieved trying to recreate your dialog using my suggestions:
And here's the code:
MainActivity.java
public class MainActivity extends ActionBarActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
AlertDialog.Builder mBuilder = new AlertDialog.Builder(this);
mBuilder.setTitle("Discounts");
mBuilder.setAdapter(new MyAdapter(), null);
mBuilder.setPositiveButton("OK",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.setNegativeButton("Cancel",new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
}
});
mBuilder.create().show();
}
class RowItem{
public String field1;
public String field2;
public String field3;
public RowItem(String f1,String f2, String f3){
field1=f1;
field2=f2;
field3=f3;
}
}
private class MyAdapter extends BaseAdapter{
private final ArrayList<RowItem> rows = new ArrayList<RowItem>();
public MyAdapter(){
rows.add(new RowItem("Sales","1","5%"));
rows.add(new RowItem("Extra Sales","2","50%"));
rows.add(new RowItem("Super Sales","3","70%"));
rows.add(new RowItem("Completely Free","4","100%"));
}
#Override
public int getCount(){
return rows.size();
}
#Override
public Object getItem(int p){
return rows.get(p);
}
#Override
public long getItemId(int p){
return p;
}
#Override
public View getView(int position, View v, ViewGroup parent){
v=getLayoutInflater().inflate(R.layout.row_layout,parent,false);
LinearLayout row = (LinearLayout)v.findViewById(R.id.linearlayout);
CheckBox checkBox = (CheckBox)v.findViewById(R.id.checkbox);
TextView field1 = (TextView)v.findViewById(R.id.field1);
TextView field2 = (TextView)v.findViewById(R.id.field2);
TextView field3 = (TextView)v.findViewById(R.id.field3);
row.setBackgroundColor(Color.parseColor(position%2==0?"#212121":"#424242"));
RowItem item = rows.get(position);
field1.setText(item.field1);
field2.setText(item.field2);
field3.setText(item.field3);
return v;
}
}
}
row_layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/linearlayout"
android:orientation="horizontal"
android:weightSum="1"
android:layout_width="match_parent" android:layout_height="wrap_content">
<CheckBox
android:layout_width="0dp"
android:layout_weight="0.1"
android:layout_height="wrap_content"
android:text=""
android:id="#+id/checkbox"
android:checked="false" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field1"
android:layout_weight="0.6" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field2"
android:layout_weight="0.15" />
<TextView
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="myText"
android:id="#+id/field3"
android:layout_weight="0.15" />
</LinearLayout>
Related
I'm trying to add a ripple effect for the entire row (like we see in listview) in my Recycler View when the row is clicked.
I have tried
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true"
But still, I couldn't get the desired behavior. Even I have tried
android:foreground="?android:attr/selectableItemBackground"
But still no improvement
My listrow.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:orientation="horizontal"
android:background="?android:attr/selectableItemBackground"
android:clickable="true"
android:focusable="true">
<RadioButton
android:id="#+id/radioButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/heading"
android:text="Title"
android:textSize="20dp"
android:paddingTop="5dp"
android:textStyle="bold"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<TextView
android:layout_toLeftOf="#+id/subHeading"
android:id="#+id/size"
android:text="Sub Title"
android:paddingTop="3dp"
android:paddingLeft="5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
And My layout with Recycler View
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_height="wrap_content"
android:layout_width="match_parent">
<androidx.recyclerview.widget.RecyclerView
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:clipToPadding="false"
android:paddingTop="#dimen/list_item_spacing_half"
android:paddingBottom="#dimen/list_item_spacing_half"
android:layout_alignParentTop="true"
tools:context=".ListFragment"
tools:listitem="#layout/listrow" />
<Button
android:id="#+id/Save"
android:layout_width="match_parent"
android:text="Save"
android:layout_below="#+id/list"
android:textAllCaps="false"
android:layout_height="wrap_content"/>
</RelativeLayout>
Here is my adapter source code
private class listAdapter extends RecyclerView.Adapter<ViewHolder> {
private final int mItemCount;
final ArrayList<String> mTitles;
final ArrayList<String> msTitles;
ArrayList<RadioButton> radioButtons=new ArrayList<>();
listAdapter(int itemCount, ArrayList<String> titles, ArrayList<String> sTitles) {
mItemCount = itemCount;
mtitles=titles;
msTitles=sTitles;
}
#NonNull
#Override
public ViewHolder onCreateViewHolder(#NonNull ViewGroup parent, int viewType) {
return new ViewHolder(LayoutInflater.from(parent.getContext()), parent);
}
#Override
public void onBindViewHolder(ViewHolder holder, int position) {
holder.sTitle.setTag(position);
holder.radioButton.setTag(position);
holder.title.setTag(position);
holder.title.setText(mTitles.get(position));
holder.sTitle.setText(msTitles.get(position));
View.OnClickListener listener = new View.OnClickListener() {
#Override
public void onClick(View v) {
ItemCheckChanged(v);
}
};
holder.radioButton.setOnClickListener(listener);
holder.sTitle.setOnClickListener(listener);
holder.title.setOnClickListener(listener);
if(radioButtons.size()==0){
holder.radioButton.setChecked(true);
selectedItem=0;
}
radioButtons.add(holder.radioButton);
}
#Override
public int getItemCount() {
return mItemCount;
}
void ItemCheckChanged(View v){
selectedItem=(int)v.getTag();
for(RadioButton radioButton:radioButtons){
radioButton.setChecked(false);
}
radioButtons.get(selectedItem).setChecked(true);
notifyDataSetChanged();
}
public int getSelectedIndex(){
return selectedItem;
}
}
NOTE: I'm using these all things in BottomSheetDialog may it be reason?
You should set background to views that have set a click listener.
If you want to set the listener to the whole row you need to call only:
holder.itemView.setOnclickListener() and remove rest of them.
I have made a custom ArrayAdapter and tried to add the another TextView "Breed" of the animals, but when i execute the program , its not showing the Breed. Where am i doing wrong ? I am scratching my head since long. please help where is the mistake ?
MainActivity.Java
public class MainActivity extends AppCompatActivity {
ListView simpleList;
ArrayList<Item> animalList=new ArrayList<>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
simpleList = (ListView) findViewById(R.id.simpleListView);
animalList.add(new Item("Lion",R.drawable.lion,"Khatarnak"));
animalList.add(new Item("Tiger",R.drawable.tiger,"Fudu"));
animalList.add(new Item("Monkey",R.drawable.monkey,"Lallu"));
animalList.add(new Item("Elephant",R.drawable.elephant,"Jabardast"));
animalList.add(new Item("Dog",R.drawable.dog,"ItemDog"));
animalList.add(new Item("Cat",R.drawable.cat,"MeeMee"));
MyAdapter myAdapter=new MyAdapter(this,R.layout.list_view_items,animalList);
simpleList.setAdapter(myAdapter);
}
}
MyAdapter.Java
public class MyAdapter extends ArrayAdapter<Item> {
ArrayList<Item> animalList = new ArrayList<>();
public MyAdapter(Context context, int textViewResourceId, ArrayList<Item> objects) {
super(context, textViewResourceId, objects);
animalList = objects;
}
#Override
public int getCount() {
return super.getCount();
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
LayoutInflater inflater = (LayoutInflater) getContext().getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = inflater.inflate(R.layout.list_view_items, null);
TextView textView = (TextView) v.findViewById(R.id.textView);
ImageView imageView = (ImageView) v.findViewById(R.id.imageView);
TextView breedView = (TextView)v.findViewById(R.id.breed);
textView.setText(animalList.get(position).getAnimalName());
imageView.setImageResource(animalList.get(position).getAnimalImage());
breedView.setText(animalList.get(position).getBreed());
return v;
}
}
Item.Java
public class Item {
String animalName;
int animalImage;
String breedName;
public String getBreed() {
return breedName;
}
public void setBreed(String breed) {
this.breedName = breedName;
}
public Item(String animalName,int animalImage,String breedName)
{
this.animalImage=animalImage;
this.animalName=animalName;
this.breedName = breedName;
}
public String getAnimalName()
{
return animalName;
}
public int getAnimalImage()
{
return animalImage;
}
}
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<ListView
android:id="#+id/simpleListView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:divider="#000"
android:dividerHeight="2dp"/>
</RelativeLayout>
list_view_items.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
This is most likely a problem with the linear layout for each item. The orientation is horizontal, which lays each view one after the other horizontally. The problem is that the text view for the animal type has a width of fill_parent leaving no space for the breed view. If you change it to wrap_content it'll probably work for some of the cases, but might not work with everything.
In any case I think this is a problem with the views' positions and sizes. Try and move them around. Perhaps even a simple change would be placing them vertically:
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
</LinearLayout>
There's perhaps a more efficient way of doing this, but this is just an example. The inner linear layout places one text view on to of the other.
PS: your getCount method is not really doing anything. You can remove it.
replace your layout with my code your issue will resolve.
<?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="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/imageView"
android:layout_width="50dp"
android:layout_height="50dp"
android:padding="5dp" />
<TextView
android:id="#+id/textView"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="1"
android:text="Demo"
android:textColor="#000" />
<TextView
android:id="#+id/breed"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Breed"
android:textColor="#000" />
</LinearLayout>
I am trying to create an activity where the user can create some recipe and input the ingredients. every time the user enters an ingredient, it should be added to a listview. The issue I am having here is that the first input is the only one that is added to the listview not the others.
Here is the layout script:
<?xml version="1.0" encoding="utf-8"?><ScrollView
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=".Activities.AddActivity">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/recipe_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/number_of_servings" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/preparation_time" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/calories" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/ingredients"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/round_add_circle_outline_black_36dp"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
And the logic behind it :
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ingredients.add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
public class IngredientAdapter extends ArrayAdapter<String> {
ArrayList<String> mNewIngredients;
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
mNewIngredients = objects;
}
public String getItem(int position) {
return mNewIngredients.get(position).toString();
}
public View getView(int position, View convertView, ViewGroup parent) {
String item = getItem(position);
if(convertView==null)
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient,parent,false);
TextView recipeName= convertView.findViewById(R.id.new_added_ingredient);
recipeName.setText(item);
return convertView;
}
}
}
Try to scroll down your list view, maybe in your item's layout, you set the layout_height = match_parent so you do not see the other inputs.
You need to add android:fillViewport="true" to your ScrollView
Also change all child LinearLayout height to wrap_content instead of match_parent
Try this
<?xml version="1.0" encoding="utf-8"?><ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="226dp" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:orientation="vertical">
<EditText
android:id="#+id/Name_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/ServingsNbr_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/PrepTime_Et"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
<EditText
android:id="#+id/Calories_ET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="#string/app_name" />
</LinearLayout>
<LinearLayout
android:layout_marginLeft="8dp"
android:layout_marginRight="8dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/textView"
android:layout_width="match_parent"
android:layout_height="36dp"
android:text="#string/app_name"
android:textSize="26sp"
android:textAlignment="center"
android:textStyle="bold"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<EditText
android:id="#+id/new_ing"
android:layout_width="0dp"
android:layout_weight="9"
android:layout_height="36dp" />
<ImageView
android:id="#+id/add_ingredient_btn"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="36dp"
android:src="#drawable/ic_launcher_background"/>
</LinearLayout>
<ListView
android:id="#+id/ingredient_list"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
</LinearLayout>
</ScrollView>
In your implementation of IngredientAdapter, you are using your own ArrayList mNewIngredients for the data instead of the collection in the ArrayAdapter. When you add new data into ingredients, it is not added to the adapter.
To make use of the collection implemented in ArrayAdapter for simplicity. The following is an example, with a view holder so that views can be reused.
public class IngredientAdapter extends ArrayAdapter<String> {
public IngredientAdapter(Context context, ArrayList<String> objects) {
super(context, 0, objects);
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder = null;
if(convertView==null) {
convertView = LayoutInflater.from(getContext()).inflate(R.layout.ingredient, parent, false);
holder = new ViewHolder();
holder.recipeName = convertView.findViewById(R.id.new_added_ingredient);
convertView.Tag = holder;
} else {
holder = (ViewHolder)convertView.Tag;
}
String item = getItem(position);
holder.recipeName.setText(item);
return convertView;
}
}
public class ViewHolder {
public TextView recipeName;
}
The data in the adapter is now backed by the internal collection. And in the activity, you should add the new data into adapter directly.
public class AddActivity extends AppCompatActivity {
private ListView added_ing;
private IngredientAdapter adapter;
// keep this if you need it for something else
// private ArrayList<String> ingredients = new ArrayList<String>();
private EditText new_ing;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add);
adapter = new IngredientAdapter(AddActivity.this,ingredients);
added_ing = findViewById(R.id.ingredient_list);
added_ing.setAdapter(adapter);
new_ing = findViewById(R.id.new_ing);
ImageView add_ingredien = findViewById(R.id.add_ingredient_btn);
add_ingredien.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// keep this if you need it for something else
// ingredients.add(new_ing.getText().toString());
// to add adata into the adapter
adapter.Add(new_ing.getText().toString());
Toast.makeText(AddActivity.this, new_ing.getText().toString(), Toast.LENGTH_SHORT).show();
adapter.notifyDataSetChanged();
}
});
}
}
How can I delete the space when I hide a CardView? I doing an app with android and Firebase and I show the information from firebase in cardviews, but when the info isn't correct the cardview must disappear. The point is that the cardview disappear, but still uses a space in the layout, I've tried the answers in
setVisibility(GONE) view becomes invisible but still occupies space But it doesn't work for me.
#Override
protected void onStart() {
super.onStart();
firebaseAuth.addAuthStateListener(firebaseAuthListener);
final FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder> firebaseRecyclerAdapter =
new FirebaseRecyclerAdapter<CursosDB, MainActivity.CursosPViewHolder>(
CursosDB.class,
R.layout.design_row_cursos,
MainActivity.CursosPViewHolder.class,
myRef)
{
#Override
protected void populateViewHolder(MainActivity.CursosPViewHolder viewHolder, final CursosDB Cmodel, int position)
{
String estado = Cmodel.getSTATUS().toString();
if (estado.equals("OK")){
viewHolder.setPhotoURL(getApplicationContext(), Cmodel.getURI());
viewHolder.setTitle(Cmodel.getTITLE());
viewHolder.setCiudad(Cmodel.getPLACE());
viewHolder.setLevel(Cmodel.getLEVEL());
viewHolder.setDur(Cmodel.getDURACION());
viewHolder.setPrice(Cmodel.getCOSTO());
}else{
RecyclerView.LayoutParams layoutParams = (RecyclerView.LayoutParams) viewHolder.mView.getLayoutParams();
layoutParams.setMargins(0,0,0,0);
viewHolder.mView.setLayoutParams(layoutParams);
viewHolder.mView.setVisibility(View.INVISIBLE);
viewHolder.mView.setVisibility(View.GONE);
//viewHolder.mView.setVisibility(View.GONE);
//Toast.makeText(getApplicationContext(), "Ningún curso aprobado aún", Toast.LENGTH_SHORT).show();
}
viewHolder.mView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// Toast.makeText(getApplicationContext(), Cmodel.getUSERID(), Toast.LENGTH_LONG).show();
//Toast.makeText(getApplicationContext(), Cmodel.getTITLE(), Toast.LENGTH_SHORT).show();
//Toast.makeText(UsersList.this, user_key, Toast.LENGTH_LONG).show();
}
});
}
};
mCourses.setAdapter(firebaseRecyclerAdapter);
}
This is my layout:
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/cardview_cursos"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="4dp"
android:onClick="positionAction"
android:padding="4dp"
android:paddingBottom="4dp"
android:paddingEnd="4dp"
android:paddingStart="4dp"
android:paddingTop="4dp"
app:cardElevation="4dp">
<LinearLayout android:id="#+id/layout_to_hide"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/Cimg"
android:layout_width="match_parent"
android:layout_height="165dp"
android:scaleType="center"
app:srcCompat="#drawable/knowit_logo" />
<TextView
android:id="#+id/Ctitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/Cimg"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:paddingTop="8dp"
android:text="Titulo"
android:textStyle="bold" />
<TextView
android:id="#+id/Clugar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Ctitle"
android:layout_alignStart="#+id/Ctitle"
android:layout_below="#+id/Ctitle"
android:layout_marginTop="10dp"
android:text="Lugar" />
<TextView
android:id="#+id/textView29"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/Clugar"
android:layout_alignStart="#+id/Clugar"
android:layout_below="#+id/Clugar"
android:layout_marginTop="10dp"
android:text="$" />
<TextView
android:id="#+id/Cprice"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/textView29"
android:layout_marginBottom="4dp"
android:layout_toEndOf="#+id/textView29"
android:layout_toRightOf="#+id/textView29"
android:paddingLeft="5dp"
android:text="0.0" />
<TextView
android:id="#+id/Cdur"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Clugar"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginEnd="126dp"
android:layout_marginRight="126dp"
android:text="Duración" />
<TextView
android:id="#+id/Cnivel"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/Cprice"
android:layout_alignLeft="#+id/Cdur"
android:layout_alignStart="#+id/Cdur"
android:layout_marginBottom="0dp"
android:text="Nivel" />
</RelativeLayout>
</LinearLayout> </android.support.v7.widget.CardView>
This is the mView:
public static class CursosPViewHolder extends RecyclerView.ViewHolder {
View mView;
public CursosPViewHolder(final View itemView) {
super(itemView);
mView = itemView;
}
public void setTitle(String title){
TextView post_title = (TextView)mView.findViewById(R.id.Ctitle);
post_title.setText(title);
}
public void setCiudad (String ciudad){
TextView post_city = (TextView)mView.findViewById(R.id.Clugar);
post_city.setText(ciudad);
}
public void setPhotoURL(Context ctx, String pgotouserurl) {
ImageView post_image =(ImageView)mView.findViewById(R.id.Cimg);
Picasso.with(ctx).load(pgotouserurl).into(post_image);
} }
I hope you can help me.
First the XML code you've posted ils wrong closed,(must finish with a CardView tag)
On addition be sure you're pointing the CardView layout with mView field ans Only use GONE or INVISIBLE,
INVISIBLE change your Panel ( Root view ) structure but GONE don't
I have a customized ListView. Each row has an EditText, Buttons & TextView. In order to make the ListView items clickable I have kept android:descendantFocusability="blocksDescendants" for row layout. If I don't keep the descendantFocusability I am not able to implement an action for onItemClick. If I keep descendantFocusability the EditTextwhich is present in my row is not gaining focus. I want the EditText focusable and also I should be able to click on each row to navigate to another Activity. Can anyone please help me in this. Thanks all.
Edit : In the CustomAdapter for EditText, I tried keeping onTouchListenerand also onClickListenerwhere I requestFocus but that does not seem to work.
row.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"
android:id="#+id/recentrowLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="#dimen/margin_left_5"
android:clickable="true"
tools:ignore="UseCompoundDrawables,HardcodedText,ContentDescription,UselessParent" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#drawable/edit_text_bg"
android:padding="#dimen/margin_left_5" >
<RelativeLayout
android:id="#+id/rl1"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/addSubscribe"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true" />
<TextView
android:id="#+id/flikart_textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="FLIKART"
android:textColor="#color/gray"
android:textSize="#dimen/medium_text_size" />
<ImageView
android:id="#+id/addToFav"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true" />
</RelativeLayout>
<LinearLayout
android:id="#+id/rl2"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl1"
android:layout_marginLeft="#dimen/margin_30"
android:layout_marginRight="#dimen/margin_30"
android:layout_marginTop="10dp"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/text_desciption"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:lines="2"
android:text="20% off on Smart Phones and basic Handsets has upto 50% OFF only"
android:textColor="#color/blue" />
<TextView
android:id="#+id/text_desciption2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:lines="1"
android:text="somethign something,......."
android:textColor="#color/gray" />
<TextView
android:id="#+id/couponTypeText"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/margin_left_10"
android:background="#drawable/button_bg"
android:gravity="center"
android:padding="#dimen/margin_left_10"
android:text="STEAL THE DEAL"
android:textColor="#color/white"
android:textSize="#dimen/little_small_text_size" />
</LinearLayout>
<RelativeLayout
android:id="#+id/rl3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/rl2"
android:layout_marginTop="#dimen/margin_left_10" >
<TextView
android:id="#+id/text_offer_expiry"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginRight="#dimen/margin_left_5"
android:text="Ends 10 days"
android:textColor="#color/red"
android:textSize="16sp" />
<RelativeLayout
android:id="#+id/rightLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="5dp" >
<ImageView
android:id="#+id/comment_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_20"
android:background="#drawable/comments" />
<TextView
android:id="#+id/text_comments"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/comment_image_total"
android:text="100 Comments"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/ll2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="10dp"
android:layout_toLeftOf="#id/rightLayout"
android:layout_toRightOf="#id/text_offer_expiry"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/like_image_total"
android:layout_width="#dimen/dimenstion_25"
android:layout_height="#dimen/dimenstion_25"
android:background="#drawable/like" />
<TextView
android:id="#+id/text_total_likes"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/like_image_total"
android:text="999 Likes"
android:textColor="#color/gray"
android:textSize="16sp" />
</RelativeLayout>
</RelativeLayout>
<View
android:id="#+id/view"
android:layout_width="fill_parent"
android:layout_height="4dp"
android:layout_below="#id/rl3"
android:layout_marginTop="#dimen/margin_5"
android:background="#color/purple_clor" />
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="#dimen/title_bar_height"
android:layout_below="#id/view"
android:layout_centerVertical="true"
android:layout_marginTop="#dimen/margin_5" >
<RelativeLayout
android:id="#+id/likesLayout"
android:layout_width="#dimen/dimenstion_40"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:background="#drawable/unratedbkg" >
<Button
android:id="#+id/likesBtn"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/unrated" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/footermain"
android:layout_width="fill_parent"
android:layout_height="#dimen/dimenstion_40"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/likesLayout"
android:background="#color/white" >
<RelativeLayout
android:id="#+id/animLayout"
android:layout_width="100dp"
android:layout_height="#dimen/dimenstion_40"
android:layout_marginLeft="10dp"
android:layout_toRightOf="#id/likesLayout"
android:background="#drawable/ratingbkg"
android:visibility="gone" >
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/like_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/like" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_margin="#dimen/margin_5" >
<ImageView
android:id="#+id/dislike_image"
android:layout_width="#dimen/dimenstion_30"
android:layout_height="#dimen/dimenstion_30"
android:layout_centerInParent="true"
android:background="#drawable/dislike" />
</RelativeLayout>
</RelativeLayout>
<EditText
android:id="#+id/add_comment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_toRightOf="#id/animLayout"
android:focusable="false"
android:focusableInTouchMode="false"
android:hint="Add Comment"
android:inputType="text" />
</RelativeLayout>
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
please Don't use setOnItemClickListener for item click .. i think that you should be use item view click inside adapter method
convertView.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(context, "click item",Toast.LENGTH_LONG).show();
}
});
Remove this from main list item layout
android:descendantFocusability="blocksDescendants"
Thanks and enjoy this code !
Try to add this line to your activity in the manifest.xml
android:windowSoftInputMode="stateHidden|adjustResize|adjustPan"
you do like this:
first set in your listview's android:focusable="false";
then you want row to be clicked:
for this in your customAdapter you should do like this
v.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
Toast.makeText(context, "Hello world",2000).show();
}
});
It will work.
By default, your items should now have the click option, when you made android:focusable="false" in listview.
No need to use descendantFocusability in listview.
main.xml
<ListView
android:id="#+id/listView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:animationCache="false"
android:scrollingCache="false"
android:smoothScrollbar="true" >
</ListView>
Create ArrayList add data into the arraylist.
lv=(ListView)findViewById(R.id.listView1);
lv.setItemsCanFocus(true);
for(int i=0;i<30 data-blogger-escaped-br="" data-blogger-escaped-i=""> list.add(i);
}
1)create adapter for the listview and set the position as tag for the edittext.
2)Normally,when scrolling the item position will change.So,you have to get the edittext tag and set it into the edittext id.from that you can avoid the change of the item position.
holder.caption = (EditText) convertView
.findViewById(R.id.editText12);
holder.caption.setTag(position);
holder.caption.setText(list.get(position).toString());
convertView.setTag(holder);
}else {
holder = (ViewHolder) convertView.getTag();
}
int tag_position=(Integer) holder.caption.getTag();
holder.caption.setId(tag_position);
Finally,add the text watcher to the edittext and store the changes into correct position in the list.
holder.caption.addTextChangedListener(new TextWatcher() {
#Override
public void onTextChanged(CharSequence s, int start, int before,
int count) {
final int position2 = holder.caption.getId();
final EditText Caption = (EditText) holder.caption;
if(Caption.getText().toString().length()>0){
list.set(position2,Integer.parseInt(Caption.getText().toString()));
}else{
Toast.makeText(getApplicationContext(), "Please enter some value", Toast.LENGTH_SHORT).show();
}
}
#Override
public void beforeTextChanged(CharSequence s, int start, int count,
int after) {
// TODO Auto-generated method stub
}
#Override
public void afterTextChanged(Editable s) {
}
});
Refer this link..http://velmuruganandroidcoding.blogspot.in/2014/08/edittext-in-listview-android-example.html
main.xml
<?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">
<ListView android:id="#+id/MyList" android:layout_height="fill_parent"
android:layout_width="fill_parent" android:descendantFocusability="beforeDescendants">
</ListView>
</LinearLayout>
item.xml
<?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">
<EditText android:id="#+id/ItemCaption"
android:layout_height="wrap_content" android:layout_width="fill_parent"
android:layout_marginLeft="2dip" android:singleLine="true">
</EditText>
</LinearLayout>
AndroidCustomListViewActivity
public class AndroidCustomListViewActivity extends Activity {
private ListView myList;
private MyAdapter myAdapter;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
myList = (ListView) findViewById(R.id.MyList);
myList.setItemsCanFocus(true);
myAdapter = new MyAdapter();
myList.setAdapter(myAdapter);
}
public class MyAdapter extends BaseAdapter {
private LayoutInflater mInflater;
public ArrayList myItems = new ArrayList();
public MyAdapter() {
mInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
for (int i = 0; i < 20; i++) {
ListItem listItem = new ListItem();
listItem.caption = "Caption" + i;
myItems.add(listItem);
}
notifyDataSetChanged();
}
public int getCount() {
return myItems.size();
}
public Object getItem(int position) {
return position;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
holder = new ViewHolder();
convertView = mInflater.inflate(R.layout.item, null);
holder.caption = (EditText) convertView
.findViewById(R.id.ItemCaption);
convertView.setTag(holder);
} else {
holder = (ViewHolder) convertView.getTag();
}
//Fill EditText with the value you have in data source
holder.caption.setText(myItems.get(position).caption);
holder.caption.setId(position);
//we need to update adapter once we finish with editing
holder.caption.setOnFocusChangeListener(new OnFocusChangeListener() {
public void onFocusChange(View v, boolean hasFocus) {
if (!hasFocus){
final int position = v.getId();
final EditText Caption = (EditText) v;
myItems.get(position).caption = Caption.getText().toString();
}
}
});
return convertView;
}
}
class ViewHolder {
EditText caption;
}
class ListItem {
String caption;
}
}