Setting ScrollView background colour programmatically - android

I am fairly new to Android.
While using below code to set background colour for ScrollView I am getting "java.lang.NullPointerException: Attempt to invoke virtual method 'void android.view.View.setBackgroundColor(int)' on a null object reference"
ScrollView scroll = (ScrollView) mRootView.findViewById(R.id.content_scroll);
scroll.setBackgroundColor(color);
However when I use it with TextView it works fine.
int color = ContextCompat.getColor(mContext,mSetColor);
TextView textContent = (TextView) mRootView.findViewById(R.id.content);
textContent.setText(mCurrentContent.getmTextContent());
textContent.setBackgroundColor(color);
textContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
Below is the complete code, seems like I am missing on something basic.
package com.example.android.tourguide;
import android.content.Context;
import android.support.v4.content.ContextCompat;
import android.util.TypedValue;
import android.view.View;
import android.widget.ImageView;
import android.widget.ScrollView;
import android.widget.TextView;
public class ContentAdapter {
private Content mCurrentContent;
public View mRootView;
private int mSetColor;
private Context mContext;
public ContentAdapter(Content content, View rootView, int setColor,Context context) {
mRootView = rootView;
mCurrentContent = content;
mSetColor = setColor;
mContext= context;
}
public View setContent() {
int color = ContextCompat.getColor(mContext,mSetColor);
ImageView imageView = (ImageView) mRootView.findViewById(R.id.image);
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
imageView.setImageResource(mCurrentContent.getmImageResourceID());
TextView imageText = (TextView) mRootView.findViewById(R.id.image_description);
imageText.setText(mCurrentContent.getmImageDescriptionText());
TextView textContent = (TextView) mRootView.findViewById(R.id.content);
textContent.setText(mCurrentContent.getmTextContent());
textContent.setBackgroundColor(color);
textContent.setTextSize(TypedValue.COMPLEX_UNIT_SP, 20);
ScrollView scrollView = (ScrollView) mRootView.findViewById(R.id.content_scroll);
scrollView.setBackgroundColor(color);
return mRootView;
}
}
Complete XML Code.
<?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/contentView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/primary_light"
android:orientation="vertical">
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="4"
android:adjustViewBounds="true" />
<TextView
android:id="#+id/image_description"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:gravity="center_vertical"
android:text="abc"
android:textAlignment="center"
android:textAppearance="#android:style/TextAppearance.Material.Display1"
tools:targetApi="lollipop" />
<View
android:layout_width="fill_parent"
android:layout_height="2dp"
android:background="#c0c0c0" />
<ScrollView
android:id="#+id/content_scroll"
android:layout_width="match_parent"
android:layout_weight="8"
android:layout_height="0dp"
android:background="#color/colorAccent"
>
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:text="abc"
android:textAppearance="#android:style/TextAppearance.Material.Small"
tools:targetApi="lollipop"
/>
</ScrollView>
</LinearLayout>
Output
Need to fill the white space below green background

If i were you when i use ScrollView, my layout xml would be like this:
<ScrollView
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="8"
android:id="#+id/content_scroll"
>
<LinearLayout
android:id="#+id/layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:id="#+id/content"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="fill"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:textAppearance="#android:style/TextAppearance.Material.Small"
tools:targetApi="lollipop"
android:text="abc"
/>
</LinearLayout>
</ScrollView>
ScrollView must has a child layout. Then i will set the color on the LinearLayout for your case:
layout.setBackgroundColor(your color);

public ContentAdapter(Content content, View rootView, int setColor,Context context) {mSetColor = setColor; }
Cheak once you are sending color from Activity to ContentAdapter Constructor set color is getting null so you get nullpointerException

Related

Horizontal Recyclerview not scrolling properly

I have a fragment layout that is a part of a PageViewer.
The fragment has 2 RecyclerViews - one on the top of the layout which is horizontal, the other one at the bottom which is vertical.
Here is my XML :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="7dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:text="#string/fragment_marketplace_marketplace_title"
android:textSize="30sp"
android:textStyle="bold" />
<SearchView
android:id="#+id/fragment_marketplace_searchview"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:queryHint="Search..."
app:iconifiedByDefault="false"
tools:layout_editor_absoluteX="1dp"
tools:layout_editor_absoluteY="1dp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:layout_marginTop="15dp"
android:text="#string/fragment_marketplace_discover_products_from_myverte"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment_marketplace_brands_recycler_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="15dp"
android:orientation="horizontal"
app:layoutManager="android.support.v7.widget.LinearLayoutManager"
tools:listitem="#layout/fragment_marketplace_vendor_row_item" />
<android.support.v4.widget.NestedScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginStart="14dp"
android:layout_marginLeft="14dp"
android:background="#color/very_light_grey"
android:paddingTop="15dp"
android:text="#string/fragment_marketplace_featured_products"
android:textSize="17sp"
android:textStyle="bold" />
<android.support.v7.widget.RecyclerView
android:id="#+id/fragment_marketplace_products_recycler_view"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:background="#color/very_light_grey"
tools:listitem="#layout/fragment_marketplace_products_row_item" />
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</LinearLayout>
1) When scrolling left/right the scrolling sometimes gets stuck and not responsive. Why does this happen?
2) How do I make the view shows some part of the next views in the list, so it will make the filling of an actual scrollable list and not just a stale image?
edit -
here is my row item 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"
android:layout_width="match_parent"
android:layout_height="200dp"
android:padding="10dp">
<ImageView
android:id="#+id/vendorImageview"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="#mipmap/ic_launcher"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.5"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</android.support.constraint.ConstraintLayout>
edit -
my recyclerview initing -
private void initViews(View view) {
gson = new Gson();
miniVendorModelList = new ArrayList<>();
miniProductModelList = new ArrayList<>();
searchView = view.findViewById(R.id.fragment_marketplace_searchview);
Drawable drawable = getResources().getDrawable(R.drawable.search_widget_very_light_grey_background);
searchView.setBackground(drawable);
//adapters
vendorsAdapter = new VendorAdapter(miniVendorModelList);
productsAdapter = new ProductsAdapter(miniProductModelList, getContext());
//lists
vendorsList = view.findViewById(R.id.fragment_marketplace_brands_recycler_view);
productsList = view.findViewById(R.id.fragment_marketplace_products_recycler_view);
vendorsList.setNestedScrollingEnabled(false);
productsList.setNestedScrollingEnabled(false);
//brands recycler
vendorsList.setHasFixedSize(true);
vendorsList.setLayoutManager(new LinearLayoutManager(getContext(),LinearLayoutManager.HORIZONTAL, false));
vendorsList.setAdapter(vendorsAdapter);
//products recycler
productsList.setLayoutManager(new GridLayoutManager(getContext(), 2));
productsList.setHasFixedSize(true);
productsList.setAdapter(productsAdapter);
}
my adapter -
package com.twoverte.adapters;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import com.twoverte.R;
import com.twoverte.adapters.holder.VendorsHolder;
import com.twoverte.models.Vendor.MiniVendorModel;
import java.util.ArrayList;
public class VendorAdapter extends RecyclerView.Adapter<VendorsHolder> {
private ArrayList<MiniVendorModel> miniVendorModels;
public VendorAdapter(ArrayList<MiniVendorModel> miniVendorModels) {
this.miniVendorModels = miniVendorModels;
}
#NonNull
#Override
public VendorsHolder onCreateViewHolder(#NonNull ViewGroup viewGroup, int i) {
View view = LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.fragment_marketplace_vendor_row_item, viewGroup, false);
return new VendorsHolder(view);
}
#Override
public void onBindViewHolder(#NonNull VendorsHolder vendorsHolder, int i) {
MiniVendorModel model = miniVendorModels.get(i);
Picasso.get().load(model.getImageURL()).memoryPolicy(MemoryPolicy.NO_CACHE).into(vendorsHolder.vendorImageView);
}
#Override
public int getItemCount() {
return miniVendorModels.size();
}
}
How do I make the view shows some part of the next views in the list, so it will make the filling of an actual scrollable list and not just a stale image?
What you can do here is make your layout/fragment_marketplace_vendor_row_item occupy say 80% width also add some padding
update width to 80%
android:layout_width="0dp"
android:layout_weight="0.8"
When scrolling left/right the scrolling sometimes gets stuck and not responsive. Why does this happen?
this might have been answered here
Found a solution for bad scrolling - wrapped the horizontal RV with a NestedScrollView. Works perfectly, I have no idea why. Just trial and error.
If someone knows why this works it would be awesome.

How Can I Add Two Buttons In A Row Of ListView Without Overlapping

I want to make a app like that custom layout ScreenShot: .This is one row and I add like that row 100 more.So the people can clik number or its information(i).
But it is shown like that : two buttons overlapping .But I want two button next to each other it is like that in custom_layout.xml(you can see screen shot).
This is my custom_layout.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
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">
<Button
android:id="#+id/button"
android:layout_width="298dp"
android:layout_height="wrap_content"
android:text="Button"
tools:layout_editor_absoluteX="16dp"
tools:layout_editor_absoluteY="87dp" />
<Button
android:id="#+id/button2"
android:layout_width="51dp"
android:layout_height="49dp"
android:text="Button"
tools:layout_editor_absoluteX="317dp"
tools:layout_editor_absoluteY="87dp" />
</android.support.constraint.ConstraintLayout>
This is my activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:fillViewport="true">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ListView
android:id="#+id/listView1"
android:layout_width="162dp"
android:layout_height="424dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="87dp" />
</RelativeLayout>
</ScrollView>
And this is my MainActivity.java:
package com.hey.task.attendancesystem;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ListView;
public class MainActivity extends AppCompatActivity {
Button[] button=new Button[15];
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ListView listView = (ListView) findViewById(R.id.listView1);
CustomAdapter customAdapter = new CustomAdapter();
//listeyi customlayouttaki şekilde oluşturdum hepsini
listView.setAdapter(customAdapter);
listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
}
});
}
class CustomAdapter extends BaseAdapter {
#Override
public int getCount() {
return button.length;
}
#Override
public Object getItem(int i) {
return null;
}
#Override
public long getItemId(int i) {
return 0;
}
#Override
public View getView(int i, View view, ViewGroup viewGroup) {
view = getLayoutInflater().inflate(R.layout.custom_layout,null);
Button button1 = (Button) view.findViewById(R.id.button);
Button button2 = (Button) view.findViewById(R.id.button2);
for (int j = 0;j<15;j++)
button1.setText(i + "");
for (int j = 0;j<15;j++)
button2.setText("i");
return view;
}
}
}
I don't know if i get your question but you can simply use LinearLayout with weight. eg:-
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:orientation="horizontal"
android:weightSum="1">
<Button
android:id="#+id/button_1"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 1" />
<Button
android:id="#+id/button_2"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".5" // or whatever you want
android:background="?android:attr/selectableItemBackground"
android:gravity="center"
android:padding="10dp"
android:text="Title 2" />
</LinearLayout>
You can use linear layout with a android:weightSum="" attribute in place of constraint layout in your custom_layout.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="match_parent"
android:weightSum="1.0"
android:orientation="horizontal"
android:gravity="center">
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.2"/>
<Button
android:layout_height="50dp"
android:layout_width="match_parent"
android:text="Button"
android:layout_weight="0.8"/>
</LinearLayout>
weightSum attribute is useful for having the layout rendered correctly for any device, which will not happen if you set width and height directly.

Why does relative layout in listview not expand correctly with content when font is huge

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

Why are adjacent Buttons moving when I change the size of the text in a Button

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

How to add a view to custom listview in android?

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.

Categories

Resources