how to add fixed button and edittext to bottom of screen - android

I have a custom ListView.
I want to add a fixed button on the bottom of the screen.
How should I change my layout?
My screen XML code:
<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:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ListAdapter" >
<ImageView
... />
<TextView
... />
</RelativeLayout>
My custom arrayAdapter source code:
#Override
public View getView(int position, View convertView, ViewGroup parent) {
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View rowView = inflater.inflate(R.layout.msg_detail_layout, parent, false);
msgBodyCustomer = (TextView) rowView.findViewById(R.id.MsgBodyCustomer);
contactImageCustomer = (ImageView) rowView.findViewById(R.id.PhotoImageCustomer);
if(!smslist.get(position).getType().equals(INBOX)){
msgBodyOwner.setText(smslist.get(position).getBody());
contactImageCustomer .setImageURI((smslist.get(position).getPicture()));
return rowView;
}
}

Edited:try this
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context=".ListAdapter" >
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button5"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<ListView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:id="#+id/listView7"
android:layout_alignRight="#+id/PhotoImageOwner"
android:layout_above="#+id/button5" />
</RelativeLayout>

Alternatively, you can add footer for your custom listview
The footer will always be at bottom
Code :
<?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:paddingTop="7dip"
android:paddingBottom="7dip"
android:orientation="horizontal"
android:gravity="center">
<ImageView
android:id="#+id/footer_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:gravity="center"
/>
<TextView
android:text="#string/footer_text_1"
android:id="#+id/footer_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
</LinearLayout>
</LinearLayout>
Refer above in your java file onCreate() where you are initialising your listview:
View footerView = ((LayoutInflater) ActivityContext.getSystemService(Context.LAYOUT_INFLATER_SERVICE)).inflate(R.layout.footer_layout, null, false);
<your_list_view>.addFooterView(footerView);
Reference credit to : this answer

Related

setVisibility(View.Gone) work but the view still own the space

I make a CustomAdapter extends BaseAdapter.In it's getView() method , I use ViewHolder. And I set a clickListener with a TextView to set a view (call it A)gone and another view (call it B)visible , but when I click the TextView , the A GONE but it leave a space ,so the B cannot match the parent.
My code like
#Override
public View getView(int position, View convertView, ViewGroup parent) {
final ViewHolder viewHolder;
if (convertView == null){
convertView = mLayoutInflater.inflate(R.layout.customlayout,parent,false);
viewHolder = new ViewHolder();
}else {
viewHolder = (ViewHolder) convertView.getTag();
}
viewHolder.textView = (TextView) convertView.findViewById(R.id.textview);
viewHoler.a = (LinearLayout) convertView.findViewById(R.id.a);
viewHoler.b = (LinearLayout) convertView.findViewById(R.id.b);
viewHolder.textview.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (viewHolder.a.getVisibility() == View.GONE){
viewHolder.b.setVisibility(View.GONE);
viewHolder.a.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}else {
viewHolder.a.setVisibility(View.GONE);
viewHolder.b.setVisibility(View.VISIBLE);
notifyDataSetChanged();
}
}
});
convertView.setTag(viewHolder);
return convertView;
}
the customlayout code is
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
It should be show like this way when I click the textView
but it always like another way below , just like the A view still take the space ,that like call setVisibility(View.INVISIBLE) or not setVisibility(View.GONE)
the A view is not appear because that although B view has disappeared but it still take the space
Why will it behave like that? How should I do to solve it ?
Thank you for your help.
Try with wrap_content on your LinearLayouts.
Like this -
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="150dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scaleType="centerCrop"
android:src="#drawable/bg"/>
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/a"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button a"/>
</LinearLayout>
<LinearLayout
android:id="#+id/b"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="visible">
<Button
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="button b"/>
</LinearLayout>
</LinearLayout>
</LinearLayout>

Aligning ListView text element with TableLayout element

I have a TableLayout with two TextView elements representing the headings of a table, beneath the TableLayout I have a ListView each ListView item has a checkbox followed by two TextView elements representing the value for that column. So something like this
The problem is the right hand column isn't aligned with the TableRow second cell. Instead it appears just to the right of the end of the first ListView item text element. How can I acheive this?
Here's what I have
main_activity.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/parentPanel"
tools:context=".MainActivity">
<TableLayout
android:id="#+id/surveys_table"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin">
<TableRow android:layout_height="wrap_content">
<TextView
android:id="#+id/survey_name_header"
android:layout_weight="1"
android:background="#drawable/header_cell_shape"
android:text="#string/survey_name"
android:textColor="#color/white" />
<TextView
android:id="#+id/survey_class_header"
android:layout_weight="1"
android:background="#drawable/header_cell_shape"
android:text="#string/survey_class"
android:textColor="#color/white" />
</TableRow>
</TableLayout>
<ListView
android:id="#+id/listView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/surveys_table" />
</RelativeLayout>
survey_list_item.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:id="#+id/checkBox1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="33dp"
android:layout_marginTop="7dp" />
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="40dp"
android:layout_marginTop="7dp"
android:layout_toRightOf="#+id/textView1" />
</RelativeLayout>
I would assume I need to modify the layout parameters in the addView() method of my ListAdapter but I'm not sure how?
#Override
public View getView(int position, View convertView, final ViewGroup parent) {
View view = null;
if (convertView == null) {
view = inflater.inflate(R.layout.survey_list_item, parent, false);
CheckBox checkBox = (CheckBox) view.findViewById(R.id.checkBox1);
TextView surveyName = (TextView) view.findViewById(R.id.textView1);
TextView surveyClass = (TextView) view.findViewById(R.id.textView2);
//What to do here?
} else {
view = convertView;
}
return view;
}

GridView with image and text

I'm very new in android and need a little help with this task.
I have gridview witch pull images and text from database and display them. Currently is displayed like image and on the right of the image is text. I want to make text under the image. Can you help me with this task. This is the gridview.xml
<LinearLayout
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_weight="0.1" >
<GridView
android:id="#+id/gridView1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:horizontalSpacing="5dp"
android:verticalSpacing="5dp"
android:numColumns="2" >
</GridView>
</LinearLayout>
And this is how I display image and text in it table_column.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/bckimageviw"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:text="Name"
/>
</LinearLayout>
</LinearLayout>
And if need getView()
public View getView(int position, View convertView, ViewGroup parent) {
// TODO Auto-generated method stub
LayoutInflater inflater = (LayoutInflater) context
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
if (convertView == null) {
convertView = inflater.inflate(R.layout.table_column, null);
}
// ColPhoto
ImageView imageView = (ImageView) convertView.findViewById(R.id.ColPhoto);
imageView.getLayoutParams().height = 80;
imageView.getLayoutParams().width = 80;
imageView.setPadding(10, 10, 10, 10);
imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
try
{
imageView.setImageBitmap((Bitmap)MyArr.get(position).get("ImageThumBitmap"));
} catch (Exception e) {
// When Error
imageView.setImageResource(android.R.drawable.ic_menu_report_image);
}
// ColName
TextView txtName = (TextView) convertView.findViewById(R.id.ColName);
txtName.setPadding(5, 0, 0, 0);
txtName.setText("" + MyArr.get(position).get("name").toString());
return convertView;
}
u can use relative layout and set align textview to imageview.
try this.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ff0000"
android:gravity="center" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/ColName"
android:layout_alignRight="#+id/ColName"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/ColPhoto"
android:gravity="center"
android:text="Name name" />
</RelativeLayout>
I don't know why add a single element LinearLayout in another LinearLayout.
if grid item need to be put in correct position, I'd like use RelativeLayout instead.
`
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"
/>
<TextView android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#id/ColPhoto"
android:text="Name"
/>
</RelativeLayout>
`
Try to change your orientation to "vertical".
EDIT:
Like, Tr4X said you don't need second LinearLayout. You can put both in one.
<?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"
android:background="#drawable/bckimageviw" >
<ImageView
android:id="#+id/ColPhoto"
android:layout_width="0dp"
android:layout_height="0dp"/>
<TextView
android:id="#+id/ColName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
</LinearLayout>

cant see my footer with ListActivity

I am trying to do footer to my custom list but still cant work it out.
My aim is just to add some button to end of the list.
I tried this:
adapter = new GeneralRssAdapter(this,R.layout.titlesrss, data);
LayoutInflater inflater = getLayoutInflater();
lv.addFooterView( inflater.inflate( R.layout.footer_layout, null ), null, false);
lv.setAdapter(adapter);
but still cant see any footer.
Heres my xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:id="#+id/linearLa"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/iconremove"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="5dip"
android:scaleType="centerCrop"
android:src="#drawable/delete"
android:visibility="gone" />
<ImageView
android:id="#+id/iconfav"
android:layout_width="50dip"
android:layout_height="50dip"
android:layout_margin="5dip"
android:scaleType="centerCrop"
android:src="#drawable/rss" />
<TextView
android:id="#+id/textView"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal|center"
android:layout_margin="5dip"
android:layout_weight="1"
android:textSize="20dip" />
</LinearLayout>
<ListView android:id="#+id/ListView01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
</RelativeLayout>
and the footer:
<?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" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1.20"
android:src="#drawable/tutorialrss"
/>
</LinearLayout>
hope you can HELP me:)
try this
adapter = new GeneralRssAdapter(this,R.layout.titlesrss, data);
LayoutInflater mInflater = (LayoutInflater)getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View footer = mInflater.inflate(R.layout.footer_layout, null);
choicelist.addFooterView(footer);
choicelist.setAdapter(adapter);
i have edited this 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:orientation="vertical" >
<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:src="#drawable/tutorialrss"
/>
</LinearLayout>
for me its working plz check once.
adapter = new ListAdapter(this, data);
ListView lvMain = (ListView) findViewById(R.id.list);
LayoutInflater mInflater = (LayoutInflater) getSystemService(Activity.LAYOUT_INFLATER_SERVICE);
final View footer = mInflater.inflate(R.layout.footer, null);
lvMain.addFooterView(footer);
lvMain.setAdapter(adapter);
What I do is set my footer/header in setContentView() then uselist xml in adapter. I don't normally like one line answers but that's all I do

android gridview fit the size in to screen

The gridview of my application has an image and a text below it for each grid.
I need to make sure that the complete list of grid fit in to the screen (no overflow at bottom).
Here is how i have defined my gridview
<GridView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gridview" android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:numColumns="3"
android:stretchMode="columnWidth"
android:layout_below="#+id/text1view"
android:gravity="center"
android:background="#drawable/home_bg"
/>
and each grid item is :
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/GridItem"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
android:gravity="center_horizontal"
>
<ImageView android:id="#+id/grid_item_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="0dip"
android:layout_margin="0dip"
>
</ImageView>
<TextView android:id="#+id/grid_item_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:padding="0dip"
android:gravity="center_horizontal"
android:layout_below="#+id/grid_item_image"
android:layout_alignParentBottom="true"
>
</TextView>
</LinearLayout>
I have tried several ways to set the grid view layout param, but i am not getting the right output. Please help me here. the gridview adapter code is
public View getView(final int position, View convertView, ViewGroup parent) {
ViewHolder holder;
if (convertView == null) {
LayoutInflater mInflater = (LayoutInflater) mContext
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
convertView = mInflater.inflate(R.layout.griditems, null);
holder = new ViewHolder();
holder.text1 = (TextView) convertView
.findViewById(R.id.grid_item_text);
Log.i(TAG, holder.text1.toString());
holder.image = (ImageView) convertView
.findViewById(R.id.grid_item_image);
// if it's not recycled, initialize some attributes
holder.image.setImageResource(gridItemIds[position]);
holder.text1.setText(gridTitles[position]);
int h = mContext.getResources().getDisplayMetrics().densityDpi;
convertView.setLayoutParams(new GridView.LayoutParams(h-50, h-20));
//holder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);
//holder.image.setScaleType(ImageView.ScaleType.FIT_XY); ......
Try this:-
grid_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="match_parent"
android:layout_height="match_parent">
<LinearLayout android:id="#+id/linearLayout1" android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical" android:gravity="center">
<ImageView android:id="#+id/imgIcon" android:src="#drawable/icon" android:layout_width="wrap_content" android:layout_height="wrap_content"></ImageView>
<TextView android:text="TextView" android:id="#+id/txtDesc" android:layout_height="wrap_content" android:layout_width="wrap_content"></TextView>
</LinearLayout>
</LinearLayout>
gridview.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"
>
<GridView xmlns:android="http://schemas.android.com/apk/res/android" android:id="#+id/mobGrid"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="10dp"
android:verticalSpacing="10dp"
android:horizontalSpacing="10dp"
android:numColumns="auto_fit"
android:columnWidth="60dp"
android:stretchMode="columnWidth"
android:gravity="center"
/>
</LinearLayout>
try
android:layout_width="fill_parent"
android:layout_height="fill_parent"
for your GridView instead of wrap_content!

Categories

Resources