How to Create a button with another button from a popupwindow - java

I am trying to create an application which when a button from a popup window is clicked a new button will be created in another layout of the same activity.
Here is the code for the popupwindow.xml
<?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:id="#+id/start_goal"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/start_goal" />
</RelativeLayout>
Here is the code for the activitymain.xml
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.example.eugene.trackeruptest3.MainActivity"
android:background="#color/darkgrey"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="355dp">
<Button
android:id="#+id/setupgoal"
android:layout_width="350dp"
android:layout_height="wrap_content"
android:text="#string/setup_goal"
android:textSize="25sp" />
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#+id/buttonContainer">
</RelativeLayout>
</LinearLayout>
Here is the java code for the popupwindow
public class creategoal extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.popupwindow);
DisplayMetrics dm = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(dm);
int width = dm.widthPixels;
int height = dm.heightPixels;
getWindow().setLayout((int)(width*.8),(int)(height*.6));
}
}
And here is the java code for the MainActivity.java
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button setupgoal = (Button) findViewById(R.id.setupgoal);
setupgoal.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
startActivity(new Intent(MainActivity.this,creategoal.class));
}
});
}
}

Have you tried using visibility:gone on your XML and setting it with view.setVisibility(Visibility.VISIBLE); on your onClick method?

Related

layout inflater its not work i want in onlick functionality

I am not able to to understanding Layout Inflater its not work in correct way please help me one. Layout Infleter if i click that that Images it's not going text page in that i wrote hi hello
public class Add_viewActivity extends AppCompatActivity {
private ImageView cartbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_add_view);
cartbtn = findViewById(R.id.cartbtn);
cartbtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
LayoutInflater inflater = getLayoutInflater();
View view = inflater.inflate(R.layout.text_resource, null);
TextView textid = view.findViewById(R.id.textid);
}
});
}
}
<?xml version="1.0" encoding="utf-8"?>
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".Add_viewActivity">
<ImageView
android:id="#+id/cartbtn"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_alignParentRight="true"
android:background="#null"
android:src="#drawable/ic_cart_gray"
tools:layout_editor_absoluteX="137dp"
tools:layout_editor_absoluteY="274dp"
tools:ignore="MissingConstraints" />
</LinearLayout>
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="#+id/textid"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="hi hello"/>
</androidx.constraintlayout.widget.ConstraintLayout>
Layout inflator cannot be inflated in on parent's onCreate(). You can only do it in fragment and call it from the parent's onCreate ().

why my other view doesn't move up when the snackbar show after using coordinator layout?

so here is my xml file
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="Button"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent" />
</LinearLayout>
</android.support.design.widget.CoordinatorLayout>
and here is my java file:
public class MainActivity extends AppCompatActivity {
Button myButton;
Snackbar mySnackbar;
CoordinatorLayout myCoordinatorLayout;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
myButton = (Button) findViewById(R.id.button);
myCoordinatorLayout = (CoordinatorLayout) findViewById(R.id.myCoordinatorLayout);
myButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
mySnackbar = Snackbar.make(myCoordinatorLayout,"Test Snakcbar",BaseTransientBottomBar.LENGTH_INDEFINITE);
mySnackbar.setDuration(8000);
mySnackbar.setAction("OK", new View.OnClickListener() {
#Override
public void onClick(View v) {
mySnackbar.dismiss();
}
});
mySnackbar.show();
}
});
}
}
but my button still stick in the bottom and doesn't move up when the snackbar show. I want my button move up like in this video:
https://developer.android.com/images/training/snackbar/snackbar_button_move.mp4
what should I do ?
You don't need the LinearLayout. Also, you don't need to put constraints, since its coordinator layout and not ConstraintLayout.
Try this instead:
<?xml version="1.0" encoding="utf-8"?>
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/myCoordinatorLayout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:id="#+id/button"
android:layout_width="168dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:text="Button" />
</android.support.design.widget.CoordinatorLayout>

When I open a new activity from an intent (button) from my previous activity, the button remains on the new activity

I have an issue of a button remaining on the new activity, from which the button on the previous activity had launched.
Example: Activity A contains Button (B1). On clickevent of B1 takes you to Activity B. When B1 is clicked it successfully takes you to Activity B.
Extra Information: Activity A and Activity B are both WebViews and are on relative layout.
Issue: There is no Button on Activity B, but when I open my app on my phone, B1 appears.
Any help will be appreciated I am very new to coding!
This is Class A
WebView myBrowser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_drugs);
myBrowser = (WebView)findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/drugs.html");
myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
Button btndrugslaw = (Button) findViewById(R.id.drugslaw);
btndrugslaw.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intentdruglaw = new Intent(Drugs.this, DrugLaw.class);
startActivity(intentdruglaw);
}
});
}
This is Activity A
<ScrollView
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<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"
tools:context="kyr.com.knowyourrights.Drugs"
android:orientation="vertical"
>
<WebView
android:id="#+id/mybrowser"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
>
</WebView>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Take me to the law"
android:id="#+id/drugslaw"
android:layout_below="#id/mybrowser"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
</ScrollView>
This is Class B
WebView myBrowser;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_parking_fine);
myBrowser = (WebView)findViewById(R.id.mybrowser);
myBrowser.loadUrl("file:///android_asset/druglaw.html");
myBrowser.setScrollBarStyle(View.SCROLLBARS_INSIDE_OVERLAY);
}
This is Activity B
<ScrollView
android:layout_height="match_parent"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent">
<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"
tools:context="kyr.com.knowyourrights.DrugLaw"
android:orientation="vertical"
>
<WebView
android:id="#+id/mybrowser1"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
</WebView>
</RelativeLayout>
</ScrollView>

Change Layouts Dynamically in 1 Activity

I have the same request of this Post and I followed the FoamyGuy's Answer but I don't get the introLayout with the ImageView displaying.
I just want to show my introLayout first and then change it to my WebView.
here is my main.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">
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible"
>
<ImageView android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
/>
</LinearLayout>
in MainActivity.java
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
RelativeLayout introLayout = (RelativeLayout) findViewById(R.id.introLayout);
introLayout.setVisibility(View.GONE);
webview = (WebView)findViewById(R.id.browser);
webview.setVisibility(1);
}
Help?
I don't know if it would make any difference but you could try :
webview.setVisibility (View.VISIBLE);
Instead of :
webview.setVisibility (1);
PS: With View.GONE you make the View not visible and with View.VISIBLE you make the View visible :).
Just because you did the opposite thing in your MainActivity then you discribed.
Your introLayout is set View.GONE, change to
introLayout.setVisibility(View.VISIBLE);
You need to make some logic in your code, when you will hide this "#+id/introLayout" and then show your WebView.
Ok, here is how would i do that if you dont need that layout after few seconds:
ActivityMain
public class Splash extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.splash);
Thread timer = new Thread(){
public void run(){
try{
sleep(3000);
}catch(InterruptedException e){
e.printStackTrace();
}finally{
Intent startSpalsh = new Intent("com.yourpackagename.secondactivity");
startActivity(startSpalsh);
}
}
};
timer.start();
}
#Override
protected void onPause() {
// TODO Auto-generated method stub
super.onPause();
finish();
}
#Override
protected void onDestroy() {
// TODO Auto-generated method stub
super.onDestroy();
finish();
}
}
splash.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/introLayout"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:gravity="center"
android:background="#color/white"
android:visibility="visible">
<ImageView
android:layout_width="wrap_content"
android:contentDescription="splash screen"
android:id="#+id/splash"
android:src="#drawable/splash"
android:layout_height="wrap_content"
android:scaleType="centerInside"/>
</RelativeLayout>
SecondActivity
public class SecondActivity extends AppCompatActivity {
WebView webView;
TextView txtChk;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second);
webview = (WebView)findViewById(R.id.browser);
}
second.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent">
<WebView
android:id="#+id/browser"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible" />
</LinearLayout>
First, change de ubication of intro. Set it below of WebView in your layout. WebView and intro don't need to set tag visibility. So... In your onCreate() apply this:
new Handler().postDelayed(new Runnable() { #Override public void run() { introLayout.setVisibility(View.GONE); } }, 2000);
2000 = 2 seconds. Change this like you want.
This code will be later that your initialize WebView and IntroLayout.
Change the LinearLayout by RelativeLayout in your layout how principal container.

Java/Android change text inside layout from a fragment

I have a problem with my first application,this is also my first question,i have 2 activity and 2 layout
this is the MainActivit.java
public class MainActivity extends TabActivity {
TabHost tabHost;
public static int meth;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LoadAllVar();
UpdateHud(meth);
TabSwitcher();
}
public static int LoadAllVar(){
//Load all var from files (not added for now)
meth = 200;
return meth;
}
private void UpdateHud(int meth){
String methstring = Integer.toString(meth);
TextView textSell = (TextView)findViewById(R.id.textMethCount);
textSell.setText(methstring);
}
public void TabSwitcher() {
tabHost = getTabHost();
TabHost.TabSpec tab1spec = tabHost.newTabSpec("Tab1");
tab1spec.setIndicator("Cook");
Intent firstintent = new Intent(this, CookTab.class);
tab1spec.setContent(firstintent);
tabHost.addTab(tab1spec);
}
}
and this is the second Activity
public class CookTab extends Activity {
public static int meth;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.cooktab);
LoadVarFromCook();
CheckCookClick();
}
public void LoadVarFromCook (){
meth = LoadAllVar();
String methstring = Integer.toString(meth);
Toast.makeText(getApplicationContext(),methstring,Toast.LENGTH_SHORT).show();
}
public void CheckCookClick (){
Button buttoncook = (Button) findViewById(R.id.buttonCook);
buttoncook.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
meth = meth + 1;
Toast.makeText(getApplicationContext() , "+1" , Toast.LENGTH_SHORT).show();
//HERE I NEED TO UPDATE THE VALUE OF METH INSIDE THE ACTIVITY_MAIN.XML
}
});
}
}
this is the MainActivity layout
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content">
<TabHost
android:layout_width="fill_parent"
android:layout_height="match_parent"
android:id="#android:id/tabhost">
<TabWidget
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#android:id/tabs"></TabWidget>
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="#android:id/tabcontent"></FrameLayout>
</TabHost>
<RelativeLayout
android:layout_width="fill_parent"
android:layout_height="100dp"
android:layout_alignParentBottom="true"
android:id="#+id/layoutHUD">
<TextView
android:layout_width="50dp"
android:layout_height="40dp"
android:id="#+id/textMethCount"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_marginLeft="28dp"
android:layout_marginStart="28dp" />
</RelativeLayout>
</RelativeLayout>
and this is the second tab layout
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout
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="100dp"
android:layout_height="60dp"
android:id="#+id/textMeth"
android:layout_marginTop="250dp"
android:layout_marginLeft="145dp"
android:text="Meth"
android:gravity="center"
android:textSize="29dp"
android:textStyle="bold"/>
<Button
android:layout_width="100dp"
android:layout_height="60dp"
android:id="#+id/buttonCook"
android:layout_below="#+id/textMeth"
android:layout_alignLeft="#+id/textMeth"
android:layout_alignStart="#+id/textMeth"
android:text="Cook"/>
</RelativeLayout>
I need to change the text inside a TextView located inside the activity_main.xml when i press a button inside the cooktab.xml how can i do that? findViewById() dosen't work
P.S. for now i add only one tab ,i will add more tab but for now i think about the first one
Try using EventBus it allows you to call functions in other parts of your app. Alternatively use a standard java interface link
I resolve the problem following this tutorial http://mrbool.com/how-to-create-an-activity-android-with-a-tabhost-view/27990

Categories