android make button square - android

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

Related

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.

how to store text value for a radiogroup in sqlite database?

i've a radio group radiogoup1 that hat two radiobutton rbtn1, rbtn2. say i want to store Male for rbtn1 and Female for rbtn2 in database. How to do it? I am mentioning the .xml and .java file.
sqlliteexample.xml :
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/name"
android:paddingLeft="5dp"
android:textAppearance="?android:attr/textAppearanceSmall" />
<EditText
android:id="#+id/editName"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textPersonName" >
<requestFocus />
</EditText>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textAge"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/age" >
</TextView>
<EditText
android:id="#+id/editAge"
android:layout_width="80dp"
android:layout_height="wrap_content"
android:ems="10"
android:inputType="number" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content" >
<TextView
android:id="#+id/textContcat"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:paddingLeft="5dp"
android:text="#string/contact" />
<EditText
android:id="#+id/editContact"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:ems="10"
android:inputType="textMultiLine" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_marginTop="0dp"
android:orientation="horizontal" >
<TextView
android:id="#+id/sLabel"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:gravity="left|center"
android:paddingLeft="5dp"
android:paddingRight="10dp"
android:text="#string/sx" />
<RadioGroup
android:id="#+id/RadioGroup01"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<RadioButton
android:id="#+id/malebutton"
android:layout_width="wrap_content"
android:layout_height="17dp"
android:text="#string/mal"
android:textSize="15sp" />
<RadioButton
android:id="#+id/femalebutton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/femal"
android:textSize="15sp" />
</RadioGroup>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<Button
android:id="#+id/savebutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:gravity="left|center"
android:text="#string/save"
android:textSize="13sp" />
<Button
android:id="#+id/viewbutton"
style="?android:attr/buttonStyleSmall"
android:layout_width="wrap_content"
android:layout_height="32dp"
android:layout_gravity="center"
android:gravity="center|left"
android:text="#string/view"
android:textSize="13sp" />
</LinearLayout>
SqlLiteExample.java :
public class SqlLiteExample extends Activity implements OnClickListener, OnCheckedChangeListener {
Button sqlUpdate, sqlView;
EditText etName,etAge,etContact;
RadioGroup rdgrp;
RadioButton selectedRadioButton;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.sqlliteexample);
sqlUpdate = (Button) findViewById(R.id.savebutton);
etName = (EditText) findViewById(R.id.editName);
etAge = (EditText) findViewById(R.id.editAge);
etContact = (EditText) findViewById(R.id.editContact);
rdgrp.setOnCheckedChangeListener(this);
sqlView = (Button) findViewById(R.id.viewbutton);
sqlView.setOnClickListener(this);
sqlUpdate.setOnClickListener(this);
}
#Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
switch(arg0.getId()){
case R.id.savebutton:
boolean didWork = true;
try{
String name = etName.getText().toString();
String age = etAge.getText().toString();
String contact = etContact.getText().toString();
MyDB entry = new MyDB(SqlLiteExample.this);
entry.open();
entry.createEntry(name,age,contact);
entry.close();
}catch(Exception e){
didWork = false;
String error = e.toString();
Dialog d = new Dialog(this);
d.setTitle("Error");
TextView tv = new TextView(this);
tv.setText(error);
d.setContentView(tv);
d.show();
}finally{
if(didWork){
Dialog d = new Dialog(this);
d.setTitle("Updated");
TextView tv = new TextView(this);
tv.setText("Success");
d.setContentView(tv);
d.show();
}
}
break;
case R.id.viewbutton:
Intent i = new Intent("com.bysakiralam.mydatabase.DISPLAYRECORDS");
startActivity(i);
break;
}
}
#Override
public void onCheckedChanged(RadioGroup arg0, int arg1) {
// TODO Auto-generated method stub
switch(arg1){
case R.id.malebutton:
break;
case R.id.femalebutton:
break;
}
} }
There is no need to override onCheckedChanged() use following in your on savebutton click
rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= (RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString();
and now use radiovalue to store in database
Edit: forget (
rdgrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= ((RadioButton)this.findViewById(rdgrp.getCheckedRadioButtonId())).getText().toString();
try this
myRadioGrp=(RadioGroup)findViewById(R.id.RadioGroup01);
String radiovalue= ((RadioButton)this.findViewById(myRadioGrp.getCheckedRadioButtonId())).getText().toString();

TableLayout inside ListView

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.

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