I have made custom listview in android activity which has progressbar. On top of that listview there is another progress bar which shows progress as day passes in the month. Now i want to set a bar according to the progress of the month progress bar in custom listview which has four progress bars. Please see the attached image.
The progress of the month progress bar and the bar which we put in listview progress bar should be at same position means aligned and this bar which we put should move when the top progress increases as day passes.
In Layout file I have used LinearLayout where I want to put the bar.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent" android:layout_height="wrap_content"
android:layout_marginTop="5dip">
<RelativeLayout android:id="#+id/Top"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content">
<TextView android:id="#+id/LeftText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_alignParentLeft="true" />
<RelativeLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_alignParentRight="true">
<TextView android:id="#+id/TextOne" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_marginRight="5dip" />
<TextView android:id="#+id/TextTwo" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="#string/outof"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextOne" android:layout_marginRight="5dip" />
<TextView android:id="#+id/RightText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="O"
android:textColor="#FFFFFF" android:textStyle="normal"
android:layout_toRightOf="#+id/TextTwo" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout android:id="#+id/Bottom"
android:layout_below="#+id/Top" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_centerHorizontal="true">
<ProgressBar android:id="#+id/CustomPro"
android:layout_width="250dip" android:layout_height="wrap_content"
android:progress="0" android:max="100" android:secondaryProgress="0"
style="?android:attr/progressBarStyleHorizontal"
android:progressDrawable="#drawable/accountdataprogress"
android:layout_centerHorizontal="true" />
<TextView android:id="#+id/MiddleText" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:text="0%"
android:textColor="#000000" android:textStyle="bold"
android:layout_centerHorizontal="true" />
</RelativeLayout>
<RelativeLayout android:id="#+id/Middle"
android:layout_centerHorizontal="true" android:layout_width="250dip"
android:layout_height="wrap_content" android:layout_below="#+id/Top">
<LinearLayout android:layout_width="wrap_content"
android:layout_height="wrap_content" android:id="#+id/PuttingBar"
android:layout_alignParentLeft="true">
</LinearLayout>
</RelativeLayout>
<View android:layout_width="fill_parent" android:layout_height="10dip"
android:background="#00000000" android:layout_below="#+id/Bottom" />
</RelativeLayout>
In Java I have done something like this.
import java.util.ArrayList;
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ProgressBar;
import android.widget.TextView;
public class ProgressListAdapter extends BaseAdapter
{
private Context mContext;
private ArrayList<ProgressList> mPList;
private TextView mLeft, mMiddle, mRight, mTextOne;
private ProgressBar mCustomProgressBar;
private LinearLayout mLinearLayout;
public ProgressListAdapter(Context nContext, ArrayList<ProgressList> nPList)
{
this.mContext = nContext;
this.mPList = nPList;
}
#Override
public int getCount()
{
return mPList.size();
}
#Override
public Object getItem(int nPosition)
{
return mPList.get(nPosition);
}
#Override
public long getItemId(int nPosition)
{
return nPosition;
}
#Override
public View getView(int position, View convertView, ViewGroup parent)
{
ProgressList mEntry = mPList.get(position);
if (convertView == null)
{
LayoutInflater mInflater = (LayoutInflater) mContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.customprogress, null);
}
mLeft = (TextView) convertView.findViewById(R.id.LeftText);
mLeft.setText(mEntry.getValueText());
mMiddle = (TextView) convertView.findViewById(R.id.MiddleText);
mCustomProgressBar = (ProgressBar) convertView.findViewById(R.id.CustomPro);
int mBarPosition = Usage.mElapsed;
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
View mView = new View(mContext);
LinearLayout.LayoutParams layoutParams = new LinearLayout.LayoutParams(2, 25);
layoutParams.setMargins(mBarPosition, 0, 0, 0);
mView.setMinimumHeight(25);
mView.setMinimumWidth(2);
mView.setBackgroundColor(Color.rgb(12, 56, 99));
mLinearLayout.addView(mView, layoutParams);
mView.invalidate();
if(mEntry.getValueNumber() > mBarPosition)
{
mCustomProgressBar.setProgress(mBarPosition);
mCustomProgressBar.setSecondaryProgress(mEntry.getValueNumber());
}
else if(mEntry.getValueNumber() <= mBarPosition)
{
mCustomProgressBar.setProgress((mEntry.getValueNumber()));
mCustomProgressBar.setSecondaryProgress(0);
}
mMiddle.setText(Integer.toString(mEntry.getValueNumber()) + "%");
mTextOne = (TextView) convertView.findViewById(R.id.TextOne);
mTextOne.setText(mEntry.getValue());
mRight = (TextView) convertView.findViewById(R.id.RightText);
mRight.setText(mEntry.getValueOne());
return convertView;
}
}
But When I run It shows extra bar in progress bar like this
Anybody has done kind of work please give me your hand. Waiting for fruitful reply.
Thanks.
You should add call to removeAllViews in your getView funcion:
mLinearLayout = (LinearLayout) convertView.findViewById(R.id.PuttingBar);
mLinearLayout.removeAllViews();
View mView = new View(mContext);
That's because LinearLayout accumulates views, and there might be multiple calls to your addView function. So you need to remove already added view before adding another PuttingBar with new margin.
Related
I'm working on a similiar problem developing my chat and I was checking on how whatsapp solves it to get an idea to work on it.
On mobile and inside a conversation, when your keyboard is up on the screen and you click on attachments, the attachments window overrides the keboard like the image below. It also look likes the activity under it freezes ( you can look at the slash "|" on the EditText, it seems that is freeze ).
I was wondering how WP could override the keyboard and why it looks like the activity under it is freezing, thanks in advance!
/Use the below code Snipet without using any library/
action_attach.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
openDialog();
}
});
private void openDialog() {
LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
// inflate the custom popup layout
final View inflatedView;
inflatedView = layoutInflater.inflate(R.layout.custom_dialog_options_menu, null, false);
LinearLayout layoutGallery;
layoutGallery = (LinearLayout) inflatedView.findViewById(R.id.layoutGallery);
layoutGallery.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
FloatingView.dismissWindow();
}
});
FloatingView.onShowPopup(this, inflatedView);
}
/**Sample Layout for Pop Up window ie: custom_dialog_options_menu
and change layout according to your choice***/
<?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="vertical"
android:paddingTop="82dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFF"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layoutGallery"
android:layout_width="0dp"
android:layout_marginTop="18dp"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_weight="1"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/gallery_attach_icon_selector"/>
<TextView
android:id="#+id/tvGallery"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Gallery"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutPhoto"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/camera_attach_icon_selector"/>
<TextView
android:id="#+id/tvPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Camera"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
<LinearLayout
android:id="#+id/layoutVideo"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginRight="40dp"
android:layout_weight="1"
android:layout_marginTop="18dp"
android:orientation="vertical">
<ImageView
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_gravity="center"
android:src="#drawable/video_attach_icon_selector"/>
<TextView
android:id="#+id/tvVideo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:text="Video"
android:layout_marginTop="10dp"
android:textSize="16sp"
android:textColor="#4d4747"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
/**FloatingView Class**/
public class FloatingView
{
/* Floating view is used to display a custom view for attachments in the chat screen */
import android.app.Activity;
import android.graphics.Point;
import android.view.Display;
import android.view.Gravity;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.PopupWindow;
private static PopupWindow popWindow;
private FloatingView() {
}
public static void onShowPopup(Activity activity, View inflatedView) {
// get device size
Display display = activity.getWindowManager().getDefaultDisplay();
final Point size = new Point();
display.getSize(size);
// fill the data to the list items
// set height depends on the device size
popWindow = new PopupWindow(inflatedView, size.x, ViewGroup.LayoutParams.WRAP_CONTENT,
true);
// set a background drawable with rounders corners
popWindow.setBackgroundDrawable(activity.getResources().getDrawable(
R.drawable.comment_popup_bg));
// make it focusable to show the keyboard to enter in `EditText`
popWindow.setFocusable(true);
// make it outside touchable to dismiss the popup window
popWindow.setOutsideTouchable(true);
popWindow.setSoftInputMode(WindowManager.LayoutParams.SOFT_INPUT_ADJUST_PAN);
// show the popup at bottom of the screen and set some margin at
// bottom ie,
popWindow.showAtLocation(activity.getCurrentFocus(), Gravity.BOTTOM, 0,
0);
}
public static void dismissWindow() {
popWindow.dismiss();
}
I have a list view that for each item needs to display an image, a book title, the author and the price. Under normal circumstances my output is fine and looks like the following
My client has a large portion of customers that need to set the font size to huge. When this is done and the authors name is very long my output looks like below
This is not right and I would like it to look like this instead
In other words the height of list view and elements inside grow to fit larger amounts of text.
Here is the code which replicates my problem.
The book class
public class Book {
public String name;
public String author;
public String price;
public Book(String name, String author, String price){
this.name = name;
this.author = author;
this.price = price;
}
}
The main activity code
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ListView;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
ListView listView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
listView = (ListView) findViewById(R.id.list);
ArrayList<Book> bookList = new ArrayList<Book>();
bookList.add(new Book("Book 1", "By Christopher Christopheson", "£0.50"));
bookList.add(new Book("Book 2", "Author 2", "£0.75"));
BookAdapter adapter = new BookAdapter(this, R.layout.row, bookList);
listView.setAdapter(adapter);
}
}
The book list view adapter
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import java.util.ArrayList;
import android.widget.TextView;
public class BookAdapter extends ArrayAdapter<Book> {
private ArrayList<Book> items;
private LayoutInflater lInflater;
public BookAdapter(Context context, int textViewResourceId, ArrayList<Book> items){
super(context, textViewResourceId, items);
lInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
this.items = items;
}
#Override
public View getView(int position, View convertView, ViewGroup parent){
View v = convertView;
if(v == null){
v = lInflater.inflate(R.layout.row, null);
}
Book b = items.get(position);
if(b!= null){
TextView bookName = (TextView)v.findViewById(R.id.book_name);
TextView bookPrice = (TextView)v.findViewById(R.id.book_price);
TextView bookAuthor = (TextView)v.findViewById(R.id.book_author);
if(bookName != null){
bookName.setText(b.name);
}
if(bookPrice != null){
bookPrice.setText(b.price);
}
if(bookAuthor != null){
bookAuthor.setText(b.author);
}
}
return v;
}
}
activity_main.xml
<?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="barnetttabs.com.basiclistview.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
android:orientation="vertical" >
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:layout_width="match_parent">
</ListView>
</LinearLayout>
</android.support.constraint.ConstraintLayout>
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="wrap_content"
android:minHeight="150dp"
android:paddingTop="4dp"
android:paddingBottom="4dp"
>
<TextView
android:id="#+id/price_background"
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_alignParentTop="true"
android:background="#0000ff"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:minHeight="150dp"
android:layout_toLeftOf="#id/price_background"
android:layout_alignParentLeft="true"
android:gravity="top"
android:background="#d3d3d3"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:paddingLeft="110dp"
android:paddingRight="4dp"
/>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:src="#drawable/lvplaceholderimage"
/>
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#id/book_image"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignLeft="#id/price_background"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true"
android:layout_below="#id/book_price"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
I've tried fixing the layout_height of the relative layout in row.xml. This keeps the height consistent but when the author is too long some of the text is hidden. When I've searched for solutions on this most things say the solution is to set height as wrap_content which I have done but it doesn't the problem.
Is there a way to fix this through either changing the xml layout or the java code? Maybe the relative layout I've used here in row.xml is not the right approach?
Chetan's suggestion was the way to go. The basic idea is to change from a relative layout to a horizontal linear layout. The middle portion is given weight 0 and the 2 end bits have their lengths set as before. The only drawback is the method needs to have a nested layout to get the blue bit to work but hopefully not too much of a performance hit.
In activity_main.xml change the listview code to
<ListView
android:id="#+id/list"
android:layout_height="wrap_content"
android:divider="#android:color/transparent"
android:dividerHeight="5.0sp"
android:layout_width="match_parent">
</ListView>
Change row.xml to
<?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:minHeight="150dp"
android:layout_marginTop="8dp"
android:layout_marginBottom="8dp"
android:orientation="horizontal"
android:background="#d3d3d3"
>
<ImageView
android:id="#+id/book_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/lvplaceholderimage"
android:layout_gravity="top"
/>
<TextView
android:id="#+id/book_name"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_gravity="top"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:layout_weight="1"
/>
<RelativeLayout
android:layout_width="150dp"
android:layout_height="wrap_content"
android:minHeight="150dp"
android:background="#0000ff">
<TextView
android:id="#+id/book_price"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_alignParentTop="true"
android:textColor="#fff"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingTop="4dp"
/>
<TextView
android:id="#+id/book_author"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17sp"
android:layout_below="#id/book_price"
android:layout_alignParentBottom="true"
android:paddingLeft="4dp"
android:paddingRight="4dp"
android:paddingBottom="4dp"
/>
</RelativeLayout>
</LinearLayout>
Then it looks like this
Here is the layout when each Button has the same size text:
And here is the layout when I increase the size of the bottom right Button's text:
What's going on here? Why is the "0" Button moving lower than the two Buttons adjacent to it?
Here is the layout:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="6dp">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColorHint="#color/hint_color"
android:singleLine="true"
android:textSize="40sp"/>
<GridLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:columnCount="3">
<Button
android:id="#+id/the_1_button"
android:background="#android:color/white"
android:text="1"/>
<Button
android:id="#+id/the_2_button"
android:background="#android:color/white"
android:text="2"/>
<Button
android:id="#+id/the_3_button"
android:background="#android:color/white"
android:text="3"/>
<Button
android:id="#+id/the_4_button"
android:background="#android:color/white"
android:text="4"/>
<Button
android:id="#+id/the_5_button"
android:background="#android:color/white"
android:text="5"/>
<Button
android:id="#+id/the_6_button"
android:background="#android:color/white"
android:text="6"/>
<Button
android:id="#+id/the_7_button"
android:background="#android:color/white"
android:text="7"/>
<Button
android:id="#+id/the_8_button"
android:background="#android:color/white"
android:text="8"/>
<Button
android:id="#+id/the_9_button"
android:background="#android:color/white"
android:text="9"/>
<ImageButton
android:id="#+id/the_backspace_button"
style="#style/Widget.AppCompat.Button"
android:background="#android:color/white"
android:src="#drawable/ic_backspace"/>
<Button
android:id="#+id/the_0_button"
android:background="#android:color/white"
android:text="0"/>
<Button
android:id="#+id/the_done_button"
android:layout_width="88dp"
android:layout_height="48dp"
android:minWidth="0dp"
android:minHeight="0dp"
android:background="#android:color/white"
android:text="done"/>
</GridLayout>
</LinearLayout>
Update:
I made the text of the "DONE" Button super large -- 100sp -- and set maxWidth and maxHeight for it equal to the height and width of the other Buttons. Here is the result:
*the blue is the GridLayout's background, the red the "0" Button's, and the yellow the "DONE" Button's
Why would changing the size of the text of the "DONE" Button affect anything other than that specific Button if the size of the Button never changes?
I think there is a problem with the done button. You set the background of it. Then you also set its text as "done" .
Just set it text empty and the problem may resolve ;)
What about using the grid:layout_rowWeight or grid:layout_rowHeight attributes to be the same for every button?
Look at this:
https://stackoverflow.com/a/32291319/2205825
Try this .....
Use GridView instead of many Button that not give you any Layout problem (Adjustment of Views).
Have look on the below example that solve your problem about Adjustment of Buttons
use this layout instead of your 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"
android:padding="6dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1">
<TextView
android:id="#+id/input_textview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:singleLine="true"
android:textSize="40sp" />
</LinearLayout>
<GridView
android:id="#+id/gridview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:columnWidth="100dp"
android:gravity="center"
android:numColumns="3" />
</LinearLayout>
add these 2 line in your OnCreate method .....
GridView gridview = (GridView) findViewById(R.id.gridview);
gridview.setAdapter(new CustomAdapter(this));
add this adapter in your project .....
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.Button;
import android.widget.ImageButton;
/**
* Created by sushildlh on 10/14/16.
*/
public class CustomAdapter extends BaseAdapter {
private Context mContext;
// Keep all Images in array
public String[] text = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "", "0", "DONE"
};
// Constructor
public CustomAdapter(Context c) {
mContext = c;
}
public int getCount() {
return 12;
}
public Object getItem(int position) {
return null;
}
public long getItemId(int position) {
return 0;
}
// create a new textView for each item referenced by the Adapter
public View getView(int position, View convertView, ViewGroup parent) {
Holder holder = null;
if (holder == null) {
LayoutInflater inflater = ((Activity) mContext).getLayoutInflater();
convertView = inflater.inflate(R.layout.item, parent, false);
holder = new Holder();
holder.button = (Button) convertView.findViewById(R.id.button);
holder.image = (ImageButton) convertView.findViewById(R.id.image);
convertView.setTag(holder);
} else {
holder = (Holder) convertView.getTag();
}
if (position != 9) {
holder.image.setVisibility(View.GONE);
holder.button.setText(text[position]);
} else {
holder.image.setImageResource(R.drawable.ic_backspace_black_24dp); //change ic_backspace_black_24dp into to your image ......
holder.button.setVisibility(View.GONE);
}
return convertView;
}
public static class Holder {
Button button;
ImageButton image;
}
}
and this is item.xml layout into your layout folder ....
<?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">
<Button
android:id="#+id/button"
android:layout_width="100dp"
android:layout_height="60dp"
android:gravity="center"
android:background="#null"
android:textSize="25dp"/>
<ImageButton
android:id="#+id/image"
android:layout_width="100dp"
android:layout_height="60dp"
android:focusable="false"
android:background="#null"
android:gravity="center" />
</LinearLayout>
above code give you the output like this ....
Note:- Always use GridView OR ListView for same pattern of Views .....
You can use the android:layout_weight property.
It appears that the GridLayout is reserving enough height for the actual text size and some default padding despite the layout_height for the Done button.
Build it with 'Linear Layouts' instead.
Was facing the same problem, so just i did was to change the height and width of the button to it parent RowTable's match parent & wrap content and it solved my problem
I'm trying to implement a ListView with an embedded Button. I've made my list item view separately and followed this tutorial.
When I try running it, I get an NPE on the button's setOnClickListener because it says it's a null reference. Kindly point out where I've gone wrong or if I need to do anything else.
Here's my code:
Main activity:
<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=".Data">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="#+id/scrollView"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp" >
<LinearLayout
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:layout_width="319dp"
android:layout_height="232dp"
android:id="#+id/imageView"
android:src="#drawable/image" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ems="3"
android:id="#+id/editText"
android:autoText="false"
android:inputType="number"
android:layout_marginTop="30dp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Enter value"
android:id="#+id/textView"
android:textSize="24dp"
android:layout_alignBottom="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<Button
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:id="#+id/button"
android:layout_alignBottom="#+id/editText"
android:layout_toRightOf="#+id/editText"
android:layout_toEndOf="#+id/editText"
android:layout_marginLeft="37dp"
android:layout_marginStart="37dp"
android:onClick="bringList"
android:clickable="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select reason:"
android:id="#+id/textView2"
android:textSize="24dp"
android:layout_below="#+id/editText"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<ListView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/listView"
android:layout_below="#+id/textView2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</LinearLayout>
</ScrollView>
List Item:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Text"
android:id="#+id/listitem"
android:textSize="24dp"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="38dp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Info"
android:id="#+id/listbutton"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignBottom="#+id/listitem"
android:textSize="15dp" />
Java code:
package com.example.abhinav.data;
import android.content.Context;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;
import java.util.ArrayList;
import java.util.List;
public class Data extends ActionBarActivity {
private ArrayList<String> data = new ArrayList<String>();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_data);
ListView lv = (ListView)findViewById(R.id.listView);
makeList();
lv.setAdapter(new MyListAdapter(this, R.layout.list, data));
}
private void makeList() {
for(int i = 1; i<6; i++) {
data.add("Theorem"+i);
}
}
public void bringList() {
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_data, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
private class MyListAdapter extends ArrayAdapter<String> {
private int listlayout;
public MyListAdapter(Context context, int resource, List<String> objects) {
super(context, resource, objects);
listlayout = resource;
}
#Override
public View getView(int position, View convertView, ViewGroup parent) {
ViewHolder vw = null;
if(convertView == null) {
LayoutInflater inflater = LayoutInflater.from(getContext());
convertView = inflater.inflate(listlayout, parent, false);
ViewHolder viewHolder = new ViewHolder();
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
viewHolder.button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//pop up dialog with theorem info
Toast.makeText(getContext(), "Button presss", Toast.LENGTH_SHORT).show();
}
});
convertView.setTag(viewHolder);
}
else {
vw = (ViewHolder) convertView.getTag();
vw.title.setText(getItem(position));
}
return convertView;
}
}
public class ViewHolder {
TextView title;
Button button;
}
}
Change this Lines
viewHolder.title = (TextView) findViewById(R.id.listitem);
viewHolder.button = (Button) findViewById(R.id.listbutton);
to
viewHolder.title = (TextView) convertView.findViewById(R.id.listitem);
viewHolder.button = (Button) convertView.findViewById(R.id.listbutton);
NPE is because you reading view incorrectly.
It seems to me that your List Item might only inflate the TextView element, because it's the actuall root element for the XML.
I recommend wrapping both the TextView and the Button in your list.xml file with a LinearLayout so the actual root view will wrap all your views.
Here's an example of what stacking both views vertically would look like with a LinearLayout for a ListView item.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/listitem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="38dp"
android:text="New Text"
android:textSize="24dp" />
<Button
android:id="#+id/listbutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/listitem"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:text="Info"
android:textSize="15dp" />
</LinearLayout>
I don't know if this is the case, but you are creating two different ViewHolders. Use vw = new ViewHolder();
And maybe you need to use convertView.findViewById(R.id.listbutton);
This is where I am stuck now. I am stuck at dynamically adding buttons to gridview.
- My gridview stars with a one button.
- When user clicks that button a context menu , pops up, asking user to enter info, once that is done. A block in the grid view is created with that info
- This is shown in the picture
I have pasted the code. I do not have clear idea on how to do it. I am assuming it will be inflating a new view and adding it to parent(layout or grid ?) view. I do not have much idea on how to code it. I have tried many things from google. I thought I could start from simple image gridview and modify it to my needs but it did not work.
Please provide some directions.
create_team_new.xml(layout where gridview resides)
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
android:id="#+id/parentcreateteam"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:background="#drawable/background">
<TextView
android:id="#+id/citytextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/city"
android:layout_alignTop="#+id/teamcity"
android:layout_alignParentLeft="true" />
<AutoCompleteTextView
android:id="#+id/teamname"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/teamcity"
android:layout_alignRight="#+id/gridviewplayer"
android:text="#string/teamname"
android:textSize="18sp" >
<requestFocus />
</AutoCompleteTextView>
<Button
android:id="#+id/createteam"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:text="Create Team"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:background="#drawable/red_button"
style="#+style/button_text" />
<TextView
android:id="#+id/teamnametextview"
android:layout_width="wrap_content"
android:layout_height="22dp"
android:layout_alignBaseline="#+id/teamname"
android:layout_alignBottom="#+id/teamname"
android:layout_alignParentLeft="true"
android:text="#string/teamname" />
<AutoCompleteTextView
android:id="#+id/teamcity"
android:layout_width="244dp"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/cricket"
android:layout_alignRight="#+id/gridviewplayer"
android:layout_below="#+id/teamname"
android:layout_marginTop="22dp"
android:ems="10"
android:text="#string/city"
android:textSize="18sp" >
</AutoCompleteTextView>
<TextView
android:id="#+id/radiotextview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/teamcity"
android:layout_marginTop="15dp"
android:text="Pick a Sport" />
<GridView
android:id="#+id/gridviewplayer"
android:layout_width="match_parent"
android:layout_height="170dp"
android:layout_above="#+id/createteam"
android:layout_below="#+id/pick_sport"
android:layout_centerHorizontal="true"
android:background="#drawable/gv_bkg"
android:padding="5dp"
android:numColumns="4" >
</GridView>
<RadioGroup
android:id="#+id/pick_sport"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/createteam"
android:layout_below="#+id/radiotextview"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/cricket"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Cricket" />
<RadioButton
android:id="#+id/soccer"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Soccer" />
</RadioGroup>
</RelativeLayout>
gv_createplayer.xml(view to be inflated inside gridview)
<?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"
>
<Button android:id="#+id/btn_player"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text= "+"
android:background="#drawable/red_button">
</Button>
</LinearLayout>
PlayerAdapter.java(gridview Adapter)
import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.GridView;
import android.widget.ImageView;
public class PlayerAdapter extends BaseAdapter {
private Context mContext;
LayoutInflater inflater;
// Keep all Images in array
public Integer[] mThumbIds = {};
/* R.drawable.gridview_createteam, R.drawable.pic2,
R.drawable.pic3, R.drawable.pic4,
R.drawable.pic5, R.drawable.pic6,
R.drawable.pic7, R.drawable.pic8,
R.drawable.pic9, R.drawable.pic10,
R.drawable.pic11, R.drawable.pic12,
R.drawable.pic13, R.drawable.pic14,
R.drawable.pic15
};
*/
// Constructor
public PlayerAdapter(Context c){
mContext = c;
}
#Override
public int getCount() {
return mThumbIds.length;
}
#Override
public Object getItem(int position) {
return mThumbIds[position];
}
#Override
public long getItemId(int position) {
return 0;
}
#SuppressWarnings("static-access")
#Override
public View getView(int position, View convertView, ViewGroup parent) {
View view = new View(mContext);
view.inflate(mContext, R.layout.gv_createplayer,null);
return view;
}
}
1) How to create a list view inside a main view, only using a half of the screen.
This is done by the weight attribute. A rough example for your sketch: The top wrapper (edittexts and buttons) has a weight of .45, the listview has a weight of .45 and the footer has a weight of .1. You can specify the weight-sum in the surrounding view; but default should be 1.
2) How to populate list view in grid manner (a row of two columns will repeat itself)
Well, with the GridView (the adapter API is the same) (: