I have a horizontal scroll bar that holds 12 buttons, I want to scroll to a specific button to be the middle of the screen,
Currently I am using
monthScrollView.smoothScrollBy((int) buttons.get(arrayNumber).getX(),0);
Which scrolls the button just off the screen, so how to I find the value of half the screen to subtract?
Try
int[] loc = new int[2];
buttons.get(arrayNumber).getLocationInWindow(loc);
horizontalScrollView.scrollTo(loc[0], 0);
Example Code -
activity_scroll.xml
<?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"
android:background="#android:color/black">
<HorizontalScrollView
android:id="#+id/horizon"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:id="#+id/button_1"
android:layout_width="100dp"
android:layout_height="100dp"
android:text="1"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_2"
android:layout_width="100dp"
android:text="2"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_3"
android:layout_width="100dp"
android:text="3"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_4"
android:layout_width="100dp"
android:text="4"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_5"
android:layout_width="100dp"
android:text="5"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_6"
android:text="6"
android:layout_width="100dp"
android:layout_height="100dp"
android:layout_margin="5dp" />
<Button
android:id="#+id/button_7"
android:layout_width="100dp"
android:text="7"
android:layout_height="100dp"
android:layout_margin="5dp" />
</LinearLayout>
</HorizontalScrollView>
</RelativeLayout>
TestActivity.java
public class TestActivity extends AppCompatActivity implements View.OnClickListener {
View[] views = new View[7];
int res[] = {R.id.button_1, R.id.button_2, R.id.button_3, R.id.button_4, R.id.button_5, R.id.button_6, R.id.button_7};
HorizontalScrollView horizontalScrollView;
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_scroll);
for (int i = 0; i < res.length; i++) {
views[i] = findViewById(res[i]);
views[i].setOnClickListener(this);
}
horizontalScrollView = (HorizontalScrollView) findViewById(R.id.horizon);
}
#Override
public void onClick(View v) {
int[] loc = new int[2];
views[3].getLocationInWindow(loc);
horizontalScrollView.scrollTo(loc[0], 0);
}
}
My working example.
Related
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.
I want to make a simple Sudoku game in which i want to create 3*3 matrix.I created a matrix but i getting problem how to insert some value at specific position in Grid Layout.Please can anyone help me.
I have done up to this so far...
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mScrollView = (ScrollView) findViewById(R.id.scroll_view);
mScrollView.setSmoothScrollingEnabled(true);
mGrid = (GridLayout) findViewById(R.id.grid_layout);
// mGrid.setOnDragListener(new DragListener());
final LayoutInflater inflater = LayoutInflater.from(this);
for (int i = 0; i < NBR_ITEMS; i++) {
final View itemView = inflater.inflate(R.layout.item_grid, mGrid, false);
final TextView text = (TextView) itemView.findViewById(R.id.text);
if ((i == 4)) {
text.setText("3");
}
if ((i == 2)) {
text.setText("2");
}
if ((i == 7)) {
text.setText("1");
}
final int size = mGrid.getChildCount();
System.out.println("size===" + size);
itemView.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
final int index = calculateNewIndex(itemView.getX(), itemView.getY());
System.out.println("index====" + index);
System.out.println("click on cell");
}
});
// itemView.setOnLongClickListener(new LongPressListener());
mGrid.addView(itemView);
}
}
Simply you can create 3*3 matrix using LinearLayout and do wahtever you want like this,
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:weightSum="1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="1"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="2"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="3"
android:gravity="center"
android:textSize="24sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="4"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="5"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="6"
android:gravity="center"
android:textSize="24sp"
/>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight=".33"
android:orientation="horizontal"
android:weightSum="1"
>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="7"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="8"
android:gravity="center"
android:textSize="24sp"
/>
<TextView
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight=".33"
android:text="9"
android:gravity="center"
android:textSize="24sp"
/>
</LinearLayout>
</LinearLayout>
Like This
I have created a recyclerview usin cardview and added button in each of the items of the recyclerview. I have already integrated the button click event . Now, when I click the button of one of the recyclerview item it responds normally and the button of other items responds too at the same time as if they are also clicked.what is the problem? how can it be solved?
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/frame_layout"
android:layout_width="match_parent"
android:layout_height="95dp"
android:orientation="horizontal"
android:weightSum="3">
<android.support.v7.widget.CardView
android:id="#+id/cv"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#android:color/transparent"
app:cardCornerRadius="1dp"
app:cardElevation="5dp"
app:cardUseCompatPadding="true">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="100dp"
android:clickable="true">
<ImageView
android:id="#+id/icon_vendor1"
android:layout_width="60dp"
android:layout_height="60dp"
android:layout_margin="5dp"
android:layout_marginRight="10dp"
android:padding="0dp"
android:scaleType="fitXY"
android:src="#drawable/ic_direction" />
<TextView
android:id="#+id/vendorName1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_marginTop="10dp"
android:layout_toRightOf="#+id/icon_vendor1"
android:text="Kozzaja Infotech Pvt Ltd"
android:textSize="20sp" />
<RatingBar
android:id="#+id/rating"
style="#style/RatingBar"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/vendorName1"
android:layout_marginTop="4dp"
android:layout_toEndOf="#+id/imageView2"
android:layout_toRightOf="#+id/icon_vendor1" />
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="38dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/vendorName1"
android:layout_marginBottom="3dp"
android:layout_marginTop="8dp"
android:orientation="horizontal">
<LinearLayout
android:id="#+id/layout"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:orientation="horizontal">
<ImageButton
android:id="#+id/button4"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="10dp"
android:src="#drawable/ic_call_black_36dp" />
<ImageButton
android:id="#+id/button3"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:background="#drawable/circle_button"
android:padding="20dp"
android:src="#drawable/ic_messenger_outline_black_36dp" />
<ImageButton
android:id="#+id/button2"
android:layout_width="38dp"
android:layout_height="match_parent"
android:background="#drawable/circle_button"
android:src="#drawable/ic_directions_black_24dp" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="38dp"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/layout"
android:background="#drawable/circle_button"
android:tag="off"
android:text="+"
android:textSize="30sp" />
<View
android:layout_width="11dp"
android:layout_height="38dp"
android:background="#ffffff" />
</LinearLayout>
</RelativeLayout>
</android.support.v7.widget.CardView>
This is my adapter's code.
#Override
public void onBindViewHolder(final ViewHolder holder, int position) {
holder.title.setText(workers.get(position).getName());
holder.rating.setRating(workers.get(position).getRatings());
Picasso.with(context).load(workers.get(position).getImageLogo()).fit().into(holder.imageView);
holder.plus.setTag("off");
holder.plus.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
/// button click event
if (holder.plus.getTag().toString().equals("off")) {
holder.plus.setTag("on");
holder.plus.setText("-");
//show the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.left_to_right));
holder.layout.setVisibility(View.VISIBLE);
} else if (holder.plus.getTag().toString().equals("on")) {
holder.plus.setTag("off");
holder.plus.setText("+");
//hide the options button using animation
holder.layout.startAnimation(AnimationUtils.loadAnimation(v.getContext(), R.anim.right_to_left));
final Handler handler = new Handler();
handler.postDelayed(new Runnable() {
#Override
public void run() {
//disappear the button after 500ms
holder.layout.setVisibility(View.GONE);
}
}, 500);
}
}
});
}
i want clickable buttons in zoomview .but my problem is that when i set the view in
zoomview then buttons are not responding while click,i am stuck at this point any help
is apreciated
my code is
public class SongsActivity extends Activity {
ImageView imvw;
DrawView drawView;
ImageView imageView;
View dv;
RelativeLayout rdown;
private static final float MIN_ZOOM = 1f, MAX_ZOOM = 1f;
Matrix matrix = new Matrix();
Matrix savedMatrix = new Matrix();
static final int NONE = 0;
static final int DRAG = 1;
static final int ZOOM = 2;
private static final RelativeLayout New = null;
int mode = NONE;
PointF start = new PointF();
PointF mid = new PointF();
float oldDist = 1f;
RelativeLayout main_container;
private ZoomView zoomView;
Button BtnWelcome;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.songs_layout);
try{
imvw = (ImageView) findViewById(R.id.imageView1);
View v = ((LayoutInflater)
getSystemService(Context.LAYOUT_INFLATER_SERVICE))
.inflate(R.layout.songs_layout, null, false);
BtnWelcome = (Button) findViewById(R.id.button4);
v.setLayoutParams(new RelativeLayout.LayoutParams(
LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
main_container = (RelativeLayout) findViewById(R.id.Map);
zoomView = new ZoomView(this);
zoomView.addView(v);
main_container.addView(zoomView);
//BtnWelcome.bringToFront();
BtnWelcome.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
// TODO Auto-generated method stub
Intent intent = new Intent(SongsActivity.this,
Wavepointsactivity.class);
intent.putExtra("Welcome", "Welcome");
startActivity(intent);
}
});
}catch (Exception e) {
// TODO: handle exception
}
}
}
and my xml is
<?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="vertical" >
<!-- Screen Design for the SONGS -->
<RelativeLayout
android:id="#+id/tabconteffnt"
android:layout_width="match_parent"
android:layout_height="50dp"
android:layout_gravity="top"
android:background="#2E2E2E"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Cedar Breaks-Map"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/white" />
</RelativeLayout>
<RelativeLayout
android:id="#+id/Map"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="top"
android:gravity="center_horizontal" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_marginBottom="70dp"
android:scaleType="fitXY"
android:src="#drawable/map" />
<Button
android:id="#+id/button4"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignParentRight="true"
android:layout_below="#+id/button3"
android:layout_marginRight="37dp" />
<Button
android:id="#+id/button5"
style="?android:attr/buttonStyleSmall"
android:layout_width="40dp"
android:layout_height="60dp"
android:layout_below="#+id/button4"
android:layout_toLeftOf="#+id/button4"
android:text="Button" />
<Button
android:id="#+id/button6"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignLeft="#+id/button5"
android:layout_below="#+id/button5"
android:layout_marginTop="18dp"
android:text="Button" />
<Button
android:id="#+id/button7"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_below="#+id/button6"
android:layout_centerHorizontal="true"
android:layout_marginTop="21dp"
android:text="Button" />
<Button
android:id="#+id/button3"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_below="#+id/button2" />
<Button
android:id="#+id/button11"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignTop="#+id/button9"
android:text="Button" />
<Button
android:id="#+id/button10"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_above="#+id/button9"
android:layout_alignRight="#+id/button11"
android:text="Button" />
<Button
android:id="#+id/button8"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button10"
android:layout_alignBottom="#+id/button10"
android:text="Button" />
<Button
android:id="#+id/button9"
style="?android:attr/buttonStyleSmall"
android:layout_width="50dp"
android:layout_height="50dp"
android:layout_alignBaseline="#+id/button7"
android:layout_alignBottom="#+id/button7"
android:layout_alignLeft="#+id/button8"
android:text="Button" />
<Button
android:id="#+id/button2"
style="?android:attr/buttonStyleSmall"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_alignLeft="#+id/button3" />
<RelativeLayout
android:id="#+id/view1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_alignBottom="#+id/imageView1"
android:layout_alignLeft="#+id/view1" >
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
There is some problem in initialising your id.button1 is not declared in your xml.So it is a null pointer exception.
BtnWelcome = (Button) findViewById(R.id.button1);
Give proper id of the button.It may resolve the error..
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,