Why are the textviews invisible? - android

I use the following layout (shortened):
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android" ...>
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>
Like this (shortened also):
LayoutInflater inflater = parent.getLayoutInflater();
View layout = inflater.inflate(R.layout.merger_sale, null);
AlertDialog.Builder builder = new AlertDialog.Builder(parent);
builder.setTitle(R.string.sell_stock_after_merger)
.setView(layout)
.setPositiveButton(R.string.sell_all, new OnClickListener() {...
.setNeutralButton(R.string.trade_sell, new OnClickListener() {...
.setNegativeButton(R.string.trade_all, new OnClickListener() {...
AlertDialog dialog = builder.create();
dialog.show();
The layout has no errors, nor does the code. The layout shows exactly as it should in Eclipse's layout editor.
The dialog appears when it should appear, buttons behave how they should, except for the texview (#+id/sell_text) [and the other textviews in the shortened <TableRow>s], which don't appear.
What am I overlooking? Thanks!

I think it would work if you change the width and height to wrap content instead of match_parent.
match_parent means it would be fillparent which may fill the entire layout.
Or try giving weights to each item like layout_weight with value as 0 .This may work either

try it:
<TableLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content" >
<TableRow ...>
<TextView
android:id="#+id/sell_text"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="#string/sell" />
<SeekBar
android:id="#+id/sell"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</TableRow>
</TableLayout>

Related

How to add & stack childView to a LinearLayout?

I got The specified child already has a parent. You must call removeView() on the child's parent first. ERROR.
But I do not want to remove the parent view.
If you click a button, a message must be added at the end of LinearLayout's childViews.
This is my Java code:
Button addBTN = findViewById(R.id.addButton);
LinearLayout messageListLL = findViewById(R.id.messageList_LinearLayout);
addBTN.setOnClickListener((v) -> {
MessageItem messageItem = new MessageItem();
View messageView = View.inflate(this, R.layout.item_message, messageListLL);
messageListLL.addView(messageView, messageListLL.getChildCount());
});
This is the part of activity's xml:
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_above="#id/adView"
android:layout_below="#id/userContainer"
android:background="#CCC"
android:padding="15dp">
<LinearLayout
android:id="#+id/messageList_LinearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<Button
android:id="#+id/addButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:backgroundTint="#color/design_default_color_primary"
android:text="ADD"
android:textColor="#android:color/white" />
</LinearLayout>
</ScrollView>
This is item_message.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="wrap_content"
android:layout_centerVertical="true"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left" />
<EditText
android:layout_width="300dp"
android:layout_height="wrap_content" />
<ImageView
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_gravity="right"
android:background="#FFD9D9" />
</LinearLayout>
So, I'd like to make a view with item_message and put it into LinearLayout in the activity's xml, which has messageList_LinearLayout for the id attribute.
The button is also in the LinearLayout. The button must be placed at the end of the LinearLayout.
And whenever, I click the button, a message view must be created and added at the end of the messageList and also before the button.
You can just change the third parameter into null.
View messageView = getLayoutInflater().inflate(this, R.layout.item_message, null);
messageListLL.addView(messageView, messageListLL.getChildCount() - 1)
The third parameter specifies its root view. You can inflate the messageView without any roots.

Android: Custom FlowLayout

I want to make this kind of menu of tags in android. Fill layout dynamically and align to center.
How can I do that?
Edited using library recommended by #Dory
<FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:f="http://schemas.android.com/apk/res-auto"
android:orientation="horizontal"
f:debugDraw="false"
f:weightDefault="0"
f:layoutDirection="ltr"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingLeft="6dip"
android:paddingTop="6dip"
android:paddingRight="12dip"
android:paddingBottom="12dip"
android:background="#android:color/black"
android:id="#+id/l_flow"/>
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginBottom="3dp"/>
Flowlayout in one xml file Button is in another xml file. I'm inflating Button then fLayout.addView(button);
what I get is this Padding top and bottom between views is higher than expected
finally I did it with help of this library. I have used only class FlowLayout and attr.xml My main.xml looks like this:
<com.example.FlowTest.FlowLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/flow"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:background="#android:color/white"/>
and my item layout looks like this:
<Button xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:padding="5dp"/>
And in my Activity's onCreate method:
LayoutInflater mInflater = LayoutInflater.from(this);
mFlowLayout = (FlowLayout) findViewById(R.id.flow);
String [] names = {"goods", "shops", "cars", "washing machine","blablabla","clothes","books"};
for(int i = 0; i<names.length;i++){
Button b = (Button)mInflater.inflate(R.layout.item_flow,mFlowLayout, false);
b.setText(names[i]);
mFlowLayout.addView(b);
}
Take a look
here, you can use this library
Edit:
There is attributes provided in the where you can set horizontal and verticalspacing of the Flow Layout. See below sample xml :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:f="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FlowLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
f:horizontalSpacing="5dp"
f:verticalSpacing="5dp" />
</RelativeLayout>
Happy coding :)

Adding buttons below GraphView, all inside a Fragment

I have been trying to add Button's below a GraphView, and all these elements are part of a Fragment. Tried many approaches but none of them worked properly.
This is the layout file for the Fragment (fragment_graph.xml).
<FrameLayout 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="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical"
/>
</FrameLayout>
And this is the Java code dynamically adding a graph and button, placed in the Fragment's onViewCreated (View view, Bundle savedInstanceState).
storageGraph = new LineGraphView(getActivity(), graphLabel);
storageGraph.addSeries(graphSeries); // More config calls follow
...
LinearLayout view = (LinearLayout)getView().findViewById(R.id.graph_fragment_layout);
Button button = new Button(getActivity());
button.setText("Test");
view.addView(storageGraph);
view.addView(button);
The Button is not visible though I have set orientation to vertical for the LinearLayout containing it.
EDIT - solved!
I found that nesting the graph under its own LinearLayout and the buttons under another LinearLayout, and both of these wrapped in a LinearLayout fixed the problem! The LinearLayout containing the graph must be weighted (I chose a weight of 0.8).
Layout file looks like:
<FrameLayout 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="com.nma.util.sdcardtrac.GraphFragment">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/graph_fragment_wrap"
android:orientation="vertical"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="0.8"
android:id="#+id/graph_fragment_layout"
android:orientation="vertical" />
<LinearLayout
android:id="#+id/graph_buttons"
android:orientation="horizontal"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_gravity="center_horizontal"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:src="#drawable/ic_navigation_previous_item"/>
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_navigation_next_item"/>
</LinearLayout>
</LinearLayout>
</FrameLayout>
I've just tried it and it works. Perhaps your graph is taking all the available space, so added button is below the screen? Try to wrap your LinearLayout into a ScrollView and see if there is a button in the bottom.

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>

How can I set attribute onClick to a ScrollView?

I have the following layout
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
<LinearLayout
android:id="#+id/answerMainFrame"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:isScrollContainer="true"
android:onClick="toQuestion" >
<ImageView
android:id="#+id/answer_img"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:contentDescription="#string/question_img_cd" />
<TextView
android:id="#+id/answer"
style="#style/Question" />
</LinearLayout>
Sometimes the ScrollView is too small and won't fill the whole screen but still I want to call the method toQuestion() clicking anywhere on the screen.
I've tried setting android:layout_height="wrap_content" to the LinearLayout and android:onClick="toQuestion" to the ScrollView but same result.
You could try to let the LinearLayout implement the onClick attribute and then set:
android:fillViewport="true"
to the ScrollView.
I had a similar issue, and simply got rid of the scrollview itself. Instead, I directly inserted the TextView itself, with the constraints previously put on the ScrollView. The whole purpose of my ScrollView was to scroll in the TextView, not among several items, so it seemed a more elegant way to do.
Add a (vertical) scrollbar to my textview in activity.hmtl :
<TextView
android:id="#+id/tvInfo"
(...)
android:scrollbars="vertical"
/>
Add this to the onCreate method:
oTV = (TextView) findViewById(R.id.tvInfo);
oTV.setMovementMethod(new ScrollingMovementMethod());
No need to write the scrolling response. I like that.
I found the solution here (thx Juned Mughal) :
http://www.android-examples.com/make-textview-scrollable-in-android-programmatically/
To make the scroll view fill the screen change:
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#android:color/white" >
to
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#android:color/white" >
and for the click put this in your onCreate method:
((ScrollView) findViewById(R.id.scrollView))
.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
toQuestion();
}
});

Categories

Resources