android onDraw() method is not called at first time - java

I have tested this android keyboard, but I have detected a problem, the keyboard is not displayed at the first time when I have debug the source code I have detected that the method onDraw() in the file KeyboardView.java is not called at the first time ie when I quit the application and I go back the keyboard will be displayed after that it will be displayed for all applications.
UPDATE:
#Override
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
// Round up a little
if (mKeyboard == null) {
setMeasuredDimension(getPaddingLeft() + getPaddingRight(), getPaddingTop() + getPaddingBottom());
} else {
int width = mKeyboard.getMinWidth() + getPaddingLeft() + getPaddingRight();
//MeasureSpec.getSize(widthMeasureSpec) = 0 when is called at first time (and in this case onDraw will not be called),
//after that when I quit my application and reopen it the value became different of 0, onDraw will be called and keyboard is displayed
if (MeasureSpec.getSize(widthMeasureSpec) < width + 10) {
width = MeasureSpec.getSize(widthMeasureSpec);
}
setMeasuredDimension(width, mKeyboard.getHeight() + getPaddingTop() + getPaddingBottom());
}
}
I have remarked that MeasureSpec.getSize(widthMeasureSpec) is equal to 0 when it called first time.
I think this is the root cause for not displaying keyboard at first call.
Can someone have an explanation why MeasureSpec.getSize(widthMeasureSpec) returns 0?
Here is my layout:
<FrameLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_height="wrap_content"
android:layout_width="match_parent"
android:background="#color/ime_background_letters" >
<FrameLayout
android:id="#+id/keyboard_wrapper"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal|bottom" >
<com.android.inputmethod.latin.car.KeyboardView
android:id="#+id/keyboard"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:focusable="true"
app:keyTextColorPrimary="#color/ime_foreground_numbers"
android:layout_gravity="center_horizontal"
style="#style/Keyboard" />
<com.android.inputmethod.latin.car.KeyboardView
android:id="#+id/popup_keyboard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="#dimen/keyboard_popup_offset"
android:visibility="gone"
app:keyTextColorPrimary="#color/ime_foreground_numbers"
android:layout_gravity="top|center"
style="#style/Keyboard" />
</FrameLayout>
<FrameLayout
android:id="#+id/lockout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:alpha="0.935"
android:clickable="true" >
<TextView
android:id="#+id/lockout_label"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:fontFamily="sans-serif-condensed"
android:layout_gravity="center"
android:textColor="#color/ime_foreground_letters"
android:textSize="#dimen/keyboard_lockout_text_size"
android:text="#string/park_to_use_keyboard" />
</FrameLayout>
</FrameLayout>

Related

How can I check if at least one ToggleButton has been Checked in a row of Buttons?

I'm working on creating an application, which amongst other features has an integrated GAD Test functionality (self test to calculate and measure the user's stress level). This is how it looks:
It is consisted of a Table, with multiple rows of ToggleButtons. This is the code for 1 of the buttons, as an example:
<ToggleButton
android:id="#+id/row1_btn4"
android:layout_width="200px"
android:layout_height="60dp"
android:layout_gravity="center_horizontal"
android:background="#drawable/button_border"
android:gravity="center"
android:paddingStart="10px"
android:paddingEnd="10px"
android:scaleX="0.5"
android:scaleY="0.65"
android:textColor="#color/white"
android:textOff=" "
android:textOn="✓"
android:textSize="28sp" />
and this is the code to check whether a button is checked or not:
row1_btn4.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
if (isChecked) {
gadpoints += 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else if (!isChecked) {
gadpoints -= 3;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
} else {
gadpoints += 0;
((DataSite) getActivity().getApplication()).setGadPoints(gadpoints);
}
}
});
Everything is working as it should, if a ToggleButton is checked, the user is awarded the given points. However, I would like to implement 2 things:
a) Make it so that only 1 button from each row can be checked, and prevent the user from checking another one from the same row if he / she already checked 1
b) Check whether in a row of buttons none of them has been checked, and if so, notify the user
I cannot think of a feasible solution to this, because I will be essentially checking if a button hasn't been checked, but then again, some of them are meant to be unchecked. Any ideas?
You can manage having only one entry selected per row using a RadioGroup. To start the RadioGroup with nothing selected you can call clearCheck() on it.
To make sure that a selection was made in all rows when they click "Submit" you can call getCheckedRadioButtonId() on each group/row, it will return -1 if nothing was selected.
If you want to customize what the radio buttons look like there are lots of examples of how to do that here.
Here is an example of what that would look like in onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
List<RadioGroup> rows = Arrays.asList(
findViewById(R.id.row1),
findViewById(R.id.row2)
);
// Start out with all buttons un-checked
for(RadioGroup row : rows) {
row.clearCheck();
}
// make a list of button IDs for each row to
// simplify calculating the score
List<List<Integer>> rowIds = Arrays.asList(
Arrays.asList(R.id.row1_btn1, R.id.row1_btn2, R.id.row1_btn3, R.id.row1_btn4),
Arrays.asList(R.id.row2_btn1, R.id.row2_btn2, R.id.row2_btn3, R.id.row2_btn4)
);
Button submit = findViewById(R.id.submit);
submit.setOnClickListener(v -> {
// check that all rows have selections
boolean missing_values = false;
for(RadioGroup row : rows) {
// This will be -1 if nothing was selected
if( row.getCheckedRadioButtonId() == -1 ) {
missing_values = true;
}
}
if( missing_values ) {
Toast.makeText(this, "Missing entries on some rows", Toast.LENGTH_LONG).show();
}
else {
// handle the result - add up scores or whatever is needed and send the data
// to the model
int score = 0;
for(int r = 0; r < rows.size(); ++r) {
int sel = rows.get(r).getCheckedRadioButtonId();
score += rowIds.get(r).indexOf(sel);
}
Toast.makeText(this, "All rows selected - score = " + score, Toast.LENGTH_LONG).show();
}
});
}
and the XML that goes along with it
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.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=".MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row1_title"
android:text="Row 1"
android:layout_margin="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/row1"
app:layout_constraintBottom_toBottomOf="#id/row1"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/row1"
android:orientation="horizontal"
app:layout_constraintStart_toEndOf="#id/row1_title"
app:layout_constraintTop_toTopOf="parent">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row1_btn1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row1_btn2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row1_btn3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row1_btn4"/>
</RadioGroup>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row2_title"
android:text="Row 2"
android:layout_margin="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="#id/row2"
app:layout_constraintBottom_toBottomOf="#id/row2"/>
<RadioGroup
android:layout_width="0dp"
android:layout_height="wrap_content"
android:id="#+id/row2"
android:orientation="horizontal"
app:layout_constraintStart_toEndOf="#id/row2_title"
app:layout_constraintTop_toBottomOf="#id/row1">
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row2_btn1"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row2_btn2"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row2_btn3"/>
<RadioButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/row2_btn4"/>
</RadioGroup>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Submit"
android:id="#+id/submit"
android:layout_margin="8dp"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toBottomOf="#id/row2"
/>
</androidx.constraintlayout.widget.ConstraintLayout>
Instead of using toggle buttons, use a Radio Group for each row.
Then you can design a custom drawable for Radio Buttons to make them look like what you currently have.
Create custom button using this
<?xml version="1.0" encoding="utf-8"?>
<item android:state_checked="false" android:drawable="#drawable/ic_box"/>
<item android:state_checked="true" android:drawable="#drawable/ic_check"/>
You can get Box and Check drawable from Resource Manager in android studio.

Android studio automatic horizontalscrollview infinite scrolling till end then repeat

Im using android studio , i have xml which have horizontalscrollview
Then linearlayout then multiple imageviews ..
<HorizontalScrollView
android:id="#+id/horizontalScrollView1"
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_launcher" />
</LinearLayout>
What i need is to make this horizantalscrollview to keep automatically infinite scrolling the images till the end then repeat the scrolling again and again.
what i mean by auto scrolling that the horizontalscrollview keep scrolling without using hand
Did you try to use wrap_content in layout_width?
Now, u adapt your width layout to the width of the screen, i think that if u put the value wrap_content it will solve ur problem (u have layout_height=wrap_content, but the height is "asociated" with a Vertical scroll, not with horizontal scroll):
android:layout_width="wrap_content"
I hope this help u.
ok Thanks for your support .. i found the solution
Timer timer1 = new Timer("horizontalScrollViewTimer");
timer1.scheduleAtFixedRate(new TimerTask() {
#Override
public void run() {
runOnUiThread(new Runnable() {
#Override
public void run() {
if (scrollingLeft) {
if (scroll.getScrollX() == 0) {
scroll.smoothScrollBy(5, 0);
scrollingLeft = false;
} else {
scroll.smoothScrollBy(-5, 0);
}
} else {
if (scroll.canScrollHorizontally(View.FOCUS_RIGHT)) {
scroll.smoothScrollBy(5, 0);
} else {
scroll.smoothScrollBy(-5, 0);
scrollingLeft = true;
}
}
}
});
}
}, 1000, 50);

ScrollView with floating button

I have made an app where an activity contains two TextView, one ImageView and FloatingActionButton. I want to make the layout like this.
Here is my code.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView 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:fillViewport="true"
tools:context=".DetailsActivity"
android:id="#+id/scrollView"
>
<android.support.design.widget.CoordinatorLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
>
<!-- <ProgressBar
android:id="#+id/progressBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center" /> -->
<TextView
android:id="#+id/titleTextView"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="15dp"
android:textSize="25sp"
android:textStyle="bold"
/>
<TextView
android:id="#+id/blogContent"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:textSize="16sp"
/>
<ImageView
android:id="#+id/youtubeThumbnailImage"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginBottom="20dp"
android:src="#mipmap/ic_launcher"
android:visibility="gone"
/>
</LinearLayout>
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="start"
android:layout_margin="20dp"
android:src="#drawable/icons8_share_480"
app:fabSize="auto"
/>
</android.support.design.widget.CoordinatorLayout>
</ScrollView>
and I use the code from this post to make it hide when scroll down on all android api's.
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
scrollView.setOnScrollChangeListener(new View.OnScrollChangeListener() {
#Override
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
if (scrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (scrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
} else {
scrollView.getViewTreeObserver().addOnScrollChangedListener(new ViewTreeObserver.OnScrollChangedListener() {
#Override
public void onScrollChanged() {
int mScrollY = scrollView.getScrollY();
if (mScrollY > 0 && fab.isShown()) {
fab.setVisibility(View.GONE);
} else if (mScrollY < 0) {
fab.setVisibility(View.VISIBLE);
}
}
});
}
This works on android 6.0. But higher than 6.0, the floating button appears on the corner top right of activity. Here is the pic below.
I tried many property options like- android:layout_gravity="" and put the FloatingButton directly on main LinearLayout or FrameLayout. Still these Layouts did not work.
Using a CoordinatorLayout, you should try setting the FloatingActionButtons anchor to reference the LinearLayout and the use layout_anchorGravity to position the FAB.
Example code for your FAB:
<android.support.design.widget.FloatingActionButton
android:id="#+id/fab"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_anchor="#id/linearLayoutID"
app:layout_anchorGravity="bottom|right|end"
android:layout_margin="16dp"
android:src="#drawable/icons8_share_480"
/>
Remember to set an ID for you LinearLayout. I used #id/linearLayoutID as a placeholder.

RecyclerView - dynamic height without shifting elements

What I need:
I tried doing this using a RecyclerView that sets the height of the item dynamically based on the model. I am using
Layout:
<?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"
android:id="#+id/card_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
card_view:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/white"
android:orientation="vertical"
android:gravity="center|bottom"
android:layout_weight="0">
<TextView
android:id="#+id/teacher"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:padding="5dp"
android:textColor="#android:color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/room"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/teacher"
android:padding="5dp"
android:textColor="#android:color/black"
android:textSize="16sp" />
<TextView
android:id="#+id/subject"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#1976D2"
android:gravity="center_horizontal"
android:layout_alignParentBottom="true"
android:paddingBottom="8dp"
android:paddingTop="8dp"
android:textColor="#ffffff"
android:textSize="13sp"
android:maxLines="3"
android:lines="1" />
</RelativeLayout>
</android.support.v7.widget.CardView>
Adapter-code:
override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): SampleViewHolders {
val layoutView = LayoutInflater.from(parent.context).inflate(
R.layout.rv_grid, null)
layoutView.getViewTreeObserver().addOnPreDrawListener(object : ViewTreeObserver.OnPreDrawListener {
override fun onPreDraw(): Boolean {
val lp = layoutView.getLayoutParams()
if (lp is StaggeredGridLayoutManager.LayoutParams) {
val sglp = lp as StaggeredGridLayoutManager.LayoutParams
when (viewType) {
1 -> sglp.isFullSpan = false
2 -> {
sglp.height = layoutView.height * 2;
layoutView.refreshDrawableState();
}
3 -> sglp.height = layoutView.height * 3;
}
layoutView.setLayoutParams(sglp)
val lm = (parent as RecyclerView).layoutManager as StaggeredGridLayoutManager
lm.invalidateSpanAssignments()
}
layoutView.getViewTreeObserver().removeOnPreDrawListener(this)
return true
}
})
return ViewHolder(layoutView)
}
Where it does multiplies with the number of hours the meeting takes.
The problem is that if there is a one-hour meeting after a 2-hour meeting, it gets shifted to the next day.
See:
With 2-hour meetings:
With 1-hour meetings:
How can I fix this in the adapter/how can I adapt the height without shifting the elements?
Import the library into your project.
Grab via maven
<dependency>
<groupId>com.github.alamkanak</groupId>
<artifactId>android-week-view</artifactId>
<version>1.2.6</version>
<type>aar</type>
</dependency>
Grab via gradle
compile 'com.github.alamkanak:android-week-view:1.2.6'
Add WeekView in your xml layout.
<com.alamkanak.weekview.WeekView
android:id="#+id/weekView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:eventTextColor="#android:color/white"
app:textSize="12sp"
app:hourHeight="60dp"
app:headerColumnPadding="8dp"
app:headerColumnTextColor="#8f000000"
app:headerRowPadding="12dp"
app:columnGap="8dp"
app:noOfVisibleDays="3"
app:headerRowBackgroundColor="#ffefefef"
app:dayBackgroundColor="#05000000"
app:todayBackgroundColor="#1848adff"
app:headerColumnBackground="#ffffffff"/>
Write the following code in your java file.
/
/ Get a reference for the week view in the layout.
mWeekView = (WeekView) findViewById(R.id.weekView);
// Set an action when any event is clicked.
mWeekView.setOnEventClickListener(mEventClickListener);
// The week view has infinite scrolling horizontally. We have to provide the events of a
// month every time the month changes on the week view.
mWeekView.setMonthChangeListener(mMonthChangeListener);
// Set long press listener for events.
mWeekView.setEventLongPressListener(mEventLongPressListener);
Implement WeekView.MonthChangeListener, WeekView.EventClickListener, WeekView.EventLongPressListener according to your need.
Provide the events for the WeekView in WeekView.MonthChangeListener.onMonthChange() callback. Please remember that the calendar pre-loads events of three consecutive months to enable lag-free scrolling.
MonthLoader.MonthChangeListener mMonthChangeListener = new MonthLoader.MonthChangeListener() {
#Override
public List<WeekViewEvent> onMonthChange(int newYear, int newMonth) {
// Populate the week view with some events.
List<WeekViewEvent> events = getEvents(newYear, newMonth);
return events;
}
};

ImageButtons being squished when my drag and drop ImageButton is moved

So I have 4 normal imagebuttons that I have set up and then a 5th imagebutton that is a drag and drop. I want the 4 non drag and drop imagebuttons to stay in a particular spot no matter what happens with the imagebutton that is drag and drop. When I move my drag and drop imagebutton, the other imagebuttons get moved and are being squished also.
Any idea how I can move this drag and drop imagebutton without affecting the other imagebuttons in any way?
Here is my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:background="#drawable/mars" >
<TextView
android:id="#+id/scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/mainhut" />
<ImageButton
android:id="#+id/polarCapButton1"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/polarCapButton2"
android:layout_marginTop="-70dp"
android:layout_marginLeft="575dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap" />
<ImageButton
android:id="#+id/spaceRockButton1"
android:layout_marginTop="100dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spacerock" />
<ImageButton
android:id="#+id/spaceRockButton2"
android:layout_marginTop="-140dp"
android:layout_marginLeft="520dp"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/spacerock" />
<ImageButton
android:id="#+id/meteor"
android:visibility="invisible"
android:orientation="vertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/meteor"
android:layout_marginTop="-100dp"
android:layout_marginLeft="400dp" />
</LinearLayout>
Here is the code for my drag and drop imagebutton:
mainHut = (ImageButton) findViewById(R.id.mainHut);
mainHut.setOnTouchListener(new OnTouchListener()
{
#Override
public boolean onTouch(View v, MotionEvent event)
{
if (event.getAction() == MotionEvent.ACTION_DOWN)
{
mainHutSelected = true;
}//end if
if (event.getAction() == MotionEvent.ACTION_UP)
{
if (mainHutSelected == true)
{
mainHutSelected = false;
}//end if
}//end if
else if (event.getAction() == MotionEvent.ACTION_MOVE)
{
if (mainHutSelected == true)
{
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(v.getWidth(), v.getHeight());
params.setMargins((int)(event.getRawX() - event.getRawY() / 2), (int) (event.getRawY() - v.getHeight() * 1.5), (int) (event.getRawX() - v.getWidth() / 2), (int) (event.getRawY() - v.getHeight() * 1.5));
v.setLayoutParams(params);
}//end if
}//end else
return false;
}//end onTouch function
});//end setOnClickListener
Thanks in advance for the help.
I suggest you use a FrameLayout. This will make your imageviews stackable.
<FrameLayout>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
<ImageButton/>
</FrameLayout>
You will need to set them all to have a static position on the screen based on its parents origin. The 5th ImageButton, you can use it to set the onTouch listener like you did.
Edit: OR you can do something like this (If you wanna keep the linear layout):
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gllayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/mars"
android:orientation="vertical" >
<TextView
android:id="#+id/scores"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
<ImageButton
android:id="#+id/polarCapButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/polarcap"
android:orientation="vertical" />
<ImageButton
android:id="#+id/polarCapButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="575dp"
android:layout_marginTop="-70dp"
android:background="#drawable/polarcap"
android:orientation="vertical" />
<ImageButton
android:id="#+id/spaceRockButton1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="100dp"
android:background="#drawable/spacerock"
android:orientation="vertical" />
<ImageButton
android:id="#+id/spaceRockButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="520dp"
android:layout_marginTop="-140dp"
android:background="#drawable/spacerock"
android:orientation="vertical" />
<ImageButton
android:id="#+id/meteor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="400dp"
android:layout_marginTop="-100dp"
android:background="#drawable/meteor"
android:orientation="vertical"
android:visibility="invisible" />
</LinearLayout>
<ImageButton
android:id="#+id/mainHut"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="put offset here"
android:background="#drawable/mainhut" />
</FrameLayout>
Just change the marginTop on mainHut to the offset you want to set it at!
Without looking at your XML file, it is probably due to the use of a relative layout - the 4 static buttons may be positioned relative to the button that is being moved, and will move in real-time when dragging and dropping. Try using absolute positioning (e.g., android:layout_marginTop="30dp") instead of relative, or using another type of layout. If you could post both your Activity where this happens and your XML file for that layout, that will better help provide some information for more appropriate answers.
I'm not sure if this is what you want.
But you could use a floating imageButton, using the Window Manager. But this way the button would be a standalone overlay and no longer be part of the 4-buttons View. See this post: What APIs in Android is Facebook using to create Chat Heads?

Categories