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");
}
});
}
Related
I have been reading about how to dynamically add an EditText field to my Linear Layout, every-time a user clicks a TextView (which has an onClick Listener attached).
I have had some mild success - I know that the EditText field is being created because when the button is clicked, all other elements move up as if something is being added to the screen.
My problem is that the EditText aren't visible and I haven't a clue why that is, so any help would be appreciated.
The the app isn't crashing so nothing to add in terms of the StackTrace and Log, as far as the app is concerned, everything is being created.
<?xml version="1.0" encoding="utf-8"?>
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fillViewport="true">
<LinearLayout 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/linearLayout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#color/facebookBlue"
android:orientation="vertical"
android:weightSum="1"
tools:context="com.test.practise.AddTeamMembers">
<android.support.design.widget.TextInputEditText
android:id="#+id/tv_teamNames"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="#string/teamName"
android:textColor="#android:color/background_light"
android:textColorLink="#android:color/background_light"
android:textSize="30sp"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.26"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/textView3"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.47"
android:gravity="center"
android:text="Enter Player Names Below!"
android:textColor="#android:color/background_light"
android:textSize="24sp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name1"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerVertical="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name2"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_centerHorizontal="true"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.16"
android:orientation="vertical"
android:weightSum="1">
<EditText
android:id="#+id/et_team_name3"
android:layout_width="232dp"
android:layout_height="37dp"
android:layout_gravity="center"
android:background="#android:color/background_light"
android:ems="10"
android:hint="Team Name"
android:imeOptions="actionDone"
android:inputType="text"
android:paddingLeft="70dp"
android:singleLine="true"
tools:layout_editor_absoluteX="76dp"
tools:layout_editor_absoluteY="188dp" />
</LinearLayout>
<LinearLayout
android:id="#+id/editTextGroupLayout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
>
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_weight="0.07"
android:orientation="vertical"
android:weightSum="1">
<TextView
android:id="#+id/tv_add_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center"
android:text="+ Add Name"
android:textColor="#android:color/background_light"
android:textSize="16dp" />
</LinearLayout>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:weightSum="1">
<Button
android:id="#+id/button"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:background="#color/facebookBlue"
android:gravity="center"
android:text="Ready to join!"
android:textColor="#android:color/background_light" />
</LinearLayout>
</LinearLayout>
</ScrollView>
Below is the AddTeamMembers Class that calls the above XML
public class AddTeamMembers extends Fragment implements
View.OnClickListener
{
private SharedPreferences pref;
private TextView tv_teamNames, tv_add_name;
private LinearLayout mLayout;
//The below method must be overridden in order to implement a fragment -
this changes the lifecycle method
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.activity_add_team_members,
container, false);
initViews(view);
return view;
}
#Override
public void onViewCreated(View view, Bundle savedInstanceState) {
// 0, Sets Shared pref mode to private
pref = getActivity().getPreferences(0);
tv_teamNames.setText(pref.getString(Constants.Team_Name, ""));
}
private void initViews(View view) {
tv_teamNames = (TextView) view.findViewById(R.id.tv_teamNames);
tv_add_name = (TextView) view.findViewById(R.id.tv_add_name);
mLayout = (LinearLayout) view.findViewById(R.id.editTextGroupLayout);
tv_add_name.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.tv_add_name:
createEditTextView();
break;
}
}
#TargetApi(Build.VERSION_CODES.M)
public void createEditTextView() {
try{
//dynamically create new EditText when user clicks to add another
name
//target user using API 22 and above (lollipop and above)
EditText editTextView = null;
if (android.os.Build.VERSION.SDK_INT >=
android.os.Build.VERSION_CODES.M) {
editTextView = new EditText(getContext());
}else{
Toast.makeText(getActivity(), "App is not supported on this
device",
Toast.LENGTH_LONG).show();
}
editTextView.setGravity(Gravity.CENTER);
LinearLayout.LayoutParams params = new
LinearLayout.LayoutParams(LinearLayout.LayoutParams.WRAP_CONTENT,
LinearLayout.LayoutParams.WRAP_CONTENT, 1);
editTextView.setLayoutParams(params);
mLayout.addView(editTextView);
}catch(Exception e){
Log.d(TAG, "Failed to create new edit text");
}
}
}
[![Before Button is clicked][1]][1]
[![I have clicked 2 add 3 EditText here to make it obvious whats happening]
[2]][2]
[1]: https://i.stack.imgur.com/DGoPd.png
[2]: https://i.stack.imgur.com/3LWcY.png
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!!!
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
My app keeps crashing and I have no idea what is causing this. The app builds, but keeps crashing in the emulator.
I tried to change MainActivity, but still it's not working.
Main Activity.java
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
Button btLogout;
EditText etName, etPSN;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_login);
etName = (EditText) findViewById(R.id.etName);
etPSN = (EditText) findViewById(R.id.etPSN);
btLogout = (Button) findViewById(R.id.btLogout);
btLogout.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId()){
case R.id.btLogout:
break;
default : break;
}
}
}
activity_main.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="Name"/>
<EditText
android:id="#+id/etName"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="40dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="20sp"
android:text="PSN"/>
<EditText
android:id="#+id/etPSN"
android:layout_width="match_parent"
android:layout_marginBottom="20dp"
android:layout_height="40dp" />
<Button
android:id="#+id/btLogout"
android:text="Logout"
android:layout_width="match_parent"
android:layout_height="50dp"
android:textAlignment="gravity"
android:gravity="center|center_vertical"
android:textSize="20dp"
android:textStyle="bold"
android:capitalize="characters" />
</LinearLayout>
you must using
setContentView(R.layout.activity_main);
Instead
setContentView(R.layout.activity_login);
Hi When I change the Background color of EditText of two EditText they are looking like both merging.
My layout code goes like this `
<EditText
android:id="#+id/editText4"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text1"
android:singleLine="true" />
<EditText
android:id="#+id/editText1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text2"
android:singleLine="true" >
</EditText>
<AutoCompleteTextView android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:singleLine="true"
android:imeOptions="actionNext"/>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text3"
android:singleLine="true" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content" >
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text4"
android:singleLine="true" />
</LinearLayout>
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button" />
`
and My Activity code goes like this
public class MainActivity extends Activity {
EditText editText1, editText2, editText3, editText4;
Button button;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
init();
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
editText1.setBackgroundColor(getResources().getColor(android.R.color.primary_text_dark_nodisable));
editText3.setBackgroundColor(getResources().getColor(android.R.color.darker_gray));
}
});
}
private void init() {
editText1 = (EditText) findViewById(R.id.editText1);
editText2 = (EditText) findViewById(R.id.editText2);
editText3 = (EditText) findViewById(R.id.editText4);
button = (Button) findViewById(R.id.button1);
}
}
Refer the attached Screen shots below
Layout before clicking Button
Layout after clicking Button
As you can see here using EditText.setBackgroundColor(any color) will color your outline too. To keep the outline aspect i would recommend to include the edittext in a table row and use margin. Try this in your xml and see the result:
<TableRow
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/black"
android:orientation="vertical" >
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#android:color/holo_orange_dark"
android:layout_margin="1dip"
android:text="cosmincalistru" />
</TableRow>
You have set the same id for both the edittext. #+id/editText2
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text3"
android:singleLine="true" />
<EditText
android:id="#+id/editText2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:ems="10"
android:hint="Text4"
android:singleLine="true" />