Wrap content not working for dialog - java

My goal is to create a dialog that wraps its content height as shown in the image below.
My problem is that my dialog wants to match its parent as shown in the image below. The height should stop at the item "asdf" and this white space below "asdf" should not exist.
This is my code for the later:
private void showPopupEventTest(final String Cardid) {
Dialog dialog = new Dialog(getActivity(), R.style.Theme_AppCompat_Dialog_Alert);
dialog.getWindow().setLayout(WindowManager.LayoutParams.WRAP_CONTENT, WindowManager.LayoutParams.WRAP_CONTENT);
dialog.setContentView(R.layout.fragment_event);
SwipeRefreshLayout swipeRefreshLt = (SwipeRefreshLayout) dialog.findViewById(R.id.swipeRefreshLt);
ListView lv = (ListView) dialog.findViewById(R.id.Contacts_list_view);
SearchView SearchET = (SearchView) dialog.findViewById(R.id.SearchET);
LinearLayout layoutLinearLayout = (LinearLayout) dialog.findViewById(R.id.layoutLinearLayout);
EventAdaptor myAdapter = new EventAdaptor(getContext(), ArrayList_EventObject);
lv.setAdapter(myAdapter);
dialog.getWindow().setSoftInputMode(
WindowManager.LayoutParams.SOFT_INPUT_STATE_HIDDEN);
dialog.show();
}
And this is the 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="wrap_content"
android:orientation="vertical"
android:id="#+id/layoutLinearLayout"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">
<SearchView
android:id="#+id/SearchET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false" />
<android.support.v4.widget.SwipeRefreshLayout
android:layout_width="match_parent"
android:id="#+id/swipeRefreshLt"
android:layout_height="wrap_content">
<ListView
android:id="#+id/Contacts_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.v4.widget.SwipeRefreshLayout>
</LinearLayout>
What am I doing wrong?

try this
Window window = customDialog.getWindow();
window.setLayout(WindowManager.LayoutParams.MATCH_PARENT, WindowManager.LayoutParams.WRAP_CONTENT);
window.setGravity(Gravity.CENTER);
customDialog.show();

There are two options for you
Use RecyclerView instead of ListView
OR
use below code after setting the adapter.
public static void setListViewHeightBasedOnChildren(ListView listView) {
ListAdapter listAdapter = listView.getAdapter();
if (listAdapter == null) {
// pre - condition
return;
}
int totalHeight = 0;
for (int i = 0; i < listAdapter.getCount(); i++) {
View listItem = listAdapter.getView(i, null, listView);
listItem.measure(0, 0);
totalHeight += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams params = listView.getLayoutParams();
params.height = totalHeight + (listView.getDividerHeight() * (listAdapter.getCount() - 1));
listView.setLayoutParams(params);
listView.requestLayout();
}

Your problem is in your xml layout. Try this:
<?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:orientation="vertical"
android:id="#+id/layoutLinearLayout"
android:paddingLeft="8dp"
android:paddingRight="8dp"
android:paddingTop="8dp">
<SearchView
android:id="#+id/SearchET"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:iconifiedByDefault="false" />
<!--<android.support.v4.widget.SwipeRefreshLayout-->
<!--android:layout_width="match_parent"-->
<!--android:id="#+id/swipeRefreshLt"-->
<!--android:layout_height="wrap_content">-->
<ListView
android:id="#+id/Contacts_list_view"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<!--</android.support.v4.widget.SwipeRefreshLayout>-->
</LinearLayout>
Explanation:
The problem was the SwipeRefreshLayout. I commented it

Related

Dynamically adding a view to a custom ListView item layout not working

I wish to add a custom tailored layout for each ListView item. I have an empty linear layout in the xml layout file for ListView item layout. It has horizontal orientation.
I wish to add three FrameLayouts dynamically to above mentioned LinearLayout and set custom weight for each FrameLayout.
I found a part of the solution for it, as in the code below. Problem arises when the ListView has to be updated. ListView is initially hidden. When a certain button is pressed, ListView gets updated (hash map cleared and then filled with a new data set) and its visibility set from GONE to VISIBLE. Next thing is to loop through all the items and add designed view programmatically.
If I invoke the method add_designed_views(null); right after updating ListView items, the app loops through items, creates all the new views and adds them to right items. However, they are not added. I know this because of check if the item is already added, and if I invoke this method several times in a row, it will do the same each time, as if it didn't add anything at all.
Next thing that I tried is I postponed adding views dynamically by invoking the method add_designed_views from onClick of a dummy button. When I see the list has loaded (without the custom views) I press the dummy button and the custom views appear. But every 6th is missing. If I press it again, the missing ones appear. Which makes me even more confused.
It's obvious that the views can't be added immediately after changing the visibility of ListView. Probably because it takes the system some time to inflate the ListView, or it does some background task, but I couldn't find any listener or some function that tells me that it has finished loading so that I can add my views then.
Custom ListView item 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="wrap_content"
android:minHeight="110dp"
android:background="#drawable/textview_border_dark_line"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="21"
android:minHeight="40dp">
<TextView
android:id="#+id/text1"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="70"
android:minHeight="90dp"
android:orientation="vertical">
<LinearLayout
android:id="#+id/path_model"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal"
android:onClick="onClick_listItem"
>
<!-- Path Model. -->
<!-- This is where I add view dynamically. -->
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="40"
android:orientation="vertical">
<TextView
android:id="#+id/text2"
android:textSize="12dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="0dp"
android:layout_weight="50"
android:layout_centerInParent="true"
android:layout_centerHorizontal="true" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="50"
android:orientation="horizontal">
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_time"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/clock"
android:layout_centerVertical="true" />
<TextView
android:id="#+id/text4_1"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_time" />
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_changes"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/change"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_2"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_changes"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
<ImageView
android:id="#+id/ic_cost"
android:layout_width="16dp"
android:layout_height="16dp"
android:src="#drawable/coins"
android:layout_centerVertical="true"/>
<TextView
android:id="#+id/text4_3"
android:textSize="14dp"
android:textColor="#454545"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="50"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ic_cost"
/>
</RelativeLayout>
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_weight="10">
</RelativeLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<RelativeLayout
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="21"
android:minHeight="38dp">
<TextView
android:id="#+id/text3"
android:textSize="16sp"
android:textStyle="bold"
android:textColor="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingLeft="10dp"
android:paddingTop="15dp"
/>
</RelativeLayout>
</LinearLayout>
add_designed_views method (Java):
public void add_designed_views(View view)
{
for(int i=0; i<hashmap_search_results_list.size(); i++)
{
/* Check if it is already added. We have to check,
because we run it twice in consecutive order. */
LinearLayout listView_item = (LinearLayout)getViewByPosition(i, listView_search_results);
View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
LinearLayout already_added_ll_path = (LinearLayout)((ViewGroup)path_model).getChildAt(0);
if(already_added_ll_path != null)
{
Log.d(TAG, "Already has linear layout: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
continue;
}
Log.d(TAG, "Doing: " + hashmap_search_results_list.get(i).get("latest_departure_time"));
Search_Result_Item result_item = (Search_Result_Item)search_results_list.get(i);
/* How to access each view at each item position in list view. */
//LinearLayout listView_item = (LinearLayout) getViewByPosition(i, listView_search_results);
//View middle_ground = ((ViewGroup) listView_item).getChildAt(1);
//LinearLayout path_model = (LinearLayout) ((ViewGroup) middle_ground).getChildAt(0);
/* Setting the proportions of lines regarding walking time and riding time. */
HrMin hrmin_walking_to_entrance_time = result_item.walking_to_entrance_time();
HrMin hrmin_riding_time = result_item.travel_time();
HrMin hrmin_walking_from_exit_time = result_item.walking_from_exit_time();
int int_walking_to_entrance_time = hrmin_get_minutes(hrmin_walking_to_entrance_time);
int int_riding_time = hrmin_get_minutes(hrmin_riding_time);
int int_walking_from_exit_time = hrmin_get_minutes(hrmin_walking_from_exit_time);
int total = int_walking_to_entrance_time +
int_riding_time +
int_walking_from_exit_time;
float weight_walking_to_entrance = (float)int_walking_to_entrance_time/total;
float weight_bus_ride = (float)int_riding_time/total;
float weight_walking_from_exit = (float)int_walking_from_exit_time/total;
/* How to add a new layout, view to existing layout. */
LinearLayout ll_path = new LinearLayout(this);
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams
(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT);
/* Pedestrian image. */
ImageView pedestrian = new ImageView(this);
pedestrian.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
int dimensionInPixel = 16;
int dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian.setLayoutParams(pedestrian_params);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_to_entrance);
View gray_line = new View(this);
gray_line.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line.setLayoutParams(gray_line_params);
frameLayout_pedestrian_container.addView(gray_line);
frameLayout_pedestrian_container.addView(pedestrian);
frameLayout_pedestrian_container.setLayoutParams(frame_layout_params);
ll_path.addView(frameLayout_pedestrian_container);
/* Bus image. */
FrameLayout frameLayout_bus_container = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_bus_params = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_bus_ride);
ImageView bus = new ImageView(this);
bus.setBackground(getResources().getDrawable(R.drawable.bus_rnd));
dimensionInPixel = 17;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams bus_params = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
bus.setLayoutParams(bus_params);
/* Green line. */
View green_line = new View(this);
green_line.setBackground(getResources().getDrawable(R.drawable.horizontal_green_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams green_line_params = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
green_line.setLayoutParams(green_line_params);
frameLayout_bus_container.addView(green_line);
frameLayout_bus_container.addView(bus);
frameLayout_bus_container.setLayoutParams(frame_layout_bus_params);
ll_path.addView(frameLayout_bus_container);
/* Walking from exit path. */
ImageView pedestrian2 = new ImageView(this);
pedestrian2.setBackground(getResources().getDrawable(R.drawable.runner_rnd));
dimensionInPixel = 16;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams pedestrian_params2 = new FrameLayout.LayoutParams(
dimensionInDp,
dimensionInDp,
Gravity.CENTER_VERTICAL);
pedestrian2.setLayoutParams(pedestrian_params2);
/* Gray Line. */
FrameLayout frameLayout_pedestrian_container2 = new FrameLayout(this);
LinearLayout.LayoutParams frame_layout_params2 = new LinearLayout.LayoutParams(
0,
LinearLayout.LayoutParams.MATCH_PARENT,
weight_walking_from_exit);
View gray_line2 = new View(this);
gray_line2.setBackground(getResources().getDrawable(R.drawable.horizontal_gray_line));
dimensionInPixel = 4;
dimensionInDp = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dimensionInPixel, getResources().getDisplayMetrics());
FrameLayout.LayoutParams gray_line_params2 = new FrameLayout.LayoutParams(
LinearLayout.LayoutParams.MATCH_PARENT,
dimensionInDp,
Gravity.CENTER_VERTICAL);
gray_line2.setLayoutParams(gray_line_params2);
frameLayout_pedestrian_container2.addView(gray_line2);
frameLayout_pedestrian_container2.addView(pedestrian2);
frameLayout_pedestrian_container2.setLayoutParams(frame_layout_params2);
ll_path.addView(frameLayout_pedestrian_container2);
ll_path.setLayoutParams(params);
ll_path.setOrientation(LinearLayout.HORIZONTAL);
path_model.addView(ll_path);
}
}
Part where I update the ListView and show it:
search_results.setVisibility(View.VISIBLE);
ListView is a child of two layouts:
<LinearLayout
android:id="#+id/search_results"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/white"
android:visibility="gone"
android:paddingTop="140dp"
android:orientation="vertical"
android:onClick="onClick_nothing">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/search_options_list"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_constraintBottom_toBottomOf="#+id/mapView"
app:layout_constraintTop_toTopOf="#+id/mapView"
android:background="#android:color/white"
android:orientation="vertical"
>
<ListView
android:id="#android:id/list"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:scrollbarAlwaysDrawVerticalTrack="true"
android:scrollbarThumbVertical="#android:color/darker_gray"
/>
</RelativeLayout>
</LinearLayout>
One thing that helped is Handler.postDelayed():
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
add_designed_views (null);
}
}, 300);
This function delays method call for 300 mS. Programmatically added layouts are visible. But, still every 6th is missing. They also appear after I touch any of the items.

Android making an scrolled two independent listview in activity

I am working in Android Studio in a new app and I want to create a layout with two independent scrolled listview (each listview scrolling in independent way) in the same activity. I cannot obtain the target that I want. Could you give me some advice?
I am not sure about efficient way but I worked in following way.
First, I have created two dynamic ListView and put in into one activity.
It will calculate the height dynamically rather than match_parent.
Here is the code:
public void setListViewDynamicHeight(ListView listView) {
ListAdapter adapter = listView.getAdapter();
if (adapter == null) {
return;
}
int height = 0;
int desiredWidth = View.MeasureSpec.makeMeasureSpec(listView.getWidth(), View.MeasureSpec.UNSPECIFIED);
for (int i = 0; i < adapter.getCount(); i++) {
View listItem = adapter.getView(i, null, listView);
listItem.measure(desiredWidth, View.MeasureSpec.UNSPECIFIED);
height += listItem.getMeasuredHeight();
}
ViewGroup.LayoutParams layoutParams = listView.getLayoutParams();
layoutParams.height = height + (listView.getDividerHeight() * (adapter.getCount() - 1));
listView.setLayoutParams(layoutParams);
listView.requestLayout();
}
You can build it in your layout xml like this:
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_1"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
<android.support.v4.widget.NestedScrollView
android:id="#+id/nested_scroll_view_2"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Hallo!" />
<!--Other views inside NestedScrollView-->
</android.support.v4.widget.NestedScrollView>
</LinearLayout>

Android - Dynamic Edit Text Not Visible

I need to show n number of Edit text added dynamically below a static Edit text.
I am adding the ET but some how it is not visible. What am doing wrong??
Here is my code:
private void addEditTextView(int numberOfViews){
// Using layout params same as above static ET
ViewGroup.LayoutParams layoutParams = staticEditText.getLayoutParams();
for (int index = 0; index <= numberOfViews; index++) {
final EditText newET = new EditText(getActivity());
//Added below 2 lines just to make sure width and height are coming
layoutParams.height = ViewGroup.LayoutParams.WRAP_CONTENT;
layoutParams.width = ViewGroup.LayoutParams.MATCH_PARENT;
newET.setLayoutParams(layoutParams);
newET.setHint("Select ME");
newET.setFocusable(false);
newET.setId(index);
newET.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//TODO set time
Log.d("Clicked...",newET.getId()+"");
}
});
//parentLayout is Linear Layout with Vertical orientation
parentLayout.addView(newET);
}
}
XML code :
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<EditText
android:id="#+id/staticEditText"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="3dp"
android:background="#drawable/rectangle_bg_dark_gray_border"
android:focusable="false"
android:hint="staticEditText"
android:padding="7dp" />
</LinearLayout>
UPDATE : parentLayout.getChildCount() is coming correctly. New views are getting added but not visible!
Hi try adding a Scrollview
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/parentLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<!-- EDITTEXT here -->
</LinearLayout>
</ScrollView>
</LinearLayout>

LinearLayout not displaying Items

What i'm trying to do is add create an application that locates some local business. So the activity I'm doing displays the business information. Some business have severals stores located on the city, and some others only have one. So here is my problem: In the View I have Scroll View with all the elements, and a linear layout inside the scrollview. If the business has more then one store, i will add each store information in a new text view and add the text view to the layout (this is done all by code). But at the moment to display the layout, it only displays me one store, instead of showing me 3 or 4. What I'm I doing wrong? Here is the method setupViews(), which is the one in chanrge of the displaying:
private void setupViews() throws SQLException {
ImageView iv = (ImageView) findViewById(R.id.negocio_logo);
try {
iv.setImageBitmap(BitmapFactory.decodeByteArray(toShow.getImgSrc(),
0, toShow.getImgSrc().length));
} catch (NullPointerException npe) {
iv.setBackgroundColor(Color.WHITE);
}
TextView nombreEmpresa = (TextView) findViewById(R.id.nombre_empresa);
nombreEmpresa.setText(toShow.getNombre());
TextView descripcionEmpresa = (TextView) findViewById(R.id.descripcion_empresa);
descripcionEmpresa.setText(toShow.getDescripcion());
TextView direccionEmpresa = (TextView) findViewById(R.id.direccion_empresa);
direccionEmpresa.setText(toShow.getDireccion());
LinearLayout rl = (LinearLayout) findViewById(R.id.linear_layout_si);
TextView suc = (TextView) findViewById(R.id.sucursales_empresa);
sucursalDAO sDAO = new sucursalDAO();
boolean tieneSucursales = sDAO.hasSucursales(toShow.getId());
if (tieneSucursales == false) {
suc.setVisibility(View.GONE);
// sucs.setVisibility(View.GONE);
} else {
suc.setVisibility(View.VISIBLE);
ArrayList<String> sucursales = sDAO.getStringSucursales(toShow
.getId());
ArrayList<TextView> tvs = new ArrayList<TextView>();
for (int i = 0; i < sucursales.size(); i++) {
TextView tv = new TextView(this);
tv.setText(sucursales.get(i));
tv.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,
LayoutParams.WRAP_CONTENT));
tvs.add(tv);
}
for (int i = 0; i < tvs.size(); i++) {
rl.addView(tvs.get(i), i);
}
}
}
And here is the XML of my view:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/RL"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/White"
android:orientation="vertical" >
<ScrollView
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/widgetscroll"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/White"
android:orientation="vertical" >
<ImageView
android:id="#+id/negocio_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="#string/logo_negocio" />
<TextView
android:id="#+id/datos_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/negocio_logo"
android:background="#drawable/barra"
android:text="#string/datos_empresa"
android:textColor="#color/White" />
<TextView
android:id="#+id/nombre_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/datos_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/descripcion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/nombre_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/direccion_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/descripcion_empresa"
android:text="#string/testing" />
<TextView
android:id="#+id/sucursales_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/direccion_empresa"
android:background="#drawable/barra"
android:text="#string/sucursales"
android:visibility="gone" />
<LinearLayout
android:id="#+id/linear_layout_si"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/sucursales_empresa" >
</LinearLayout>
<TextView
android:id="#+id/contacto_empresa"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/linear_layout_si"
android:text="#string/testing" />
</RelativeLayout>
</ScrollView>
</RelativeLayout>
I think you forgot to set orientation on LinearLayout and it's showing horitzontal
I bet you forgot to set the orientation of the LinearLayout.

How to display the same layout for two different tabs in a TabHost?

I'm working on an Android application with an Activity that uses a tab layout. There are two tabs which switch between the content being shown in some TextViews.
This means that the two tab specifications point to the same layout (linear) for content, R.id.plantilla:
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
<TabWidget
android:id="#android:id/tabs"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<include layout="#layout/plantilla"/>
</FrameLayout>
</LinearLayout>
But this only works if I switch to tab 2 and back to 1, i.e when the activity launches, the Layout "plantilla" can't be seen before tabs are changed. This is my problem.
What's the most simple way to get around this?
PD: I have tried to duplicate the line
<include layout="#layout/plantilla">
in the tabhost xml, but in this case I can not access at the TextViews objects from the Java code using findViewById(R.id.someTextView);
I don't believe you can use include, I think you need to actually define your layout twice in the xml with different id tags.
Or you could simply define the container view and then add views to it programatically. I've done that before. Here's how I did it:
tablayout
<?xml version="1.0" encoding="utf-8"?>
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent" >
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:padding="5dp" >
<TextView
android:id="#+id/setupheader"
android:layout_width="fill_parent"
android:layout_height="20dp"
android:textSize="15dp" />
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="30dp"
android:gravity="bottom" />
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp" >
<!-- General Info Tab -->
<LinearLayout
android:id="#+id/general_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
</LinearLayout>
<!-- Tool Tab -->
<LinearLayout
android:id="#+id/tool_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/list1"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
<!-- Offset Tab -->
<LinearLayout
android:id="#+id/offset_tab"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<ListView
android:id="#+id/list2"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:drawSelectorOnTop="false" />
</LinearLayout>
<!-- Notes Tab -->
<LinearLayout
android:id="#+id/note_tab"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical" >
</LinearLayout>
</FrameLayout>
</LinearLayout>
</TabHost>
Following is my tab activity (I've removed significant portions, but it should show well enough how I went about doing tabs without separate activites for each tab).
tabactivity
public class SetupDisplay extends TabActivity {
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.setupdetailmain);
Bundle extras = getIntent().getExtras();
RowId = extras.getLong("RowId");
StartTab = extras.getInt("StartTab");
// ***************************************************************
// Set up the tabs in the tabhost
// ***************************************************************
tabHost = getTabHost();
TabHost.TabSpec spec;
spec = tabHost.newTabSpec("General").setIndicator("General")
.setContent(R.id.general_tab);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Tools").setIndicator("Tools")
.setContent(R.id.tool_tab);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Offsets").setIndicator("Offsets")
.setContent(R.id.offset_tab);
tabHost.addTab(spec);
spec = tabHost.newTabSpec("Notes").setIndicator("Notes")
.setContent(R.id.note_tab);
tabHost.addTab(spec);
populateTabs(StartTab);
}
// ***************************************************************
// Clear views from linear layout tabs
// ***************************************************************
private void clearTabs() {
general.removeAllViews();
notes.removeAllViews();
}
// ***************************************************************
// Fill the tabs
// ***************************************************************
private void populateTabs(int TabShown) {
generaltab();
tooltab();
notestab();
offsettab();
tabHost.setCurrentTab(TabShown);
}
// ***************************************************************
// Fill the General tab
// ***************************************************************
private void generaltab() {
general = (LinearLayout) findViewById(R.id.general_tab);
String prgdisp = SETUPINFO_PGM1;
if (SETUPINFO_PGM2 != null) {
prgdisp = prgdisp + ", " + SETUPINFO_PGM2;
}
if (SETUPINFO_PGM3 != null) {
prgdisp = prgdisp + ", " + SETUPINFO_PGM3;
}
TextView programs = new TextView(this);
programs.setText("Program(s): " + prgdisp);
programs.setTextSize(20);
general.addView(programs);
if (SETUPINFO_KIT == null || SETUPINFO_KIT.equals("No Kit")) {
} else {
TextView kit = new TextView(this);
kit.setText("Kit: " + SETUPINFO_KIT);
kit.setTextSize(20);
general.addView(kit);
}
if (SETUPINFO_FIXTURE1 == null && SETUPINFO_FIXTURE2 == null) {
} else {
String fixtdisp = SETUPINFO_FIXTURE1;
if (SETUPINFO_FIXTURE2 != null) {
fixtdisp = fixtdisp + ", " + SETUPINFO_FIXTURE2;
}
TextView fixtures = new TextView(this);
fixtures.setText("Fixture(s): " + fixtdisp);
fixtures.setTextSize(20);
general.addView(fixtures);
TextView fixtureloc = new TextView(this);
fixtureloc.setText("Fixture Location: " + SETUPINFO_FIXTURELOC);
fixtureloc.setTextSize(20);
general.addView(fixtureloc);
}
if (SETUPINFO_VISE == null) {
} else {
TextView vise = new TextView(this);
vise.setText("Vise(s): " + SETUPINFO_VISE);
vise.setTextSize(20);
general.addView(vise);
}
}
// ***************************************************************
// Fill the Offset tab
// ***************************************************************
private void offsettab() {
wpccount = 0;
for (int i = 0; i < 20; i++) {
if (wpcdesc[i] != null) {
wpcdisplayhold[wpccount] = wpcid[i] + " - " + wpcdesc[i];
wpcidhold[wpccount] = wpcid[i];
wpcdeschold[wpccount] = wpcdesc[i];
wpccount++;
}
}
wpcdisplay = new String[wpccount];
for (int i = 0; i < wpccount; i++) {
wpcdisplay[i] = wpcdisplayhold[i];
}
mWPCView = (ListView) findViewById(R.id.list2);
mWPCView.setAdapter(new ColorizingListAdapter(SetupDisplay.this,
wpcdisplay, "Offset"));
registerForContextMenu(mWPCView);
}
// ***************************************************************
// Fill the Notes tab
// ***************************************************************
private void notestab() {
notes = (LinearLayout) findViewById(R.id.note_tab);
notestxt = new TextView(this);
notestxt.setText(SETUPINFO_NOTES);
notestxt.setTextSize(15);
notes.addView(notestxt);
}
}
Hope this helps.
Please remove the include tag from your xml layout and leave something simple:
<?xml version="1.0" encoding="utf-8"?>
<!-- Tab Host -->
<TabHost xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#android:id/tabhost"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<!-- Bottom tab bar -->
<LinearLayout
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:padding="5dp">
<FrameLayout
android:id="#android:id/tabcontent"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:layout_weight="1"/>
<!-- -5dip bottom margin for not showing the annoying bottom line -->
<TabWidget
android:id="#android:id/tabs"
android:layout_width="fill_parent"
android:layout_height="65dip"
android:layout_weight="0"
android:layout_marginBottom="-5dip"/>
</LinearLayout>
</TabHost>
And from your TabActivity class just put this in onCreate():
//Set tab host
tabHost = getTabHost();
//First Tab
TabSpec firstTabSpec = tabHost.newTabSpec("FirstTab");//title
firstTabSpec.setIndicator("FirstTab", getResources().getDrawable(R.drawable.item_first_tab)); //icon
Intent firstTabIntent = new Intent(this, FirstTab.class);//action
firstTabSpec.setContent(firstTabIntent);//set tab
//Second Tab
TabSpec secondTabSpec = tabHost.newTabSpec("SecondTab");//title
secondTabSpec.setIndicator("SecondTab", getResources().getDrawable(R.drawable.second_tab));//icon
Intent secondTabIntent = new Intent(this, FirstTab.class);//action
secondTabSpec.setContent(secondTabIntent);//set tab
//Add tabs to tab host
tabHost.addTab(firstTabSpec);//add first tab
tabHost.addTab(secondTabSpec);//add second tab which goes to your FirstTab.class
You can create different Activities for your tabs or use a single one.

Categories

Resources