There is a page in my android application which has a slide out menu and some data that needs to be displayed in the main screen. The slide out menu has the required menu options.
the main page is in ListView format. I want a TableLayout format layout inside this ListView.
The problem is when i change the layout to TableLayout, it messes up with my java activity file.
I want to display data extracted from an external source on the main screen of this page in table format.
The XML and Java Activity files are enclosed.
Thanks in advance.
XML Code
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#drawable/galaxy">
<!-- Menu Panel -->
<RelativeLayout
android:id="#+id/menuPanel"
android:layout_height="match_parent"
android:layout_width="wrap_content"
android:gravity="right"
android:background="#drawable/grey"
android:orientation="vertical" >
<TextView
android:id="#+id/menu_title_1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentTop="true"
android:layout_marginLeft="0dp"
android:paddingLeft="15dp"
android:gravity="center_vertical"
android:background="#drawable/grey"
android:textStyle="bold"
android:textAppearance="?android:attr/textAppearanceMedium"
android:textColor="#android:color/black"
android:text="MAIN MENU">
</TextView>
<View
android:id="#+id/menu_item_divider_1"
android:layout_width="fill_parent"
android:layout_height="0.5dp"
android:layout_marginLeft="0dp"
android:layout_marginRight="0dp"
android:layout_below="#+id/menu_title_1"
android:background="#000000"/>
<Button
android:id="#+id/readall"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/menu_item_divider_1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="All Parameters"/>
<Button
android:id="#+id/param1"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/readall"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 1"/>
<Button
android:id="#+id/param2"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param1"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 2"/>
<Button
android:id="#+id/param3"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="15dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param2"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Parameter 3"/>
<Button
android:id="#+id/dtc"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/param3"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="D.T.C."/>
<Button
android:id="#+id/logout"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_marginLeft="0dp"
android:gravity="center_vertical"
android:textColor="#000000"
android:layout_below="#+id/dtc"
android:textAppearance="?android:attr/textAppearanceSmall"
android:text="Log Out"/>
</RelativeLayout>
<!-- Sliding Panel -->
<LinearLayout
android:id="#+id/slidingPanel"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="left"
android:orientation="vertical"
android:background="#drawable/grey" >
<RelativeLayout
android:id="#+id/header"
android:layout_width="match_parent"
android:layout_height="50dp"
android:background="#drawable/black" >
<View
android:id="#+id/header_vertical_divider_1"
android:layout_width="2dp"
android:layout_height="fill_parent"
android:layout_alignParentTop="true"
android:layout_marginLeft="15dp"
android:layout_toRightOf="#+id/menuViewButton"
android:background="#drawable/grey" />
<ImageView
android:id="#+id/menuViewButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:layout_marginLeft="15dp"
android:clickable="true"
android:contentDescription="Main Menu"
android:textColor="#000000"
android:src="#drawable/icon"
android:visibility="visible" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_marginLeft="22dp"
android:layout_toRightOf="#+id/header_vertical_divider_1"
android:text="ECU Description"
android:textAppearance="?android:attr/textAppearanceLarge"
android:textColor="#ffffff" />
</RelativeLayout>
<View
android:id="#+id/dividerHeaderBottom"
android:layout_width="fill_parent"
android:layout_height="1dp"
android:background="#414141" ></View>
<ListView
android:id="#+id/list"
android:layout_width="match_parent"
android:layout_height="fill_parent"
android:divider="#000000"
android:dividerHeight="0.5dp"
android:background="#drawable/galaxy" >
</ListView>
</LinearLayout>
<LinearLayout
android:id="#+id/footer"
style="#android:style/ButtonBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
android:layout_alignParentBottom="true"
android:layout_gravity="bottom|center"
android:background="#drawable/black"
android:orientation="horizontal" >
<EditText
android:id="#+id/DefUid"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_weight="0.50"
android:digits="0123456789."
android:hint="IP Address"
android:maxLength="15"
android:maxWidth="350dip"
android:singleLine="true"
android:windowSoftInputMode="adjustNothing" >
</EditText>
<EditText
android:id="#+id/DefUid"
android:layout_width="0px"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_below="#+id/textView2"
android:layout_weight="0.20"
android:maxWidth="80dip"
android:hint="Port"
android:digits="0123456789"
android:maxLength="4"
android:singleLine="true" >
</EditText>
<Button
android:id="#+id/conn"
android:layout_width="0px"
android:layout_height="40dp"
android:layout_weight="0.15"
android:text="C"/>
<Button
android:id="#+id/disc"
android:layout_width="0px"
android:layout_height="40dp"
android:layout_weight="0.15"
android:text="D"/>
</LinearLayout>
</FrameLayout>
Java Activity File
public class LayerStack extends Activity
{
//Declare
private LinearLayout slidingPanel;
private boolean isExpanded;
private DisplayMetrics metrics;
private ListView listView;
private RelativeLayout headerPanel;
private RelativeLayout menuPanel;
private int panelWidth;
private ImageView menuViewButton;
private static final int HIDE_TOOLBARS = 0;
private static final int SHOW_TOOLBARS = 1;
private static final int TOGGLE_TOOLBARS = 2;
Handler handler;
LinearLayout toolbarTop, toolbarBottom;
FrameLayout.LayoutParams menuPanelParameters;
FrameLayout.LayoutParams slidingPanelParameters;
LinearLayout.LayoutParams headerPanelParameters ;
LinearLayout.LayoutParams listViewParameters;
//#SuppressWarnings("deprecation")
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.page7);
//ImageView myImage = (ImageView) findViewById(R.id.menuPanel);
//myImage.setAlpha(100);
// Get references to the elements in the layout
//Initialize
metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
panelWidth = (int) ((metrics.widthPixels)*0.50);
headerPanel = (RelativeLayout) findViewById(R.id.header);
headerPanelParameters = (LinearLayout.LayoutParams) headerPanel.getLayoutParams();
headerPanelParameters.width = metrics.widthPixels;
headerPanel.setLayoutParams(headerPanelParameters);
menuPanel = (RelativeLayout) findViewById(R.id.menuPanel);
menuPanelParameters = (FrameLayout.LayoutParams) menuPanel.getLayoutParams();
menuPanelParameters.width = panelWidth;
menuPanel.setLayoutParams(menuPanelParameters);
slidingPanel = (LinearLayout) findViewById(R.id.slidingPanel);
slidingPanelParameters = (FrameLayout.LayoutParams) slidingPanel.getLayoutParams();
slidingPanelParameters.width = metrics.widthPixels;
slidingPanel.setLayoutParams(slidingPanelParameters);
listView = (ListView) findViewById(R.id.list);
listViewParameters = (LinearLayout.LayoutParams) listView.getLayoutParams();
listViewParameters.width = metrics.widthPixels;
listView.setLayoutParams(listViewParameters);
//Slide the Panel
menuViewButton = (ImageView) findViewById(R.id.menuViewButton);
menuViewButton.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
if(!isExpanded)
{
isExpanded = true;
//Expand
new ExpandAnimation(slidingPanel, panelWidth,
Animation.RELATIVE_TO_SELF, 0.0f,
Animation.RELATIVE_TO_SELF, 0.50f, 0, 0.0f, 0, 0.0f);
Button b2 = (Button) findViewById(R.id.readall);
b2.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
Intent i = new Intent(LayerStack.this,AppActivity8.class);
startActivity(i);
finish();
}
});
Button b1 = (Button) findViewById(R.id.logout);
b1.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
new AlertDialog.Builder(LayerStack.this)
.setTitle("Log Out Confirmation")
.setMessage("Are you sure you want to log out?")
.setNegativeButton("No", null)
.setPositiveButton("Yes", new DialogInterface.OnClickListener()
{
#Override
public void onClick(DialogInterface arg0,int arg1)
{
Intent i = new Intent(LayerStack.this,AppActivity4.class);
startActivity(i);
finish();
}
}).create().show();
}
});
}
else
{
isExpanded = false;
//Collapse
new CollapseAnimation(slidingPanel,panelWidth,
TranslateAnimation.RELATIVE_TO_SELF, 0.50f,
TranslateAnimation.RELATIVE_TO_SELF, 0.0f, 0, 0.0f, 0, 0.0f);
}
}
});
}
}
I Used MultiColumn ListView Instead.
Related
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 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 wanted to display the full screen of my bitmap image(selected from gallery or captured image) when the imageView is clicked.
imageView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(zoomOut) {
Toast.makeText(getApplicationContext(), "NORMAL SIZE!", Toast.LENGTH_LONG).show();
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT));
imageView.setAdjustViewBounds(true);
zoomOut =false;
}else{
Toast.makeText(getApplicationContext(), "FULLSCREEN!", Toast.LENGTH_LONG).show();
imageView.setLayoutParams(new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
zoomOut = true;
}
}
});
And this is my xml(Activity B)
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:fillViewport="true"
android:orientation="vertical"
xmlns:android="http://schemas.android.com/apk/res/android">
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#mipmap/dark_blue"
android:weightSum="1">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter Claims Information"
android:id="#+id/textViewClaims"
android:paddingTop="30sp"
android:textSize="20dp"
android:paddingLeft="15sp"
android:textColor="#color/white"
android:layout_weight="0.05" />
<View
android:layout_marginLeft="10dp"
android:layout_width="340sp"
android:layout_height="2dp"
android:background="#c0c0c0"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Claims Type : "
android:paddingTop="20sp"
android:paddingLeft="15sp"
android:textSize="17dp"
android:textColor="#color/white"
android:id="#+id/textViewClaimsType"
android:layout_weight="0.05" />
<Spinner
android:layout_width="340dp"
android:layout_marginLeft="10dp"
android:background="#color/light_gray"
android:layout_height="48dp"
android:id="#+id/spinner1"
android:spinnerMode="dropdown" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Claims Amount :"
android:paddingTop="20sp"
android:paddingLeft="15sp"
android:textSize="17dp"
android:textColor="#color/white"
android:id="#+id/textViewAmount"
android:layout_weight="0.05" />
<EditText
android:layout_width="220dp"
android:layout_height="wrap_content"
android:layout_margin="10sp"
android:background="#drawable/round_corner_square"
android:id="#+id/editTextAmount"
android:hint=" RM "
android:paddingLeft="20dp"
android:layout_weight="0.09" />
<ImageView
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"
android:layout_marginTop="20dp"
android:layout_width="350dp"
android:src="#mipmap/no_image"
android:layout_height="300dp"
android:id="#+id/imageView" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Add picture"
android:id="#+id/buttonAdd"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textAppearance="?android:attr/textAppearanceLarge"
android:text="Enter Description : "
android:paddingTop="30sp"
android:paddingLeft="15sp"
android:textSize="17dp"
android:textColor="#color/white"
android:id="#+id/textView12"
android:layout_weight="0.05" />
<EditText
android:layout_width="340dp"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:background="#drawable/round_corner_square"
android:layout_weight="0.12"
android:paddingLeft="20dp"
android:paddingBottom="80dp"
android:layout_x="14dp"
android:layout_y="146dp"
android:id="#+id/textDescription" />
<Button
android:layout_width="159dp"
android:layout_height="wrap_content"
android:text="save"
android:layout_marginTop="20dp"
android:id="#+id/buttonSave"
android:layout_gravity="center_horizontal"
android:layout_weight="0.05" />
</LinearLayout>
</ScrollView>
Activity B
Before click
After click
Click again to zoom out
edit your code with following
TextView mTextViewClaims = (TextView) findViewById(R.id.textViewClaims);
TextView mTextViewClaimsType = (TextView) findViewById(R.id.textViewClaimsType);
TextView mTextViewAmount = (TextView) findViewById(R.id.textViewAmount);
TextView mTextView12 = (TextView) findViewById(R.id.textView12);
EditText mEditTextAmount = (EditText) findViewById(R.id.editTextAmount);
EditText mTextDescription = (EditText) findViewById(R.id.textDescription);
Button mButtonAdd = (Button) findViewById(R.id.buttonAdd);
Button mButtonSave = (Button) findViewById(R.id.buttonSave);
imageView .setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if(zoomOut) {
Toast.makeText(getApplicationContext(), "NORMAL SIZE!", Toast.LENGTH_LONG).show();
float scale = getContext().getResources().getDisplayMetrics().density;
imageView.setLayoutParams(new LinearLayout.LayoutParams((int)(350 * scale + 0.5f) , (int)(300 * scale + 0.5f)));
imageView.setAdjustViewBounds(true);
mTextViewClaims.setVisibility(View.VISIBLE);
mTextViewClaimsType.setVisibility(View.VISIBLE);
mTextViewAmount.setVisibility(View.VISIBLE);
mTextView12.setVisibility(View.VISIBLE);
mEditTextAmount.setVisibility(View.VISIBLE);
mTextDescription.setVisibility(View.VISIBLE);
mButtonAdd.setVisibility(View.VISIBLE);
mButtonSave.setVisibility(View.VISIBLE);
zoomOut =false;
}else{
Toast.makeText(getApplicationContext(), "FULLSCREEN!", Toast.LENGTH_LONG).show();
float scaleHeigth = getApplicationContext().getResources().getDisplayMetrics().heightPixels;
float scaleWidth = getApplicationContext().getResources().getDisplayMetrics().widthPixels;
imageView.setLayoutParams(new LinearLayout.LayoutParams((int)scaleWidth , (int)scaleHeigth));
imageView.setScaleType(ImageView.ScaleType.FIT_XY);
mTextViewClaims.setVisibility(View.GONE);
mTextViewClaimsType.setVisibility(View.GONE);
mTextViewAmount.setVisibility(View.GONE);
mTextView12.setVisibility(View.GONE);
mEditTextAmount.setVisibility(View.GONE);
mTextDescription.setVisibility(View.GONE);
mButtonAdd.setVisibility(View.GONE);
mButtonSave.setVisibility(View.GONE);
zoomOut = true;
}
}
});
I have several Buttons inside a linear layout
One of the buttons (sett) needs to be square however the height changes depending on the screen size and resolution
How to i make the width the same as the height?
the button in question is "#+id/sett"
My code is below
activity_activity1.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/background2"
android:background="#EEEDEB"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/sodexotop" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginLeft="15dp"
android:layout_marginRight="15dp"
android:background="#ffffff"
android:gravity="center_horizontal"
android:orientation="vertical" >
<Button
android:id="#+id/Button01"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_fault"
android:text="Enter Fault"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="#drawable/butt1"
android:textSize="30sp" />
<Button
android:id="#+id/Button03"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_send"
android:text="Send Data"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="#drawable/butt1"
android:textSize="30sp" />
<Button
android:id="#+id/Button04"
android:layout_width="match_parent"
android:layout_marginLeft="50sp"
android:layout_marginRight="50sp"
android:onClick="clk_clear"
android:text="Clear Data"
android:textColor="#000000"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="#drawable/butt1"
android:textSize="30sp" />
<Button
android:id="#+id/sett"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="10sp"
android:paddingTop="10sp"
android:paddingRight="10sp"
android:paddingBottom="10sp"
android:layout_marginTop="10sp"
android:background="#drawable/butt1"
android:onClick="clk_sett"
android:text="S"
android:textColor="#000000"
android:textSize="50sp" />
<Button
android:id="#+id/exitbtn2"
android:text="O"
android:textColor="#000000"
android:textSize="30sp"
android:layout_width="60sp"
android:layout_height="0dp"
android:layout_weight="1"
android:layout_marginTop="10sp"
android:background="#drawable/butt1"
android:onClick="clk_exit" />
<TextView
android:id="#+id/label_xxx3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#000000"
android:text=""
android:layout_marginTop="10sp"
android:textSize="10sp" />
</LinearLayout>
<ImageView
android:id="#+id/imageView2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:adjustViewBounds="true"
android:src="#drawable/sodexobottom" />
</LinearLayout>
Activity1.java
public class Activity1 extends Activity {
String BACK="";
public static Drawable myDrawable;
/** Called when the activity is first created. */
DBAdapter db;
final int ACTIVITY_CHOOSE_FILE = 1;
#Override
public void onStart()
{
// RUN SUPER | REGISTER ACTIVITY AS INSTANTIATED IN APP CLASS
super.onStart();
App.Activity1 = this;
}
#Override
public void onCreate(Bundle savedInstanceState) {
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
getWindow().setFormat(PixelFormat.RGBA_8888);
getWindow().addFlags(WindowManager.LayoutParams.FLAG_DITHER);
setContentView(R.layout.activity_activity1);
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int h = metrics.heightPixels;
int w = metrics.widthPixels;
int w1 = (w/2);
String testStringw = Integer.toString(w);
String testStringh = Integer.toString(h);
Button txt1 = (Button) findViewById(R.id.Button01);
Typeface font1 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt1.setTypeface(font1);
Button txt2 = (Button) findViewById(R.id.Button03);
Typeface font2 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt2.setTypeface(font2);
Button txt3 = (Button) findViewById(R.id.Button04);
Typeface font3 = Typeface.createFromAsset(getAssets(), "fonts/DistProTh.otf");
txt3.setTypeface(font3);
Button txt4 = (Button) findViewById(R.id.sett);
Typeface font4 = Typeface.createFromAsset(getAssets(), "fonts/WebSymbols-Regular.otf");
txt4.setTypeface(font4);
txt4.setLayoutParams(new LayoutParams(60, 60));
Button txt5 = (Button) findViewById(R.id.exitbtn2);
Typeface font5 = Typeface.createFromAsset(getAssets(), "fonts/heydings_controls.ttf");
txt5.setTypeface(font5);
}
Any help is appreciated
Mark
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;
}