I have made custom toolbar with search and setting icon . when i click on search icon edittext should apper with match_parent and should occupy available space till the search icon . However it is not happening . it just appear with very small space at the top-left side . Please help where i am wrong
I would like to share my whole code with xml
main_activity.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_width="match_parent"
android:layout_height="match_parent"
android:background="#DCDCDC"
android:padding="0dp"
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="myresolver.faisal.home.com.myresolver.MainActivity">
<include
android:id="#+id/toolbar"
layout="#layout/my_toolbar"></include>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:fillViewport="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:padding="1dp"
android:weightSum="4"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96"></ImageView>
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:orientation="horizontal"
android:weightSum="3">
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
<ImageView
style="#style/icon"
android:background="#drawable/fear_96" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
mytoolbar.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.Toolbar 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="wrap_content"
android:background="#color/orange"
app:titleTextColor="#color/white"></android.support.v7.widget.Toolbar>
my main_menu.xml
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools">
<item
app:showAsAction="always"
android:title="#string/search"
android:icon="#drawable/ic_search_white_24dp"
android:id="#+id/search"
></item>
<item
app:showAsAction="always"
android:title="#string/setting"
android:icon="#drawable/ic_settings_white_24dp"
android:id="#+id/setting"></item>
</menu>
my search_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="match_parent"
android:orientation="horizontal">
<EditText
android:id="#+id/searchEditText"
android:layout_width="match_parent" // THIS IS NOT WORKING
android:layout_height="wrap_content"
android:imeOptions="actionSearch"
android:inputType="text" />
</LinearLayout>
My activity
EditText searchEditText;
boolean isSearchBoxOpen = false;
Toolbar toolbar;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
}
#Override
public boolean onPrepareOptionsMenu(Menu menu) {
getMenuInflater().inflate(R.menu.menu_main, menu);
return super.onPrepareOptionsMenu(menu);
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case R.id.search:
onSearchedHandler();
break;
case R.id.setting:
Toast.makeText(this, "Setting is UnderConstruction", Toast.LENGTH_LONG).show();
break;
}
return super.onOptionsItemSelected(item);
}
public void onSearchedHandler() {
if (isSearchBoxOpen) {
String word = searchEditText.getText().toString();
if (word != null) {
Toast.makeText(this, "Searching for ..." + word, Toast.LENGTH_LONG).show();
}
} else {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(true);
actionBar.setCustomView(R.layout.search_layout);
actionBar.setDisplayShowTitleEnabled(false);
searchEditText = (EditText) actionBar.getCustomView().findViewById(R.id.searchEditText);
searchEditText.requestFocus();
InputMethodManager keyboard = (InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
keyboard.showSoftInput(searchEditText, InputMethodManager.HIDE_IMPLICIT_ONLY);
isSearchBoxOpen = true;
}
}
#Override
public void onBackPressed() {
if (isSearchBoxOpen) {
ActionBar actionBar = getSupportActionBar();
actionBar.setDisplayShowCustomEnabled(false);
actionBar.setDisplayShowTitleEnabled(true);
isSearchBoxOpen = false;
return;
}
super.onBackPressed();
}
Use the below code for toolbar xlm layout :
<android.support.v7.widget.Toolbar
xmlns:app="schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/primaryColor"
android:contentInsetLeft="0dp"
android:contentInsetStart="0dp"
app:contentInsetLeft="0dp"
app:contentInsetStart="0dp"
android:contentInsetRight="0dp"
android:contentInsetEnd="0dp"
app:contentInsetRight="0dp"
app:contentInsetEnd="0dp" />
When the custom view is inflated as a result of calling setCustomView(resId), wrap_content is forced upon it (if you look into the view dump, you can see that the wrapping LinearLayout is shrunk, not the EditText).
You can set the custom view in the following way:
View customView = LayoutInflater.from(this).inflate(R.layout.search_layout, null);
ActionBar.LayoutParams params = new ActionBar.LayoutParams(ActionBar.LayoutParams.MATCH_PARENT, ActionBar.LayoutParams.MATCH_PARENT);
actionBar.setCustomView(customView, params);
just add this line to take full width
app:contentInsetStart="0dp"
The padding in the RelativeLayout in main_activity.xml is preventing it from taking the full space so instead of having
<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:background="#DCDCDC"
android:padding="0dp"
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="myresolver.faisal.home.com.myresolver.MainActivity">
you move the paddings to the scrollview such that
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/toolbar"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:fillViewport="true">
Related
There are a few buttons in my activity which do not get clicked on the first attempt but they get clicked from the second time onwards. It seems that the button gets focus on the first click and gets actually pressed from the second time onwards. What can be the reason that these buttons do not get clicked the first time?
I have noticed that this behaviour is only shown by buttons that are at the very bottom of my activity.
activity_main.xml
<?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"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.me.proj.view.activities.MallActivity">
<android.support.design.widget.AppBarLayout
android:id="#+id/appbar_layout"
android:layout_width="match_parent"
android:layout_height="200dp"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
app:title=" "
android:layout_height="match_parent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<android.support.v4.view.ViewPager
android:id="#+id/viewpager_mall_showcase_images"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
app:layout_collapseMode="pin" />
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v4.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#ffe5e5e5"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<include layout="#layout/info_card" />
<include layout="#layout/photo_card" />
<include layout="#layout/address_card" />
<include layout="#layout/opening_hours_card" />
<include layout="#layout/review_card" /> <!-- Button in this file has the problem-->
</LinearLayout>
</android.support.v4.widget.NestedScrollView>
</android.support.design.widget.CoordinatorLayout>
review_card.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:card_view="http://schemas.android.com/apk/res-auto"
card_view:cardCornerRadius="4dp"
card_view:cardElevation="0.5dp"
card_view:cardMaxElevation="1dp"
card_view:cardPreventCornerOverlap="false"
card_view:cardUseCompatPadding="true"
android:layout_width="match_parent"
android:layout_marginTop="7dp"
android:background="#color/colorWhite"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/colorWhite">
<TextView
android:id="#+id/txt_rating"
android:background="#drawable/blue_button_background"
android:text="3.8"
android:textColor="#color/colorWhite"
android:layout_marginTop="10dp"
android:textSize="24sp"
android:gravity="center"
android:layout_marginStart="10dp"
android:layout_width="60dp"
android:layout_height="40dp" />
<TextView
android:id="#+id/txt_reviews_title"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textColor="#color/primary_text"
android:textSize="16sp"
android:layout_toEndOf="#+id/txt_rating"
android:layout_marginStart="20dp"
android:textStyle="bold"
android:paddingTop="1dp"
android:layout_alignTop="#+id/txt_rating"
android:text="Based on 98 reviews"/>
<TextView
android:id="#+id/txt_read_all_reviews"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Read All Reviews"
android:layout_alignStart="#id/txt_reviews_title"
android:layout_toEndOf="#id/txt_rating"
android:layout_alignBottom="#id/txt_rating"
android:gravity="bottom"
android:paddingBottom="1dp"
android:textColor="#color/red"
android:layout_below="#id/txt_reviews_title"/>
<View
android:id="#+id/view_dummy1"
android:layout_width="match_parent"
android:layout_height="0.2dp"
android:layout_below="#id/txt_read_all_reviews"
android:layout_marginTop="10dp"
android:background="#color/divider"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginStart="16dp"
android:layout_below="#+id/view_dummy1">
<LinearLayout
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_weight="0.6"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<TextView
android:id="#+id/txt_how_did_your_visit_go"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="How did your visit go?"
android:textStyle="bold"
android:textSize="14sp"
android:textColor="#color/primary_text" />
<TextView
android:id="#+id/txt_tell_everyone_about_it"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Tell everyone about it!"
android:layout_marginTop="5dp"
android:textColor="#color/secondary_text"
android:textSize="12sp"
/>
</LinearLayout>
<!--This got some problem-->
<Button
android:id="#+id/btn_add_review"
android:layout_width="0dp"
android:layout_height="40dp"
android:text="Add Your Review"
android:layout_marginEnd="16dp"
android:layout_marginTop="8dp"
android:textAllCaps="false"
android:textColor="#color/colorWhite"
android:background="#drawable/blue_button_background"
android:layout_weight="0.5"/>
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
findViewById(R.id.btn_add_review).setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Toast.makeText(MainActivity.this, "clicked", Toast.LENGTH_SHORT).show();
}
});
}
Code in my MainActivity.class:
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View view = findViewById(R.id.sub_layout);
Button button = (Button) view.findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//Do your stuff
Toast.makeText(MainActivity.this, "hi", Toast.LENGTH_SHORT).show();
}
});
}
}
Code in my activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="eflair.myapplication.MainActivity">
<include
android:id="#+id/sub_layout"
layout="#layout/sub_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</android.support.constraint.ConstraintLayout>
Code in my sub_layout.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/app_name" />
</LinearLayout>
This issue is actually a bug in the NestedScrollView class for a while now.
I solved this issue my making some tweaks to the source code of the NestedScrollView class, as follows :-
public class NestedScrollView extends FrameLayout implements NestedScrollingParent,
NestedScrollingChild, ScrollingView {
...
#Override
public boolean onTouchEvent(MotionEvent ev) {
switch (actionMasked) {
case MotionEvent.ACTION_DOWN: {
if (getChildCount() == 0) {
return false;
}
//add this condition
if (!inChild((int) ev.getX(), (int) ev.getY())) {
return false;
}
if ((mIsBeingDragged = !mScroller.isFinished())) {
final ViewParent parent = getParent();
if (parent != null) {
parent.requestDisallowInterceptTouchEvent(true);
}
}
...
}
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev) {
...
switch (action & MotionEventCompat.ACTION_MASK) {
...
case MotionEvent.ACTION_DOWN: {
...
// replace this line:
// mIsBeingDragged = !mScroller.isFinished();
mIsBeingDragged = false;
...
}
}
}
}
After doing this it should work fine.
I have two ViewPager on the same view with weight= 70/30 . The problem is when the keyboard pops, my both view keep the same weight and do not display my EditText.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true"
tools:context="com.suez.memboard.fragments.formWaterQuality">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:weightSum="100"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="30">
<com.suez.memboard.controls.TextView
android:id="#+id/path"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:textAppearance="#style/FreeTextBold" />
<LinearLayout
android:id="#+id/date_picker"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="10dp">
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="forms.label.intervention_date"
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text=" : "
android:textAppearance="#style/FreeText"
android:textSize="15sp" />
<com.suez.memboard.controls.DateTimeText
android:id="#+id/date"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.7"
android:drawableRight="#drawable/ic_date_range_black_24dp"
android:focusable="false"
android:hint="forms.label.intervention_date"
fab:type="date" />
</LinearLayout>
<Spinner
android:id="#+id/spin_location"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabA"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
<ImageButton
android:id="#+id/buttonAdd"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
android:src="#drawable/ic_add_box_white_48dp" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewA"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_weight="70">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<android.support.design.widget.TabLayout
android:id="#+id/tabB"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/suez_blue_1"
app:tabGravity="fill" />
</LinearLayout>
<android.support.v4.view.ViewPager
android:id="#+id/viewB"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
<FAB ....>
I think my last solution is to make disappear my picker at the top when my keyboard pops. How can I do this?
Hope this help you :
public class YourActivity extends BaseActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.your_activity);
attachKeyboardListeners();
}
#Override
protected void onShowKeyboard(int keyboardHeight) {
// when keyboard is shown
yourWidget.setVisibility(View.GONE);
}
#Override
protected void onHideKeyboard() {
// when keyboard is hidden
yourWidget.setVisibility(View.VISIBLE);
}
}
Try to use ScrollView in parent view, for example-
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:fab="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:focusableInTouchMode="true">
<ScrollView
android:id="#+id/scroll_view_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:fillViewport="true"
android:scrollbars="none">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorWhite"
android:orientation="vertical">
..................................
type your code here
</LinearLayout>
</LinearLayout>
</ScrollView>
</RelativeLayout>
You can set listeners when keyboard is opened and closed.
// ContentView is the root view of the layout of this activity/fragment
contentView.getViewTreeObserver().addOnGlobalLayoutListener(
new ViewTreeObserver.OnGlobalLayoutListener() {
#Override
public void onGlobalLayout() {
Rect r = new Rect();
contentView.getWindowVisibleDisplayFrame(r);
int screenHeight = contentView.getRootView().getHeight();
// r.bottom is the position above soft keypad or device button.
// if keypad is shown, the r.bottom is smaller than that before.
int keypadHeight = screenHeight - r.bottom;
Log.d(TAG, "keypadHeight = " + keypadHeight);
if (keypadHeight > screenHeight * 0.15) { // 0.15 ratio is perhaps enough to determine keypad height.
// keyboard is opened
} else {
// keyboard is closed
}
}
});
I have one relativeLayout and three view's inside RelativeLayout. There views are place on each other.
<RelativeLayout
android:id="#+id/includelayoutgroup"
android:layout_width="match_parent"
android:layout_height="match_parent">
<View xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/transparentColorView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible" />
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/keyboard_main_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:visibility="visible">
<include
layout="#layout/transfer_keyboard_fragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/finalRequestView"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:clickable="true"
android:visibility="visible">
<include
layout="#layout/fragment_transfer_new_version_container_child"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
</RelativeLayout>
Is it a possible to reverse position my view's? I mean,in my code last Layout is finalRequestView and in button click i would to my last view in first position,and other view's place on my view,
How i can solve this problem?
I try to reverse layout by following demo & it works as you want, please take a look
ReverseLayout.java
public class ReverseLayout extends AppCompatActivity {
LinearLayout llRoot;
Button btReverseLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.reverse_layout);
llRoot = (LinearLayout) findViewById(R.id.llRoot);
btReverseLayout = (Button) findViewById(R.id.btReverseLayout);
btReverseLayout.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
ArrayList<View> alViews = new ArrayList<View>();
for (int i = llRoot.getChildCount() - 1; i >= 0; i--) {
View view = llRoot.getChildAt(i);
llRoot.removeViewAt(i);
alViews.add(view);
}
for (int j = 0; j < alViews.size(); j++) {
llRoot.addView(alViews.get(j));
}
}
});
}
}
reverse_layout.xml layout file
<?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:id="#+id/activity_6"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.tosc185.testproject.Activity6">
<LinearLayout
android:id="#+id/llRoot"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FF0000"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#00FF00"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#0000FF"
android:gravity="center"
android:orientation="vertical"
android:padding="15dp">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:textSize="16dp" />
</LinearLayout>
</LinearLayout>
<Button
android:id="#+id/btReverseLayout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Reverse Layout" />
</RelativeLayout>
I am using sliding panel layout and in that i have ONE floating buttons.Now when i Click on the button the view Slides first and then i have to click again on button to perform action.
I want to perform action on only single click without the sliding movement of parent view.
Look at these images
Look at 1 images
look at this
Here is my
XML
<?xml version="1.0" encoding="utf-8"?>
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true"
android:layout_alignStart="#+id/message" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
here is my java code
SlidingPaneLayout sp;
#Override
protected void onCreate(Bundle savedInstanceState)
{
context = this;
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
call = (FloatingActionButton) findViewById(R.id.call);
sp = (SlidingPaneLayout) findViewById(R.id.Sliding);
assert sp != null;
sp.setPanelSlideListener(this);
sp.setSliderFadeColor(Color.TRANSPARENT);
sp.openPane();
mList = new ArrayList<Contacts>();
call.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Log.e("CALL","CALL");
try
{
if(number.isEmpty())
{
Toast.makeText(context,"Please select contact first",Toast.LENGTH_SHORT).show();
}
else
{
calling();
number=null;
}
}
catch (Exception e)
{
e.printStackTrace();
Toast.makeText(context,"This is not the valid contact",Toast.LENGTH_SHORT).show();
}
}
});
}
Use this code:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:layout_height="match_parent"
android:layout_width="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<android.support.v4.widget.SlidingPaneLayout
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:id="#+id/Sliding"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="300dp"
android:layout_height="match_parent"
android:id="#+id/leftSide"
android:background="#ef3"
>
<ListView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/contact"
android:background="#drawable/bg_key"
android:layout_alignParentTop="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
</android.support.v4.widget.SlidingPaneLayout>
<RelativeLayout
android:layout_width="match_parent"
android:background="#color/colorAccent"
android:id="#+id/rightSide"
android:layout_marginLeft="170dp"
android:layout_height="match_parent">
<android.support.design.widget.FloatingActionButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:fabSize="mini"
android:focusable="false"
app:srcCompat="#drawable/ic_menu_camera"
android:id="#+id/call"
android:layout_alignParentTop="true" />
</RelativeLayout>
</RelativeLayout>
I am trying to change programmatically the background color of ListView item when the user tap on it. I am trying to implement a solution using setOnItemClickListener, but I have trouble making the listener work. Here is my code:
ArrayList<Integer> menuIcons = app.Theme.Icons.getMenuPanelIconList();
ArrayList<String> menuText = app.Languages.Active.getMenuPanelItemNames();
listview_menu = (ListView) findViewById(R.id.listview_menu);
listview_menu.setBackgroundColor(Color.parseColor("#" + app.Theme.ThemeBackColor));
MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
listview_menu.setAdapter(menuPanelListViewAdapter);
listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
for (int i = 0; i < listview_menu.getChildCount(); i++) {
if(position == i ){
listview_menu.getChildAt(i).setBackgroundColor(Color.BLUE);
}else{
listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
}
}
});
Here is my mainactivity.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:fresco="http://schemas.android.com/apk/res-auto"
android:id="#+id/relativelayout_base"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/relativelayout_toolbar"
android:layout_width="match_parent"
android:layout_height="?android:actionBarSize">
<LinearLayout
android:id="#+id/linearlayout_menu"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:layout_alignParentLeft="true"
android:clickable="true"
android:gravity="center"
android:paddingLeft="10dp"
android:paddingRight="10dp">
<ImageView
android:id="#+id/imageview_menu"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/menushow_g" />
</LinearLayout>
<com.hotelstayapp.android.custom.CustomTextView
android:id="#+id/customtextview_title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#+id/linearlayout_menu"
android:textColor="#FFFFFF"
android:textSize="27sp"
android:ellipsize="end"
android:singleLine="true"
android:paddingBottom="1dp"/>
</RelativeLayout>
<android.support.v4.widget.DrawerLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
tools:context="com.hotelstayapp.android.UI.ActivityMain"
android:id="#+id/drawer_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#+id/relativelayout_toolbar">
<FrameLayout
android:id="#+id/content_frame"
android:layout_width="match_parent"
android:layout_height="match_parent">
</FrameLayout>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/relativelayout_menupanel"
android:layout_width="280dp"
android:layout_height="match_parent"
android:layout_gravity="left">
<RelativeLayout
android:id="#+id/relativelayout_menuheader"
android:layout_width="match_parent"
android:layout_height="130dp"
android:clickable="true">
<com.facebook.drawee.view.SimpleDraweeView
fresco:actualImageScaleType="centerCrop"
android:id="#+id/imageview_menubgimage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
/>
<com.facebook.drawee.view.SimpleDraweeView
android:layout_width="240dp"
android:layout_height="70dp"
fresco:actualImageScaleType="fitCenter"
android:id="#+id/imageview_menulogo"
android:layout_centerInParent="true"
/>
</RelativeLayout>
<ListView
android:id="#+id/listview_menu"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="#id/relativelayout_menuheader"
android:layout_gravity="right"
android:divider="#android:color/transparent"
android:dividerHeight="0dp"
android:paddingTop="10dp"
android:smoothScrollbar="true"
android:focusable="false"
android:focusableInTouchMode="false">
</ListView>
</RelativeLayout>
</android.support.v4.widget.DrawerLayout>
</RelativeLayout>
Here is my ListView items 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">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:weightSum="1"
android:paddingBottom="14dp"
android:paddingTop="14dp"
android:paddingLeft="0dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="0.8"
android:gravity="center">
<ImageView
android:layout_width="23dp"
android:layout_height="23dp"
android:id="#+id/imageViewIcon"
android:layout_centerVertical="true"
android:layout_alignParentLeft="true"
android:src="#drawable/menuhome_g"
android:layout_centerHorizontal="true" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal"
android:layout_weight="0.2"
android:paddingLeft="15dp"
android:gravity="center_vertical">
<com.hotelstayapp.android.custom.CustomTextView
android:id="#+id/textViewMenuItemName"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center|left"
android:text="Home"
android:textColor="#000000"
android:textSize="15sp"
android:textStyle="bold"/>
</LinearLayout>
</LinearLayout>
<View
android:id="#+id/viewMenuDivider"
android:layout_width="match_parent"
android:layout_height="1dp"
android:background="#eeeeee"
android:layout_marginTop="5dp"
android:layout_marginBottom="5dp"></View>
</LinearLayout>
First change background of all item to transperent then set clicked view's background to your desired color like below.
MenuPanelListViewAdapter menuPanelListViewAdapter = new MenuPanelListViewAdapter(ActivityMain.this, menuIcons, menuText);
listview_menu.setAdapter(menuPanelListViewAdapter);
listview_menu.setOnItemClickListener(new AdapterView.OnItemClickListener() {
#Override
public void onItemClick(AdapterView<?> adpterView, View view, int position,
long id) {
for (int i = 0; i < listview_menu.getChildCount(); i++) {
listview_menu.getChildAt(i).setBackgroundColor(Color.TRANSPARENT);
}
view.setBackgroundColor(Color.BLUE);
menuPanelListViewAdapter.notifyDataSetChanged(); // this is importent
}
});