I have MainActivity which has got a button that opens Activity 2. Everything from Activity 2 should be calculated and be run but the user should rest at MainActivity? How do I do this?
I found a solution where I dont run the "setContentView()" but then my app crashes.
You want to change the view of application but does not want the user to change activity.
Use two different layout in xml file with id :
<?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="match_parent">
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/first_view">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Button1"
android:id="#+id/button"
android:background="#358a32" />
</LinearLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:id="#+id/second_view"
android:visibility="gone">
</LinearLayout>
<LinearLayout>
Then onclick of button :
public class MainActivity extends Activity {
boolean firstViewOff = false;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Button button = (Button) findViewById(R.id.button1);
LinearLayout first_view = (LinearLayout) findViewById(R.id.first_view);
LinearLayout second_view = (LinearLayout) findViewById(R.id.second_view);
button.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
firstViewOff = true;
first_view.setVisibility(View.GONE);
second_view.setVisibility(View.VISIBLE);
}
});
}
#override
public void onBackPressed(){
super.onBackPressed();
if(firstViewOff){
second_view.setVisibility(View.GONE);
first_view.setVisibility(View.VISIBLE);
firstViewOff = false;
}
}
}
Why i attached backpressed :
Because when user backpress then it will just directly show firstview without closing,
Related
I am trying to set the text of a Check Box from an Edit Text but it's not working.
I've created an Edit Text with a Button that when clicked creates a Check Box and gets the text for the Check Box from the Edit Text. The app crahes when I click on the Button.
XML
<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_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/rootContainer"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:gravity="center"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:id="#+id/new_task"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:hint="New Task"
android:inputType="textCapSentences" />
<Button
android:id="#+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:text="Create" />
</LinearLayout>
</ScrollView>
JAVA
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = (Button) findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
LinearLayout linearLayout = findViewById(R.id.rootContainer);
EditText newTask = findViewById(R.id.text);
CheckBox checkBox = new CheckBox(MainActivity.this);
String task = newTask.getText().toString();
newTask.setText(task);
checkBox.setText(task);
// Add Checkbox to LinearLayout
if (linearLayout != null) {
linearLayout.addView(checkBox);
}
}
});
}
id of EditText in your layout is android:id="#+id/new_task"
whereas in Activity you using
EditText newTask = findViewById(R.id.text);
So you should use same id in Activity as in xml
Remove below line from onClick event
LinearLayout linearLayout = findViewById(R.id.rootContainer);
and add that line in onCreate() method.
Because linearLayout is your root container.
you don't need to find every time.
And same for the button.
Also there is issue related id.
You are finding text id but you mentation new_task in xml.
So below line should be EditText newTask = findViewById(R.id.new_task);
I have one activity with two layouts one is main layout and the other layout name is layout2. Both layout files contain one button each. it works good if i press button on main layout it take me to layout 2 but the problem is when i click button on layout 2 to take me back to main layout i does not work.
MainActivity
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.Button;
public class MainActivity extends AppCompatActivity {
Button button1,button2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button1=(Button)findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
}
});
LayoutInflater inflater=this.getLayoutInflater();
View view=inflater.inflate(R.layout.layout2,null);
button2=(Button) view.findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
}
});
}
}
These are the layout files to display
Mainlayout
<?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/activity_main"
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="com.example.lidiawood.myapplication.MainActivity">
<Button
android:text="Go to layout 2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="#+id/textView"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginTop="52dp"
android:id="#+id/button1" />
</RelativeLayout>
Layout 2
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:text="go to main layout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="14dp"
android:layout_marginStart="14dp"
android:layout_marginTop="40dp"
android:id="#+id/button2" />
</RelativeLayout>
Can any one please help me how can i get display for the mian layout from the componnent of layout 2
The onClick event listener is attached to the button in the inflated view, not to the button in the activity view. It doesn't matter that you're using the same layout, it's a different button.
This situation should never happen, because you, generally speaking, need different fragments or activities, when changing the layout. Changing the content view this way is not a good idea. If you still what to use your code, then you need the following:
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
onContentViewOneInflated();
}
private void onContentViewOneInflated() {
Button button1 = (Button) findViewById(R.id.button1);
button1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.layout2);
onContentViewTwoInflated();
}
}
}
private void onContentViewTwoInflated() {
Button button2 = (Button) findViewById(R.id.button2);
button2.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
setContentView(R.layout.activity_main);
onContentViewOneInflated();
}
}
}
I haven't tried the code. If you have any issues, comment.
I want to change images in my popupwindow. I am trying to change ImageView image source which is in test.xml while my contentView is activity_main. When I try to just call imageView.setImageResource(android.R.drawable.ic_menu_help); it gives me nullpointerexception I realized i have to use inflate method, but when I use it it doesn't do what I want.I know there is a lot of questions regarding this, but none of them really helped me.
This is 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/relative"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.android.popupwindow.MainActivity">
<Button
android:text="Test"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/button"
android:layout_centerVertical="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
test.xml:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#android:color/holo_blue_dark"
android:id="#+id/test1"
>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/linear1">
<ImageView
android:layout_width="75dp"
android:layout_height="75dp"
android:src="#android:drawable/btn_star_big_on"
android:id="#+id/imageView1"
android:layout_weight="1"
/>
</LinearLayout>
</RelativeLayout>
my main.java:
public class MainActivity extends AppCompatActivity {
private PopupWindow popupWindow;
private RelativeLayout relativeLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button test = (Button) findViewById(R.id.button);
relativeLayout = (RelativeLayout) findViewById(R.id.relative);
final View view1 = getLayoutInflater().inflate(R.layout.test, null);
ImageView imageView = (ImageView) view1.findViewById(R.id.imageView1);
imageView.setImageResource(android.R.drawable.ic_menu_help); //Image source is not changed in my popupWindow,
// but it is changed when I call relativeLayout.addView(view1);
test.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
//relativeLayout.addView(view1); // <-- this does the trick, but its not what i want
View container = getLayoutInflater().inflate(R.layout.test, null);
popupWindow = new PopupWindow(container, android.app.ActionBar.LayoutParams.MATCH_PARENT, android.app.ActionBar.LayoutParams.WRAP_CONTENT, true);
popupWindow.showAtLocation(relativeLayout, Gravity.BOTTOM, 0, 0);
container.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View view, MotionEvent motionEvent) {
popupWindow.dismiss();
return true;
}
});
}
});
}
}
You're inflating two separate views. Obviously what you do in one will have no impact on the other. You should set the popup view to your originally inflated view:
popupWindow = new PopupWindow(view1,
RelativeLayout.LayoutParams.MATCH_PARENT,
RelativeLayout.LayoutParams.WRAP_CONTENT,
true);
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);
}
I'm using eclipse to code my android app and I'm wondering how I can change the background image of my MainActivity when I click a button. I have img1.png and img2.png. The background is currently set on img1.png with the following xml code:
<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:background="#drawable/img1"
tools:context=".MainActivity" >
<Button
android:id="#+id/btn1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginLeft="46dp"
android:layout_marginTop="55dp"
android:background="#android:color/transparent"
android:text="" />
</RelativeLayout>
I'm just unsure what java code I would use to change the background image on btn1 click.
This code can be used to set background image programmattically
RelativeLayout layout =(RelativeLayout)findViewById(R.id.relativelayout);
layout.setBackgroundResource(R.drawable.img1);
This could be a solution.
In your layout
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="#+id/lyt_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/img1"
tools:context=".MainActivity" >
In your Activity
RelativeLayout layout;
public void onCreate(Bundle savedInstanceState){
super.onCreate(Bundle savedInstanceState);
setContentView(your_xml.xml);
layout = (RelativeLayout) findById(R.id.lyt_main);
button = (Button) findById(R.id.lyt_main);
button.setOnClickListener(new OnClickListener{
public void onClick(View v) {
layout.setBackgroundDrawable(your_image));
}
});
}
Add android ID to Your Layout:
<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:background="#drawable/img1"
android:id="#+id/rlayout"
tools:context=".MainActivity" >
...
</RelativeLayout>
Now In Your MainActivity.java :
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = (Button) findViewById(R.id.btn1);
btn.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
RelativeLayout rlayot = (RelativeLayout) findViewById(R.id.rlayout);
rlayot.setBackgroundResource(R.drawable.img2);
}
});
}