Scroll entire page when particular EditText has focus? - java

I have registration page,in that four EditText exists,when I click on third EditText one LinearLayout(assume password guidelines) has to be visible and at the same time total layout should be scroll to view all guidelines.When other EditText (1,2 or 4) got focus this LinearLayout(password guidelines) should be closed. I tried lot and I searched lot , no solution worked for me,please help me. For understand I did small project ,please watch it.
public class MainActivity extends AppCompatActivity {
private EditText mEditText1,mEditText2,mEditText3,mEditText4;
private LinearLayout ll,root;
ScrollView mScrollView ;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
root = (LinearLayout) findViewById(R.id.ll_linear_layout);
ll = (LinearLayout) findViewById(R.id.ll);
mEditText1 = (EditText) findViewById(R.id.et1);
mEditText2 = (EditText) findViewById(R.id.et2);
mEditText3 = (EditText) findViewById(R.id.et3);
mEditText4 = (EditText) findViewById(R.id.et4);
mEditText1.setOnFocusChangeListener(mListener1);
mEditText2.setOnFocusChangeListener(mListener2);
mEditText3.setOnFocusChangeListener(mLayoutExtract);
mEditText4.setOnFocusChangeListener(mListener4);
mScrollView = new ScrollView(this);
}
View.OnFocusChangeListener mListener1 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mListener2 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
};
View.OnFocusChangeListener mLayoutExtract = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
ll.setVisibility(View.VISIBLE);
} else {
ll.setVisibility(View.GONE);
}
}
};
View.OnFocusChangeListener mListener4 = new View.OnFocusChangeListener() {
#Override
public void onFocusChange(View view, boolean b) {
if (b) {
} else {
}
}
}};
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>

Try this
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:id="#+id/ll_linear_layout"
xmlns:android="http://schemas.android.com/apk/res/android">
<EditText
android:layout_marginTop="200dp"
android:id="#+id/et1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="first name"/>
<EditText
android:id="#+id/et2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<EditText
android:id="#+id/et3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
<LinearLayout
android:id="#+id/ll"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="300dp"
android:visibility="gone">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="75dp"
android:textColor="#000000"
android:text="Hidable text"/>
</LinearLayout>
<EditText
android:id="#+id/et4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="second name"/>
</LinearLayout>
</ScrollView>
Try this use android:windowSoftInputMode="AdjustResize" in Your Manifest where your are decraing YOur Activity like below code
<activity
android:name=".YourActivity"
android:parentActivityName="XXX.XXX.XXXX"
android:windowSoftInputMode="AdjustResize" />

Related

Show entire Bottom sheet with toolbar when keyboard is visible

in my application i have 'bottom sheet dialog fragment' when i open it, it work fine, like below image:
but when keyboard is visible the upper toolbar disappear, like below image show.
My Question is how to make toolbar always visible even when keybord is visible....
Here is my '"sheet bottom xml"'
<RelativeLayout
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:id="#+id/lyt_parent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="10dp"
android:orientation="vertical">
<com.google.android.material.appbar.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:layout_alignParentTop="true"
android:background="#color/grey_5"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center_horizontal"
android:orientation="horizontal">
<ImageButton
android:id="#+id/bt_close"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
app:srcCompat="#drawable/ic_close"
app:tint="#color/grey_80" />
<ImageButton
android:id="#+id/save"
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
app:srcCompat="#drawable/ic_bookmark_border"
app:tint="#color/grey_80" />
<ImageButton
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
android:layout_weight="1"
app:srcCompat="#drawable/ic_repeat"
app:tint="#color/grey_80" />
<ImageButton
android:layout_width="?attr/actionBarSize"
android:layout_height="?attr/actionBarSize"
app:srcCompat="#drawable/ic_more_vert"
app:tint="#color/grey_80" />
</LinearLayout>
</com.google.android.material.appbar.AppBarLayout>
<androidx.core.widget.NestedScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_above="#+id/app_bar_layout2"
android:layout_below="#+id/app_bar_layout"
android:clipToPadding="false"
android:fillViewport="true"
android:scrollbars="none"
android:scrollingCache="true"
app:layout_behavior=
"#string/appbar_scrolling_view_behavior">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:padding="#dimen/spacing_large">
<androidx.recyclerview.widget.RecyclerView
android:id="#+id/bottom_sheet_recycler_comment"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:itemCount="10" />
<LinearLayout
android:id="#+id/lyt_spacer"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:visibility="gone">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:text="No comments yet"
android:textSize="22dp"
android:textStyle="bold" />
</LinearLayout>
</LinearLayout>
</androidx.core.widget.NestedScrollView>
<LinearLayout
android:id="#+id/app_bar_layout2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:clipToPadding="false"
android:paddingBottom="10dp"
android:scrollbars="none"
android:scrollingCache="true"
app:layout_anchorGravity="bottom|center">
<EditText
android:id="#+id/post_view_enter_comment_edit_Text"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_margin="4dp"
android:layout_marginEnd="5dp"
android:layout_weight="1"
android:background="#drawable/edit_text_round_bg"
android:hint="Write comment..."
android:lineSpacingExtra="2dp"
android:maxLength="400"
android:maxLines="5"
android:textColor="#color/black"
android:textSize="17sp"/>
<ImageView
android:id="#+id/post_view_send_comment"
android:layout_width="40dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:layout_marginVertical="5dp"
android:layout_marginEnd="10dp"
android:background="#drawable/shape_circle"
android:backgroundTint="#color/blue_grey_300"
android:clickable="true"
android:padding="4dp"
android:paddingStart="6dp"
android:src="#android:drawable/ic_menu_send" />
</LinearLayout>
</RelativeLayout>
And also here my "BottomFragment.java".
public class FragmentBottomSheetDialogFull extends
BottomSheetDialogFragment {
private BottomSheetBehavior mBehavior;
private AppBarLayout app_bar_layout;
private RelativeLayout parent_lyt;
private RecyclerView recyclerView;
TopicRecyclerAdapter adapter;
ArrayList<String> topicsArrayList, selectedArray;
ConstraintLayout sheet_constraint_layout;
private String Post_id;
private String content;
private String topic;
public void setData(String post_id, String post_content,
String topic) {
this.Post_id = post_id;
this.content = post_content;
this.topic = topic;
}
#NonNull
#Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
final BottomSheetDialog dialog = (BottomSheetDialog)
super.onCreateDialog(savedInstanceState);
final View view = View.inflate(getContext(),
R.layout.fragment_bottom_sheet_dialog_full, null);
setStyle(DialogFragment.STYLE_NORMAL,
R.style.DialogStyle);
dialog.setContentView(view);
mBehavior = BottomSheetBehavior.from((View)
view.getParent());
mBehavior.setPeekHeight(BottomSheetBehavior.PEEK_HEIGHT_AUTO);
app_bar_layout = (AppBarLayout)
view.findViewById(R.id.app_bar_layout);
parent_lyt = (RelativeLayout)
view.findViewById(R.id.lyt_parent);
mBehavior.setMaxHeight((Tools.getScreenHeight() / 2) +
100);
recyclerView =
view.findViewById(R.id.bottom_sheet_recycler_comment);
topicsArrayList = new ArrayList<>();
selectedArray = new ArrayList<>();
for (int i = 0; i < 50; i++) {
topicsArrayList.add("my " + i);
}
adapter = new TopicRecyclerAdapter(topicsArrayList,
selectedArray);
recyclerView.setLayoutManager(new
LinearLayoutManager(getContext()));
recyclerView.setAdapter(adapter);
adapter.notifyDataSetChanged();
mBehavior.setBottomSheetCallback(new
BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(#NonNull View
bottomSheet, int newState) {
if (BottomSheetBehavior.STATE_HIDDEN ==
newState) {
dismiss();
}
}
#Override
public void onSlide(#NonNull View bottomSheet,
float slideOffset) {
}
});
((ImageButton)
view.findViewById(R.id.bt_close)).setOnClickListener(new
View.OnClickListener() {
#Override
public void onClick(View v) {
dismiss();
}
});
return dialog;
}
#Override
public void onStart() {
super.onStart();
mBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
private void hideView(View view) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = 0;
view.setLayoutParams(params);
// dismiss();
// dismissAllowingStateLoss();
}
private void showView(View view, int size) {
ViewGroup.LayoutParams params = view.getLayoutParams();
params.height = size;
view.setLayoutParams(params);
}
private int getActionBarSize() {
final TypedArray styledAttributes =
getContext().getTheme().obtainStyledAttributes(new int[]
{android.R.attr.actionBarSize});
int size = (int) styledAttributes.getDimension(0, 0);
return size;
}
}
What exactly i seek to achieve is shown in this image, the toolbar is stable despite keyboard is visible or not.
Thanks for any help
You can try this to initialize your activity in manifest.
<activity
android:name=".Activities"
android:configChanges="orientation|screenSize|layoutDirection"
android:exported="true"
android:screenOrientation="portrait"android:windowSoftInputMode="adjustPan|adjustResize|stateAlwaysVisible"/>
The most convenient way that I found to change this is by creating style:
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize</item>
And set this in onCreate method of your BottomSheetDialogFragment:
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle)}
Hope this will help you.
The best way to do that is to make specific style for that inside styles.xml
<style name="DialogStyle" parent="Theme.Design.Light.BottomSheetDialog">
<item name="android:windowIsFloating">false</item>
<item name="android:statusBarColor">#android:color/transparent</item>
<item name="android:windowSoftInputMode">adjustResize|stateAlwaysVisible</item>
Once we can't initialize any fragment or BottomSheetDialogFragment inside our manifest, the best way is to go to styles.xml (DialogStyle) and and make
<item name="android:windowsSoftInputMode">adjustResize|stateAlwaysVisible</item>
After that we should override onCreate inside our FragmentBottomSheetDialog.java
override void onCreate(#Nullable Bundle savedInstanceState)
super.onCreate(savedInstanceState);
setStyle(DialogFragment.STYLE_NORMAL, R.style.DialogStyle);
}
this works prefectly
Thanks for #hardik-kotadiya

Android bottom sheet does not collapse

I'm trying to create a simple Bottom Sheet layout, One button shows and other collapse, but its not working. I was following this tutorial but still nothing.So I'm trying to pass the state inside of buttons and then pass into the callback method to hide or expand, and the bottom sheet can't be scrolled down!
This is the layout.
<android.support.design.widget.CoordinatorLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/activity_botton_test"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<ScrollView
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"
android:paddingTop="24dp">
<Button
android:id="#+id/button_1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Button 1"
android:padding="16dp"
android:layout_margin="8dp"
android:textColor="#android:color/white"
android:background="#android:color/holo_green_dark"/>
<Button
android:id="#+id/button_2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_margin="8dp"
android:text="Button 2"
android:textColor="#android:color/white"
android:background="#android:color/holo_blue_light"/>
<Button
android:id="#+id/button_3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:layout_margin="8dp"
android:text="Button 3"
android:textColor="#android:color/white"
android:background="#android:color/holo_red_dark"/>
</LinearLayout>
</ScrollView>
<android.support.v4.widget.NestedScrollView
android:id="#+id/bottom_sheet"
android:layout_width="match_parent"
android:layout_height="350dp"
android:clipToPadding="true"
android:background="#android:color/holo_orange_light"
app:layout_behavior="android.support.design.widget.BottomSheetBehavior"
>
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:text="opaa"
android:padding="16dp"
android:textSize="16sp"/>
</android.support.v4.widget.NestedScrollView>
Activity :
private View bottomSheet;
private Button button1,button2,button3;
private BottomSheetBehavior mBottomSheetBehavior;
bottomSheet = findViewById( R.id.bottom_sheet );
button1 = (Button)findViewById(R.id.button_1);
button2 = (Button)findViewById(R.id.button_2);
button3 = (Button)findViewById(R.id.button_3);
mBottomSheetBehavior = BottomSheetBehavior.from(bottomSheet);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
mBottomSheetBehavior.setBottomSheetCallback(new BottomSheetBehavior.BottomSheetCallback() {
#Override
public void onStateChanged(View bottomSheet, int newState) {
if (newState == BottomSheetBehavior.STATE_COLLAPSED) {
mBottomSheetBehavior.setPeekHeight(0);
}
if(newState == BottomSheetBehavior.STATE_EXPANDED){
mBottomSheetBehavior.setPeekHeight(500);
}
}
#Override
public void onSlide(View bottomSheet, float slideOffset) {
}
});
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_EXPANDED);
}
});
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_COLLAPSED);
}
});
I ran into the same issue when trying that tutorial myself.
You have to set the BottomSheetBehavior to be hideable. Moreover, the COLLAPSED state may not be what you are looking for. To make the BottomSheet disappear completely, you have to set the state to HIDDEN. Like this:
mBottomSheetBehavior.setHideable(true);
mBottomSheetBehavior.setState(BottomSheetBehavior.STATE_HIDDEN);
Put TextView into LinearLayout

Android background color for accordion menu

I want the code in the Java file to set the background color of whatever button was clicked, and keep it, even after you let go of the button. Thanks in advance
I don't really get the formatting on here. I hope it's clear what belongs where.
Java:
package mika.actual;
import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
public class accordion extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.accordian);
Button btnProfile = (Button) findViewById(R.id.btnProfile);
Button btnSettings = (Button) findViewById(R.id.btnSettings);
Button btnPrivacy = (Button) findViewById(R.id.btnPrivacy);
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
btnProfile.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.VISIBLE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnSettings.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.VISIBLE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.GONE);
}
});
btnPrivacy.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
View panelProfile = findViewById(R.id.panelProfile);
panelProfile.setVisibility(View.GONE);
View panelSettings = findViewById(R.id.panelSettings);
panelSettings.setVisibility(View.GONE);
View panelPrivacy = findViewById(R.id.panelPrivacy);
panelPrivacy.setVisibility(View.VISIBLE);
}
});
}
}
XML:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF"
android:orientation="vertical">
<Button
android:id="#+id/btnProfile"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Profile"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelProfile"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#FFFFFFFF">
<LinearLayout
android:id="#+id/panelProfile1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Name" />
<EditText
android:id="#+id/txtName"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelProfile2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strSurname"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Surname" />
<EditText
android:id="#+id/txtSurname"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnSettings"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Settings"
android:textColor="#FFFFFFFF" />
<LinearLayout
android:id="#+id/panelSettings"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:id="#+id/panelSettings1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strMail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="e-mail" />
<EditText
android:id="#+id/txtMail"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
<LinearLayout
android:id="#+id/panelSettings2"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#FFFFFFFF">
<TextView
android:id="#+id/strPhone"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Phone" />
<EditText
android:id="#+id/txtPhone"
android:layout_width="match_parent"
android:layout_height="wrap_content" />
</LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
<Button
android:id="#+id/btnPrivacy"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Privacy"
android:textColor="#FFFFFFFF" />
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:id="#+id/panelPrivacy"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:background="#FFFFFFFF">
<CheckBox
android:id="#+id/checkFacebook"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Facebook"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkLinkedIn"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="LinkedIn"
android:textColor="#ff355689">
</CheckBox>
<CheckBox
android:id="#+id/checkTwitter"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Twitter"
android:textColor="#ff355689">
</CheckBox>
</LinearLayout>
</ScrollView>
</LinearLayout>
If I understood correctly, what you want to do is, when clicking a button and expanding its sub-menu, change the background of that button until it is clicked again. You can do this with a Tag on the Button:
btnSettings.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// DO STUFF
//if button is not selected, change background and tag the View as selected
if(v.getTag() == null || v.getTag().equals("not_selected")) {
v.setBackgroundColor(Color.RED);
v.setTag("selected");
//show sub-menu
}
//if button is already selected, reset background and tag the View as not selected
else{
v.setBackgroundResource(android.R.drawable.btn_default);
v.setTag("not_selected");
//hide sub menu
}
}
});
Notice that v.setBackgroundResource(android.R.drawable.btn_default); is resetting the Button background to an android drawable. You may want to change this to a custom drawable or color.
You could have used an ExpandableListView to implement this kind of menu.
button.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_DOWN) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.presed_color));
} else if (event.getAction() == MotionEvent.ACTION_UP) {
view.setBackgroundColor(ContextCompat.getColor(context, R.color.unpresed_color));
}
return false;
}
});

The application may be doing too much work on its main thread. Even using AsyncTask

I am creating a android application.
In my app I have one listView and one gridView and when I set the addapter of those view I get the The application may be doing too much work on its main thread
I have try to put those setadapter on a AsyncTask but nothing change.. I do not understand.
My Activity :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
showToolbarTitle(false);
getSupportActionBar().setDisplayHomeAsUpEnabled(true);
getSupportActionBar().setHomeButtonEnabled(true);
toolbarTitle.setText("PARALLAX + FADING IMAGEVIEW");
toolbarTitle.setVisibility(View.INVISIBLE);
scrollview.setScrollViewCallbacks(this);
listView = (ListView) findViewById(R.id.listview);
gridView = (GridView) findViewById(R.id.gridView);
new LongOperation().execute(); //I call the AsyncTask
gridView.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
if (event.getAction() == MotionEvent.ACTION_MOVE) {
return true;
}
return false;
}
});
}
private class LongOperation extends AsyncTask<String, Void, String> {
#Override
protected String doInBackground(String... params) {
CustomList adapter = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, false);
// listView.setAdapter(adapter); // If I do this I will get the error
CustomList adapterGrid = new
CustomList(ParallaxLikeIoshedActivity.this, test, imageId, true);
// gridView.setAdapter(adapterGrid); // If I do this I will get the error
return "Executed";
}
#Override
protected void onPostExecute(String result) {
}
#Override
protected void onPreExecute() {}
#Override
protected void onProgressUpdate(Void... values) {}
}
This is the layout of the Activity :
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:fab="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
tools:context=".MainActivity">
<com.github.ksoichiro.android.observablescrollview.ObservableScrollView
android:id="#+id/scrollview"
android:scrollbars="none"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent">
<FrameLayout
android:id="#+id/image_container"
android:layout_width="match_parent"
android:layout_height="250dp">
<android.support.v4.view.ViewPager
android:id="#+id/pager"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="#000000"/>
<!--<ImageView
android:id="#+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"
android:src="#drawable/moviethumbnail"
tools:ignore="ContentDescription" />-->
</FrameLayout>
<LinearLayout
android:id="#+id/header"
android:layout_below="#id/image_container"
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="match_parent"/>
<RelativeLayout
android:background="#222"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<LinearLayout
android:gravity="center_vertical|center_horizontal"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true"
android:id="#+id/linearLayout">
<TextView
android:textAppearance="?android:textAppearanceMedium"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="MHeader 1 !!!"
android:textSize="40sp"
android:textColor="#color/white"/>
</LinearLayout>
</RelativeLayout>
</LinearLayout>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#id/header"
android:background="#ff7f7f7f"
android:showDividers="middle">
<com.example.edouard.NonScrollListView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/listview"
android:visibility="visible"/>
<GridView
android:paddingLeft="5dp"
android:paddingRight="5dp"
android:paddingBottom="5dp"
android:paddingTop="20dp"
android:divider="#000000"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/gridView"
android:visibility="invisible"/>
</FrameLayout>
</RelativeLayout>
</com.github.ksoichiro.android.observablescrollview.ObservableScrollView>
</FrameLayout>

Fragment onCreate() issue

i need to declare my Chronometer on onCreate() of my fragment because if I declare on onCreateView(), every time the ChronometerFragment is called the Chronometer is reseted.
My actual code:
ChronometerFragment.class
public class CronometroFragment extends Fragment {
Button btStart, btPause, btReset;
boolean click;
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View v = inflater.inflate(R.layout.fragment_cronometro, container,
false);
final Cronometro cronometro = (Cronometro) v.findViewById(R.id.cronometro);
click = true;
btStart = (Button) v.findViewById(R.id.btStart);
btPause = (Button) v.findViewById(R.id.btPause);
btReset = (Button) v.findViewById(R.id.btReset);
btStart.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
cronometro.start();
}
});
btPause.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
cronometro.pause();
}
});
btReset.setOnClickListener(new View.OnClickListener() {
public void onClick(View arg0) {
cronometro.setBase(SystemClock.elapsedRealtime());
cronometro.stop();
}
});
return v;
}
fragment_cronometro.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#f0f0f0"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/Title"
android:layout_width="match_parent"
android:layout_height="40sp"
android:gravity="center" >
<TextView
android:id="#+id/titleText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/title_cronometro"
android:textSize="35sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/Timer"
android:layout_width="match_parent"
android:layout_height="70sp"
android:gravity="center"
android:orientation="horizontal" >
<com.hello.app.Cronometro
android:id="#+id/cronometro"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:textSize="45sp" />
</LinearLayout>
<LinearLayout
android:id="#+id/Start"
android:layout_width="match_parent"
android:layout_height="50sp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btStart"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="#string/button_start" />
</LinearLayout>
<LinearLayout
android:id="#+id/PauseStop"
android:layout_width="match_parent"
android:layout_height="50sp"
android:gravity="center"
android:orientation="horizontal" >
<Button
android:id="#+id/btPause"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_pause" />
<Button
android:id="#+id/btReset"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="#string/button_reset" />
</LinearLayout>
</LinearLayout>
My problem is that user cannot change fragment because this reset the Chronometer.
Please, can someone help me?
Thanks.
This question is badly formulated.. But in your onCreate, put setretaininstance(true). This should work. :-)

Categories