I currently wanted to follow this layout but not sure how to control the buttons below. My problem is that the image stretched itself whatever the listview's width is. I wanted to follow the layout below. I am using Android's back button.
I am not quite familiar in programmatically controlling the position of the image and it's default size. The button is inside addFooterView(btnLoadMore);
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);
// Getting listview from xml
ListView lv = (ListView) rootView.findViewById(android.R.id.list);
// Creating a button - Load More
Button btnLoadMore = new Button(mContext);
btnLoadMore.setBackgroundResource(R.drawable.navigation_back);
RelativeLayout.LayoutParams params2 = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
params2.addRule(RelativeLayout.ALIGN_PARENT_LEFT);
btnLoadMore.setLayoutParams(params2);
// Adding button to listview at footer
lv.addFooterView(btnLoadMore);
return rootView;
}
load_main_groups_activty
<?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="fill_parent"
android:orientation="vertical"
android:background="#color/white"
>
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
>
</ListView>
<FrameLayout
android:id="#+id/contentFragment"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1" />
</RelativeLayout>
Try to implement this:
<?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="fill_parent"
android:orientation="vertical"
android:background="#color/white"
>
<!-- Main ListView
Always give id value as list(#android:id/list)
-->
<ListView
android:id="#android:id/list"
android:layout_width="fill_parent"
android:layout_alignParentTop="true"
android:layout_alignParentBottom="true"
android:layout_height="wrap_content"
android:listSelector="#drawable/list_selector">
</ListView>
<ImageButton
android:id="#+id/imageButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true"
android:background="#null"
android:src="#drawable/navigation_back" />
</RelativeLayout>
I am sure it'll help- you. Thanks
To keep the image from stretching, you can create a LinearLayout and place the button inside it. By setting the gravity attribute, you can position the button in the center, left or right of the ListView footer. After adding the button to the LinearLayout, the LinearLayout can be added to ListView footer.
See if the following code gets you the layout you want:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.load_main_groups_activty, container, false);
// Getting listview from xml
ListView lv = (ListView) rootView.findViewById(android.R.id.list);
// Creating a button - Load More
Button btnLoadMore = new Button(mContext);
btnLoadMore.setBackgroundResource(R.drawable.navigation_back);
LinearLayout llFooter = new LinearLayout(mContext);
LinearLayout.LayoutParams paramsBtn = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT);
paramsBtn.gravity = Gravity.LEFT;
llFooter.addView(btnLoadMore, paramsBtn);
//RelativeLayout.LayoutParams params2 = new
//RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,
//RelativeLayout.LayoutParams.MATCH_PARENT);
//llFooter.setLayoutParams(params2);
// Adding button to listview at footer
lv.addFooterView(llFooter);
return rootView;
}
Related
I am new to android app developing as i was creating spinner i noticed a extra space / padding vertically to drop down list of the spinner at the start and end of the drop down.
MainActivity.java:
public class MainActivity extends AppCompatActivity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Spinner sr = (Spinner) findViewById(R.id.spinner);
String[] days = getResources().getStringArray(R.array.days);
ArrayAdapter<String> ar = new ArrayAdapter<>(this, R.layout.single_row, days);
sr.setAdapter(ar);
}
}
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#666666"
tools:context="com.xxxxx.defaultspinner.MainActivity">
<Spinner
android:id="#+id/spinner"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="20dp"
android:spinnerMode="dialog"
android:background="#898989">
</Spinner>
</RelativeLayout>
single_row.xml
<TextView
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/app_name"
android:textColor="#FFFFFF"
android:textSize="26sp"
android:background="#214161">
</TextView>
I set background of all the views to different color so that i can identify the source of the extra space / padding. But the the extra space / padding has white background which none of the view has.
Note this is not because of the spinnerMode="dialog" option. this behavior is also happens when spinnerMode="dropdown". How can i remove this space ? or i am doing something wrong ?
You just need to override the getDropDownView method in the adapter.
#Override
public View getDropDownView(int position, View convertView, ViewGroup parent) {
parent.setPadding(0, 0, 0, 0);
return convertView;
}
try adding this to your TextView
android:includeFontPadding="false"
it will remove the TextView extra top and bottom padding
So I think I have gone through all the answers here on stackoverflow for this problem. However, noone of them have helped.
I am currently working with the camera2 example by Google trying to add an imageview "imageOverlay" that covers the whole screen. However, imageview is always null when trying to retrieve it. Any idea what I am missing?
Edit:
What has been tried:
Moving the ImageView in the camera_fragment.xml into the FrameLayout so that I can retrieve it just like the Button. This seemed like the best idea to make it an overlay.
Using getView()
Using getActivity()
Cleaning and rebuilding.
Using View view = inflater.inflate(R.layout.camera_fragment, null);
Camera_Activity xml:
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#000"
tools:context="com.irg.hig.imageregistrationgame.CameraActivity"
/>
camera_fragment.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<com.irg.hig.imageregistrationgame.CameraTextureView
android:id="#+id/texture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />
<ImageView
android:id="#+id/imageOverlay"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.5" />
<FrameLayout
android:id="#+id/control"
android:layout_width="match_parent"
android:layout_height="112dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:background="#color/control_background">
<Button
android:id="#+id/picture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="#string/picture" />
<ImageButton
android:id="#+id/info"
android:contentDescription="#string/description_info"
style="#android:style/Widget.Material.Light.Button.Borderless"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|right"
android:padding="20dp" />
</FrameLayout>
</RelativeLayout>
CameraFragment.java:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.camera_fragment , container, false);
imageView = (ImageView) view.findViewById(R.id.imageOverlay);
return view;
}
#Override
public void onViewCreated(final View view, Bundle savedInstanceState) {
view.findViewById(R.id.picture).setOnClickListener(this);
view.findViewById(R.id.info).setOnClickListener(this);
mTextureView = (CameraTextureView) view.findViewById(R.id.texture);
//imageView = (ImageView) view.findViewById(R.id.imageOverlay);
}
The problem was that there was a second xml file in layout-v21 that was used and not the one in the Layout folder that I was trying to change.
Thanks to everyone that tried to help, it was appreciated immensely.
Try this.
LayoutInflater inflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View view = inflater.inflate(R.layout.your_layout, null);
Try to replace : View view = inflater.inflate(R.layout.camera_fragment , container, false);
by View view = inflater.inflate(R.layout.camera_fragment, null);
i want to remove added textfields and add other ones based on what is selected from a spinner.
here is my code:
in xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#f4f4f4"
android:id="#+id/taskoptionfragmentlinearlayout" >
<TextView
android:layout_marginTop="10dp"
android:textStyle="bold"
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Select A Search Option" />
<Spinner
android:layout_marginTop="20dp"
android:id="#+id/gis_search"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
<ProgressBar
android:id="#+id/progressBar1"
style="?android:attr/progressBarStyleLarge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
in class:
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)
{
final View rootView = inflater.inflate(R.layout.fragment_tasklist_options, container, false);
#Override
public void onItemSelected(AdapterView<?> parent, View view,
int position, long id) {
ll = (LinearLayout)rootView.findViewById(R.id.taskoptionfragmentlinearlayout);
//create textfields....
TextView tv = new TextView(getActivity());
tv.setText(INPUTFLDLABELAArray.get(i));
tv.setGravity(Gravity.RIGHT);
tv.setTextSize(18);
tv.setTypeface(Typeface.DEFAULT_BOLD);
tv.setPadding(0, 0, 15, 0);
ll.addView(tv);
}
i summarized the code to make it short.
so when an item in spinner is selected, the textfield shows. then when another item is selected, a textfield shows under the previous textfield. so what i want is to (refresh/reset) the layout so that every time an item is selected, the old textfield is removed and the new one is added.
try calling ll.removeAllViews() before adding the new ones
I've made the next layout xml code which I call it layout_one -
<?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" >
<LinearLayout
android:id="#+id/layout_one"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="50dp"
android:layout_marginTop="50dp"
android:orientation="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="100dp"
android:text="Testing 1"
android:textSize="20sp" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/info_layout"
android:visibility="gone">
<TextView
android:id="#+id/textView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="TextView" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
As you can see there are two layout in it - one with textview and button and one with textview.
The layout with the only textview - is gone, And i want by a click on the button, from the visible layout, it will be shown.
Now at the activity i wrote the next code -
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.test_layout);
ScrollView sv = new ScrollView(this);
LinearLayout ll = new LinearLayout(this);
ll.setOrientation(LinearLayout.VERTICAL);
sv.addView(ll);
LinearLayout newView0 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView1 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView2 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
LinearLayout newView3 = (LinearLayout)View.inflate(this, R.layout.layout_one, null);
ll.addView(newView0);
ll.addView(newView1);
ll.addView(newView2);
ll.addView(newView3);
setContentView(sv);
newView1.findViewById(R.id.layout_one).setBackgroundColor(0xff0000ff);
TextView tv3 = (TextView) newView3.findViewById(R.id.textView1);
tv3.setText("Suprise Suprise");
infoLay = (View) findViewById(R.id.info_layout);
ll.addView(newView3);
}
Now there is something I don't understad - How could I set an on click listener to the button that will know which layout to show?
Tanks for any kind of help
Actually, there are three linear layouts, which seems redundant.
You can set the onclick listener by checking if textview of each layout is empty or not.
First off, have a look at this http://developer.android.com/training/improving-layouts/reusing-layouts.html. Use that <include> tag instead of dynamically creating all these layouts in your Activity class. Define your ScrollView, and all your LinearLayouts in your test_layout.xml file.
Then, in your Activity, all you have to do is get a reference to those views that's you've defined using findViewById.
Once you've got that working we can address your question as follows:
setContentView(R.layout.test_layout);
LinearLayout layout1 = (LinearLayout)findViewById(R.id.layout1);
LinearLayout layout2 = (LinearLayout)findViewById(R.id.layout2);
LinearLayout layout3 = (LinearLayout)findViewById(R.id.layout3);
OnClickListener listener = new OnClickListener() {
#Override
public void onClick(View view) {
((View)view.getParent().getParent()).findViewById(R.id.textView2).setVisibility(View.VISIBLE);
}
});
layout1.findViewById(R.id.button1).setOnClickListener(listener);
layout2.findViewById(R.id.button1).setOnClickListener(listener);
layout3.findViewById(R.id.button1).setOnClickListener(listener);
I'd also add that it seems like you're trying to create a list here, in which case you should use a ListView instead of many LinearLayouts.
I have listview that i want to fill the whole screen,There are four items in listview. It leaves empty space below after four items are filled.You can see in the screenshot. It leaves the blank space. I want whole screen to be covered.
I would like to have like this:
Here is the source Code
MainActivity.java.
public class MainActivity extends Activity {
ListView resultPane;
List<Taskinfo> list;
CustomAdapter adapter;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
resultPane = (ListView) findViewById(R.id.mylist);
list = new ArrayList<Taskinfo>();
Resources res = getResources(); // resource handle
Drawable drawable = res.getDrawable(R.drawable.browse_home);
list.add(new Taskinfo("Browse", drawable));
drawable = res.getDrawable(R.drawable.jewelry);
list.add(new Taskinfo("Whats New", drawable));
drawable = res.getDrawable(R.drawable.show);
list.add(new Taskinfo("Upcoming Show", drawable));
drawable = res.getDrawable(R.drawable.contact);
list.add(new Taskinfo("Contact Us", drawable));
adapter = new CustomAdapter(this, list);
resultPane.setAdapter(adapter);
}
}
CustomAdapter.java
public class CustomAdapter extends BaseAdapter {
private Context context;
private List<Taskinfo> list;
public CustomAdapter(Context context, List<Taskinfo> list) {
this.context = context;
this.list = list;
}
public View getView(int index, View view, final ViewGroup parent) {
if (view == null) {
LayoutInflater inflater = LayoutInflater.from(parent.getContext());
view = inflater.inflate(R.layout.single_list_item, parent, false);
}
Taskinfo t = list.get(index);
RelativeLayout l = (RelativeLayout) view
.findViewById(R.id.testrelative);
l.setBackgroundDrawable(t.getImage());
TextView textView = (TextView) view.findViewById(R.id.title);
textView.setText(t.getName());
return view;
}
}
single_list_item.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/testrelative"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/browse_home"
android:orientation="horizontal"
android:padding="5dip" >
<!-- ListRow Left sied Thumbnail image -->
<LinearLayout
android:id="#+id/thumbnail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_marginRight="5dip"
android:padding="3dip" >
</LinearLayout>
<TextView
android:id="#+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="16dp"
android:text=""
android:textColor="#040404"
android:textSize="15dip"
android:textStyle="bold"
android:typeface="sans" />
</RelativeLayout>
activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="45sp"
android:id="#+id/llayout"
android:background="#drawable/navbar"
android:padding="3sp" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical|center_horizontal"
android:layout_weight="1"
android:gravity="center_horizontal"
android:text="Golden Stone"
android:textColor="#color/white"
android:textSize="20sp" >
</TextView>
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_below="#id/llayout"
android:layout_height="match_parent" >
</ListView>
</RelativeLayout>
Using ListView in such case is strange and unnecessary. Think about your constraints (like: what if items overflow?) and just use a proper layout manager. Like a vertical LinearLayout with the last item having a non-zero layout weight.
ListViews make sense only if you need an abstraction that generates list items on-the-fly.
You would want to set a background on the ListView. Currently your view should be going to the bottom, but the background of the ListView is transparent, so setting it white should achieve what you're asking.
<ListView ...
android:background="#android:color/white"
... />
if there are so few items, you can use the LinearLayout instead, and give weights for each of its items.
However, do note that android supports many devices and screens, so you might want to have a limitation of how small each row would be.
anyway, in case you wish to make each row the fitting height, you can check its size and then divide by the number of items.
in order to get the size of the listView , you can use this small snippet i've made .