Checkbox if condition? - android

so I have an android app with 2 Activities
1 - Main
2 - Second
Main activity has 1 single checkbox in a RelativeLayout I want to know how to use the checkbox to make a condition where if the check box is checked I want to make the ImageView = VISIBLE and the TextView = GONE in the Second activity and vice versa if its unchecked.
I have tried to use shared preferences and also intent but I keep messing up somewhere where either the checkbox wont matter or the screen just ends up showing blank. One other thing is that in Main activity I have a button that has onClick and already has an intent. I just want to know how to create the if condition with the checkbox for the Second activity. Sadly I would post my entire code but I have scrambled it up so bad trying to figure this out its all over the place and doesn't make sense anymore.
ok I tried to a few things, tried a posted answer but atleast I was able to clean up the code enough to share now but it messes up really badly and closes the app, sorry for any weird formatting, new to this
activity_main.xml
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/rlay"
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"
tools:context="sam.plsnyc.com.plsign.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Company"
android:id="#+id/textView"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/editText"
android:textAlignment="center"
android:textIsSelectable="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_alignParentTop="true"
android:layout_toRightOf="#+id/textView"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Signage"
android:id="#+id/textView2"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignBottom="#+id/editText2"
android:textAlignment="center"
android:textIsSelectable="false" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_below="#+id/editText"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Make Sign"
android:id="#+id/button"
android:layout_centerHorizontal="true"
android:layout_below="#+id/editText2" />
<ImageView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/imgView"
android:focusable="true"
android:src="#drawable/logo"
android:scaleType="fitXY"
android:visibility="visible"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<CheckBox
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Use Logo"
android:id="#+id/logobox"
android:layout_below="#+id/textView2"
android:layout_alignParentStart="true" />
</RelativeLayout>
activity_display_message.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_margin="0dp"
android:singleLine="false"
android:id="#+id/activity_display_message"
tools:context=".MainActivity"
android:focusableInTouchMode="false"
android:focusable="true"
android:nestedScrollingEnabled="false"
android:orientation="vertical"
android:weightSum="1">
<ImageView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="1"
android:scaleType="fitXY"
android:src="#drawable/logo"
android:visibility="gone"
android:id="#+id/iV2" />
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:maxLines="1"
android:textStyle="bold"
android:textSize="275sp"
android:layout_weight=".5"
android:textAlignment="center"
android:text="LOGO"
android:visibility="invisible"
android:id="#+id/restxttop"
android:elegantTextHeight="false" />
</me.grantland.widget.AutofitLayout>
<me.grantland.widget.AutofitLayout
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight=".5"
android:textAlignment="center"
android:maxLines="1"
android:textSize="275sp"
android:text="SIGNAGE"
android:id="#+id/restxtbot"
/>
</me.grantland.widget.AutofitLayout>
</LinearLayout>
MainActivity.java
public class MainActivity extends Activity {
public final static String KEY_CHECKED = "chkd";
CheckBox lbox;
Button button;
EditText etxt;
EditText etxt2;
String toptxt;
String bottxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);{
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
etxt = (EditText) findViewById(R.id.editText);
toptxt = etxt.getText().toString();
etxt2 = (EditText) findViewById(R.id.editText2);
bottxt = etxt2.getText().toString();
Intent i = new Intent(v.getContext(), DisplayMessageActivity.class);
i.putExtra("text1", toptxt);
i.putExtra("text2", bottxt);
startActivity(i);
lbox = (CheckBox) findViewById(R.id.logobox);
Intent i2 = new Intent (MainActivity.this, DisplayMessageActivity.class);
i2.putExtra(KEY_CHECKED, lbox.isChecked());
startActivity(i2);
}
});
}
}
}
DisplayMessageActivity.java
public class DisplayMessageActivity extends Activity {
TextView ttxt;
TextView btxt;
#Override
protected void onCreate(Bundle savedInstanceState) {
this.getWindow().requestFeature(Window.FEATURE_NO_TITLE);
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_display_message);
btxt = (TextView) findViewById(R.id.restxtbot);
btxt.setText(getIntent().getStringExtra("text2"));
AutofitHelper.create(btxt);
ImageView image = (ImageView) findViewById(R.id.iV2);
Boolean chk = getIntent().getBooleanExtra(MainActivity.KEY_CHECKED, false);
if (chk)
{
ttxt.setVisibility(View.GONE);
image.setVisibility(View.VISIBLE);
}else{
ttxt.setVisibility(View.VISIBLE);
ttxt = (TextView)findViewById(R.id.restxttop);
ttxt.setText(getIntent().getStringExtra("text1"));
AutofitHelper.create(ttxt);
ttxt.setPaintFlags(ttxt.getPaintFlags()| Paint.UNDERLINE_TEXT_FLAG);
}
}
}

If you open the second activity from the main you can put the isChecked value in the intent else in this simple case make the value static

Related

using scrollview to show button behind edittext keyboard

I have a layout in which I have an image,textview, editbox and a button so whenever the keyboard opens I wanted my screen to scroll down to the button so that the user can always type and press submit button. I did do this by using scrollview but than I removed the scroll view did some changes and now when I again use a scrollview it does not work.
here is the code please check.
layout_file:
<?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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="16dp"
android:paddingLeft="16dp"
android:paddingRight="16dp"
android:paddingTop="16dp"
tools:context="myapp.anis.hotel.MainActivity"
android:background="#drawable/backgroundhotel"
android:fitsSystemWindows="true">
<ScrollView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/scroll">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:text="Please share your feedback"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView6"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_below="#+id/imageView"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<TextView
android:text="#string/textview"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/thanks"
android:shadowColor="#android:color/background_light"
android:textAppearance="#style/TextAppearance.AppCompat.Large"
android:textColorHighlight="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:fontFamily="serif"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="15dp" />
<ImageView
android:layout_width="wrap_content"
app:srcCompat="#drawable/hotellogo"
android:id="#+id/imageView"
android:layout_height="150dp"
android:layout_marginTop="15dp"
android:layout_below="#+id/thanks"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
<EditText
android:layout_width="match_parent"
android:ems="10"
android:maxLines="50"
android:background="#android:drawable/editbox_background"
android:layout_marginTop="15dp"
android:id="#+id/feedback"
android:hint="Enter FeedBack"
android:inputType="textMultiLine"
android:gravity="left"
android:textColorHint="#android:color/background_dark"
android:textColor="#android:color/background_dark"
android:textColorHighlight="#android:color/background_dark"
android:layout_below="#+id/textView6"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_height="180dp" />
<ImageButton
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:srcCompat="#drawable/submitting1"
android:background="#android:color/transparent"
android:onClick="onSubmit"
android:id="#+id/submit"
android:layout_below="#id/feedback"
android:layout_alignParentBottom="true"
android:layout_marginTop="20dp"
android:layout_centerHorizontal="true"
/>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
and in my main activity I am using the scrollto method to go to my image button.
public class MainActivity extends AppCompatActivity {
ImageButton submit;
EditText feedBack;
static public String saveFeedBack="";
ScrollView view;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
submit = (ImageButton) findViewById(R.id.submit);
feedBack = (EditText) findViewById(R.id.feedback);
view = (ScrollView) findViewById(R.id.scroll);
view.scrollTo(submit.getScrollX(),submit.getScrollY());
}
public void onSubmit(View view){
saveFeedBack = feedBack.getText().toString();
if(saveFeedBack.length() >=4) {
Intent intent = new Intent(this, Main3Activity.class);
startActivity(intent);
}
else{
Toast.makeText(getApplicationContext(),"Enter valid Feedback please.",Toast.LENGTH_SHORT).show();
}
}
#Override
protected void onRestart() {
super.onRestart();
feedBack.setText("");
feedBack.setHint("Enter Feedback");
}
}
but when I run my application the scrollview scrolls to my edit text but not my button. I have tried adjustResize,adjustPan and all those things but none of them worked.
ok after much of research I have solved your problem. Actually the thing is when you use scroll to any position inside onCreate it doesn't work because a small delay is required to inflate the view.
So use the below code inside your onCreate:
view.postDelayed(new Runnable() {
#Override
public void run() {
view.fullScroll(ScrollView.FOCUS_DOWN);
}
},200);
Hope it Helps!!!

Click TextView inside linear layout inside ScrollLayout

this below code for my login_activity xml:
<?xml version="1.0" encoding="utf-8"?>
<ScrollView
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:background="#color/colorBackgroundLogin"
android:clipToPadding="false"
android:fillViewport="false">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear_layout"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#color/colorPrimary"
android:paddingTop="20dp"
android:paddingBottom="20dp">
<ImageView
android:layout_width="match_parent"
android:layout_height="150dp"
android:src="#drawable/login"
tools:ignore="ContentDescription"/>
</RelativeLayout>
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="textEmailAddress"
android:id="#+id/email_edit_text"
android:hint="#string/email_address_string" />
<EditText
android:layout_marginTop="20dp"
android:layout_marginRight="30dp"
android:layout_marginLeft="30dp"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:inputType="numberPassword"
android:id="#+id/password_edit_text"
android:hint="#string/password_string" />
<Button
android:layout_width="match_parent"
android:textAllCaps="true"
android:text="#string/login_string"
android:background="#drawable/button_style"
android:layout_marginTop="20dp"
android:layout_marginRight="50dp"
android:layout_marginLeft="50dp"
android:textColor="#fff"
android:layout_gravity="center_horizontal"
android:layout_height="wrap_content"
android:id="#+id/login_button" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
</LinearLayout>
</ScrollView>
I want to click on TextView id=need_an_account that like below:
public class LoginActivity extends Activity implements View.OnClickListener{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_login);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
}
Note that i don't want to use findViewById() method
But onClick not Working at all how can I solve this problem?
If you don't want to use OnClickListener, Here is an alternative -
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:textStyle="italic"
android:onClick="myTextViewMethod"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
Now write myTextViewMethodin you Activity
public void myTextViewMethod(View v) {
// Do your stuff here...
}
Plus point, No need to implement OnClickListener, Android will do it in background.
And if you wanna use an Interface View.OnClickListener
Simple, set need_an_account.setOnClickListener(this);
In oncreate add this,
TextView need_an_account = (TextView) findViewById(R.id.need_an_account);
need_an_account.setOnClickListener(this);
You can add OnClick to your xml to avoid using findViewById:
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#color/colorPrimaryDark"
android:text="#string/need_an_account"
android:layout_marginTop="20dp"
android:clickable="true"
android:textStyle="italic"
android:onClick="textViewClick"
android:layout_gravity="center_horizontal"
android:id="#+id/need_an_account" />
and then write your function in the activity as:
public void textViewClick(View v) {
switch (v.getId())
{
case R.id.need_an_account:
// some code
break;
}
}
no need to implement an OnClickListener if you dont want to define the TextView programatically. Remember to set TextView to clickable and for authenticity i also set background to ?selectableItemBackground so the user knows that the TextView can be clicked

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.

Android: OnClick method for Button doesn't respond

I didn't have this problem until now. I don't know what is the problem here because in the same layout i have several buttons and only buttons which are in LinearLayout won't respond.
This is the layout where my buttons won't respond onClick (EDITED):
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/scrollView1"
android:background="#drawable/texture"
android:layout_width="match_parent"
android:layout_height="match_parent">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="600dp"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin">
<EditText
android:id="#+id/input_first_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size">
<requestFocus />
</EditText>
<EditText
android:id="#+id/input_last_name"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_first_name"
android:ems="10"
android:inputType="text"
android:textSize="#dimen/text_size" />
<EditText
android:id="#+id/input_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentRight="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/input_last_name"
android:ems="10"
android:inputType="number"
android:textSize="#dimen/text_size" />
<ImageView
android:id="#+id/profile_image"
android:layout_width="140dp"
android:layout_height="140dp"
android:layout_below="#+id/input_age"
android:layout_centerHorizontal="true"
android:src="#drawable/add" />
<Button
android:id="#+id/save_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/edit_button"
android:onClick="run"
android:text="#string/save_button" />
<Button
android:id="#+id/edit_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_below="#+id/slider"
android:text="#string/edit_button" />
<Button
android:id="#+id/delete_button"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_below="#+id/slider"
android:layout_toLeftOf="#+id/save_button"
android:layout_toStartOf="#+id/save_button"
android:text="#string/delete_button" />
<Button
android:id="#+id/edit_birthday_date"
style="#style/MyCustomButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/profile_image"
android:layout_centerHorizontal="true" />
<LinearLayout
android:id="#+id/button_container1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/cake_image"
android:layout_marginTop="10dp"
android:clickable="true"
android:weightSum="3"
android:orientation="horizontal">
<Button
android:id="#+id/button_movie"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_books"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_tech"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container1"
android:layout_marginTop="10dp"
android:orientation="horizontal"
android:weightSum="3"
android:clickable="true">
<Button
android:id="#+id/button_body_care"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
<Button
android:id="#+id/button_clothes"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1" />
<Button
android:id="#+id/button_accessories"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center_horizontal"
android:layout_weight="1"/>
</LinearLayout>
<LinearLayout
android:id="#+id/button_container3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="#+id/button_container2"
android:layout_marginTop="10dp"
android:clickable="true"
android:orientation="horizontal">
<Button
android:id="#+id/button_games"
style="#style/MyCustomButton"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignEnd="#+id/edit_button"
android:layout_alignTop="#+id/slider"
android:layout_gravity="center_horizontal"/>
</LinearLayout>
<ImageView
android:id="#+id/cake_image"
android:layout_width="40dp"
android:layout_height="40dp"
android:layout_alignStart="#+id/edit_birthday_date"
android:layout_below="#+id/edit_birthday_date"
android:layout_marginTop="20dp"
android:src="#drawable/birthday_cake" />
<TextView
android:id="#+id/turning_age"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="#+id/cake_image"
android:layout_toEndOf="#+id/cake_image"
android:text="Large Text"
android:textAppearance="?android:attr/textAppearanceLarge" />
<com.daimajia.slider.library.SliderLayout
android:id="#+id/slider"
android:layout_width="match_parent"
android:layout_height="200dp"
android:layout_below="#+id/cake_image" />
</RelativeLayout>
</ScrollView>
I have set for every linear layout visibility when one button which is working is clicked to visible, so i think that is not the problem.
This is the code in java:
if (getMovieCategory.equals("movies")) {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
}
});
} else {
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
dbh.deleteMovies(getMovieCategory);
}
});
}
Here everything is working except onClick. I'm getting no errors. My button doesn't respond on click.
try by handling setOnClickListener out side the if condition.
.You're coding with a lot of redundancy and risking introducing errors, some of which may not be immediately obvious. Here's a leaner and clearer version of your posted code sample, using one onClick method to handle your if(){else} conditions.
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(
getMovieCategory.equals("movies") ? R.drawable.ic_cancelar : R.drawable.ic_add, 0,0,0
);
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
dbh.deleteMovies(getMovieCategory);
}
}
});
EDIT 1: I mistakenly left out your String movies = "movies" and have added it back in. If you're only going to use this String once, to pass it as a parameter for dbh.updateCategoryMovies(birthdayId, movies), then you could simply call dbh.updateCategoryMovies(birthdayId, "movies") and delete String movies = "movies"
EDIT 2: Without seeing your entire source code, I don't know if you're at all changing the value of getMovieCategory somewhere. If not, then the below sample will change it on every click of the button (once you launch the app to test the sample, your buttonCategoryMovie will be a generic Button; once you start clicking the button, it should toggle between showing your R.drawable.ic_cancelar and R.drawable.ic_add resources. If the button toggles between these two drawables, then you at least know that the button is in fact receiving the onClick.
Remember that, if the value of getMovieCategory doesn't change on each click, your button will always perform only one of the sets of onClick actions and it'll look like nothing's happening.
If it works for you as I've described, then you're on your way ;)
buttonCategoryMovie.setText("MOVIES");
buttonCategoryMovie.setTextColor(Color.parseColor("#ffffff"));
buttonCategoryMovie.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (getMovieCategory.equals("movies")) {
getMoviesCategory = "not movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_cancelar, 0, 0, 0);
String movies = "movies";
dbh.updateCategoryMovies(birthdayId, movies);
} else {
getMoviesCategory = "movies"; // this is here just to test
buttonCategoryMovie.setCompoundDrawablesWithIntrinsicBounds(R.drawable.ic_add, 0, 0, 0);
dbh.deleteMovies(getMovieCategory);
}
}
});
Try changing android:visibility="gone" to android:visibility="visible" in your LinearLayout.
And again add android:clickable="true" to your LinearLayout

I clicked on my clear button and it doesn't work

THANK YOU FOR ALL OF YOUR HELP!! I NEED TO READ A TUTORIAL. :)
I know it's probably elementary and as you can tell I'm a newbie. I compared my code to others and don't see anything wrong. Thanks! When I click the clear Button after entering data, my edit fields are not cleared.
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
potsize.setText("");
tocall.setText("");
bepercent.setText("");
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
potoddscalc.setText("5");
}
});
}
XML:
RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
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"
tools:context=".MainActivity" >
<requestFocus />
<TextView
android:id="#+id/tv_be_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_to_call"
android:layout_below="#+id/tv_to_call"
android:layout_marginTop="34dp"
android:text="#string/BE_Per"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_to_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_pot_size"
android:layout_below="#+id/tv_pot_size"
android:layout_marginTop="33dp"
android:text="#string/to_call"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="16dp"
android:text="#string/pot_size"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_odds_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_be_per"
android:layout_alignTop="#+id/tv_pot_odds"
android:ems="8" />
<EditText
android:id="#+id/et_pot_size"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignTop="#+id/tv_pot_size"
android:layout_marginLeft="18dp"
android:layout_toRightOf="#+id/tv_pot_odds"
android:ems="10" />
<EditText
android:id="#+id/et_to_call"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_pot_size"
android:layout_alignTop="#+id/tv_to_call"
android:ems="10" />
<EditText
android:id="#+id/et_be_per"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/et_to_call"
android:layout_alignTop="#+id/tv_be_per"
android:ems="10" />
<TextView
android:id="#+id/tv_pot_odds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignLeft="#+id/tv_be_per"
android:layout_below="#+id/et_be_per"
android:layout_marginTop="20dp"
android:text="#string/pot_odds"
android:textStyle="bold" />
<Button
android:id="#+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignRight="#+id/tv_pot_odds_calc"
android:layout_below="#+id/tv_pot_odds_calc"
android:layout_marginTop="24dp"
android:text="#string/clear_button" />
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="#+id/btn_clear"
android:layout_alignBottom="#+id/btn_clear"
android:layout_alignLeft="#+id/tv_pot_odds_calc"
android:text="#string/calc_button" />
Try this instead. I believe your problem may be arising from creating new EditTexts and TextView objects every time you click the clear button. In the below code, the EditTexts and TextView are declared only once ,in the OnCreate method. Also, getText().clear() is the "official" way of clearing the text from an EditText (not that it really makes a visual difference).
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
potsize.getText().clear();
tocall.getText().clear();
bepercent.getText().clear();
potoddscalc.setText("5");
}
});
}
// try this way
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="5dp"
android:orientation="vertical"
tools:context=".MainActivity" >
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_pot_size"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="pot_size"
android:textStyle="bold" />
<EditText
android:id="#+id/et_pot_size"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_to_call"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="to_call"
android:textStyle="bold" />
<EditText
android:id="#+id/et_to_call"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_be_per"
android:layout_width="0dp"
android:layout_weight="0.20"
android:gravity="right"
android:layout_height="wrap_content"
android:text="BE_Per"
android:textStyle="bold" />
<EditText
android:id="#+id/et_be_per"
android:layout_width="0dp"
android:layout_weight="0.80"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="10" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_marginTop="10dp">
<TextView
android:id="#+id/tv_pot_odds"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="pot_odds"
android:textStyle="bold" />
<TextView
android:id="#+id/tv_pot_odds_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:ems="8" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:gravity="center">
<Button
android:id="#+id/btn_calc"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="calc_button" />
<Button
android:id="#+id/btn_clear"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginLeft="10dp"
android:text="clear_button" />
</LinearLayout>
</LinearLayout>
public class MyActivity extends Activity {
private EditText potsize;
private EditText tocall;
private EditText bepercent;
private Button clear;
private Button calc;
private TextView potoddscalc;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
clear = (Button)findViewById(R.id.btn_clear);
calc = (Button)findViewById(R.id.btn_calc);
potsize = (EditText) findViewById(R.id.et_pot_size);
tocall = (EditText) findViewById(R.id.et_to_call);
bepercent = (EditText) findViewById(R.id.et_be_per);
potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
clear.setOnClickListener(new View.OnClickListener() {
public void onClick(View v)
{
clearOrReset();
}
});
}
private void clearOrReset(){
potsize.setText("");
tocall.setText("");
bepercent.setText("");
potoddscalc.setText("5");
}
}
declare all three objects of Edittext in the oncreate method and then try it
enter code here #Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button clear = (Button)findViewById(R.id.btn_clear);
EditText potsize = (EditText) findViewById(R.id.et_pot_size);
EditText tocall = (EditText) findViewById(R.id.et_to_call);
EditText bepercent = (EditText) findViewById(R.id.et_be_per);
TextView potoddscalc = (TextView)findViewById(R.id.tv_pot_odds_calc);
public void onClick(View v)
{
swiech(v.getid())
{
case R.id.btn_clear:
potsize.setText("");
tocall.setText("");
bepercent.setText("");
potoddscalc.setText("5");
}
});
}

Categories

Resources