Android programming help - android

I'm working on an app where i have textview's in one layout and a button that sends you to a second layout with Edittext's. Every edittext is for an textview. How can i replace text in a textview with the text in edittext with a button in the second layout?

you mean like this ??
in the method onCreate() :
btn.setOnClickListener(this);
txtView = (TextView)findViewById(R.id.mytxtView);
editTxt = (EditText) findViewById(R.id.myeditText);
and then , ovverride the onClick method like this :
#Override
public void onClick(View v ) {
txtView.setText(editText.getText());
}

textview textview = (textview)findViewById(R.layout.nameoftextview);
edittext edittext = (edittext)findViewById(R.layout.nameofedittext);
textview.settext(edittext.text());

First of all, you will have to pass the edittext value to the first activity through intent.
Eg:
Intent i = new Intent(this, FirstActivity.class);
i.putExtra("edittext_value", edittext.getText().toString());
startActivity(i);
Then inside your first activity, you will have to fetch this data as:
String value;
Bundle extras = this.getIntent().getExtras();
if (extras != null) {
value = extras.getString("edittext_value");
textview.setText(value);
}
Hope this may help you.

From what I understand is that you want your second activity (let's call it Activity2) to pass text back to the first one (Activity1). To do that, you have to (some code comes from :
Change the way you open Activity2 to
Intent EditIntent = new Intent(this, Activity2.class);
//Other stuff you may want to do with intent
startActivityForResult(EditIntent , 0);
Add override to you Activity1
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
if (data.hasExtra("myText")) {
//get your data with data.getExtras().getString("myText")
}
}
}
Change what button on your Activity2 does
{
Intent returnData= new Intent();
returnData.putExtra("myText", /*Text from your EditText*/);
if (getParent() == null) { //This part was taken from StackOverflow by Ilya Taranov
setResult(Activity.RESULT_OK, returnData);
} else {
getParent().setResult(Activity.RESULT_OK, returnData);
}
finish();
}
This should return text from EditText from Activity2 to Activity1. Code was not tested

create a variable for the textview to access it like
Textview txt = (Textview) finviewByid........;
implement the following code on button click listener
txt.setText(edittext.getText().toString());

Related

Polygon Onclick Openwindow

I want make application on Android with multiple polygon, and when i click one polygon it show something like open window and show detail of the polygon. Is there any solution to make function like that?
Yes you can use ImageButtons for polygon images and handle their click events by opening new activity or dialog for showing information about the polygons.
You will handle the click events like this:
ImageButton ib = findViewById(R.id.polygon1);
ib.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
//show dialog or new activity with details
Intent i=new Intent();
int polygon_id = id_of_polygon;
i.putExtra("id", polygon_id);
i.startActivity(this,DetailsActivity.class);
finish();
}
});
You can get this value in details activity like this:
int id;
if (savedInstanceState == null) {
Bundle extras = getIntent().getExtras();
if(extras == null) {
id= 0;
} else {
id= extras.getInt("id");
}
}
Then in this activity you can show details about the polygon.
All this code will be added to the onCreate() methods of the activities.

How to check if a button is pressed in another activity?

In my first Activity I made that you can open a pop-up window (it's another activity), where you can put some data in and it needs to be sent back to the original Activity. Since I will need to put the data I got into a ListView, I think I need to check if the button is pressend and in the onClick add to the ListView. How to do that?
Here's the code of the pop-up Activity:
public class AddCoin extends Activity {
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
final Intent intentAdd = new Intent(this, PortfolioActivity.class);
super.onCreate(savedInstanceState);
setContentView(R.layout.add_coin_window);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8), (int)(height*0.4));
Button add_coin = findViewById(R.id.add_toport_button);
add_coin.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
EditText inputNameText = (EditText) findViewById(R.id.text_input_name);
EditText inputPriceText = (EditText) findViewById(R.id.text_input_price);
EditText inputAmmountText = (EditText) findViewById(R.id.text_input_ammount);
String inputName = inputNameText.getText().toString();
String inputPrice = inputPriceText.getText().toString();
String inputAmmount = inputAmmountText.getText().toString();
intentAdd.putExtra("Name", inputName);
intentAdd.putExtra("Price", inputPrice);
intentAdd.putExtra("Ammount", inputAmmount);
finish();
}
});
}
}
Proper way is calling activity with startActivityForResult().
But if you can not do this you can use EventBus
You can achieve this nature using startActivityForResult() method of Activity class.
Refer this link startActivityForResult
You have to use startActivityForResult(), put data into an Intent on called activity and then get results back using onActivityResult(int requestCode, int resultCode, Intent data) on the calling activity

Android Studio onClick button to display ImageView in another activity

I have setup 2 activities - one and two .
For activity 1, I have a EditText and a button. When user open the app, it will show Activity One(just like the screenshot) to prompt user to key in a number. For example, 1 or 2.
What I am trying to do is that: I want to display a ImageView in activity 2 when user key in a number in EditTextfollow by a click on the button in activity 1.
If user key in 1, it will display 1.png in activity 2
If user key in 2, it will display 2.png in activity 2
If user key in 3, it will display 3.png in activity 2
etc...
the image will get from drawable folder.
Please refer to this screenshot
[![enter image description here][1]][1]
I can pass the integer value through Intent from activity 1 to 2 but I can't do it for ImageView. so that means the if else loop i have already done just that the ImageView cant display.
get_image.setBackgroundResource(R.drawable.1); // here i can only key in 01 ( it will get image from Drawable folder 1.png). i cant put int value into it.
Or i shouldn't use get_image.setBackgroundResource?? Anyone can help? I stuck here for 1 day...
thanks in advance!
please check screenshot -> http://i.stack.imgur.com/53vjy.jpg
You said that you can pass integer value from activity 1 to 2 so just use that to find your image.
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
if(1 == yourValue) {
//set 01.png
} else {...}
I may missing somethings because i can't understand when you said that "but i cant do it for imageview".
EDIT: after your addtional code.
So you must map your integer value with your resource file name. You could not put your integer value to get_image.setBackgroundResource(R.drawable.id).
I think in your situation you should use an array just store id of resource you need int[] drawableIds = {R.drawable.01, R.drawable.02} in your Activity2
and then use like this get_image.setBackgroundResource(drawableIds[yourIntegerValue-1]) (ofcourse you should take care array out of index when you use this method).
Try below code for your solution,
For Activity One write below code to redirect on activity 2
Intent intent = new Intent(Activity1.this, Activity2.class);
intent.putExtra("SelectedNumber", editText.getEditableText.toString());
startActivity(intent);
Now In Activity 2 write below code in onCreate method
int selectedNumber = 1;
if(getIntent().getExtras() != null)
{
selectedNumber = getIntent().getExtras().getInt("SelectedNumber");
}
switch(selectedNumber)
{
case 1: // set your 01.png Image
break;
case 2: // set your 02.png Image
break;
// And so
}
Try this way might helps you.
String mDrawableName = "1"; //editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
then your Activity2 use,
setImageResource(resID);
(or)
setImageDrawable(getResources().getDrawable(resID));
Finally,
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
String mDrawableName = editText.getText().toString();
int resID = getResources().getIdentifier(mDrawableName , "drawable", getPackageName());
Intent ii=new Intent(Activity.this, Activity1.class);
ii.putExtra("resId", resID);
startActivity(ii);
}
});
Activity2
public class Activity2 extends Activity
{
private ImageView img;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.intent);
img = (ImageView)findViewById(R.id.img);
Intent iin= getIntent();
Bundle b = iin.getExtras();
if(b!=null)
{
int drawableId =(int) b.get("redId");
img.setImageResource(drawableId);
}
}
}
you can pass resource value in extras. try this way.
EditText edit = (EditText) findViewById(R.id.yourEdittext);
Button btn = (Button) findViewById(R.id.yourButton);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
Intent i = new Intent(ActivityOne.this, ActivityTwo.class);
if(edit.getText().toString().trim().equals("1")){
i.putExtra("image", R.drawable.01);
startActivity(i);
}else if(edit.getText().toString().trim().equals("2")){
i.putExtra("image", R.drawable.02);
startActivity(i);
}else{
Toast.makeText(getApplicationContext(), "Please Enter Valid Value.",
Toast.LENGTH_SHORT).show();
}
}
});
and in your ActivityTwo.java
ImageView imageView = (ImageView)findViewById(R.id.yourImageViewId);
imageView.setImageResource(getIntent().getIntExtra("image",0));
Happy Coding.
In activity 2's on create
get_image= (ImageView)findViewById(R.id.imageView);
then use switch case for values from intent then
switch(intentValues){
case 1:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.1));
break;
case 2:
get_images.setImageDrawable(getResources().getDrawable(R.drawable.2));
break;
}

How can I calculate the values entered in edittext and transfer from one activity to another?

I am devoloping a roof calculater. i have three edittext and calculate button in the first activity. In the second activity i have a textview that shows the result.
EditText editText1Değer1Kod;
EditText editText2Değer2Kod;
EditText editText3Değer3Kod;
Button hesaplaKod;
I want to take these three value then calculate with formula then show the result in second activity as a textview.
How can i calculate (sum or add this three value) then show in the second activity?
Use in first Activity
hesaplaKod.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
int value1= Integer.parseInt(EditText01.getText().toString());
int value2= Integer.parseInt(EditText01.getText().toString());
int value3= Integer.parseInt(EditText01.getText().toString());
int add = value1+value2+value3;
Intent intent = new Intent(FirstActivity.this,SecondActivity.class);
intent.putExtra("AddValue", add);
startActivity(intent);
}
});
And in onCreate of Second Activity get the value from this
int addvalue = getIntent().getExtras().getInt("AddValue");
enter code herejust pass calculate the number and pass the using intent to other activity
in First Activity :
Intent intent=new Intent(IntentsDemo2.this,Activity2.class);
intent.putExtra("result", result);
startActivity(intent);
in Second Activity :
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main2);
Intent sender=getIntent();
int result =sender.getExtras().getInt("result");
}

How to pass value through Intent

HI all,
this is my class A, where on button click , i m sending a int variable to class B
Intent bgIntent = new Intent(Background.this, MainScreen.class);
bgIntent.putExtra("background", bgColor);
startActivity(bgIntent);
and on class B
Intent bgIntent = getIntent();
bgGlobal = bgIntent.getIntExtra("background",-1 );
if(bgGlobal == 0)
{
DetailsTextView.setBackgroundResource(R.color.a0);
}
else
if(bgGlobal == 1)
{
DetailsTextView.setBackgroundResource(R.color.a1);
}
But the problem is i am getting a blank view.My view is not coming up with textview.
is this proper to set background
"DetailsTextView.setBackgroundResource"???
If you want to change the color of a View use http://developer.android.com/reference/android/view/View.html#setBackgroundColor(int)
for example:
DetailsTextView.setBackgroundColor(getResources().getColor(R.color.txt_green));
Anyway, it's not clear if you want to change the screen's background or the textview's background.
Also
if(bgGlobal == 0){...} else ...
is wrong. You should do something of the like
if(bgGlobal != -1)
{
[Use intent to read color]
}else{
[set default color]
}
If you see a blank view it's possibly due to a wrong XML layout.
Edit: To retrieve the extra
getIntent().getExtras().getInt("background",-1);

Categories

Resources