Image does not display in full screen when ImageView is clicked - android

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;
}
}
});

Related

Dynamic list not showing all elements in a linear layout

I have a horizontal linear layout that is filled with TextViews from a dynamic list, but it doesn't show all elements since it seems to be out of the parent's bounds. What I need is to fill the space below if needed.
Here's my code:
The xml:
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<ImageView
android:layout_width="150dp"
android:layout_height="150dp"
android:id="#+id/imageView_serie" android:contentDescription="imatge_serie"/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Nom_serie"
android:textSize="20dp"
android:layout_marginLeft="8dp"
android:id="#+id/nom_serie" android:contentDescription="nom_serie" tools:text="Nom_serie"
android:layout_toRightOf="#id/imageView_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize = "16dp"
android:layout_marginLeft="8dp"
android:textStyle="bold"
android:id="#+id/any_serie" android:contentDescription="any_serie" tools:text="any_serie"
android:layout_toRightOf="#id/nom_serie"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Sinopsis"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/sinopsi_serie" android:contentDescription="sinopsis_serie" tools:text="sinopsis_serie"
android:layout_toRightOf="#id/imageView_serie"
android:layout_below = "#id/nom_serie"
/>
<RatingBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="26dp"
android:id="#+id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
android:layout_centerVertical="true"
style="?android:attr/ratingBarStyleSmall"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Puntuacio"
android:textSize="12dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "8dp"
android:id="#+id/puntuacio" android:contentDescription="puntuacio" tools:text="puntuacio"
android:layout_toRightOf="#id/ratingbar_serie"
android:layout_below = "#id/imageView_serie"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Casting:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/casting" android:contentDescription="casting" tools:text="casting"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/cast" android:contentDescription="cast" tools:text="cast"
android:layout_toRightOf="#id/casting"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="65dp"
android:layout_height="wrap_content"
android:text="Direccio:"
android:textSize="14dp"
android:layout_marginLeft="8dp"
android:layout_marginTop = "24dp"
android:id="#+id/direccio" android:contentDescription="direccio" tools:text="direccio"
/>
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="10dp"
android:layout_marginLeft="8dp"
android:id="#+id/dir" android:contentDescription="dir" tools:text="dir"
android:layout_toRightOf="#id/direccio"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Temporades:"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/nTemporades" android:contentDescription="nTemporades" tools:text="nTemporades"
/>
</RelativeLayout>
<RelativeLayout android:layout_width="wrap_content" android:layout_height="wrap_content">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Comentaris"
android:textSize="24dp"
android:textStyle="bold"
android:id="#+id/titol_comentaris_serie" android:contentDescription="titol_comentari_serie" tools:text="titol_comentari_serie"
/>
</RelativeLayout>
<LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:id="#+id/panell"
android:orientation="horizontal">
</LinearLayout>
And this how I generate the text views from the list:
LinearLayout panell = (LinearLayout) findViewById(R.id.panell);
for (int i = 1; i <= 20; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT, LinearLayout.LayoutParams.WRAP_CONTENT);
TextView btn = new TextView(this);
btn.setId(i);
final int id_ = btn.getId();
btn.setText("button " + id_);
//btn.setBackgroundColor(Color.rgb(70, 80, 90));
panell.addView(btn, params);
btn = ((TextView) findViewById(id_));
btn.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Toast.makeText(view.getContext(),
"Button clicked index = " + id_, Toast.LENGTH_SHORT)
.show();
}
});
}
Here's my :
output
I recommend Google's flexbox-layout project: https://github.com/google/flexbox-layout
A FlexboxLayout (when configured correctly) has the ability to add any number of child TextViews in a line (like LinearLayout would), but will automatically "wrap" to the next line if a child wouldn't fit.
Why not try to use a ListView , if u add the same propreties every time

How to place check or tick mark in dialog if particular item (exists in horizontal layout) is selected

I am trying to make custom dialog in which i want to make selection of textview and image visible if user is going to selection .
in below code i have given three text views and four image view, want that if user is going to select text view or image view it should be text color change and on image it should be tick mark should be appear.
like this attached image .
attached image
As per attached image i want to make selection if user select image it should appear as a tick mark and if user selects any text views then tick should below of the text views and color also changed.
MainActivity
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
TextView tvMsg11 =(TextView) dialog.findViewById(R.id.tvMsg11);
TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
}
}
cust.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:background="#drawable/custom_button_background"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>
<!-- <LinearLayout
android:id="#+id/linearImag2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/linearImage1"
android:weightSum="2">
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
</LinearLayout>-->
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearImage1"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>
After apply above code
I am getting like this image
this image
We can do it easily as per attached image
Made visible when need of Highlighter under text views or adjacent of image.
or Made invisible when not need or pressed That's it .
Wrote code for same which is working .
text color change on the basis of text selection :
text_color.xml
<?xml version="1.0" encoding="utf-8"?>
<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:state_pressed="true" android:color="#00b200"></item>
<item android:state_focused="true" android:color="#00b200"/>
<item android:state_selected="true" android:color="#000000"/>
<item android:color="#d3d3d3"></item>
</selector>
For Dialog :-
final Dialog dialog = new Dialog(this);
dialog.setContentView(R.layout.cust);
dialog.setTitle("Your title");
dialog.setCancelable(false);
dialog.show();
TextView d_btn_ok = (TextView) dialog.findViewById(R.id.btnOk);
TextView d_btn_cancel = (TextView) dialog.findViewById(R.id.btnCancel);
d_btn_ok.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dialog.dismiss();
}
});
d_btn_cancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
dialog.dismiss();
}
});
final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
final TextView tvMsg1111=(TextView) dialog.findViewById(R.id.tvMsg1111);
final TextView tvMsg11 = (TextView) dialog.findViewById(R.id.tvMsg11);
ImageView tick5=(ImageView) dialog.findViewById(R.id.tick5);
ImageView tick6=(ImageView) dialog.findViewById(R.id.tick6);
ImageView tick7=(ImageView) dialog.findViewById(R.id.tick7);
tvMsg11.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
// final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
//tvMsg11.setImageDrawable(getResources().getDrawable(R.drawable.tick));
tvMsg111.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg11.setSelected(true);
tick5.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick7.setVisibility(View.INVISIBLE);
}
});
// final TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
tvMsg111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg1111.setSelected(false);
tvMsg111.setSelected(true);
tick6.setVisibility(View.VISIBLE);
tick7.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
//final TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);
tvMsg1111.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
tvMsg11.setSelected(false);
tvMsg111.setSelected(false);
tvMsg1111.setSelected(true);
tick7.setVisibility(View.VISIBLE);
tick6.setVisibility(View.INVISIBLE);
tick5.setVisibility(View.INVISIBLE);
}
});
/*TextView tvMsg111 =(TextView) dialog.findViewById(R.id.tvMsg111);
TextView tvMsg1111 =(TextView) dialog.findViewById(R.id.tvMsg1111);*/
ImageView image1 = (ImageView) dialog.findViewById(R.id.image1);
ImageView tick1=(ImageView)dialog.findViewById(R.id.tick1);
ImageView tick2=(ImageView)dialog.findViewById(R.id.tick2);
ImageView tick3=(ImageView)dialog.findViewById(R.id.tick3);
ImageView tick4=(ImageView)dialog.findViewById(R.id.tick4);
image1.setImageDrawable(getResources().getDrawable(R.drawable.one));
image1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick1.setVisibility(View.VISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
ImageView image2 = (ImageView) dialog.findViewById(R.id.image2);
// ImageView image21 = (ImageView) dialog.findViewById(R.drawable.four);
// for replacing image on click of image
image2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick2.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image2.setImageDrawable(getResources().getDrawable(R.drawable.two));
ImageView image3 = (ImageView) dialog.findViewById(R.id.image3);
image3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick3.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick4.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image3.setImageDrawable(getResources().getDrawable(R.drawable.three));
ImageView image4 = (ImageView) dialog.findViewById(R.id.image4);
image4.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//final ImageView imageView = (ImageView) dialog.findViewById(R.id.image2);
tick4.setVisibility(View.VISIBLE);
tick1.setVisibility(View.INVISIBLE);
tick2.setVisibility(View.INVISIBLE);
tick3.setVisibility(View.INVISIBLE);
/* imageView.setImageDrawable(getResources().getDrawable(R.drawable.tick));
imageView.setImageDrawable(getResources().getDrawable(R.drawable.two));*/
}
});
image4.setImageDrawable(getResources().getDrawable(R.drawable.four));
cust.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="430dp"
android:orientation="vertical"
android:background="#ffffff"
android:theme="#style/AppTheme">
<TextView
android:id="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="24dp"
android:layout_marginLeft="20dp"
android:textSize="20sp"
android:textColor="#293448"
android:text="Update your water intake" />
<TextView
android:id="#+id/tvMsg1"
android:layout_below="#+id/tvMsg"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="25dp"
android:layout_marginLeft="21.5dp"
android:textSize="16sp"
android:textColor="#293448"
android:text="How many glasses of water do you
drink daily?" />
<LinearLayout
android:id="#+id/linearImage21"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/tvMsg1"
android:weightSum="3">
<TextView
android:id="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="23.5dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="4 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg111"
android:layout_toRightOf="#+id/tvMsg11"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#drawable/text_color"
android:layout_marginLeft="8dp"
android:layout_height="wrap_content"
android:text="6 Glasses"
android:textSize="14sp"/>
<TextView
android:id="#+id/tvMsg1111"
android:layout_toRightOf="#+id/tvMsg111"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="10dp"
android:textColor="#drawable/text_color"
android:layout_height="wrap_content"
android:text="8 Glasses"
android:textSize="14sp"/>
</LinearLayout>
<LinearLayout
android:id="#+id/lineartick"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="2dp"
android:layout_below="#+id/linearImage21"
android:weightSum="3">
<ImageView
android:id="#+id/tick5"
android:layout_width="16dp"
android:layout_weight="1"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginRight="12dp"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick6"
android:layout_width="16dp"
android:layout_weight="0.7"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_marginLeft="1dp"
android:layout_toRightOf="#+id/image1"
android:layout_height="10dp" />
<ImageView
android:id="#+id/tick7"
android:layout_width="16dp"
android:layout_weight="1.3"
android:src="#drawable/texthiglighter"
android:visibility="invisible"
android:layout_height="10dp" />
</LinearLayout>
<TextView
android:id="#+id/container"
android:layout_below="#+id/linearImage21"
android:layout_marginTop="48dp"
android:layout_marginLeft="21.5dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:textSize="16sp"
android:textColor="#293448"
android:alpha="80"
android:text="Your container size?" />
<LinearLayout
android:id="#+id/linearImage1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="20dp"
android:layout_below="#+id/container"
android:weightSum="4">
<ImageView
android:id="#+id/image1"
android:layout_width="0dp"
android:layout_weight="1"
android:checked="true"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick1"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image1"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image2"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick2"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image2"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image3"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick3"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_toRightOf="#+id/image3"
android:layout_height="13dp" />
<ImageView
android:id="#+id/image4"
android:layout_width="0dp"
android:layout_weight="1"
android:layout_height="wrap_content" />
<ImageView
android:id="#+id/tick4"
android:layout_width="13dp"
android:layout_weight="0.1"
android:src="#drawable/tick"
android:layout_marginLeft="0.1dp"
android:visibility="invisible"
android:layout_marginRight="25dp"
android:layout_toRightOf="#+id/image4"
android:layout_height="13dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/linearscale"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:layout_marginTop="16dp"
android:layout_below="#+id/linearImage1"
android:weightSum="4">
<TextView
android:id="#+id/onne"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginLeft="5dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="150 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/twoe"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="250 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/three"
android:layout_width="wrap_content"
android:layout_weight="1"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="500 ML"
android:textAllCaps="true"
android:textSize="14sp"/>
<TextView
android:id="#+id/foure"
android:layout_width="wrap_content"
android:layout_weight="1"
android:layout_marginRight="6dp"
android:textColor="#283d65"
android:layout_height="wrap_content"
android:text="1 LTR"
android:textAllCaps="true"
android:textSize="14sp"/>
</LinearLayout>
<View
android:id="#+id/viewp"
android:layout_width="match_parent"
android:layout_height="1dp"
android:layout_marginLeft="6dp"
android:layout_marginRight="6dp"
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:alpha="4"
android:background="#000000"
android:layout_below="#+id/linearscale"></View>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/linearscale"
android:orientation="horizontal"
android:layout_marginTop="28dp"
android:weightSum="2"
>
<TextView
android:id="#+id/btnOk"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="1"
android:background="#fff"
android:layout_marginLeft="86.5dp"
android:textSize="16sp"
android:text="CANCEL"
android:textAllCaps="true"
android:textColor="#293448" />
<TextView
android:id="#+id/btnCancel"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_marginLeft="48.5dp"
android:layout_weight="1"
android:textSize="16sp"
android:layout_marginRight="6dp"
android:background="#fff"
android:text="SUBMIT"
android:textAllCaps="true"
android:textColor="#21c8d7" />
</LinearLayout>
</RelativeLayout>

How to position an ImageView in the same place in different screen sizes?

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.

TextView change to ImageView on same ButtonClick- uncompleted

I have a button. In onClick() it will show TextView and ImageView at the same time, with the same outcome.But I am facing a problem, the image doesn't load completely. Can anyone tell me why is it so? Thanks in advance!
Code for Button onClick
b.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View view) {
switch(count){
case 0 :
df = Typeface.createFromAsset(getAssets(),
"fonts/IDAutomationHC39M_FREE.otf");
ConvertToBitmap();
break;
case 1:
df = Typeface.createFromAsset(getAssets(),
"fonts/code128.ttf");
ConvertToBitmap();
break;
case 2:
df = Typeface.createFromAsset(getAssets(),
"fonts/Molot.otf");
ConvertToBitmap();
break;
case 3:
df = Typeface.createFromAsset(getAssets(),
"fonts/Days.otf");
ConvertToBitmap();
break;
case 4:
df = Typeface.createFromAsset(getAssets(),
"fonts/Paranoid.otf");
ConvertToBitmap();
break;
}
}
});
Code for TextView and ImageView
public void ConvertToBitmap(){
if (et.getText().toString().equals("")) {
//Clear textView
tv.setText("");
et.setText("");
tv.buildDrawingCache();
iv.setImageBitmap(tv.getDrawingCache());
}
else {
TextView tv1 = (TextView) findViewById(R.id.textView1);
tv1.setTypeface(df);
String editTextValue = et.getText().toString();
tv.setText(editTextValue);
tv.buildDrawingCache();
bmap = tv.getDrawingCache();
iv.setImageBitmap(bmap);
}
}
EDITED!
Layout.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/gameScreen"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_weight="1"
android:orientation="vertical" xmlns:app="http://schemas.android.com/tools">
<com.example.textdrawn3.Text
android:id="#+id/textDrawn"
android:layout_width="match_parent"
android:layout_height="46dp"
android:layout_weight="3.11" />
<TextView
android:id="#+id/textView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:text="TextView" />
<ImageView
android:id="#+id/imageView1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="0.75"
android:orientation="horizontal"
android:src="#drawable/ic_launcher" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="bottom"
android:background="#0000cd"
android:orientation="horizontal" >
<Button
android:id="#+id/btnText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Text"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
<Button
android:id="#+id/btnStop"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Stop"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
<Button
android:id="#+id/btnOK"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="OK"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
<Button
android:id="#+id/btnImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Image"
android:textColor="#ffffff"
android:textSize="25sp"
android:typeface="serif" />
<Spinner
android:id="#+id/fonts_spinner"
android:layout_width="135dp"
android:layout_height="wrap_content"
android:textAlignment="center"
app:context=".TextDrawn3" />
<EditText
android:id="#+id/editText1"
android:layout_width="140dp"
android:layout_height="wrap_content"
android:ems="10"
android:freezesText="false"
android:hint="text"
android:maxLength="10"
android:maxLines="1"
android:textAlignment="center"
android:textColor="#e6e6fa"
android:textSize="25sp"
android:typeface="serif"
android:visibility="visible" >
<requestFocus />
</EditText>
</LinearLayout>

half or quarter black screen in android

I have an android activity that when I launch sometimes (about 1 in 4 times) it only draws quarter or half the screen before showing it. When I change the orientation or press a button the screen draws properly.
I'm just using TextViews, Buttons, Fonts - no drawing or anything like that.
All of my code for initialising is in the onCreate(). In this method I'm loading a text file, of about 40 lines long, and also getting a shared preference. Could this cause a delay so that it can't draw the intent?
Thanks in advance if anyone has seen anything similar.
EDIT - I tried commenting out the loading of the word list, but it didn't fix the problem.
EDIT 2 - I didn't manage to resolve the problem, but managed to improve it by adding a timer right at the end of the onCreate. I set a low refresh rate and in the tick changed the text of one of the controls. This then seemed to redraw the screen and get rid of the black portions of the screen.
Here is the activity
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/mainRelativeLayout"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="#drawable/blue_abstract_background" >
<RelativeLayout
android:id="#+id/relativeScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<TextView
android:id="#+id/ScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true"
android:padding="10dip"
android:text="SCORE:"
android:textSize="18dp" />
/>
<TextView
android:id="#+id/ScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toRightOf="#id/ScoreLabel"
android:padding="5dip"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_centerVertical="true"
android:layout_marginLeft="5dp"
android:layout_marginRight="5dp"
android:minWidth="25dp"
android:text="0"
android:textSize="18dp" />
<TextView
android:id="#+id/GameTimerLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerVertical="true"
android:layout_toLeftOf="#id/GameTimerText"
android:padding="10dip"
android:text="TIMER:"
android:textSize="18dp" />
/>
</RelativeLayout>
<RelativeLayout
android:id="#+id/relativeHighScore"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_below="#id/relativeScore" >
<TextView
android:id="#+id/HighScoreLabel"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:padding="10dip"
android:text="HIGH SCORE:"
android:textSize="16dp" />
<TextView
android:id="#+id/HighScoreText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_toRightOf="#id/HighScoreLabel"
android:padding="10dip"
android:text="0"
android:textSize="16dp" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_alignParentLeft="true"
android:layout_below="#+id/relativeHighScore"
android:orientation="vertical" >
<LinearLayout
android:id="#+id/linearMissing"
android:layout_width="fill_parent"
android:layout_height="80dp"
android:layout_gravity="center"
android:orientation="vertical" >
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TextView
android:id="#+id/MissingWordText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="fill_parent"
android:layout_gravity="center_horizontal"
android:layout_marginTop="25dp"
android:text="MSSNG WRD"
android:textSize="22dp"
android:typeface="normal" />
</LinearLayout>
<View
android:layout_width="match_parent"
android:layout_height="2dp"
android:background="#color/yellow_text" />
<TableLayout
android:id="#+id/buttonsTableLayout"
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<TableRow
android:id="#+id/tableRow3"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal"
android:paddingTop="5dp" >
<Button
android:id="#+id/aVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/a"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/eVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_marginBottom="10dp"
android:layout_marginRight="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/e"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
<TableRow
android:id="#+id/tableRow4"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="center_horizontal" >
<Button
android:id="#+id/iVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_buttoni"
android:text="#string/i"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/oVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/o"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
<Button
android:id="#+id/uVowelButton"
android:layout_width="66dp"
android:layout_height="66dp"
android:layout_gravity="center_horizontal"
android:layout_margin="5dp"
android:background="#drawable/blue_vowel_button"
android:text="#string/u"
android:textColor="#color/yellow_text"
android:textSize="30dp" />
</TableRow>
</TableLayout>
<TextView
android:id="#+id/TimeToStartText"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:textSize="24dp" />
<Button
android:id="#+id/startButton"
style="#style/yellowShadowText"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_marginTop="10dp"
android:background="#drawable/blue_button_menu"
android:gravity="center"
android:padding="10dip"
android:text="START!"
android:textSize="28dp" />
</LinearLayout>
</RelativeLayout>
And the OnCreate() and a few methods:
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_game);
isNewWord = true;
m_gameScore = 0;
m_category = getCategoryFromExtras();
// loadWordList(m_category);
initialiseTextViewField();
loadHighScoreAndDifficulty();
setFonts();
setButtons();
setStartButton();
enableDisableButtons(false);
}
private void loadHighScoreAndDifficulty() {
//setting preferences
SharedPreferences prefs = this.getSharedPreferences(m_category, Context.MODE_PRIVATE);
int score = prefs.getInt(m_category, 0); //0 is the default value
m_highScore = score;
m_highScoreText.setText(String.valueOf(m_highScore));
prefs = this.getSharedPreferences(DIFFICULTY, Context.MODE_PRIVATE);
m_difficulty = prefs.getString(DIFFICULTY, NRML_DIFFICULTY);
}
private void initialiseTextViewField() {
m_startButton = (Button) findViewById(R.id.startButton);
m_missingWordText = (TextView) findViewById(R.id.MissingWordText);
m_gameScoreText = (TextView) findViewById(R.id.ScoreText);
m_gameScoreLabel = (TextView) findViewById(R.id.ScoreLabel);
m_gameTimerText = (TextView) findViewById(R.id.GameTimerText);
m_gameTimerLabel = (TextView) findViewById(R.id.GameTimerLabel);
m_timeToStartText = (TextView) findViewById(R.id.TimeToStartText);
m_highScoreLabel = (TextView) findViewById(R.id.HighScoreLabel);
m_highScoreText = (TextView) findViewById(R.id.HighScoreText);
}
private String getCategoryFromExtras() {
String category = "";
Bundle extras = getIntent().getExtras();
if (extras != null) {
category = extras.getString("category");
}
return category;
}
private void enableDisableButtons(boolean enable) {
Button button = (Button) findViewById(R.id.aVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.eVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.iVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.oVowelButton);
button.setEnabled(enable);
button = (Button) findViewById(R.id.uVowelButton);
button.setEnabled(enable);
}
private void setStartButton() {
m_startButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
m_gameScore = 0;
updateScore();
m_startButton.setVisibility(View.GONE);
m_timeToStartText.setVisibility(View.VISIBLE);
startPreTimer();
}
});
}

Categories

Resources