TextView with wrap_content height obscured by ListView below it - android

I am not seeing my "Hello World" textview.
The listview, which I populate with elements, is all that shows.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_above="#+id/recipe_list_view"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="match_parent"></ListView>
</RelativeLayout>
public class MainActivity extends AppCompatActivity {
private ListView mListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
loadListView();
}
private void loadListView() {
mListView = findViewById(R.id.recipe_list_view);
final ArrayList<Recipe> recipes = Recipe.getRecipesFromFile("recipes.json", this);
RecipeAdapter a = new RecipeAdapter(this, recipes);
mListView.setAdapter(a);
}
}

Your ListView fills the entire screen, and then you say that the TextView should be above the ListView, which makes it appear outside the screen, so it's not visible.
You need to invert the dependency.
So instead of the TextView being above the ListView, the ListView should be below the TextView.

RelativeLayout causes the last item covers the previous one if it has match_parent width and height. Change the order or use LinearLayout instead of.

you can use LinearLayout as the parent with Vertical Orientation it will work perfectly:-
I have corrected your xml code , use this code it works
<LinearLayout
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:orientation="vertical"
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"
android:layout_above="#+id/recipe_list_view"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>
</LinearLayout>
it will work and both textview and listview will be shown .
incase you want to use relative layout only just make listview also wrap_content
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
tools:context="lister.quantumproductions.com.lister.MainActivity">
<TextView
android:id="#+id/recipe_text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"></TextView>
<ListView
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/recipe_text_view"></ListView>
</RelativeLayout>

try this
<TextView
android:id="#+id/text_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Hello World!"></TextView>
<ListView
android:layout_below="#id/text_view"
android:id="#+id/recipe_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"></ListView>

Related

Android Wear: Card Scroll View Does Not Scroll

I am today testing out the card scroll view but with all the examples I followed my one does not scroll and I am not sure why.
card_scroll_view_test.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.wearable.view.BoxInsetLayout 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="match_parent"
android:background="#color/black">
<android.support.wearable.view.CardScrollView
android:id="#+id/card_scroll_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_box="all">
<android.support.wearable.view.CardFrame
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="HELLO"
android:textColor="#color/black"
android:textSize="20sp" />
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/big_text"
android:textColor="#color/black"
android:textSize="14sp" />
</LinearLayout>
</android.support.wearable.view.CardFrame>
</android.support.wearable.view.CardScrollView>
</android.support.wearable.view.BoxInsetLayout>
So this contains a card frame with a linear layout that holds two child text views. The last text view holds a really big string to test for scrolling
WearActivity.java
public class WearActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.card_scroll_view_test);
setupCardScrollView();
}
private void setupCardScrollView(){
CardScrollView cardScrollView =
(CardScrollView) findViewById(R.id.card_scroll_view);
cardScrollView.setExpansionEnabled(true);
cardScrollView.setExpansionDirection(CardFrame.EXPAND_DOWN);
cardScrollView.setCardGravity(Gravity.BOTTOM);
}
}
I also checked to see if the root box inset layout might have been the issue by making the CardScrollView the root view, but the problem persists.
It seems to work if you:
Remove BoxInsetLayout (CardScrollView become parent view)
Set setExpansionFactor on CardScrollView and set the properties on CardFrame as well.
Update to your code:
private void setupCardScrollView(){
CardScrollView cardScrollView =
(CardScrollView) view.findViewById(R.id.card_scroll_view);
cardScrollView.setExpansionEnabled(true);
cardScrollView.setExpansionDirection(1);
cardScrollView.setExpansionFactor(10.0F);
CardFrame cardFrame = (CardFrame) view.findViewById(R.id.card_frame);
cardFrame.setExpansionEnabled(true);
cardFrame.setExpansionDirection(1);
cardFrame.setExpansionFactor(10.0F);
}

Resize layout to get into other layout

I created a menuView.xml layout to be in all of the layouts of my activity. This layout has one column on each border and a title bar like this:
ComposeView http://img845.imageshack.us/img845/2121/d6zp.png
I insert this layout in the other layouts this way:
<!-- Show menu -->
<com.example.MenuView
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
But if one of the layouts has full screen view, part of this view gets covered by the MenuView, so...
How could I tell to this view to adapt its size to the blank space inside the MenuView to not get covered by it?
UPDATE -- full XML included
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:background="#drawable/degradado">
<!-- Show menu -->
<com.example.MenuView
android:layout_width="match_parent"
android:layout_height="match_parent" />
<RelativeLayout
android:id="#+id/Left_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
<RelativeLayout
android:id="#+id/Right_layout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentRight="true"
android:layout_alignParentBottom="true" >
//Here go buttons, views, etc...
</RelativeLayout>
What happens here is that these 2 Relative layouts get covered by the MenuView (The darkest gre borders and the top black bar), and the ideal way would be that these 2 layouts get fitted to the blank space (the clearest gray).
I can solve this setting margin sizes to the Relative layouts to fit inside of it, but i know this is not the best way to do it, so I don't know if there is another way.
I think the best way to solve your issue is with inheritance.
If you define an Activity that can be used as a template for all your fleshed out Activitys to add their content to.
I don't know what you custom menu is 'made of' but as a simple example:
Create a basic Activity with code:
public class ActivityWithMenu extends Activity
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_with_menu_layout);
}
}
and xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ActivityWithMenu" >
<TextView
android:layout_width="match_parent"
android:layout_height="20dip"
android:background="#ff000000"
android:textColor="#ffffffff"
android:text="Main Menu Title Bar"
android:id="#+id/mainmenutitle" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentLeft="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/lefthandmenu" />
<LinearLayout
android:layout_width="20dip"
android:layout_height="match_parent"
android:layout_below="#+id/mainmenutitle"
android:layout_alignParentRight="true"
android:background="#ff999999"
android:orientation="vertical"
android:id="#+id/righthandmenu" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_toLeftOf="#+id/righthandmenu"
android:layout_toRightOf="#+id/lefthandmenu"
android:layout_below="#+id/mainmenutitle"
android:orientation="vertical"
android:id="#+id/activitycontent"
/>
</RelativeLayout>
Then create your xml for a specific Activity, in this case a simple 'Hello World' layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbars="vertical" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#ff00ff00"
android:text="Hello World!"
/>
</LinearLayout>
</ScrollView>
But now when you write the code for this Activity, extend 'ActivityWithMenu' instead of the Activity class direct and inflate this xml layout as follows:
public class Activity1 extends ActivityWithMenu
{
#Override
protected void onCreate(Bundle savedInstanceState)
{
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
LinearLayout ll = (LinearLayout)findViewById(R.id.activitycontent);
ScrollView sv = (ScrollView)this.getLayoutInflater().inflate(R.layout.activity1_layout, ll, false);
ll.addView(sv);
}
}
I have added the code for making the Activity fullscreen here instead of in the parent ActivityWithMenu class assuming that you wouldn't want them all displayed that way but you could move it into the parent class if appropriate.
Hope this helps.

Android List Below Toggle Buttons

I have a list that is intended to be below toggle buttons. The list grabs data from a server and then parses them. My XML is as follows:
<?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:orientation="horizontal" >
<ToggleButton
android:id="#+id/toggle_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:textOff="Apps"
android:textOn="Apps" />
<ToggleButton
android:id="#+id/toggle_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/toggle_button1"
android:layout_weight="1"
android:textOff="VMs"
android:textOn="VMs" />
<ToggleButton
android:id="#+id/toggle_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/toggle_button2"
android:layout_weight="1"
android:textOff="Groups"
android:textOn="Groups" />
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toggle_button1" />
</RelativeLayout>
Code for the actual fragment:
public class ProblemFragment extends SherlockListFragment
{
private SeparatedListAdapter list;
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
this.getSherlockActivity().setContentView(R.layout.problem_layout);
list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
ToggleButton b1 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button1);
ToggleButton b2 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button2);
ToggleButton b3 = (ToggleButton) this.getSherlockActivity().findViewById(R.id.toggle_button3);
setListAdapter(list);
refresh();
}
public void refresh()
{
list = new SeparatedListAdapter(this.getSherlockActivity(), new Layout(R.layout.separated_list_adapter_two_text, R.id.two_text_title, R.id.two_text_desc));
refreshStats();
}
public void refreshStats()
{
//Omitted parsing code
list.addSection(new String("Hello world!!"));
setListAdapter(list);
}
}
However, when I use setListAdapter(list), the buttons are overwritten. They are visible before the app retrieves the data and parses it, but they are overwritten after I call setListAdapter. How can i fix this?
First, remove
android:orientation="horizontal"
from your root layout. RelativeLayout doesn't have an orientation property. Also, weight is for child elements of a LinearLayout and when you use it then you should assign the width of each child view to 0dp for horizontal orientation and height="0dp" for vertical orientation.
Then wrap your ToggleButtons in a LinearLayout, vertical or horizontal orientation, and give it the property
android:layout_alignParentTop="true"
then give your ListView the property
android:layout_below="#id/idOfLinearLayout"
So it may look something like
<?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/toggleLL"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true">
<ToggleButton
android:id="#+id/toggle_button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Apps"
android:textOn="Apps" />
<ToggleButton
android:id="#+id/toggle_button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="VMs"
android:textOn="VMs" />
<ToggleButton
android:id="#+id/toggle_button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textOff="Groups"
android:textOn="Groups" />
</LinearLayout>
<ListView
android:id="#+id/mylist"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/toggleLL" />
</RelativeLayout>
I also removed the RelativeLayout properties from the ToggleButtons since they are now wrapped in a LinearLayout. And you had a circular view error there with assigning the second ToggleButton to the right of itself which may have been a copy/paste error. Hope this helps.
Note that the default orientation for a LinearLayout is horizontal so leaving that property out will give you that effect.
Oh! I can not test your XML but I think that you need scrollbars! If the list is filled with a lot of entries, it can became bigger that the screen, making the buttons disappear because they are pushed up by the list. Try to add a scroll to the whole layout.
Something like this:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<!-- Original layout here -->
</ScrollView>
</LinearLayout>
Of course, if you just put only one layout inside the scrollview, there is no need for the outer layout:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:android="http://schemas.android.com/apk/res/android">
<!-- Original layout here -->
</ScrollView>

Is it possible to have components other than List in ListActivity

By using the following code.
BuyActivity.java
public class BuyActivity extends ListActivity {
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.transaction_list_view);
OrderAdapter orderAdapter = new OrderAdapter(this,
R.layout.custom_row_view, new ArrayList<Object>());
setListAdapter(orderAdapter);
orderAdapter.add(new Object());
orderAdapter.add(new Object());
}
}
transaction_list_view.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">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data"
/>
</LinearLayout>
custom_row_view.xml
Please refer to custom_row_view.xml
I was able to get the following outcome.
Besides list, I would also like to have a static label at the bottom of ListActivity.
I try
transaction_list_view.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">
<ListView android:id="#id/android:list"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="2"
android:drawSelectorOnTop="false">
</ListView>
<TextView android:id="#id/android:empty"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="No data"
/>
<TextView
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:text="THIS IS TOTAL STATIC LABEL"
/>
</LinearLayout>
But I am getting
What my expectation is
Is there anything I had missed out?
You have indicated that your ListView is supposed to fill_parent in the vertical orientation, leaving no room for your other widgets. Your weight is being ignored. You probably want to have your android:layout_height be 0dip instead.
Try setting the height of the static textview to wrap_content.
Well you could try Relative Layout to just set the static label anchored to the bottom of the screen. If that's not what you meant, I would try to help you further on. Good luck

Visibility of ScrollView

I am using two ScrollViews and want to set visibility of one as GONE or INVISIBLE but when I do it by following java code the application terminates. If I do the same thing using Properties pan, in the eclipse, it just works. What is wrong?
Java code is:
public class Test extends Activity {
List<ScrollView> sv;
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
setContentView(R.layout.main);
}
}
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="fill_vertical">
<ScrollView android:layout_height="wrap_content" android:id="#+id/scrollView1" android:layout_width="match_parent">
<LinearLayout android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/linearLayout1" android:orientation="vertical">
<TextView android:text="TextView" android:id="#+id/textView1" android:layout_width="wrap_content" android:layout_height="wrap_content"></TextView>
</LinearLayout>
</ScrollView>
<ScrollView android:layout_height="wrap_content" android:layout_width="match_parent" android:id="#+id/scrollView2">
<TextView android:text="TextView" android:layout_width="match_parent" android:id="#+id/textView2" android:layout_height="match_parent"></TextView>
</ScrollView>
</LinearLayout>
you haven't setContentview(yourLayout.xml)
it wont be able to findViewById.
do the findViewById AFTER the setContentView
First of you have to set layout file.
setContentView(R.layout.new_view);
sv = new ArrayList<ScrollView>();
sv.add((ScrollView)findViewById(R.id.scrollView1));
sv.add((ScrollView)findViewById(R.id.scrollView2));
sv.get(1).setVisibility(View.GONE);
try this if you are getting any error then please post logcat.

Categories

Resources