searchview not expanding full width - android

Basically I want the searachview to expand and collapse when there is a imagebutton behind which should disappear when icon is clicked and view should appear when searchview collapses.
I am using search view not in action bar .When i click on the search icon it expands only half the screen but it should expand complete width but should wrap_content when I close the searchview.
<SearchView
android:id="#+id/searchView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true">
I also tried :
android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();
params.setLayoutParams(new LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT));
But I am getting error here setLayoutParams it says "add to cast"

At first you're getting an exception, because if you look into search_view.xml located in
\Android\android-sdk\platforms\android-18\data\res\layout\
You'll find out that SearchView is basically simple LinearLayout merging several widgets together. That's why you are getting class cast exception. Instead of
android.view.ViewGroup.LayoutParams params = searchview.getLayoutParams();
use
LinearLayout.LayoutParams params = (LinearLayout.LayoutParams) searchview.getLayoutParams();
or just import correct class and then you can simply use this
import android.widget.LinearLayout.LayoutParams;
...
LayoutParams params = (LayoutParams) searchview.getLayoutParams();
than you can use LayoutParams without any prefix.
But I don't think that setting LayoutParams to wrap content will help. I would wrap your SearchView and ImageButton to RelativeLayout, where you could specify view position relative to the other views. Then simply set SearchView width to fill parent, so it could fill remaining space.

Have you tried the XML-attribute android:maxWidth like described here: http://developer.android.com/reference/android/widget/SearchView.html#attr_android:maxWidth
I am using the SearchView from actionbarsherlock and used the corresponding java-method to stretch the SearchView over the available width of the actionbar.

<item
android:id="#+id/action_search"
android:icon="#drawable/search_b"
android:orderInCategory="3"
android:title="Search"
app:showAsAction="always|collapseActionView"
app:actionViewClass="android.support.v7.widget.SearchView" />
<item
android:id="#+id/action_scan"
android:icon="#drawable/scan_b"
android:title="Scan"
android:orderInCategory="2"
app:showAsAction="ifRoom|collapseActionView" />
<item
android:id="#+id/action_add_cusomer"
android:icon="#drawable/add_cust_b"
android:title="Add Customer"
android:orderInCategory="1"
app:showAsAction="ifRoom|collapseActionView" />
</menu>
After Clicking on Search icon:

Try this according to your needs but dont think to work on your own button, you can move default searchButton to right side with giving wrap_content width parameter and then when you click to search set width to match_parent then again when you close it set width to wrap_content
Use your searchView child of RelativeLayout and be sure about right LayoutParams(RelativeLayout.LayoutParams)
<?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="wrap_content"
android:background="#color/counter_text_color" >
<Button
android:id="#+id/buttonLeftMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerInParent="true"
android:background="#drawable/ic_drawer" />
<ImageView
android:id="#+id/iv_logo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:background="#drawable/actionbar_logo" />
<TextView
android:id="#+id/tw_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="YOUR "
android:textSize="24sp" />
<SearchView
android:id="#+id/searchView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toLeftOf="#+id/buttonRightMenu" />
<Button
android:id="#+id/buttonRightMenu"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerInParent="true"
android:background="#drawable/ic_action_settings" />
</RelativeLayout>
and at your class
searchView.setOnSearchClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.RIGHT_OF, R.id.buttonLeftMenu);
params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
searchView.setLayoutParams(params);
}
});
searchView.setOnCloseListener(new OnCloseListener() {
#Override
public boolean onClose() {
// TODO Auto-generated method stub
RelativeLayout.LayoutParams params = new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT, RelativeLayout.LayoutParams.WRAP_CONTENT);
params.addRule(RelativeLayout.LEFT_OF, R.id.buttonRightMenu);
searchView.setLayoutParams(params);
return false;
}
});

Related

Android Custom Snackbar: can't remove margins

Regardless of whether or not it is a good idea to create a custom Snackbar, I have a custom Snackbar and I can't seem to get rid of the margins. I've tried several things such as adjusting in code and in layout as shown below. Nothing seems to work.
I am using the approach laid out by Yakiv here:
final CoordinatorLayout.LayoutParams params = (CoordinatorLayout.LayoutParams) snackBarView.getLayoutParams();
params.setMargins(0,0,0,0);
and other approaches such as
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp" ...
The Snackbar:
My layout file:
<view
xmlns:android="http://schemas.android.com/apk/res/android"
class="android.support.design.internal.SnackbarContentLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#color/realBlack"
android:layout_marginLeft="0dp"
android:layout_marginStart="0dp"
android:paddingLeft="0dp"
android:paddingStart="0dp">
<TextView
android:id="#+id/snackbar_text"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_weight="1"
android:paddingTop="#dimen/design_snackbar_padding_vertical"
android:paddingBottom="#dimen/design_snackbar_padding_vertical"
android:paddingLeft="#dimen/design_snackbar_padding_horizontal"
android:paddingRight="#dimen/design_snackbar_padding_horizontal"
android:textAppearance="#style/TextAppearance.Design.Snackbar.Message"
android:maxLines="#integer/design_snackbar_text_max_lines"
android:layout_gravity="center_vertical|left|start"
android:ellipsize="end"
android:textAlignment="viewStart"
android:text="get some"/>
<Button
android:id="#+id/snackbar_action"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="#dimen/design_snackbar_extra_spacing_horizontal"
android:layout_marginStart="#dimen/design_snackbar_extra_spacing_horizontal"
android:layout_gravity="center_vertical|right|end"
android:minWidth="48dp"
android:visibility="gone"
android:textColor="?attr/colorAccent"
style="?attr/borderlessButtonStyle"/>
</view>
The calling code:
_firmwareSnackbar = CustomSnackbar.make((ViewGroup) _rootView, Snackbar.LENGTH_INDEFINITE);
_firmwareSnackbar.setText(_flipper.getContext().getString(R.string.settings_firmware_available));
_firmwareSnackbar.setAction(_flipper.getContext().getString(R.string.settings_firmware_start), new View.OnClickListener() {
#Override
public void onClick(View view) {
_firmwareSnackbar.dismiss();
_firmwareSnackbar = null;
}
});
_firmwareSnackbar.show();
If you are using material components, then add this code to your style.xml file under values
<style name="Widget.SnackBar" parent="Widget.MaterialComponents.Snackbar">
<item name="android:layout_margin">0dp</item>
</style>
then
<style name="AppTheme" parent="Theme.MaterialComponents.NoActionBar">
<!-- Customize your theme here. -->
<item name="snackbarStyle">#style/Widget.SnackBar</item>
</style>
This will change margin for all existing snackBar in the app. So if you want to show a gap around SnackBar you can use padding.
val snackBar = Snackbar.make(homeLayout, "", Snackbar.LENGTH_INDEFINITE)
val snackBarLayout = snackBar.view as Snackbar.SnackbarLayout
snackBarLayout.setPadding(8, 0, 8, 0)
In dimens.xml.
Use this:
<dimen name="design_snackbar_padding_horizontal">0dp</dimen>
But remember that this will get applied to all the snackbars in your application.
Before showing the Snackbar you can remove the parent layout padding in the following way:
//...
_firmwareSnackbar.getView().setPadding(0,0,0,0);
_firmwareSnackbar.show();
Add to theme of App or Activity <item name="snackbarStyle">#style/Widget.Design.Snackbar</item>
You shoud remove paddings from a parent view:
View snackBarLayout = findViewById(R.id.mainLayout);
Snackbar globalSnackbar = Snackbar.make(snackBarLayout, "", Snackbar.LENGTH_INDEFINITE);
Snackbar.SnackbarLayout layout = (Snackbar.SnackbarLayout) globalSnackbar.getView();
layout.setPadding(0,0,0,0);

Put on the right the indicator of an ExpandableListView in Android

I have to move the indicator from the left to the right (because of the plane image). I couldn't succeed also because the expandableviewlist is inside a fragment and not inside a whole activity. Any idea? Thanks!
I don't know a way to do that from XML but i'll tell you a way to do so dynamically in your adapter.
First you have to remove group indicator from your xml
<ExpandableListView [...]
android:groupIndicator="#null" />
Then in your layout of the parent add an imageview in the right position of your layout.
Then in your custom adapter do the following
public View getGroupView(int groupPosition, boolean isExpanded,
View convertView, ViewGroup parent) {
...
if (isExpanded) {
groupHolder.img.setImageResource(R.drawable.group_down);
} else {
groupHolder.img.setImageResource(R.drawable.group_up);
}
...
}
One more solution is:
1) First set groupIndicator in your ExpandableListView to #null:
<ExpandableListView [...]
android:groupIndicator="#null" />
2) Then create group_indicator.xml file with following details:
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="#drawable/down_icon" android:state_selected="false"></item>
<item android:drawable="#drawable/up_icon" android:state_selected="true"></item>
<item android:drawable="#drawable/down_icon"></item>
</selector>
3) Then create group_header.xml layout with following details and inflate this layout in getGroupView() method of ExpandableListAdapter.java:
<?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="wrap_content"
android:padding="10dp">
<TextView
android:id="#+id/tvHeader"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:textColor="#color/white"
android:layout_centerVertical="true"
android:textSize="16sp"/>
<ImageView
android:id="#+id/ivGroupIndicator"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/group_indicator"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"/>
</RelativeLayout>
3) In getGroupView() method of your ExpandableListAdapter.java class, just set the following:
ivGroupIndicator.setSelected(isExpanded);
With this approach, your down_icon and up_icon will work properly.
Hope this helps.
put this into your xml view:
<ExpandableListView
...
android:layoutDirection="rtl" />
than you can set the gravity of your text title in your layout item according your preference.
your text view parent list item:
<TextView
...
android:gravity="center"/>
eg. result:
another solution to put the indicator on the right programmatically:
expandableListView = (ExpandableListView) findViewById(R.id.expandableListView);
Display display = getWindowManager().getDefaultDisplay();
Point size = new Point();
display.getSize(size);
int width = size.x;
Resources r = getResources();
int px = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP,
50, r.getDisplayMetrics());
if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.JELLY_BEAN_MR2) {
expandableListView.setIndicatorBounds(width - px, width);
} else {
expandableListView.setIndicatorBoundsRelative(width - px, width);
}
where expandableListView is your ExpandableListview
In your groupcustom.xml file you can use Relativelayout and put that image to
android:alignParentRight = "true";

Align icon and the title in Listview item

Im designing my interfaces for my research project on android. So I added facebook kind side navigation listview. But when the itesms are added to the list view it will not get align center horizontally..I need to get the icon origin and the title in a same horizontal line. Please be kind enough to provide me a solution for my problem. Below provided my code. I'm sorry about my english.
Here is an image of my current interface. Please refer settings button. The settings word is bit upside. I need both icon and settings word aligh horizontally in same line.
image is here http://sphotos-e.ak.fbcdn.net/hphotos-ak-ash4/485944_4792129535506_1863888545_n.jpg
slide.xml - all the items for the listview are included here
<menu xmlns:android="http://schemas.android.com/apk/res/android" >
<item
android:id="#+id/item_one"
android:icon="#drawable/settings"
android:title="#string/item_one"
android:top="30dp"
>
</item>
<item
android:id="#+id/item_two"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_three"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_four"
android:icon="#drawable/ic_launcher"
android:title="#string/item_four">
</item>
<item
android:id="#+id/item_five"
android:icon="#drawable/ic_launcher"
android:title="#string/item_one">
</item>
<item
android:id="#+id/item_six"
android:icon="#drawable/ic_launcher"
android:title="#string/item_two">
</item>
<item
android:id="#+id/item_seven"
android:icon="#drawable/ic_launcher"
android:title="#string/item_three">
</item>
<item
android:id="#+id/item_eight"
android:icon="#drawable/ic_launcher"
android:title="#string/item_four"
>
</item>
</menu>
slidemenu.xml - the listview is here
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
<LinearLayout
android:layout_width="260dip"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#2c323f"
>
<ImageView
android:id="#+id/menu_header"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="left"
android:layout_marginTop="10dp"
android:layout_marginBottom="10dp"
android:layout_marginLeft="10dp"
/>
<ListView
android:id="#+id/menu_listview"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:scrollbars="none"
android:background="#2c323f"
android:fadingEdge="none"
android:overScrollMode="never"
android:listSelector="#454b5d"
android:divider="#layout/divider"
android:dividerHeight="1sp"
android:cacheColorHint="#2c323f"
/>
</LinearLayout>
<FrameLayout
android:id="#+id/overlay"
android:layout_width="match_parent"
android:layout_height="match_parent" >
</FrameLayout>
</LinearLayout>
here is my activity class
package com.coboltforge.slidemenuexample;
import com.coboltforge.slidemenu.SlideMenu;
import com.coboltforge.slidemenu.SlideMenu.SlideMenuItem;
import com.coboltforge.slidemenu.SlideMenuInterface.OnSlideMenuItemClickListener;
import android.app.Activity;
import android.os.Bundle;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity implements OnSlideMenuItemClickListener {
private SlideMenu slidemenu;
private final static int MYITEMID = 42;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
/*
* There are two ways to add the slide menu:
* From code or to inflate it from XML (then you have to declare it in the activities layout XML)
*/
// this is from code. no XML declaration necessary, but you won't get state restored after rotation.
// slidemenu = new SlideMenu(this, R.menu.slide, this, 333);
// this inflates the menu from XML. open/closed state will be restored after rotation, but you'll have to call init.
slidemenu = (SlideMenu) findViewById(R.id.slideMenu);
slidemenu.init(this, R.menu.slide, this, 333);
// this can set the menu to initially shown instead of hidden
// slidemenu.setAsShown();
// set optional header image
slidemenu.setHeaderImage(getResources().getDrawable(R.drawable.ic_launcher));
// this demonstrates how to dynamically add menu items
SlideMenuItem item = new SlideMenuItem();
item.id = MYITEMID;
item.icon = getResources().getDrawable(R.drawable.ic_launcher);
item.label = "Dynamically added item";
slidemenu.addMenuItem(item);
// connect the fallback button in case there is no ActionBar
Button b = (Button) findViewById(R.id.buttonMenu);
b.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
slidemenu.show();
}
});
}
public void onSlideMenuItemClick(int itemId) {
switch(itemId) {
case R.id.item_one:
Toast.makeText(this, "Item one selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_two:
Toast.makeText(this, "Item two selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_three:
Toast.makeText(this, "Item three selected", Toast.LENGTH_SHORT).show();
break;
case R.id.item_four:
Toast.makeText(this, "Item four selected", Toast.LENGTH_SHORT).show();
break;
case MYITEMID:
Toast.makeText(this, "Dynamically added item selected", Toast.LENGTH_SHORT).show();
break;
}
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch(item.getItemId()) {
case android.R.id.home: // this is the app icon of the actionbar
slidemenu.show();
break;
}
return super.onOptionsItemSelected(item);
}
}
the settings word is bit upside. I need both icon and settings word
aligh horizontally in same line
I'm guessing you use this library. The problem is that you can't access the layout file that is used for the row view of the sliding menu adapter to modify it. But you can easily solve this if you copy the library's code(which consists of very few files) and put it directly in your project. Then modify the slidemenu_listitem.xml file like this:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<ImageView
android:id="#+id/menu_icon"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_marginBottom="10dip"
android:layout_marginLeft="10dip"
android:layout_marginRight="5dip"
android:layout_marginTop="10dip" />
<TextView
android:id="#+id/menu_label"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:layout_marginBottom="10dip"
android:layout_marginTop="10dip"
android:textSize="24dp" />
</LinearLayout>
Of course, you can always make a feature request to the user how made the library to modify it so you can insert your own layout file in the library(and wait for that to happen).
You can use the
android:gravity="center" (or top/bottom/left/right depending your needs)
of your textview to align text where you want.
layout_gravity sets the gravity (alignment) of the View or Layout
inside its parent. If you have a text view inside a linear layout,
then setting the layout_gravity of the text view will alter its
alignment in the layout. gravity sets the alignment of the content of
the view or layout it is applied on. If you want your text (in the
text view) to be left aligned, center, or right aligned, then modify
the gravity attribute of the text view.
From: http://thinkandroid.wordpress.com/2010/01/14/how-to-position-views-properly-in-layouts/

No scrolling effect with ScrollView in PopupWindow

I want to implement a popup menu with complex effects, one of them is to support scrolling. It seems PopupMenu and AlertDialog can not meet the requirement what I need. So I tried PopupWindow with ScrollView.
Firstly, I prepared a layout which has a simple strcture just like what ApiDemo shows:
ScrollView
LinearLayout with Vertical attribute
TextView
TextView
TextView
...
Secondarily, I new a PopupWindow with this layout and 200width/300height, show it at somewhere with showAtLocation().
I can make scrollbar displayed and has scroll effect, but TextViews in LinearLayout do NOT scroll(they are in fixed position)!
Something is wrong but I have no sense on it.
(android3.0)
Thanks for any warm-heart man who can give me some tips.
-_-!!
I also faced similar issue. Some how wrapping relative layout in scrollview is not working for popupwindow.
I tried wrapping individual views under scrollview and it worked for me. PLease have a look below. Hope this helps
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/container"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:background="#eebd9c"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
>
<ScrollView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_centerHorizontal="true"
android:layout_below="#+id/textView2" >
<TextView
android:id="#+id/textView9"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:ellipsize="marquee"
android:ems="15"
android:gravity="fill_horizontal"
android:minLines="6"
android:scrollbars="vertical"
android:singleLine="false"
android:text="Multiline text"
android:textAppearance="?android:attr/textAppearanceSmall" />
</ScrollView></RelativeLayout>
I think your requirement can be fullfilled by Dialog try the code i have given
protected void showInputDialog() {
final Dialog splRerDialog = new Dialog(getContext());
splRerDialog.setTitle("Special request.");
ScrollView scroll = new ScrollView(getContext());
LinearLayout lin = new LinearLayout(getContext());
lin.setLayoutParams( new LayoutParams(350,200));
lin.setGravity(Gravity.CENTER);
lin.setOrientation(LinearLayout.VERTICAL);
final EditText req = new EditText(getContext());
req.setLayoutParams( new LayoutParams(300,300));
lin.addView(req);
Button btn = new Button(getContext());
btn.setText("Ok");
btn.setLayoutParams( new LayoutParams(200,LayoutParams.WRAP_CONTENT));
btn.setGravity(Gravity.CENTER);
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
splRerDialog.dismiss();
}
});
lin.addView(btn);
scroll.addView(lin);
splRerDialog.addContentView(scroll, new LayoutParams(LayoutParams.WRAP_CONTENT,200));
splRerDialog.show();
}

How to use the xml setting in a view of a activity?

I want to show two views in one activity. If I clicked on button in the first view I want to see the second and other way round.
The views should not have the same size as the screen so I want e.g. to center it, like you see in first.xml.
But if I add the views with
addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
the views are not centered. They are shown at top left.
How can I use the xml settings to e.g. center it?
first.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_height="wrap_content" android:layout_width="wrap_content"
android:background="#drawable/background"
android:layout_gravity="center"
android:minWidth="100dp"
android:minHeight="100dp"
android:paddingBottom="5dp"
>
<LinearLayout android:id="#+id/head"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageButton android:id="#+id/first_button"
android:src="#drawable/show_second"
android:layout_gravity="center"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null" />
</LinearLayout>
second.xml same as first.xml but with
<ImageButton android:id="#+id/second_button"
android:src="#drawable/show_first"
... />
ShowMe.java
public class ShowMe extends Activity {
View mFirstView = null;
View mSecondView = null;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initFirstLayout();
initSecondLayout();
showFirst();
}
private void initFirstLayout() {
LayoutInflater inflater = getLayoutInflater();
mFirstView = inflater.inflate(R.layout.first, null);
getWindow().addContentView(mFirstView, new ViewGroup.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT));
ImageButton firstButton = (ImageButton)mMaxiView.findViewById(R.id.first_button);
firstButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
ShowMe.this.showSecond();
}
});
}
private void initSecondLayout() {
// like initMaxiLayout()
}
private void showFirst() {
mSecondView.setVisibility(View.INVISIBLE);
mFirstView.setVisibility(View.VISIBLE);
}
private void showSecond() {
mFirstView.setVisibility(View.INVISIBLE);
mSecondView.setVisibility(View.VISIBLE);
}}
Hope someone can help.
Thanks
Why don't you use setContentView(R.layout.yourlayout)? I believe the new LayoutParams you're passing in addContentView() are overriding those you defined in xml.
Moreover, ViewGroup.LayoutParams lacks the layout gravity setting, so you would have to use the right one for the layout you're going to add the view to (I suspect it's a FrameLayout, you can check with Hierarchy Viewer). This is also a general rule to follow. When using methods that take layout resources as arguments this is automatic (they might ask for the intended parent).
With this consideration in mind, you could set your layout params with:
FrameLayout.LayoutParams lp = new FrameLayout.LayoutParams(/* wrap wrap */);
lp.setGravity(Gravity.CENTER);
addContentView(mYourView, lp);
But I would recommend setContentView() if you have no particular needs.
EDIT
I mean that you create a layout like:
~~~/res/layout/main.xml~~~
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="....."
android:id="#+id/mainLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"/>
then in your onCreate() or init...Layout():
setContentView(R.layout.main);
FrameLayout mainLayout = (FrameLayout)findViewById(R.id.mainLayout);
// this version of inflate() will automatically attach the view to the
// specified viewgroup.
mFirstView = inflater.inflate(R.layout.first, mainLayout, true);
this will keep the layout params from xml, because it knows what kind it needs. See reference.

Categories

Resources