I am working on an android project, and for that I have a human model image:
I would like to zoom right in, so that you can only see one human model e.g. the middle one to start with, then have 2 arrows on either side of the image, and click left arrow to see the left human model, and right to see the right human model.
This is my attempt, but doesn't work
layout activity:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:showIn="#layout/activity_home_page"
tools:context="com.example.lahirufernando.skinsensor.HomePage"
android:background="#e0e0e0">
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/textViewBottom"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/peace" />
<ImageView
android:id="#+id/imageView"
android:layout_width="match_parent"
android:layout_height="350dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:background="#drawable/loginbox" />
<Button
android:id="#+id/ibLeft"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#drawable/left_arrow" />
<Button
android:id="#+id/ibRight"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/imageView"
android:layout_alignLeft="#+id/questionBtn"
android:layout_alignStart="#+id/questionBtn"
android:background="#drawable/right_arrow" />
<HorizontalScrollView
android:id="#+id/hsv"
android:layout_width="wrap_content"
android:fadingEdgeLength="100dp"
android:layout_toRightOf="#id/ibLeft"
android:layout_toLeftOf="#id/ibRight"
android:layout_height="wrap_content" >
<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">
<ImageView
android:id="#+id/humanbody"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_centerVertical="true"
android:scaleType="centerCrop"
android:background="#null" />
</LinearLayout>
</HorizontalScrollView>
<TextView
android:id="#+id/textViewBottom"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_alignParentBottom="true"
android:layout_marginBottom="10dp"
android:textColor="#FFFF"
android:text="Tip: You can zoom in and turn your device!"/>
<android.support.design.widget.FloatingActionButton
android:id="#+id/questionBtn"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignTop="#+id/bodyPart"
android:layout_marginEnd="40dp"
android:layout_marginRight="40dp"
android:layout_marginTop="35dp"
android:clickable="true"
app:backgroundTint="#FFFF"
app:fabSize="mini"
app:srcCompat="#drawable/question" />
<TextView
android:id="#+id/bodyPart"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="18dp"
android:text="Click a Body Region"
android:textColor="#000000"
android:textSize="20dp" />
activity:
private final String TAG = getClass().getSimpleName();
HorizontalScrollView hSV;
Button ibLeft,ibRight;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_home_page);
FloatingActionButton homeBtn = (FloatingActionButton) findViewById(R.id.homeButton);
FloatingActionButton questionButton = (FloatingActionButton) findViewById(R.id.questionBtn);
hSV = (HorizontalScrollView) findViewById(R.id.hsv);
ibLeft=(Button) findViewById(R.id.ibLeft);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() - 10, (int)hSV.getScrollY());
}
});
ibRight=(Button) findViewById(R.id.ibRight);
ibLeft.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
hSV.scrollTo((int)hSV.getScrollX() + 10, (int)hSV.getScrollY());
}
});
// Add image
ImageView image = (ImageView) findViewById(R.id.imageView);
image.setImageResource(R.drawable.human);
Related
I have a layout toolbar which I use in every pages using in xml.
There are some buttons in this layout and I want to create a class which takes care of the button clicks of the buttons in toolbar.xml . Is this possible? If yes how?
This is my code of the xml file:
<?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="wrap_content"
android:orientation="vertical">
<RelativeLayout
android:id="#+id/rl"
android:layout_width="match_parent"
android:layout_height="56dp">
<android.support.v7.widget.Toolbar
android:id="#+id/toolbar"
android:layout_width="match_parent"
android:layout_height="110dp"
android:layout_above="#+id/relativeLayout2"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:background="#ffa500"
android:minHeight="?attr/actionBarSize"
android:theme="?attr/actionBarTheme"
android:visibility="visible" />
<EditText
android:id="#+id/editText"
android:layout_width="match_parent"
android:layout_height="35dp"
android:layout_alignBottom="#+id/toolbar"
android:layout_centerHorizontal="true"
android:layout_marginBottom="10dp"
android:background="#drawable/round_edges"
android:ems="10"
android:hint="Search"
android:imeOptions="actionSearch"
android:inputType="textPersonName"
android:paddingLeft="10dp"
android:visibility="invisible"
android:layout_marginLeft="20dp"
android:layout_marginRight="20dp"/>
</RelativeLayout>
<ImageButton
android:id="#+id/button2"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toLeftOf="#+id/button1"
android:layout_toStartOf="#+id/button1"
android:background="#android:color/transparent"
android:src="#mipmap/cart" />
<ImageButton
android:id="#+id/button1"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:src="#mipmap/contact" />
<ImageButton
android:id="#+id/button3"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="8dp"
android:layout_marginRight="8dp"
android:layout_toLeftOf="#+id/button2"
android:layout_toStartOf="#+id/button2"
android:background="#android:color/transparent"
android:src="#mipmap/search" />
<ImageButton
android:id="#+id/button4"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentTop="true"
android:layout_marginEnd="9dp"
android:layout_marginRight="9dp"
android:layout_toLeftOf="#+id/button3"
android:layout_toStartOf="#+id/button3"
android:background="#android:color/transparent"
android:src="#mipmap/phone" />
<ImageButton
android:id="#+id/button0"
android:layout_width="56dp"
android:layout_height="56dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:background="#android:color/transparent"
android:src="#mipmap/ic_launcher" />
</RelativeLayout>
Of course you can, but for every action you must pass the context from the activity/fragment which are calling that action.
Example of class for handling editText and button1 from your layout(other elements you can add yourself)
public class TestClass {
public void getEditText(Context context, View view){
EditText et = (EditText) view.findViewById(R.id.editText);
Toast.makeText(context,"Here I handle editText!",Toast.LENGTH_SHORT).show();
}
public void setImageButton(final Context context, View view){
ImageButton imageButton = (ImageButton) view.findViewById(R.id.button1);
imageButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// do something
Toast.makeText(context,"button1 clicked!",Toast.LENGTH_SHORT).show();
}
});
}
}
You call this methods from your activity like this:
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
TestClass tc = new TestClass();
tc.getEditText(this,findViewById(android.R.id.content));
tc.setImageButton(this,findViewById(android.R.id.content));
}
I been working on a simple android app that calculates a person's Body Mass Index, I have all the features working but positioning the arrow in the right place in the color bar corresponding to the user's screen size is what Im stuck on. I have it working by setting the X and Y values of the arrow ImageView but obviously the place of the arrow changes when i test my application in different screen sizes even though im coverting a dp value to pixels. How can I position the arrow ImageView so that it stays the same in different screen sizes?
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
app:layout_behavior="#string/appbar_scrolling_view_behavior"
tools:context="coding.guillermo.bmiapp.MainActivity2"
tools:showIn="#layout/activity_main2"
android:clickable="false"
android:background="#ffffff"
android:id="#+id/relativeLayout">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI"
android:id="#+id/bmiText"
android:textSize="25dp"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="21.24"
android:id="#+id/bmiResult"
android:textSize="30dp"
android:layout_below="#+id/bmiText"
android:layout_centerHorizontal="true"
android:layout_marginTop="22dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/bmiCategory"
android:textSize="25dp"
android:text="Normal weight"
android:layout_marginTop="22dp"
android:layout_below="#+id/bmiResult"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Save result"
android:id="#+id/saveButton"
android:backgroundTint="#color/toolBarColor"
android:textColor="#ffffff"
android:layout_marginBottom="20dp"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI Log"
android:id="#+id/trackerButton2"
android:backgroundTint="#color/toolBarColor"
android:textColor="#ffffff"
android:layout_alignTop="#+id/saveButton" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imageView"
android:background="#drawable/bmibar"
android:layout_marginTop="36dp"
android:layout_below="#+id/bmiCategory" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Underweight <18.50 "
android:id="#+id/underweightText"
android:textSize="22sp"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="33dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal 18.5 - 24.99"
android:id="#+id/normalText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_below="#+id/underweightText"
android:layout_alignStart="#+id/underweightText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Overweight >=25.00"
android:id="#+id/overweightText"
android:layout_below="#+id/normalText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_alignStart="#+id/normalText" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Obese >=30.00"
android:id="#+id/obeseText"
android:textSize="22sp"
android:paddingTop="5dp"
android:layout_below="#+id/overweightText"
android:layout_alignStart="#+id/overweightText" />
public class MainActivity2 extends AppCompatActivity {
TextView resultText,bmiLabel,underWeightText,normalText,overweightText,obeseText;
RelativeLayout.LayoutParams params;
Button saveButton,trackerButton;
Result result;
EditText userName;
DBhandler dbHandler;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
// TextViews
resultText = (TextView) findViewById(R.id.bmiResult);
bmiLabel = (TextView) findViewById(R.id.bmiCategory);
underWeightText = (TextView) findViewById(R.id.underweightText);
normalText = (TextView) findViewById(R.id.normalText);
overweightText = (TextView) findViewById(R.id.overweightText);
obeseText = (TextView) findViewById(R.id.obeseText);
// Button
saveButton = (Button) findViewById(R.id.saveButton);
trackerButton = (Button) findViewById(R.id.trackerButton2);
// Getting User object from the previous activity
result = (Result) getIntent().getParcelableExtra("result");
// Database
dbHandler = new DBhandler(this);
// Displaying the arrow in the corresponding place
ImageView arrow = new ImageView(this);
params = new RelativeLayout.LayoutParams(80,80);
arrow.setImageResource(R.drawable.arrow2);
RelativeLayout rl = (RelativeLayout) findViewById(R.id.relativeLayout);
// the display of the arrow is different when tested in device's with different screen sizes
int dpValue = 0;
int dpValue2 = 166;
float d = getApplicationContext().getResources().getDisplayMetrics().density;
int margin = (int)(dpValue * d);
int margin2 = (int) (dpValue2 * d);
arrow.setX(margin);
arrow.setY(margin2);
rl.addView(arrow);
// BMI diplay
resultText.setText(Double.toString(result.getBMI()));
bmiLabel.setText(result.getBmiCategory());
// BMI category bold display
bmiCategoryBold(result.getBMI());
// Saving result to internal storage for later retrieval
saveButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
View view = (LayoutInflater.from(MainActivity2.this)).inflate(R.layout.alert_content,null);
AlertDialog.Builder alertBuilder = new AlertDialog.Builder(MainActivity2.this);
alertBuilder.setView(view);
userName = (EditText) view.findViewById(R.id.nameInput);
SimpleDateFormat dateFormat = new SimpleDateFormat("MM/dd/yyyy");
String date = dateFormat.format(new Date());
result.setDate(date);
alertBuilder.setCancelable(true).setPositiveButton("Ok", new DialogInterface.OnClickListener() {
#Override
public void onClick(DialogInterface dialog, int which) {
result.setName(userName.getText().toString());
// adding result to the SQLite database
dbHandler.addResult(result);
Toast toast = Toast.makeText(getApplicationContext(),"result saved",Toast.LENGTH_SHORT);
toast.show();
}
});
AlertDialog dialog = alertBuilder.create();
dialog.show();
Button nButton = dialog.getButton(DialogInterface.BUTTON_POSITIVE);
nButton.setBackgroundColor(getResources().getColor(R.color.toolBarColor));
nButton.setTextColor(Color.WHITE);
}
});
trackerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(getApplicationContext(),MainActivity3.class);
startActivity(intent);
}
});
}
public void bmiCategoryBold(double bmi){
if(bmi < 18.50){
underWeightText.setTypeface(null, Typeface.BOLD);
}
else if(bmi <= 24.99){
normalText.setTypeface(null,Typeface.BOLD);
}
else if(bmi<=29.99){
overweightText.setTypeface(null,Typeface.BOLD);
}
else{
obeseText.setTypeface(null,Typeface.BOLD);
}
}
}
The first pic is the app running on 1080 pixels by 1920 pixels screen and the second is a 1440 pixels by 2560 pixels screen
first pic
second pic
Add Linearlayout as subparent of childview,use its orientation and gravity attribute you can easily get the design in more optimize way and suitable for everyscreen size.
here i have used RelativeLayout as Parent and LinearLayout as sub-parent of childview.
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="10dp">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical">
<TextView
android:id="#+id/bmiText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="BMI"
android:textSize="25dp" />
<TextView
android:id="#+id/bmiResult"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="5dp"
android:text="21.24"
android:textSize="30dp" />
<TextView
android:id="#+id/bmiCategory"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Normal weight"
android:textSize="25dp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#mipmap/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:orientation="vertical"
android:padding="5dp">
<TextView
android:id="#+id/underweightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Underweight <18.50 "
android:textSize="22sp" />
<TextView
android:id="#+id/normalText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Normal 18.5 - 24.99"
android:textSize="22sp" />
<TextView
android:id="#+id/overweightText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:padding="10dp"
android:text="Overweight >=25.00"
android:textSize="22sp" />
<TextView
android:id="#+id/obeseText"
android:layout_width="wrap_content"
android:padding="10dp"
android:layout_height="wrap_content"
android:text="Obese >=30.00"
android:textSize="22sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:orientation="horizontal"
android:layout_height="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:gravity="left"
android:layout_height="wrap_content">
<Button
android:id="#+id/trackerButton2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:backgroundTint="#000000"
android:text="BMI Log"
android:gravity="center"
android:textColor="#ffffff" />
<LinearLayout
android:layout_width="match_parent"
android:gravity="right"
android:layout_height="wrap_content">
<Button
android:id="#+id/saveButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:backgroundTint="#000000"
android:text="Save result"
android:textColor="#ffffff" />
</LinearLayout>
</LinearLayout>
</LinearLayout>
</LinearLayout>
</RelativeLayout>
Try to avoid too much margin top and bottom.
I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout
I have an activity with three fragments placed in a ViewPager. On the first two fragments I have a list that drops down, kind of like an animated ExpandableListView but it is a simple RelativeLayout containing a TextView and ImageView, when pressed slides down a LinearLayout containing some elements. On clicking the RelativeLayout the followig actions are taken:
change the background color of the clicked RelativeLayout (works every time for every layout in every fragment)
change the color of the text in TextView (works every time for the first element I'll explain below)
animate the image, an arrow to change color and rotate (also works but for the first element)
The problem is as follows:
the first fragment contains only one list (a RelativeLayout that shows or hides a LinearLayout) and it works fine the background, text color and image change and the image rotates and the Linearlayout slides up and down correspondingly. The second fragment contains 4 such groupings (RelativeLayout controlling LinearLayout) and on the first time this fragment is shown only the first grouping seems to work. The other three change background and slide the Linearlayout but as far as i can tell it keeps the same color and the same image (there are two colors a white and an orange that change and since the background changes from orange to white i assume the text stays white instead of changing to orange, same goes for the image).
The punchline:
when I leave it for a few seconds and click it again it works fine, everything changes accordingly, and if i for instance flip to the third fragment and back to the second it works. On the other hand if I start the app, got to the second fragment, back to the first, and to the second again it doesn't work.
I don't know if this has something to do with the GC and recycling views or something else.
XML:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/nutritionList"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical" >
<RelativeLayout
android:id="#+id/aboutWetFood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="false"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/wetSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView04"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/wetLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/my_yellow"
android:clickable="true"
android:onClick="openNutrition"
android:tag="wet" >
<TextView
android:id="#+id/wetTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="#string/wet_food"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
<ImageView
android:id="#+id/wetArrow"
android:layout_width="12dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:src="#drawable/arrow_right_white" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/aboutDryFood"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="false"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/drySub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView06"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView05"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/dryLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/my_yellow"
android:clickable="true"
android:onClick="openNutrition"
android:tag="dry" >
<TextView
android:id="#+id/dryTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="#string/dry_food"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
<ImageView
android:id="#+id/dryArrow"
android:layout_width="12dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:src="#drawable/arrow_right_white" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/whyMixedFeeding"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="false"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/mixedSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView08"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView07"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/mixedLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/my_yellow"
android:clickable="true"
android:onClick="openNutrition"
android:tag="mixed" >
<TextView
android:id="#+id/mixedTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="#string/mixed_feeding"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
<ImageView
android:id="#+id/mixedArrow"
android:layout_width="12dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:src="#drawable/arrow_right_white" />
</RelativeLayout>
</RelativeLayout>
<RelativeLayout
android:id="#+id/nutritionOralCare"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginBottom="1dp"
android:clickable="false"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/oralSub"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="60dp"
android:orientation="vertical"
android:visibility="gone" >
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView09"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="60dp"
android:layout_marginBottom="1dp"
android:background="#EDEDED" >
<TextView
android:id="#+id/TextView10"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="SUB MENU 1"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/my_yellow" />
</RelativeLayout>
</LinearLayout>
<RelativeLayout
android:id="#+id/oralLayout"
android:layout_width="match_parent"
android:layout_height="60dp"
android:background="#color/my_yellow"
android:clickable="true"
android:onClick="openNutrition"
android:tag="oral" >
<TextView
android:id="#+id/oralTitle"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="30dp"
android:text="#string/oral_care_caps"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#color/white" />
<ImageView
android:id="#+id/oralArrow"
android:layout_width="12dp"
android:layout_height="20dp"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginRight="30dp"
android:src="#drawable/arrow_right_white" />
</RelativeLayout>
</RelativeLayout>
</LinearLayout>
</ScrollView>
Methods for changing and animating the views:
public void openNutrition(View view) {
String viewTag = view.getTag().toString();
if (viewTag.equals("wet")) {
nutrition = (RelativeLayout) findViewById(R.id.wetLayout);
nutritionSub = (LinearLayout) findViewById(R.id.wetSub);
nutritionTitle = (TextView) findViewById(R.id.wetTitle);
nutritionArrow = (ImageView) findViewById(R.id.wetArrow);
animate();
}
else if (viewTag.equals("dry")) {
nutrition = (RelativeLayout) findViewById(R.id.dryLayout);
nutritionSub = (LinearLayout) findViewById(R.id.drySub);
nutritionTitle = (TextView) findViewById(R.id.dryTitle);
nutritionArrow = (ImageView) findViewById(R.id.dryArrow);
animate();
}
else if (viewTag.equals("mixed")) {
nutrition = (RelativeLayout) findViewById(R.id.mixedLayout);
nutritionSub = (LinearLayout) findViewById(R.id.mixedSub);
nutritionTitle = (TextView) findViewById(R.id.mixedTitle);
nutritionArrow = (ImageView) findViewById(R.id.mixedArrow);
animate();
}
else if (viewTag.equals("oral")) {
nutrition = (RelativeLayout) findViewById(R.id.oralLayout);
nutritionSub = (LinearLayout) findViewById(R.id.oralSub);
nutritionTitle = (TextView) findViewById(R.id.oralTitle);
nutritionArrow = (ImageView) findViewById(R.id.oralArrow);
animate();
}
else if (viewTag.equals("changeMix")) {
nutrition = (RelativeLayout) findViewById(R.id.changeMixLayout);
nutritionSub = (LinearLayout) findViewById(R.id.foodTypeLayout);
nutritionTitle = (TextView) findViewById(R.id.changeMixText);
nutritionArrow = (ImageView) findViewById(R.id.changeMixArrow);
animate();
}
}
private void animate() {
if (nutritionSub.getVisibility() == View.GONE) {
nutritionSub.setVisibility(View.VISIBLE);
nutrition.setBackgroundColor(getResources().getColor(R.color.white));
nutritionTitle.setTextColor(getResources().getColor(
R.color.my_yellow));
nutritionArrow.setImageDrawable(getResources().getDrawable(
R.drawable.arrow_right_orange));
r = new RotateAnimation(currentPos, degrees,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* Setting arrow animation properties */
setArrowAnimation();
Animation slideDown = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_down);
nutritionArrow.startAnimation(r);
nutritionSub.startAnimation(slideDown);
} else {
nutrition.setBackgroundColor(getResources().getColor(
R.color.my_yellow));
nutritionTitle.setTextColor(getResources().getColor(R.color.white));
nutritionArrow.setImageDrawable(getResources().getDrawable(
R.drawable.arrow_right_white));
r = new RotateAnimation(degrees, currentPos,
Animation.RELATIVE_TO_SELF, 0.5f,
Animation.RELATIVE_TO_SELF, 0.5f);
/* Setting arrow animation properties */
setArrowAnimation();
Animation slideUp = AnimationUtils.loadAnimation(
getApplicationContext(), R.anim.slide_up);
slideUp.setAnimationListener(new AnimationListener() {
#Override
public void onAnimationStart(Animation animation) {
}
#Override
public void onAnimationEnd(Animation animation) {
nutritionSub.setVisibility(View.GONE);
}
#Override
public void onAnimationRepeat(Animation animation) {
}
});
nutritionArrow.startAnimation(r);
nutritionSub.startAnimation(slideUp);
}
}
private void setArrowAnimation() {
r.setInterpolator(new LinearInterpolator());
r.setDuration(500);
r.setFillEnabled(true);
r.setFillAfter(true);
}
nutrition,nutritionSub,nutritionTitle,nutritionArrow are declared as global private variables
Update
Running a log on my fragment class that extends Fragment and on OnPageChangeListener's method onPageSelected() shows that tags are set properly and visible. The issue seems to be related to the lifecycle of fragments since the changes to the text and image only occur when I flip through ALL three fragments and then return to the second.
Setting the pager:
List<Fragment> fragments = getFragments();
pageAdapter = new NutritionPageAdapter(getSupportFragmentManager(),
fragments);
ViewPager pager = (ViewPager) findViewById(R.id.viewpager);
pager.setAdapter(pageAdapter);
getFragments()
private List<Fragment> getFragments() {
List<Fragment> fList = new ArrayList<Fragment>();
fList.add(NutritionFragment.newInstance("Menu"));
fList.add(NutritionFragment.newInstance("Nutrition"));
fList.add(NutritionFragment.newInstance("Transition"));
return fList;
}
My Fragment class
public static final String FRAGMENT_TITLE = "FRAGMENT_TITLE";
private View v;
public static final NutritionFragment newInstance(String string) {
NutritionFragment f = new NutritionFragment();
Bundle b = new Bundle(1);
b.putString(FRAGMENT_TITLE, string);
f.setArguments(b);
return f;
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
String screen = getArguments().getString(FRAGMENT_TITLE);
if (screen.equals("Menu")) {
v = inflater.inflate(R.layout.nutrition_menu_activity_layout,
container, false);
} else if (screen.equals("Transition")) {
v = inflater.inflate(R.layout.nutrition_transition_activity_layout,
container, false);
} else {
v = inflater.inflate(R.layout.nutrition_nutrition_activity_layout,
container, false);
}
return v;
}
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,