here is the code, please can anyone simply tell me how to make the onClick event run?
xml file of the activity, which has a navigation-drawer fragment, An Image view, A TextView, An ImageButton
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<include
android:id="#+id/app_bar"
layout="#layout/app_bar" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignTop="#+id/drawer_layout"
android:adjustViewBounds="true"
android:contentDescription="#string/ContentDescriptionProfileImage"
android:src="#drawable/profile_pic" />
<LinearLayout
android:id="#+id/linearLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/imageView"
android:background="#FFFFFF"
android:orientation="horizontal"
android:padding="5dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="45dp">
<TextView
android:id="#+id/textView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_gravity="center_vertical"
android:padding="3dp"
android:text="#string/profileName"
android:textColor="#000000"
android:textSize="18sp"
android:textStyle="bold" />
<ImageButton
android:id="#+id/profileSettings"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:background="#null"
android:contentDescription="#string/settingsIconDescription"
android:padding="8dp"
android:onClick="onSettingsIconPressed"
android:src="#drawable/settings32" />
</RelativeLayout>
</LinearLayout>
<android.support.v4.widget.DrawerLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/app_bar">
<fragment
android:id="#+id/fragment_navigation_drawer"
android:name="studio25.materialdesign.NavigationDrawerFragment"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="start"
app:layout="#layout/fragment_navigation_drawer"
tools:layout="#layout/fragment_navigation_drawer"></fragment>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
The image of the Screen Looks like this
here is the activity class
package studio25.materialdesign;
import android.annotation.TargetApi;
import android.os.Build;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarActivity;
import android.os.Bundle;
import android.support.v7.widget.PopupMenu;
import android.support.v7.widget.Toolbar;
import android.view.ContextMenu;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ImageButton;
import android.widget.Toast;
public class ProfileView extends ActionBarActivity {
private Toolbar toolbar;
String title = "";
ImageButton setttingsButton;
#TargetApi(Build.VERSION_CODES.LOLLIPOP)
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile_view);
toolbar = (Toolbar) findViewById(R.id.app_bar);
setSupportActionBar(toolbar);
this.getSupportActionBar().setTitle(title);
getSupportActionBar().setDisplayShowHomeEnabled(true);
NavigationDrawerFragment drawerFragment = (NavigationDrawerFragment)
getSupportFragmentManager().findFragmentById(R.id.fragment_navigation_drawer);
drawerFragment.setUp((DrawerLayout) findViewById(R.id.drawer_layout), toolbar);
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
/*
Window window = this.getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
window.setStatusBarColor(this.getResources().getColor(R.color.primaryColor));
*/
}
public void onSettingsIconPressed(View v)
{
/*
PopupMenu popupMenu=new PopupMenu(this,v);
MenuInflater inflater=getMenuInflater();
inflater.inflate(R.menu.profile_settings_popup_menu,popupMenu.getMenu());
popupMenu.show();
*/
Toast.makeText(this,"Event Click Occured",Toast.LENGTH_SHORT).show();
}
}
I basically wanted a menu popup on buttonclick, but on debugging realized that my button click is not itself working.
Some tutotials say for popUp menu , minSdkVersion should be 11, even changed that.
Nothing is helping me.
Is my xml erroneous or what?
I usually like to hook up my callbacks in code (just my personal preference).
setttingsButton= (ImageButton) findViewById(R.id.profileSettings);
setttingsButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(this,"clicked!",Toast.LENGTH_SHORT).show();
}
});
With this you can remove the onClick attribute in the xml layout.
If you want a popup window then PopupWindow is a great option.
popupWindow = new PopupWindow(some_view_to_show, ViewGroup.LayoutParams.MATCH_PARENT, (int)popupHeight, false);
...
popupWindow.showAtLocation(view_to_inject_into, Gravity.NO_GRAVITY, 0, (size.y) - (int) popupHeight);
then you call popupWindow.dismiss(); to remove the popup
Your issue could also be that since you are using a RelativeLayout each element gets "stacked" on top of each other so you have essentially "buried" your ImageButton that you want to click on. So in other words some other view is blocking or consuming your clicks.
Try moving your ImageButton to be one of the last elements that you add to the xml file so that it is "on top" of everything else so that it can be clicked.
Alternatively you can try making the containing LinearLayout clickable instead of the ImageButton. So you would move the android:onClick="onSettingsIconPressed" attribute to the LinearLayout that contains the ImageButton.
Related
I wanted to create a Reveal Menu in Android just like that of Jquery mobile.
I wanted the main FrameLayout to slide to the left by 30% there by revealing another Layout behind it, which would be the menu layout.
The coding of the sliding FrameLayout was successful; that is when i click on the menu botton it side to the left 30% away.
The problem i am encountering is that when it slides to the left, the menu button stop responding at its current position but it respond at its initial position whereas the menu button has move with the framelayout by 30% to the left
here are my code
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_height="match_parent"
android:layout_width="match_parent">
<FrameLayout
android:id="#+id/act_menu"
android:layout_width="200dp"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.thankgod.slidemenu.MainActivity">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/menu"
android:layout_height="match_parent"
android:layout_width="200dp"
android:background="#color/colorPrimaryDark"
android:orientation="vertical">
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>
</FrameLayout>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/act_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.thankgod.slidemenu.MainActivity">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/Dark">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#color/colorPrimaryDark">
<Button
android:text="#string/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button" />
<Button
android:text="#string/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button3"
android:layout_alignParentTop="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
tools:ignore="RelativeOverlap" />
</RelativeLayout>
</RelativeLayout>
</FrameLayout>
</RelativeLayout>
MainActivity.java
package com.example.thankgod.slidemenu;
import android.app.Activity;
import android.support.annotation.Nullable;
import android.app.Fragment;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.FrameLayout;
public class MainActivity extends Activity {
int page = 1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
/* requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);*/
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
move();
}
});
}
public void move() {
FrameLayout frameLayout = (FrameLayout) findViewById(R.id.act_main);
Animation anim = AnimationUtils.loadAnimation(getApplicationContext(),R.anim.slide);
frameLayout.startAnimation(anim);
}
}
animation file slide.xml
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="#android:anim/linear_interpolator"
android:fillAfter="true">
<translate
android:fromXDelta="0%"
android:toXDelta="20%"
android:duration="300"/>
</set>
By clicking on the menu button again it calls the move method again but it cannot move by another 30% so it seems like not moving use the below code.
public String a = "0"; //declare this globally at the top.
button.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
if(a.equals("0")
{
move();
}
else
{
// do what you want to do by clicking on the menu button once it is moved by 30%
Toast.makeText(MainActivity.this,"Menu button Clicked", Toast.LENGTH_SHORT).show();
}
a = "1";
}
});
I am trying to make an app with Bottom Sheet. But when I run the app, the Bottom Sheet shows in main screen. It means, the Bottom Sheet appears in the main screen without clicking on the Button. An image has been attested below for clear understanding. sample image
This is the MainActivity.java class:
package com.example.qurdadzze.b;
import android.support.design.widget.BottomSheetBehavior;
import android.support.v4.widget.NestedScrollView;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button btn;
NestedScrollView bottomsheet;
BottomSheetBehavior behavior;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btn = (Button) findViewById(R.id.btn);
bottomsheet = (NestedScrollView) findViewById(R.id.bottomsheet);
behavior = BottomSheetBehavior.from(bottomsheet);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(behavior.getState()==BottomSheetBehavior.STATE_COLLAPSED)
behavior.setState(BottomSheetBehavior.STATE_EXPANDED);
else
behavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
behavior.setPeekHeight(0);
}
});
}
And this is activity_main.xml:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
tools:context="com.example.qurdadzze.b.MainActivity">
<Button
android:id="#+id/btn"
android:layout_width="150dp"
android:layout_height="50dp"
android:text="show bottom sheet"/>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="300dp"
android:background="#99cc99"
android:elevation="100dp"
android:id="#+id/bottomsheet"
app:layout_behavior="#string/bottom_sheet_behavior">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="20sp"
android:layout_gravity="center|top"
android:gravity="center"
android:text="it is me Bottom Sheet"/>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
You will have to set peekheight of Bottomsheet.
android.support.design:behavior_peekHeight The height of the bottom sheet when it is collapsed.
Try using below code in your NestedScrollView:
app:behavior_peekHeight="50dp"
For more details check below link:
http://www.truiton.com/2016/07/android-bottom-sheet-example/
I'm new to android app development and am trying to build an app. I have successfully built my navigation drawer but I want the menu items to open another activity when clicked. I have tried various answers on SO but my app seems to crash each time i run my app after applying any of the answers. Please what could be the cause of this.
menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="#+id/my_account"
android:title="My Account"
android:icon="#mipmap/ic_person_white_24dp"
android:onClick="settings"></item>
<item
android:id="#+id/my_settings"
android:title="Settings"></item>
<item
android:id="#+id/Logout"
android:title="Log Out"
android:icon="#mipmap/ic_exit_to_app_white_24dp"></item>
</menu>
activity.java
package com.example.orume.export;
import android.content.Intent;
import android.support.v4.widget.DrawerLayout;
import android.support.v7.app.ActionBarDrawerToggle;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class WorkActivity extends AppCompatActivity {
private DrawerLayout mDrawerLayout;
private ActionBarDrawerToggle mToggle;
private Toolbar mToolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_work);
mToolbar = (Toolbar)findViewById(R.id.toolbar);
setSupportActionBar(mToolbar);
mDrawerLayout = (DrawerLayout)findViewById(R.id.drawerLayout);
mToggle = new ActionBarDrawerToggle(this, mDrawerLayout,R.string.open,
R.string.close);
mDrawerLayout.addDrawerListener(mToggle);
mToggle.syncState();
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
final Button bExplore = (Button) findViewById(R.id.bExplore);
bExplore.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View view) {
Intent exploreIntent = new Intent(WorkActivity.this,
RegisterActivity.class);
WorkActivity.this.startActivity(exploreIntent);
}
});
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
if (mToggle.onOptionsItemSelected(item)){
return true;
}
return super.onOptionsItemSelected(item);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
MenuInflater inflater = getMenuInflater();
inflater.inflate(R.menu.menu_search, menu);
return true;
}
}
activity.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.DrawerLayout
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"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:orientation="vertical"
android:id="#+id/drawerLayout"
android:background="#d8dadc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<include layout="#layout/toolbar"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
</LinearLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="10dp">
</RelativeLayout>
<android.support.design.widget.NavigationView
android:layout_width="wrap_content"
android:layout_height="match_parent"
app:menu="#menu/navigation_menu"
android:layout_gravity="start"
android:background="#color/colorPrimary"
app:headerLayout="#layout/navigation_header">
</android.support.design.widget.NavigationView>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="60dp">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:background="#d8dadc">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="false"
android:padding="10dp">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool"
android:id="#+id/bExplore"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool_2"
android:id="#+id/bPrice"
android:layout_marginLeft="5dp"
android:text="Explore"
android:textAllCaps="false"
android:textColor="#191919"/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_alignParentBottom="false"
android:padding="10dp"
android:layout_marginTop="75dp">
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool"
android:id="#+id/bS"
android:layout_marginRight="5dp"/>
<Button
android:layout_width="0dp"
android:layout_height="70dp"
android:layout_weight="0.5"
android:background="#mipmap/bg_tool_2"
android:id="#+id/bA"
android:layout_marginLeft="5dp"
android:text="Explore"
android:textAllCaps="false"
android:textColor="#191919"/>
</LinearLayout>
</RelativeLayout>
</ScrollView>
</android.support.v4.widget.DrawerLayout>
In your menu.xml you have added this line android:onClick="settings". remove that line or add the following code to your MainActivity.java
public boolean settings(MenuItem item) {
// actions
return true;
}
I have made an Android app containing a CoordinatorLayout with a AppBar and a NestedScrollView. In my NestedScrollView I have a list of items, and a button which scroll to an item in my NestedScrollView.
When clicking the button, the Android app scroll down to the item, but doesn't show the whole item, only partially: http://i.stack.imgur.com/8kuq0.png
I expected something like the following: http://i.stack.imgur.com/k0A5N.png
It seems like the amount the app needs to scroll is about the same size as the AppBar, in order to show the whole view. If I remove the scroll flag in my layout file below, I get the expected behavior.
What do I do wrong?
Update (12/01/2016): I've updated the pictures, so they are bigger. Furthermore as I wrote in Herry's response, I'm in doubt if View.requestRectangleOnScreen() is the right method to call, or if I should use a different layout then NestedScrollView.
My activity is coded as follows:
package com.example.sij.coordinatorlayoutbug;
import android.graphics.Rect;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
public class MainActivity extends AppCompatActivity {
TextView itemToNavigateTo;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
itemToNavigateTo = (TextView)findViewById(R.id.itemToNavigateTo);
Button navigatorButton = (Button)findViewById(R.id.navigator_button);
navigatorButton.setOnClickListener(navigateToItemListener());
}
View.OnClickListener navigateToItemListener() {
return new View.OnClickListener() {
#Override
public void onClick(final View v) {
new Handler(Looper.getMainLooper()).post(new Runnable() {
#Override
public void run() {
itemToNavigateTo.requestRectangleOnScreen(
new Rect(
0,
0,
itemToNavigateTo.getWidth(),
itemToNavigateTo.getHeight()));
}
});
}
};
}
}
And the layout file:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout
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.support.design.widget.AppBarLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
app:layout_scrollFlags="scroll|enterAlways" />
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/navigator_button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/scroll_to_item" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item1"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
<!-- Items 2 to 5 omitted for brevity -->
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/item6"/>
<TextView
android:id="#+id/itemToNavigateTo"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/lorem_ipsum"
android:paddingBottom="20dp"/>
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
you need to add android:fillviewport="true" in your
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
android:fillViewport="true">
there is two LinearLayouts header and footer in main activity
also RecyclerView inside swipeRefreshLayout between them
now i want to hide header and footer with use LayoutParam, but it shows gap after changing layoutParam and swipeRefreshLayout's height doesn't changes.
how to fix it?
I have try to solve your problem use the demo.
the xml file
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#fff"
android:orientation="vertical">
<View
android:layout_width="match_parent"
android:layout_height="50dp"
android:onClick="click"
android:background="#fffe4a" />
<View
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="2dp"
android:background="#3489ff" />
<View
android:id="#+id/view_bottom"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#fffe4a" />
</LinearLayout>
the java file.
package com.example.wangze.instantdemo1;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void click(View view){
View view_bottom = findViewById(R.id.view_bottom);
LinearLayout.LayoutParams layoutParams = (LinearLayout.LayoutParams) view_bottom.getLayoutParams();
layoutParams.height = 20;
view_bottom.setLayoutParams(layoutParams);
}
}
When i click the top view. The bottom is smaller,and not have gap. Maybe it can help you.