How can i overlay one Button over another button - android

i want 0 to 9 buttons in two row.Each button have overlay of another small button in right bottom corner.when i click the button,count will be displayed on overlay button.sorry for my bad english thanks in advance
<?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">
<LinearLayout
android:id="#+id/expandable2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#000000"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
RelativeLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<Button
android:id="#+id/btn0"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="0" />
<Button
android:layout_width="25dp"
android:layout_height="25dp"
android:layout_alignBottom="#id/btn0"
android:layout_alignRight="#id/btn0"
android:background="#FFFFFF"
android:text="0" />
</RelativeLayout>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="5" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
My output
any other suggestion will be appreciated

Try setting it like this
android:layout_marginLeft="-20dp"
android:layout_marginTop="10dp"

Use a textview for showing the count because a button over a button is meaningless.
Anyways how to show it.
<RelativeLayout
android:layout_width = "match_parent"
android:layout_height = "60dp">
<Button
android:layout_width = "match_parent"
android:layout_height="match_parent"
android:id="#+id/btn"/>
<Button
android:layout_width = "wrap_content"
android:layout_height="wrap_content"
android:id="#+id/countbtn"
android:layout_alignParentBottom="true"
android:layout_alignParentRight= "true"/>
</RelativeLayout>
use this view in your Grid or RecyclerView Adapter to make it 10 times as you wish to show.
incase you want to use TextView to show Count remove the countBtn and make it a textView with last two attributes same.

Use a framelayout
<FrameLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true">
<Button
android:id="#+id/icon_image"
android:layout_width="100dp"
android:layout_height="wrap_content"
android:layout_margin="5dp"
android:background="#color/black"
android:text="0" />
<Button
android:layout_width="20dp"
android:layout_height="20dp"
android:layout_gravity="bottom|right"
android:background="#color/white"
android:layout_marginLeft="6dp"
android:src="#drawable/ic_goal_check" />
</FrameLayout>
After that you can reuse this layout for all your buttons. To reuse this in your xml, create a custom view as follows :
public class YourButton extends FrameLayout {
private Button buttonOne, buttonTwo;
public YourButton(Context context) {
super(context);
this.context = context;
initializeViews();
}
public YourButton(Context context, AttributeSet attrs) {
super(context, attrs);
this.context = context;
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.GoalButton);
typedArray.recycle();
initializeViews();
}
private void initializeViews() {
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater.inflate(R.layout.your_button_layout, this);
}
#Override
protected void onFinishInflate() {
super.onFinishInflate();
buttonOne = (TextView) this.findViewById(R.id.button_one);
buttonTwo = (ImageView) this.findViewById(R.id.button_two);
}
public void setCount(String count){
buttonTwo.setText(count);
}
}
now you can use in your layout as
<yourPackageName.YourButton
android:id="#+id/button_one"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
This will keep your main layout simple and tidy.

Related

Custom View getHitRect return wrong value

I created a new custom view that looks like this
public class SearchCategoryPanel extends RelativeLayout {
private boolean hasRetrieved = false;
private GridLayout categoryContainer;
private TextView scpLoadingText;
public static final int HEIGHT = 360;
private ArrayList<OnCategoryItemClickListener> onCategoryItemClickListeners = new ArrayList<OnCategoryItemClickListener>();
public SearchCategoryPanel(Context context) {
super(context);
init();
}
public SearchCategoryPanel(Context context, AttributeSet attrs) {
super(context, attrs);
Log.i(SearchCategoryPanel.class.getSimpleName(), this.getTop() + "");
init();
}
public SearchCategoryPanel(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init();
}
public void init() {
inflate(getContext(), R.layout.search_category_panel, this);
this.scpLoadingText = (TextView) findViewById(R.id.sCategoryPanelLoadingText);
this.categoryContainer = (GridLayout) findViewById(R.id.sCategoryContainer);
this.setVisibility(View.GONE);
}
public void show() {
this.setVisibility(View.VISIBLE);
}
public void hide() {
this.setVisibility(View.GONE);
}
public void setProcessing(boolean on) {
if(!on) {
this.scpLoadingText.setVisibility(View.GONE);
} else {
this.scpLoadingText.setVisibility(View.VISIBLE);
}
}
public void addCategoryItemUsingVo(CategoryVo categoryVo) {
CategoryItem item = new CategoryItem(getContext());
item.setCategoryVo(categoryVo);
item.setOnItemClickListener(new CategoryItem.OnClickListener() {
#Override
public void onClick(CategoryVo categoryVo) {
triggerOnCategoryItemClickListener(categoryVo);
}
});
this.categoryContainer.addView(item);
}
public void removeAllItems() {
this.categoryContainer.removeAllViews();
}
public boolean hasRetrieved() {
return hasRetrieved;
}
public interface OnCategoryItemClickListener {
public void onClick(CategoryVo categoryVo);
}
public void setOnCategoryItemClickListener(OnCategoryItemClickListener listener) {
this.onCategoryItemClickListeners.add(listener);
}
private void triggerOnCategoryItemClickListener(CategoryVo vo) {
for(OnCategoryItemClickListener listener : onCategoryItemClickListeners ) {
listener.onClick(vo);
}
}
}
Then, I try to call the getHitRect
Rect rectF = new Rect();
searchCategoryPanel.getHitRect(rectF);
Log.i(ARModeActivity.class.getSimpleName(), "Rect: " + rectF.toString());
The log returns strange value: Rect: Rect(0, 0 - 1920, 1164). this means the relative layout covers the whole screen which is not correct.
The SearchCategoryPanel xml looks like this:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:background="#7F000000"
android:layout_alignParentBottom="true"
android:maxHeight="360dp"
android:layout_height="160dp">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="match_parent">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="Search Category"
android:textAlignment="center"
android:textColor="#android:color/white"
android:textSize="24dp"/>
<TextView
android:id="#+id/sCategoryPanelLoadingText"
android:layout_width="wrap_content"
android:textSize="24sp"
android:textColor="#android:color/white"
android:layout_height="match_parent"
android:text="Loading.."/>
<HorizontalScrollView
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<GridLayout
android:id="#+id/sCategoryContainer"
android:layout_width="wrap_content"
android:orientation="horizontal"
android:layout_height="wrap_content">
</GridLayout>
</HorizontalScrollView>
</LinearLayout>
</RelativeLayout>
The relative layout only has 360dp in height, andfull width (look at the xml). but it returns the wrong value. I have read about onMeasure and onSizeChanged, but nothing helped
Here How I call the search_category_panel layout:
<?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:orientation="vertical"
android:layout_height="match_parent"
android:id="#+id/rootArMode"
tools:context="com.imav.ARModeActivity">
<RelativeLayout
android:id="#+id/rlInfoARMode"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:background="#7F000000"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:orientation="vertical"
android:layout_centerHorizontal="true"
android:gravity="center"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:textColor="#FFF"
android:textSize="24sp"
android:text="Interaction Guide"/>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Select: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Tap on object with 1 finger "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Move: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and Drag Object with 1 finger "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_marginBottom="4sp"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Rotate: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and drag object with 2 fingers "/>
</LinearLayout>
<LinearLayout
android:gravity="left"
android:layout_width="match_parent"
android:layout_marginBottom="4sp"
android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="18sp"
android:textStyle="bold"
android:text="Scale: "/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:textColor="#FFF"
android:textSize="16sp"
android:text="Touch and pinch object with 2 fingers "/>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
<Button
android:id="#+id/btnCloseArMode"
android:layout_width="35dp"
android:layout_height="35dp"
android:layout_alignParentLeft="true"
android:layout_marginLeft="10dp"
android:layout_marginRight="15dp"
android:background="#drawable/ic_clear_white_24dp" />
<RelativeLayout
android:id="#+id/rlOpenActionBar"
android:layout_width="55dp"
android:layout_height="match_parent"
android:layout_alignParentRight="true">
<Button
android:id="#+id/btnOpenActionBar"
android:background="#drawable/ic_keyboard_backspace_white_24dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"/>
</RelativeLayout>
<RelativeLayout
android:layout_width="55dp"
android:id="#+id/rlActionBar"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#7F000000"
android:layout_alignParentRight="true">
<Button
android:id="#+id/btnInfoArMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_error_white_24dp"
android:layout_height="wrap_content" />
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerVertical="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnAddObjArMode"
android:layout_marginBottom="8dp"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#drawable/ic_add_white_24dp"
android:layout_below="#+id/btnSaveSceneArMode" />
<Button
android:id="#+id/btnSaveSceneArMode"
android:layout_marginBottom="8dp"
android:layout_width="wrap_content"
android:background="#drawable/ic_add_a_photo_white_24dp"
android:layout_height="wrap_content" />
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="gone"
android:id="#+id/pbSaveSceneArMode"/>
<Button
android:id="#+id/btnShareARMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_share_white_24dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true">
<LinearLayout
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<Button
android:id="#+id/btnTrashArMode"
android:layout_width="wrap_content"
android:background="#drawable/ic_delete_white_24dp"
android:layout_height="wrap_content" />
</LinearLayout>
</RelativeLayout>
</RelativeLayout>
<com.imav.view.SearchCataloguePanel
android:id="#+id/searchCataloguePanelArMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.imav.view.SearchCataloguePanel>
<com.imav.view.SearchCategoryPanel
android:id="#+id/searchCategoryPanelArMode"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
</com.imav.view.SearchCategoryPanel>
</RelativeLayout>
My first approach would be not to override the onMeasure() in your custom view SearchCategoryPanel. Because you are forcing a different dimension than the ones provided by the xml.

Multiple instances of the same android compound component

I have a custom EditText component (compound component, based on LinearLayout) and I am using multiple instances in the same Activity.
The custom component works as expected, however, when I rotate the ui, the text that was entered in the second component is suddenly copied to the first component. Otherwise, both custom components function exactly as expected.
Both components have unique id in the activity layout.
Here is the code for my customer component:
public class MyEditText extends LinearLayout{
private EditText mEditText;
TextView mTextCounter;
private int errorIconRes;
private String textHint;
private boolean showLength;
private int maxCount;
private int lines;
private boolean phoneField;
private String errorMessage;
public MyEditText(Context context, AttributeSet attrs) {
super(context, attrs);
TypedArray a = context.obtainStyledAttributes(attrs, R.styleable.ZipEditText, 0, 0);
phoneField = a.getBoolean(R.styleable.ZipEditText_phone, false);
errorIconRes = a.getInt(R.styleable.ZipEditText_icon_err, R.drawable.form_error_icon);
textHint = a.getString(R.styleable.ZipEditText_hint);
maxCount = a.getInt(R.styleable.ZipEditText_maxLength, 100);
lines = a.getInt(R.styleable.ZipEditText_lines, 1);
showLength = a.getBoolean(R.styleable.ZipEditText_showLength, false);
a.recycle();
LayoutInflater inflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
View v = inflater.inflate(R.layout.comp_zip_edittext, this, true);
mEditText = (EditText) v.findViewById(R.id.text_field);
mEditText.addTextChangedListener(getTextWatcher());
if (textHint!= null)mEditText.setHint(textHint);
mImageViewError = (ImageView) v.findViewById(R.id.icon_error);
mTextCounter = (TextView) v.findViewById((R.id.text_counter));
if (lines>1 && !phoneField) {
mEditText.setLines(lines);
mEditText.setSingleLine(false);
}
if (showLength) {
mTextCounter.setVisibility(View.VISIBLE);
mEditText.setFilters(new InputFilter[]{new InputFilter.LengthFilter(maxCount)});
mEditText.addTextChangedListener(getTextWatcherLength());
} else {
mTextCounter.setVisibility(View.GONE);
}
Here is the layout file of my custom component:
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal">
<EditText
android:id="#+id/text_field"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:gravity="left"
android:paddingLeft="10dp"
android:singleLine="true"
android:inputType="textNoSuggestions"
android:layout_alignParentTop="true"
/>
<ImageView
android:id="#+id/icon_error"
android:layout_width="20dp"
android:layout_height="20dp"
android:src="#drawable/form_error_icon"
android:layout_marginTop="10dp"
android:layout_marginRight="15dp"
android:layout_alignRight="#id/text_field"
android:layout_alignTop="#id/text_field"
android:visibility="gone"/>
</RelativeLayout>
<TextView
android:id="#+id/text_counter"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Small Text"
android:layout_gravity="right"
android:visibility="gone"/>
</LinearLayout>
And here is my layout file for the activity:
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:zip="http://schemas.android.com/apk/res-auto"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/error_msg"
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:textColor="#color/home_image_red"
android:background="#color/zip_error_gray"
android:text="Something went wrong"
android:visibility="gone"
/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:orientation="vertical"
android:paddingLeft="20dp"
android:paddingRight="20dp"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:background="#color/white" >
<TextView
android:id="#+id/upsell"
android:layout_width="wrap_content"
android:layout_height="60dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:gravity="center"
android:textSize="18sp"
android:text="Sign in to use all our features."
/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_phone"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
android:singleLine="true"
zip:phone="true"
zip:hint="#string/hint_phone"/>
<com.myproject.android.components.MyEditText
android:id="#+id/request_comments"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_marginTop="5dp"
android:gravity="left"
zip:lines="3"
zip:maxLength="120"
zip:showLength="true"
zip:hint="#string/hint_comments"/>
<CheckBox
android:id="#+id/checkbox_request_visit"
style="#style/contact_label"
android:layout_centerInParent="true"
android:text="#string/check_home_visit"
android:checked="false" />
<Button
android:id="#+id/request_button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_marginBottom="5dp"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:text="Request a Showing"
android:textColor="#color/pbz_button_primary_foreground_color"
android:textStyle="bold"
android:background="#drawable/button_registration"
android:gravity="center"/>
<TextView
android:id="#+id/terms_link"
style="#style/FinePrint"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:gravity="center_horizontal"
android:paddingBottom="5dp"
android:text="#string/request_showing_terms"
/>
</LinearLayout>
</LinearLayout>
I figured out how to do this: Instead of loading the component via id, parse the tree and load it by the position in the tree:
Instead of
mEditText = (EditText) v.findViewById(R.id.text_field);
Do this:
LinearLayout linearLayout = (LinearLayout) getChildAt(0);
mEditText = (EditText) relativeLayout.getChildAt(0);
This way, you avoid loading the wrong component when the activity / fragment is re-created after the rotation
The problem is in the same Ids of your EditText components. Try to remove it id,and create you custom components separately by inflating each view from xml, and adding it to complex layout.

Android: List View and Scroll View compatability

A quick note about my application. This is an app that uses an API to get server requests about specific players for a game called League of Legends (LoL)
I recently changed my code from a static layout of 10 pre-set most recent matches to a ListView in order to avoid cludder as well in case a player did not have 10 recent matches.
The issue I am having with this ListView, is that when it is created, you can not scroll up and down through it. That is to say that it is created in a very small height, and when you scroll in it, the page doesn't scroll, the ListView itself scrolls. This is not desirable. I would rather have the user scroll the whole screen than just the small portion of the ListView.
I constructed my xml file like so:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<ScrollView
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="12dp"
android:orientation="vertical">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin">
<EditText
android:id="#+id/summoner_name"
android:layout_width="210dp"
android:layout_height="wrap_content"
android:hint="Summoner Name"
android:imeOptions="actionSend"
android:lines="1" />
<Spinner
android:id="#+id/regions"
android:layout_width="60dp"
android:layout_height="wrap_content" />
<Button
android:id="#+id/search_button"
android:layout_width="45dp"
android:layout_height="45dp"
android:background="#drawable/searchbuttononclick" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="9dp"
android:orientation="horizontal"
android:paddingLeft="#dimen/activity_horizontal_margin">
<ImageView
android:id="#+id/leagueicon"
android:layout_width="160dp"
android:layout_height="150dp" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="20dp" />
<TextView
android:id="#+id/summoner_name_after_search"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textSize="22dp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/league_tier"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff0060a8"
android:textSize="18dp" />
<TextView
android:id="#+id/league_points"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textSize="18dp" />
</LinearLayout>
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:orientation="horizontal">
<TextView
android:id="#+id/ranked_wins"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ff00b515"
android:textSize="18dp" />
<TextView
android:id="#+id/slash_between_wins_and_losses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#ff817a81"
android:textSize="18dp" />
<TextView
android:id="#+id/ranked_losses"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="5dp"
android:textColor="#ffb5000f"
android:textSize="18dp" />
</LinearLayout>
<TextView
android:id="#+id/what_were_you_last_season"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:textColor="#ff817a81"
android:textSize="18dp" />
</LinearLayout>
</LinearLayout>
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:gravity="center_horizontal"
android:textSize="16dp"
android:id="#+id/normal_title"
android:textStyle="bold"
android:textColor="#ff000000" />
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:layout_weight="1"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:id="#+id/normal_wins"
android:gravity="center_vertical"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_one"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:id="#+id/normal_kills"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_two"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/normal_assists"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:gravity="center_vertical"
android:id="#+id/normal_wins_value"
android:paddingLeft="5dp"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_three"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:id="#+id/normal_kills_value"
android:textSize="16dp"
android:paddingLeft="5dp"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_four"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_assists_value"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_height="120dp"
android:orientation="vertical"
android:layout_weight="1">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="120dp"
android:orientation="horizontal"
android:baselineAligned="false">
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:id="#+id/normal_minion_kills"
android:gravity="center_vertical"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_five"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="14dp"
android:id="#+id/normal_neutral_minion_kills"
android:layout_height="38dp"
android:layout_gravity="center_horizontal"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_six"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/normal_turrets_destroyed"
android:textSize="12dp"
android:layout_height="38dp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="120dp"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="38dp"
android:paddingLeft="5dp"
android:gravity="center_vertical"
android:id="#+id/normal_minion_kills_value"
android:textSize="16dp"
/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_space_seven"
android:layout_height="2dp"/>
<TextView
android:layout_width="match_parent"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:id="#+id/neutral_minion_kills_value"
android:textSize="16dp"
android:layout_height="38dp"
/>
<TextView
android:layout_width="match_parent"
android:layout_height="2dp"
android:id="#+id/normal_space_eight"/>
<TextView
android:layout_width="match_parent"
android:id="#+id/normal_turrets_destroyed_value"
android:gravity="center_vertical"
android:paddingLeft="5dp"
android:textSize="16dp"
android:layout_height="38dp"
/>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
<com.ryanfolz.riotgamesapi.SwagObListView
android:id="#+id/list"
android:layout_width="wrap_content"
android:layout_height="fill_parent" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="20dp"></LinearLayout>
</LinearLayout>
</ScrollView>
</LinearLayout>
As you see, I created a custom ListView called "SwagObListView"
Here is the code for that:
public class SwagObListView extends ListView implements AdapterView.OnItemClickListener {
private List<SwagOb> swagList;
private SwagObClickListener swagClicked;
public SwagObListView(Context context) {
super(context);
}
public SwagObListView(Context context, AttributeSet attrs) {
super(context, attrs);
this.swagList = swagList;
this.swagClicked = swagClicked;
}
#Override
public void onItemClick(AdapterView<?> adapterView, View view, int i, long l) {
if (swagClicked != null) {
swagClicked.onConnectionClicked(swagList.get(i));
}
}
public void setOnSwagObClickListener(SwagObClickListener c){
this.swagClicked = c;
}
public void setSwag(List<SwagOb> swagList) {
this.swagList = swagList;
SwagObAdapter swagDapter = new SwagObAdapter(getContext(), swagList);
setAdapter(swagDapter);
setOnItemClickListener(this);
}
}
As for the SwagOb class, here is the code for that:
public class SwagOb {
private String gameType;
private Drawable championPicturePlayed;
private String kills;
private String deaths;
private Drawable[] summonerSpells;
private String assists;
private String cs;
private String gold;
private boolean won;
private Drawable itemOne, itemTwo, itemThree, itemFour, itemFive, itemSix;
private Drawable[] tempDrawable;
private Drawable[] tempDrawable2;
private String[] tempString;
private SearchPlayerFragment activty;
private CollectUserData data;
public SwagOb(String gameType, Drawable championPicturePlayed, String kills, String deaths, Drawable[] summonerSpells,
String assists, String cs, String gold, boolean won, Drawable itemOne, Drawable itemTwo, Drawable itemThree, Drawable itemFour,
Drawable itemFive, Drawable itemSix, Drawable[] tempDrawable, Drawable[] tempDrawable2, String[] tempString, SearchPlayerFragment activity, CollectUserData data){
this.gameType = gameType;
this.championPicturePlayed = championPicturePlayed;
this.kills = kills;
this.deaths = deaths;
this.summonerSpells = summonerSpells;
this.itemFive = itemFive;
this.itemTwo = itemTwo;
this.itemOne = itemOne;
this.itemThree = itemThree;
this.itemFour = itemFour;
this.itemSix = itemSix;
this.assists = assists;
this.cs = cs;
this.gold = gold;
this.won = won;
this.tempDrawable = tempDrawable;
this.tempDrawable2 = tempDrawable2;
this.tempString = tempString;
this.activty = activity;
this.data = data;
}
(This also has getters and setters but that isn't important.)
Anyone know any ideas as to why this is acting this way?
Android does not allow to add ListView inside ScrollView. Possible workaround for this will be remove scrollview and adding top content to listivew header and bottom content into listview footer.
In your scroll view add this property That will do the trick
android:fillViewport="true"

The ScrollView is scrolled up when the first elements of its LinearLayout become visible

I have a LinearLayout inside a ScrollView. The visibility of the first 3 elements of the LinearLayout is initialilly set to GONE. When I change visibility to VISIBLE, the ScrollView is scrolled up. Is it possible not to scroll it when the first views are become visible?
The code:
<com.mypackage.customWidgets.CustomScrollView
android:id="#+id/scrollView"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:layout_alignParentBottom="true" >
<LinearLayout
android:id="#+id/layout2"
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="vertical"
android:divider="#drawable/table_layout_horizontal_divider"
android:showDividers="middle|beginning" >
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/consult_now_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/consultNow1Row"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/choose_a_specialist_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/chooseSpecialist"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center"
android:src="#drawable/other_services_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/otherServices"
android:layout_marginRight="#dimen/largeSpace"
android:layout_gravity="center_vertical"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:src="#drawable/medical_card_kid_profile"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/medicalCard"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<LinearLayout
android:layout_height="wrap_content"
android:layout_width="fill_parent"
android:orientation="horizontal"
style="#style/kidProfileServicesRowStyle">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:layout_marginRight="#dimen/largeSpace"
android:src="#drawable/more_about_kid"/>
<TextView
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="#string/moreAboutKid"/>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/doctors_arrow_right"
android:layout_gravity="right"/>
</LinearLayout>
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#null"
android:src="#drawable/medical_card_kid_profile"
android:visibility="invisible"
android:layout_margin="#dimen/mediumSpace"/>
</LinearLayout>
</com.mypackage.customWidgets.CustomScrollView>
public class CustomScrollView extends ScrollView
{
public CustomScrollView(Context context, AttributeSet attrs)
{
super(context, attrs);
setHorizontalScrollBarEnabled(false);
setVerticalScrollBarEnabled(false);
}
#Override
public boolean onTouchEvent(MotionEvent ev)
{
return false;
}
#Override
public boolean onInterceptTouchEvent(MotionEvent ev)
{
return false;
}
}
The code which I use to show the elements:
RelativeLayout.LayoutParams scrollViewParams = (RelativeLayout.LayoutParams) scrollView.getLayoutParams();
scrollViewParams.height = scrollView.getHeight();
layout2.getChildAt(2).setVisibility(View.VISIBLE);
layout2.getChildAt(1).setVisibility(View.VISIBLE);
layout2.getChildAt(0).setVisibility(View.VISIBLE);
scrollView.setLayoutParams(scrollViewParams);
Mention focusable attribute for those 3 elements. Like:
android:focusable="false"
otherwise, In customScrollView override below method:
#Override
protected boolean onRequestFocusInDescendants(int direction, Rect previouslyFocusedRect) {
return true;
}

Android, trying to get buttons to be on buttom of screen

I'm trying to make a simple app to show pictures. The images get scaled to fit the screen then there is a next and prev button below the image. I would like the next and prev buttons to be at the button, but they keep getting drawn at the button of the image. Thus they move up and down, depending how big the picture is, each time you display a new image.
I found the following solution here, but it is not working for me:
android:gravity="bottom"
android:layout_alignParentBottom="true"
This is the xml file:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:gravity="bottom"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</LinearLayout>
source code
public class cFeetView extends cBaseView implements OnClickListener {
cFileNames mFileNames;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feet);
// add listeners
View mLeft = findViewById(R.id.butRight);
mLeft.setOnClickListener(this);
// add listeners
View mRight = findViewById(R.id.butLeft);
mRight.setOnClickListener(this);
mFileNames=new cFileNames();
mFileNames.Start();
DrawFeet();
}
public void DrawFeet()
{
int screenHeight;
ImageView picImage = (ImageView) findViewById(R.id.viewimage);// R.id.viewpic);
try {
String FileName = "canon20.png";
FileName=mFileNames.Current();
AssetManager assetManager = getAssets();
InputStream inputStream;
inputStream = assetManager.open(FileName);
Bitmap icon = BitmapFactory.decodeStream(inputStream);
int screenWidth = getWindowManager().getDefaultDisplay().getWidth();
screenHeight = getWindowManager().getDefaultDisplay().getHeight();
int bw = icon.getWidth();
int bh=icon.getHeight();
float t = (float) screenWidth / (float) bw;
int iConHeight=(int)((float)bh*t);
picImage.setImageBitmap(icon);
// scale it
picImage.getLayoutParams().width = screenWidth;
picImage.getLayoutParams().height =iConHeight;
Bitmap scaledIcon = Bitmap.createScaledBitmap(icon, screenWidth, iConHeight, false);
} catch (IOException e) {
}
}
// set the top and buttom margins
public void onClick(View v)
{
Intent i;
switch(v.getId())
{
case R.id.butLeft:
mFileNames.Pre();
DrawFeet();
break;
case R.id.butRight:
mFileNames.Next();
DrawFeet();
break;
}
}
} // end class
Instead of LinearLayout, use RelativeLayout in your xml file.
LinearLayout is used only to arrange them in horizontal or vertical directions.
In order to use alignParentBottom you need to use RelativeLayout. And you dont need the gravity attribute.
My suggestion for your particular scenario is that you you have a RelativeLayout and put all your buttons inside a LinearLayout that is aligned at bottom and the ImageView above it.
Psedo:
<RelativeLayout>
<ImageView />
<LinearLayout
android:layout_below="#ImageView_id"
android:layout_alignParentBottom=true>
</LinearLayout>
</RelativeLayout>
Edited with your xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/backFeetGallery"
android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="#+id/viewimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:src="#drawable/background" />
<LinearLayout
android:id="#+id/linearLayout4"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_below="#id/viewimage"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:orientation="horizontal" >
<Button
android:id="#+id/butLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Left "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butFavrest"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Favrets "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butEmail"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" email "
android:textColor="#ff0000ff" />
<Button
android:id="#+id/butRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_marginTop="12px"
android:text=" Right "
android:textColor="#ff0000ff" />
</LinearLayout>
</RelativeLayout>
You need to have everything in RelativeLayout. For example, look at this sample code which I made for one button which is aligned to bottom center of screen.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:text="Button" />
And here is the complete layout file for your layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >
<Button
android:id="#+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:text="Button" />
<Button
android:id="#+id/button4"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:text="Button" />
<Button
android:id="#+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:text="Button" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toRightOf="#+id/button2"
android:text="Button" />
<Button
android:id="#+id/button5"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_toLeftOf="#+id/button3"
android:text="Button" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="38dp"
android:src="#android:drawable/gallery_thumb" />
This is how it looks,

Categories

Resources