set Inflate layout to smaller - java

i have some problems with my layout, this is the capture of my layout inflater
the layout is to big and the button are in wrong place, i don't use a title but there is a black title background in there
i just want to make it smaller like piano tiles layout
can anyone help me to fix this?
this is my layout.xml data that will show inflater layout in menu.java
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#drawable/backgroundblankwhite"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:background="#drawable/textinflateexit"
android:singleLine="true" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquityes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#drawable/buttonquitno" />
</LinearLayout>
</LinearLayout>
and this is my menu.java that have a button to display my inflate layout
public class menu extends Activity {
private Context context = this;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.menu);
Button exit = (Button) findViewById(R.id.exit);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(context);
openDialog.setContentView(R.layout.inflatequitapp);
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
// if this button is clicked, close
// current activity
menu.this.finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
#Override
public void onBackPressed() {
// do nothing.
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
}

I have change a bit variable to check at my end..please do change variables,class to match at your end...
MainActivity.java
///---////
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
Button exit = (Button) findViewById(R.id.but);
exit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View arg0) {
final Dialog openDialog = new Dialog(MainActivity.this);
openDialog.setContentView(R.layout.main);
openDialog.setTitle("Confirm Exit");
TextView dialogTextContent = (TextView)openDialog.findViewById(R.id.exitimage);
Button dialogExitButton = (Button)openDialog.findViewById(R.id.buttonexityes);
dialogExitButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
finish();
}
});
Button dialogCloseButton = (Button)openDialog.findViewById(R.id.buttonexitno);
dialogCloseButton.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View v) {
openDialog.dismiss();
}
});
openDialog.show();
}
});
}
Dialog xml file..
<?xml version='1.0' encoding='utf-8'?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:background="#ffffff"
android:gravity="center|center_horizontal|center_vertical"
android:orientation="vertical"
android:padding="10sp" >
<TextView
android:id="#+id/exitimage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:background="#ffffff"
android:singleLine="true"
android:text="Are You Sure!!" >
<requestFocus />
</TextView>
<LinearLayout
android:id="#+id/LinearLayout1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:gravity="center" >
<Button
android:id="#+id/buttonexityes"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="Yes" />
<Button
android:id="#+id/buttonexitno"
android:layout_width="120dp"
android:layout_height="wrap_content"
android:background="#ffffff"
android:text="No" />
</LinearLayout>
</LinearLayout>
output:

you have to used below code for dialog
private void forgotPasswordDialog() {
LayoutInflater layoutInflater = LayoutInflater.from(this);
final View dialogView = layoutInflater.inflate(R.layout.dialog_forgot_password, null);
AlertDialog.Builder builder = new AlertDialog.Builder(this, R.style.PauseDialog);
final AlertDialog dialog = builder.create();
dialog.setView(dialogView);
dialog.show();
final EditText edtUserNameDialog = (EditText) dialogView.findViewById(R.id.edtUserNameDialog);
edtUserNameDialog.setText(edtUserName.getText());
final Button btnSubmit = (Button) dialogView.findViewById(R.id.btnSubmit);
btnSubmit.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (!FieldsValidator.validateUsername(edtUserNameDialog)) {
//forgotPasswordDialog();
} else if (!checkDMSID()) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
} else {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
forgotPassword(edtUserNameDialog.getText().toString());
dialog.dismiss();
}
}
});
final Button btnCancel = (Button) dialogView.findViewById(R.id.btnCancel);
btnCancel.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
KeyboardUtils.hideKeyboard(edtUserNameDialog);
dialog.dismiss();
}
});
}

Related

TextView doesn't setText inside include layout

I have a custom dialog in which I include another layout. In this layout there are 3 buttons and a TextView none of these is null. If I click the buttons they work correctly. But the TextView doesn't show any text. The text should appears when I click another button. That's how it should works
Custom dialog layout:
<LinearLayout
............
.....
<androidx.cardview.widget.CardView
android:id="#+id/result_card"
android:layout_width="match_parent"
android:layout_height="wrap_content"
card_view:cardElevation="2dp"
card_view:cardMaxElevation="2dp"
android:layout_marginStart="16dp"
android:layout_marginEnd="16dp"
android:layout_marginBottom="8dp"
android:visibility="gone"
app:cardCornerRadius="8dp">
<include android:id="#+id/result_layout" layout="#layout/result_block" />
</androidx.cardview.widget.CardView>
....
....
</LinearLayout>
The result_block layout
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:padding="16dp"
android:background="#color/colorPrimary"
android:orientation="vertical">
<TextView
android:id="#+id/result_txt"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textSize="17dp"
android:text=""
android:textColor="#color/colorWhite"
android:fontFamily="sans-serif-medium"
android:padding="8dp"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:gravity="end"
android:orientation="horizontal">
<ImageButton
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_copy_24px_outlined"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginEnd="16dp"
android:tint="#color/colorWhite"
android:src="#drawable/ic_share_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
<ImageButton
android:id="#+id/btn3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:tint="#color/colorWhite"
android:src="#drawable/ic_volume_up_24px_outlined"
android:background="?attr/selectableItemBackgroundBorderless"
/>
</LinearLayout>
</LinearLayout>
And now the dialog
private void showDiag() {
final View dialogView = View.inflate(getActivity(), R.layout.custom_dialog_layout,null);
final View resultLayout = View.inflate(getActivity(), R.layout.result_block,null);
final Dialog dialog = new Dialog(getActivity(), R.style.MyAlertDialogStyle);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.setContentView(dialogView);
final TextView resultTxt = resultLayout.findViewById(R.id.result_txt);
final ImageButton btn1 = resultLayout.findViewById(R.id.btn1);
final CardView resultCardView = dialog.findViewById(R.id.result_card_view);
final TextInputEditText editText = dialog.findViewById(R.id.text_edit);
final ImageButton clearText = dialog.findViewById(R.id.clear_text);
MaterialButton resultConfirm = dialog.findViewById(R.id.result_confirm);
ImageButton btn2 = dialogView.findViewById(R.id.copy_btn);
ImageButton btn3 = dialogView.findViewById(R.id.share_btn);
resultConfirm.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(!editText.getText().toString().equals("")) {
String theWord = editText.getText().toString();
String result = Utils.getSecondWord(theWord);
// result it shows me the correct string with no errors or something else
resultTxt.setText(result); // doesn't work. Not set the text
insertWord(theWord, result);
resultCardView.setVisibility(View.VISIBLE);
resultCardView.setVisibility(View.VISIBLE);
} else {
Toast.makeText(getActivity(), "Error", Toast.LENGTH_SHORT).show();
}
}
});
bt1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
// Share btn
bt2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
btn3.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Log.i(TAG, "result: " + resultTxt.getText().toString());
}
});
dialog.setOnShowListener(new DialogInterface.OnShowListener() {
#Override
public void onShow(DialogInterface dialogInterface) {
Utils.revealShow(dialogView, true, null, resultConfirm);
}
});
dialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
#Override
public boolean onKey(DialogInterface dialogInterface, int i, KeyEvent keyEvent) {
if (i == KeyEvent.KEYCODE_BACK){
Utils.revealShow(dialogView, false, dialog, resultConfirm);
return true;
}
return false;
}
});
dialog.getWindow().setBackgroundDrawable(new ColorDrawable(android.graphics.Color.TRANSPARENT));
dialog.show();
}
Everything inside the dialog works correctly but the TextView not. Even if I try to write something else like resultTxt.setText("Hello there"); the text not appears. Any suggestions?
Please you remove the android:text=""in TextView because textview is get default text is empty or you write anything in TextView like android:text="abc"

Android Progress Bar not working

I have made a progress bar in Android but it is not at all working. What am I doing wrong? I have just started to learn android. Below is my code.
MainActivity.java-
public class MainActivity extends AppCompatActivity {
ProgressBar progressBar;
int mporgress=0;
EditText time;
private Handler mHandler = new Handler();
private static final int PROGRESS = 0x1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
public void Startbuttononclick(View view){
Button startbutton=(Button) findViewById(R.id.button);
startbutton.setOnClickListener(new View.OnClickListener(){
#Override
public void onClick(View v) {
mporgress = Integer.parseInt(time.getText().toString());
progressBar.setProgress(mporgress);
new Thread(new Runnable() {
public void run() {
while (mporgress < 100) {
// Update the progress bar
mHandler.post(new Runnable() {
public void run() {
progressBar.setProgress(mporgress);
mporgress = doWork();
try{
Thread.sleep(1000);
}catch (InterruptedException e){}
}
});
}
}
}).start();
}
});
}
public void doprogress(View view) {
}
public int doWork(){
mporgress++;
return mporgress;
}
}
Activity_main.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:id="#+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.android.progressbar.MainActivity">
<EditText
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="Enter the time:"
android:inputType="number"
android:id="#+id/editText" />
<ProgressBar
style="?android:attr/progressBarStyleLarge"
android:layout_width="500dp"
android:layout_height="200dp"
android:id="#+id/progressBar4"
android:layout_gravity="center"
android:scrollbarSize="500dp" />
<Button
android:text="START"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_gravity="center"
android:onClick="Startbuttononclick" />
</LinearLayout>
Check out this image below-
- https://i.stack.imgur.com/2TBIb.png
Note- Enter the time in this image is an EditText with a hint and not a TextView.
It is appearing but not working.
I have done these changes so far.-
https://i.stack.imgur.com/UujpB.png
But still the progress bar is still continuously rotating.
Seems you forgot to initialize EditText -time and ProgressBar. Init it inside onCreate:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
progressBar =(ProgressBar) findViewById(R.id.progressBar4);
time = (EditText) findViewById(R.id.editText); //here
}
Also inside button click the bar:
progressBar = new ProgressBar(this);
mporgress = Integer.parseInt(time.getText().toString());
................
.............

TEXTVIEW substantiation location

I am trying to make an android code that provide a different work for each button.
so when the user press on Button1 a Textview field provides " you pressed on Button1"
my question is about the correct place that I can instantiate the Textview ONLY ONCE as the only working place is inside the Button1 Function.
Java Code for a single button:
public void Button1(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 1");
}
total java code
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Snackbar.make(view, "Replace with your own action", Snackbar.LENGTH_LONG)
.setAction("Action", null).show();
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.menu_main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
//noinspection SimplifiableIfStatement
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void Button1(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 1");
}
public void Button2(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 2");
}
public void Button3(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 3");
}
public void Button4(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 4");
}
public void Button5(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 5");
}
public void Button6(View view) {
final TextView textView=(TextView)findViewById(R.id.textView2);
textView.setText("You clicked the button 6");
}
}
Total XML Code
<?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/RelativeLayoutID"
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="com.example.sherifsaleh.xmllayout.MainActivity"
tools:showIn="#layout/activity_main"
>
<Button
android:id="#+id/button3"
android:onClick="Button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="3"
android:layout_above="#+id/button6"
android:layout_alignLeft="#+id/button6"
android:layout_alignStart="#+id/button6" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Button6"
android:text="6"
android:id="#+id/button6"
android:layout_below="#+id/button2"
android:layout_alignLeft="#+id/button9"
android:layout_alignStart="#+id/button9" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="9"
android:id="#+id/button9"
android:onClick="Button9"
android:layout_alignTop="#+id/button8"
android:layout_toRightOf="#+id/button8"
android:layout_toEndOf="#+id/button8" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="8"
android:id="#+id/button8"
android:layout_below="#+id/button5"
android:layout_toRightOf="#+id/button4"
android:layout_toEndOf="#+id/button4"
android:onClick="Button8"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="2"
android:onClick="Button2"
android:id="#+id/button2"
android:layout_above="#+id/button5"
android:layout_toRightOf="#+id/button1"
android:layout_toEndOf="#+id/button1" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="Button5"
android:text="5"
android:id="#+id/button5"
android:layout_below="#+id/button1"
android:layout_toRightOf="#+id/button1"
android:layout_toEndOf="#+id/button1" />
<Button
android:id="#+id/button1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="1"
android:onClick="Button1"
android:layout_above="#+id/button4"
android:layout_alignLeft="#+id/button4"
android:layout_alignStart="#+id/button4" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="4"
android:id="#+id/button4"
android:layout_above="#+id/button7"
android:layout_alignLeft="#+id/button7"
android:layout_alignStart="#+id/button7" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="7"
android:id="#+id/button7"
android:layout_alignParentBottom="true"
android:layout_alignLeft="#+id/textView2"
android:layout_alignStart="#+id/textView2"
android:layout_marginBottom="90dp" />
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textIsSelectable="true"
android:text="New Text"
android:id="#+id/textView2"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="80dp"
android:textSize="50dp"/>
</RelativeLayout>
Try like this.
public class MainActivity extends AppCompatActivity {
private TextView textView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
textView = (TextView)findViewById(R.id.textView2);
....
}
And your Button click method as
public void Button1(View view) {
textView.setText("You clicked the button 1");
}
1)First try to initialize your references in onCreate which loads once.So references to your views will initialize only once
eg-
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnOne = (Button) findViewById(R.id.btnOne);
btnTwo = (Button) findViewById(R.id.btnTwo);
textView = (TextView) findViewById(R.id.textView);
btnOne.setOnClickListener(this);
btnTwo.setOnClickListener(this);
}
2)Instead of calling seperate methods for each button apply click listeners on buttons as above mentioned
and implement View.OnClickListener and then
3)Override method
#Override
public void onClick(View v) {
Button b = (Button)v;
String buttonText = b.getText().toString();
textView.setText("You clicked on " + buttonText);
}
So the code in onClick is repeating in your case instead of writing each time seperately write it once and get text or you can set Tag of button and get it(in case when you want to set another value to textview instead of button text) and set to textview as i did in onClick.
It will reduce your efforts and increase your code reusability and redability too..!!!!!
Not super sure what your question is. If your asking if it's acceptable to do your findViewById in the button listener, it's acceptable but not optional. Every time you press the button the code runs. It's better to do it once in onCreate
Simply declare it at the start of your Activity
public class MainActivity extends AppCompatActivity {
private TextView textView2;
.....
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_layout);
textView2 = (TextView) findViewById(R.id.textView2);}
.....
public void Button6(View view) {
textView2.setText("bla");
}
Is that what you asked for ?

Cannot change TextView between two activities?

I'm attempting to change a TextView between two activities. The problem is, when I change the text in the other activity from main activity, it works fine, the screen goes to the other activity and the text has been changed. However I have another button which takes me to the other activity from Main, but the newly updated text has disappeared when I press it. Any help?
MyActivity.java
public class MyActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_my);
final EditText et = (EditText) findViewById(R.id.editText1);
Button b = (Button) findViewById(R.id.button1);
b.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(MyActivity.this, Second.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.my, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onClickGoToActivity(View view) {
setContentView(R.layout.second);
}
}
Second.java
public class Second extends Activity{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
TextView tv = (TextView) findViewById(R.id.textView1);
tv.setText(getIntent().getExtras().getString("thetext"));
}
}
activity_my.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:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
android:paddingBottom="#dimen/activity_vertical_margin"
tools:context=".MyActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText1"
android:layout_centerHorizontal="true"
android:hint="Change me"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button1"
android:layout_centerHorizontal="true"
android:layout_marginTop="50dp"
android:text="Add text in other activity"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/go_to_activity_button"
android:text="Go to activity"
android:layout_centerHorizontal="true"
android:layout_marginTop="200dp"
android:onClick="onClickGoToActivity"/>
</RelativeLayout>
second.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/textView1"
android:textSize="30sp"/>
</LinearLayout>
AndroidManifest.xml
Second class activity:
I presume the problem lies here:
public void onClickGoToActivity(View view) {
setContentView(R.layout.second);
}
where the newly changed TextView is being reset back to the original layout (where the TextView is empty with no text). I'm not sure how to fix this as I'm new to Android.
Thank you.
changing content view of activity is a bad idea to start another activity. Simply you just do the same thing which is
public void onClickGoToActivity(View view) {
Intent intent = new Intent(MyActivity.this, Second.class);
intent.putExtra("thetext", et.getText().toString());
startActivity(intent);
}

How to add a reset button to clear numbers on Edittext field

I want to add a button to clear the numbers on textfield by tapping on the button.
(Reset my EditText back to an empty "space" after a button has pressed that would have completed an activity with input from the EditText field.)
Cheers! Thanks!
ActivityMain XML file!
<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:background="#drawable/back"
android:orientation="vertical"
tools:context="com.miuapps.unitconverter.Centimetres_Metres" >
<TextView
android:id="#+id/textView1" //first textview
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Enter a Value(cm) :"
android:layout_gravity="center"
android:gravity="center"
android:textSize="30dp"/>
<EditText
android:id="#+id/editText1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:inputType="number" />
<Button
android:id="#+id/button1"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Convert" />
<TextView
android:id="#+id/tvDisplay1" //output textview
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="In Metres: 0"
android:textSize="30dp" />
</LinearLayout>
double counter, counter2 = 100;
double x;
Button conv;
TextView dis1, dis2;
EditText ent1;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
counter = 0;
conv = (Button)findViewById(R.id.button1);
dis1 = (TextView)findViewById(R.id.tvDisplay1);
//dis2 = (TextView)findViewById(R.id.tvDisplay2);
ent1 = (EditText)findViewById(R.id.editText1);
conv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// TODO Auto-generated method stub
x = new Integer(ent1.getText().toString());
counter = x/100;
//counter2++;
dis1.setText("In Metres: " +counter);
//dis2.setText("In Centimetres: " +counter2);
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
How about this:
ent1.setText("");
just put it at the end of your onClick Method
Edit:
conv.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// here you get the text from your editText
x = new Integer(ent1.getText().toString());
// do something with the text
counter = x/100;
// set result in another TextView
dis1.setText("In Metres: " +counter);
// work is done, so the editText can be cleared
ent1.setText("");
}
});
Edit 2 (reset button):
resetButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
// reset only by another button
ent1.setText("");
}
});
Simply add a button in your layout.
<Button
android:id="#+id/buttonReset"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Reset" />
Find that button in your activity:
Button mButtonReset = (Button) findViewById(R.id.buttonReset);
mButtonReset.setOnClickListener(new OnClickListener(){
#Override
public void onClick(View view){
ent1.setText("");
}
});
First Create Design,
Activity_main.xml like below,
<EditText
android:id="#+id/txt_1"
android:layout_width="300dp"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:ems="10"
android:hint="Enter Name"
android:inputType="textPersonName"
android:minHeight="48dp" />
<Button
android:id="#+id/btn_ok"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:text="Button" />
<Button
android:id="#+id/btn_cln"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:text="Clear" />
**Then go to MainActivity.java edit code like below, **
**
public class MainActivity extends AppCompatActivity {
Button button,button2;
EditText editText;
String name;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.btn_ok);
editText = findViewById(R.id.txt_1);
button2 = findViewById(R.id.btn_cln);
//create text add button
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
name = editText.getText().toString();
}
});
//clean,reset text button
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
editText.setText("");
}
});
}
}
**

Categories

Resources