Change Layouts Dynamically in 1 Activity - java

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.

Related

Why SetText not work in OnClick() function

I just know how to use Android Studio Code Yesterday. And I got a problem when I need to Change the text when clicking a button.
But when I text, Its don't work.
Here is my code:
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
Button bubble = (Button) findViewById(R.id.start_bubble);
bubble.setOnClickListener(this);// calling onClick() method
Button predict = (Button) bubbleView.findViewById(R.id.predict);
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
case R.id.predict:
View bubbleView = getLayoutInflater().inflate(R.layout.bubble_view, null);
TextView predict_text = (TextView) bubbleView.findViewById(R.id.predict_text);
predict_text.setText("Hi"); // <--- It don't work :(
default:
break;
}
}
}
[EDIT] []
Add some .XML file
Here is my 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:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
</LinearLayout>
and here is my bubble_view.xml, its just for a
<?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="wrap_content">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/predict"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<TextView
android:id="#+id/predict_text"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:paddingStart="16dp"
android:paddingTop="16dp"
android:paddingEnd="16dp"
android:textColor="#AA000000"
android:textSize="21sp" />
</LinearLayout>
</ScrollView>
Do you have any suggested for me ?
I'm not sure why you inflate the "bubble_view.xml" layout in the activity class. But as your question, there are two main methods to make the button clickable. There is a good explanation in your first comment which is done by Mike M. Once you inflate a layout, it will create a new instance.
Fist answer, Assuming you want everything inside the activity.
public class MainActivity extends AppCompatActivity implements View.OnClickListener {
private Button bubble;
private Button predict;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
initUIViews() // Initialze UI Views
initUIActions() // Initialize Ui Actions
}
private void initUiViews() {
Button bubble = (Button) findViewById(R.id.start_bubble);
Button predict = (Button) bubbleView.findViewById(R.id.predict);
}
private void initUIActions() {
bubble.setOnClickListener(this);// calling onClick() method
predict.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()) {
case R.id.start_bubble:
startService(new Intent(getApplicationContext(), SimpleService.class));
break;
case R.id.predict:
predict_text.setText("Hi");
break;
default:
break;
}
}
}
and restructure your XML layout as follow. There are few ways to restructure these layouts, I'll write the easiest way, but note that this is not the optimal way.
<?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:gravity="center"
android:orientation="vertical"
tools:context="com.siddharthks.sampleapp.MainActivity">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Project KHKT"
android:textColor="#AA000000"
android:textSize="21sp" />
<Button
android:id="#+id/start_bubble"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:text="Paste and Predict"
android:textSize="18sp" />
<!-- include bubble layout file -->
<include layout="#layout/bubble_view.xml" />
</LinearLayout>
Other than the include tag you can add the whole code inside to the Activity layout.
The second answer, Assuming you want activity and Service with a bubble view.
If you are looking for a bubble view, You have to create a Bubble service.
Check this answer: Bubble Example
Official Doc: Android Bubble
Try This it will add Your bubble view in your parent and you can perform any action on that particular Layout from main Layout.
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
LinearLayout parent = findViewById(R.id.activity_main); //parent layout.
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view,parent,false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
TextView predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
predict_text.setText("Hi"); // <--- It don't work :(
}
});
}
}
Add break to the each case statement in the switch.
public class MainActivity extends AppCompatActivity implements View.OnClickListener{
TextView predict_text;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button start = (Button) findViewById(R.id.start_bubble);
LinearLayout parent = findViewById(R.id.activity_main);
View childView = LayoutInflater.from(getApplicationContext()).inflate(R.layout.bubble_view, parent, false);
parent.addView(childView);
Button predict = (Button) childView.findViewById(R.id.predict);
predict_text = (TextView) childView.findViewById(R.id.predict_text);
predict.setOnClickListener(this);
start.setOnClickListener(this);
}
#Override
public void onClick(View view) {
switch (view.getId()){
case R.id.predict:
predict_text.setText("Hi");
break;
}
}
}

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>

How to Create a button with another button from a popupwindow

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?

Common Progress bar in every activity Android

I have modify the solution. I am able to get the progress bar but the progress bar never gets hide
Here is class to create a progress bar with relative layout
public class ProgressBarHandler {
private ProgressBar mProgressBar;
private Context mContext;
private View _baseView;
private View _hideView;
public ProgressBarHandler(Context context,View baseView, View hideView) {
mContext = context;
_baseView = baseView;
_hideView = hideView;
ViewGroup layout = (ViewGroup) _baseView;//((Activity) context).findViewById(android.R.id.content).getRootView();
mProgressBar = new ProgressBar(context, null, android.R.attr.progressBarStyleLarge);
mProgressBar.setIndeterminate(true);
RelativeLayout.LayoutParams params = new
RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT, RelativeLayout.LayoutParams.MATCH_PARENT);
RelativeLayout rl = new RelativeLayout(context);
rl.setGravity(Gravity.CENTER);
rl.addView(mProgressBar);
layout.addView(rl, params);
hide();
}
public void show() {
mProgressBar.setVisibility(View.VISIBLE);
_hideView.setVisibility(View.INVISIBLE);
}
public void hide() {
mProgressBar.setVisibility(View.INVISIBLE);
_hideView.setVisibility(View.VISIBLE);
}
}
This is my xml file
<FrameLayout xmlns:tools="http://schemas.android.com/tools"
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:descendantFocusability="beforeDescendants"
android:focusableInTouchMode="true"
android:orientation="vertical"
android:id="#+id/profile_activity">
<android.support.design.widget.CoordinatorLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:id="#+id/layout_to_hide"
android:layout_height="match_parent"
android:fitsSystemWindows="true">
<android.support.design.widget.AppBarLayout
android:id="#+id/app_bar_layout"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:fitsSystemWindows="true"
android:theme="#style/ThemeOverlay.AppCompat.Dark.ActionBar">
<android.support.design.widget.CollapsingToolbarLayout
android:id="#+id/collapsing_toolbar"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:fitsSystemWindows="true"
app:expandedTitleTextAppearance="#android:color/transparent"
app:contentScrim="?attr/colorPrimary"
app:layout_scrollFlags="scroll|exitUntilCollapsed">
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="#mipmap/wallpaper"
android:orientation="vertical">
<com.deerwalk.androidcommon.RoundedImageView
android:id="#+id/profile_image"
android:layout_width="100dp"
app:civ_border_color="#EEEEEE"
app:civ_border_width="4dp"
app:civ_shadow="true"
app:civ_shadow_radius="10"
app:civ_shadow_color="#8BC34A"
android:layout_height="100dp"
android:layout_alignWithParentIfMissing="false"
android:layout_centerInParent="true"
android:layout_gravity="center"
android:layout_margin="20dp"
android:src="#drawable/avatar" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:layout_below="#+id/profile_image"
android:orientation="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/txt_name"
android:layout_gravity="center_horizontal"
android:text=""
android:textColor="#color/white" />
<TextView
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:id="#+id/txt_dob"
android:layout_gravity="center_horizontal"
android:text=""
android:layout_marginTop="5dp"
android:textColor="#color/white" />
</LinearLayout>
</RelativeLayout>
</LinearLayout>
</android.support.design.widget.CollapsingToolbarLayout>
</android.support.design.widget.AppBarLayout>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerView"
android:layout_width="match_parent"
android:layout_height="match_parent"
app:layout_behavior="#string/appbar_scrolling_view_behavior">
</android.support.v7.widget.RecyclerView>
</android.support.design.widget.CoordinatorLayout>
</FrameLayout>
Here is the activity class which call the progress bar
public class ProfileActivity extends AppCompatActivity {
FrameLayout profile_root_layout;
CoordinatorLayout layot_to_hide;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
profile_root_layout = (FrameLayout) findViewById(R.id.profile_activity);
layot_to_hide = (CoordinatorLayout) findViewById(R.id.layout_to_hide);
new userProfileTask().execute();
}
public class userProfileTask extends AsyncTask<Void, Void, Boolean> {
#Override
protected void onPreExecute() {
new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).show();
super.onPreExecute();
}
#Override
protected Boolean doInBackground(Void... params) {
try {
Thread.sleep(2000);
} catch (InterruptedException e) {
e.printStackTrace();
}
return null;
}
#Override
protected void onPostExecute(Boolean aBoolean) {
new ProgressBarHandler(getBaseContext(),profile_root_layout,layot_to_hide).hide();
}
}
}
The progress bar never gets hides.Where I am doing mistake.
Make a separate class then make a static method with the parameter on it the parameter contain Activity ... call this method on ur activity and pass the Activity name in it
Its very simple, you can do in multiple ways.
1) You can define base activity and write your code for progress dialog and extends all activity from BaseActivity used Progressdialog.
2) You can have custom separate class and used in all activities.
etc
Here is good example you can modify your code like this example.
ProgressHud
Or use this too.
I would suggest create a dialog and call in your activity class like this
dialog = new Dialog(getActivity(), android.R.style.Theme_Black);
View views = LayoutInflater.from(getActivity()).inflate(R.layout.activity_dialog, null);
dialog.requestWindowFeature(Window.FEATURE_NO_TITLE);
dialog.getWindow().setBackgroundDrawableResource(R.color.feed_item_bg);
dialog.setContentView(views);
dialog.show();
and in your async task onPostExecute dismiss dialog
dialog.dismiss();
public static void getProgress(Activity activity,FrameLayout relativeLayout){
final ProgressBar pb;
int progressStatus = 0;
final Handler handler = new Handler();
pb = new ProgressBar(activity, null, android.R.attr.progressBarStyleHorizontal);
LayoutParams lp = new LayoutParams(550,LayoutParams.WRAP_CONTENT);
pb.setLayoutParams(lp);
LayoutParams params = (LayoutParams) pb.getLayoutParams();
pb.setLayoutParams(params);
pb.getProgressDrawable().setColorFilter(Color.BLUE, PorterDuff.Mode.SRC_IN);
relativeLayout.addView(pb);
new Thread(new Runnable() {
#Override
public void run() {
int progressStatus = 0;
while(progressStatus < 100){
progressStatus +=1;
try{
Thread.sleep(20);
}catch(InterruptedException e){
e.printStackTrace();
}
final int finalProgressStatus = progressStatus;
handler.post(new Runnable() {
#Override
public void run() {
pb.setProgress(finalProgressStatus);
}
});
}
}
}).start(); // Start the operation
}
You could use a parent Activity. This activity class extends AppCompatActivity . Other Activities will extend this Activity. For example the name of this activity is AbstractActivty.
public class AbstractActivty extends AppCompatActivity{
AlertDialog alertDialogProgressBar;
public void showLoadingProgressBar(String message, boolean cancalable) {
if (isActivityPaused)
return;
AlertDialog.Builder adb = new AlertDialog.Builder(this);
View view = getLayoutInflater().inflate(R.layout.alert_dialog_progressbar_layout,null);
TextView tv = (TextView) view.findViewById(R.id.idTextViewMessage);
tv.setText(message);
adb.setView(view);
alertDialogProgressBar = adb.create();
alertDialogProgressBar.setCancelable(cancalable);
alertDialogProgressBar.show();
}
public void closeProgressBar() {
if (alertDialogProgressBar == null) {
return;
}
alertDialogProgressBar.hide();
}
}
This class have a method which could be called by any other activity that extends
AbstractActivty
class MainActivty extends AbstractActivty{
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// when require to show progressbar
showLoadingProgressBar("Loading....",true)
// when required to hide call
closeProgressBar();
}
}
Note in Alert Dialog we have used a custom layout
"alert_dialog_progressbar_layout.xml"
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:padding="#dimen/text_padding_l">
<androidx.constraintlayout.widget.Guideline
android:id="#+id/guidelineVertical"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="vertical"
app:layout_constraintGuide_percent=".3"
/>
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintEnd_toStartOf="#+id/guidelineVertical"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintBottom_toBottomOf="parent"/>
<TextView
android:id="#+id/idTextViewMessage"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="Loading...."
android:textColor="#color/colorPrimary"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toEndOf="#+id/guidelineVertical"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>
Hope this will be of help . Thank you.

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