Change the size of a fragment with a button - java

I'm trying to change the weight of "MapView" and "InfoContent" when i click on a button inside the mapview fragment. The mapView Fragment is defined in the PlaceholderFragment2 Java Class.
The first click should change the weight to a new value, while the second click should restore the old value.
XMl:
<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"
tools:context=".MainActivity"
android:orientation="vertical"
>
<FrameLayout
android:id="#+id/TitleBar"
android:layout_width="fill_parent"
android:layout_height="50dp"
>
</FrameLayout>
<FrameLayout
android:id="#+id/MapView"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="1">
</FrameLayout>
<FrameLayout
android:id="#+id/InfoTitleBar"
android:layout_width="fill_parent"
android:layout_height="170dp"
>
</FrameLayout>
<FrameLayout
android:id="#+id/InfoContent"
android:layout_width="fill_parent"
android:layout_height="0dp"
android:layout_weight="0"
>
</FrameLayout></LinearLayout>
MainActivity.Java
public class MainActivity extends Activity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
PlaceholderFragment frg=new PlaceholderFragment();
PlaceholderFragment2 frg2=new PlaceholderFragment2();
PlaceholderFragment3 frg3=new PlaceholderFragment3();
PlaceholderFragment4 frg4=new PlaceholderFragment4();
FragmentManager manager=getFragmentManager();
FragmentTransaction transaction=manager.beginTransaction();
transaction.add(R.id.TitleBar, frg, "Frag_Top_tag");
transaction.add(R.id.MapView, frg2, "Frag_Middle_tag");
transaction.add(R.id.InfoTitleBar, frg3, "Frag_Bottom_tag");
transaction.add(R.id.InfoContent, frg4, "Frag_Content_tag");
transaction.commit();
}
And PlaceHolderFragment2.java
public static class PlaceholderFragment2 extends Fragment {
public PlaceholderFragment2() {
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View rootView = inflater.inflate(R.layout.mapview, container,
false);
final Button button =
(Button) rootView.findViewById(R.id.zoomMap);
button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
buttonClicked(v);
}
});
return rootView;
}

I solved it, I put the fragment in linear layout, then in the main activity I created the object, when I click just change the size of the weight of the linear layout, cos' I do not need nor by the fragment transaction.
MainActivity
private Button button;
private LinearLayout layoutListView;
private LinearLayout.LayoutParams lpListView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_container);
button = (Button) findViewById(R.id.button);
layoutListView = (LinearLayout) findViewById(R.id.listTreeView);
lpListView = new LinearLayout.LayoutParams(0, LayoutParams.MATCH_PARENT);
lpListView.weight = 20;
button.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View v) {
if (lpListView.weight == 0) {
lpListView.weight = 20;
} else {
lpListView.weight = 0;
}
layoutListView.setLayoutParams(lpListView);
}
});
}
}
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="100"
android:orientation="horizontal"
android:weightSum="100" >
<LinearLayout
android:id="#+id/listTreeView"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="20"
android:orientation="horizontal" >
<fragment
android:id="#+id/fraglistView"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.ListTreeView" />
</LinearLayout>
<LinearLayout
android:id="#+id/fragbutton"
android:layout_width="0dp"
android:layout_height="match_parent"
android:layout_weight="80"
android:background="#F000" >
<fragment
android:id="#+id/fraglistRight"
android:layout_width="match_parent"
android:layout_height="match_parent"
class="com.example.ListViewRight" />
</LinearLayout>
</LinearLayout>

Related

How to set the margin if using viewbinding

I want to set the margin on the LinearLayout in activity_main.xml, but because I'm using viewbinding, the margin doesn't show any result. Is there a way to have the margins on the LinearLayout set even when using viewbinding?
build.gradle
android {
viewBinding {
enabled = true
}
}
acivity_main.xml
<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"
android:layout_margin="20dp"
android:id="#+id/idLayout"
android:orientation="vertical">
<TextView
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="#+id/idText1"
android:textSize="20sp"
android:text="HELLO WORLD"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="click"
android:layout_margin="100dp"
android:id="#+id/idButton1"/>
</LinearLayout>
MainActivity.java
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
View view = binding.getRoot();
setContentView(view);
binding.idText1.setText("This is view binding");
binding.idButton1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Toast.makeText(getApplicationContext(), "binding", Toast.LENGTH_SHORT).show();
}
});
}

How should I correctly put some Fragment to FrameLayout in mainActivity?

I'm trying to put fragment in MainActivity's FrameLayout by clicking the button in MainActivity. It almost works, but the content of MainActivity's FrameLayout doesn't get replaced by the Fragment content, but added to it. So the question is: how to make it correct?
My MainActivity class
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button btn = findViewById(R.id.pushMeBtn);
btn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
FragmentManager f_m = getSupportFragmentManager();
FragmentTransaction f_t = f_m.beginTransaction();
f_t.replace(R.id.container_layout, new Fragment1()).addToBackStack(null).commit();
}
});
}
}
My activity_main.xml
<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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<FrameLayout
android:id="#+id/container_layout"
android:layout_width="match_parent"
android:layout_height="match_parent">
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/pushMeBtn"
android:text="push me"
android:layout_gravity="center"/>
</FrameLayout>
</RelativeLayout>
My Fragment1 class
public class Fragment1 extends Fragment {
#Nullable
#Override
public View onCreateView(#NonNull LayoutInflater inflater, #Nullable ViewGroup container, #Nullable Bundle savedInstanceState) {
return inflater.inflate(R.layout.fragment1, container,false);
}
}
My fragment.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent" android:layout_height="match_parent"
android:background="#color/colorPrimary">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="339dp"
android:gravity="center"
android:text="fragment1"
android:textSize="25sp"/>
</RelativeLayout>

Issues with Android Soft Keyboard and panAdjust

I am trying to achieve similar effect to the one found in Todoits application where you can click on FAB and it shows a floating popup for adding new task.
This is how it looks:
The closest to it I managed to achieve is this:
It is nearly there, but I want the popup to be about 10dp above the keyboard and scroll the recycle viewer to the last item but I can't get it there :/ Currently, the popup is centred.
I have tried before adding the popup to the bottom of the screen and setting android:windowSoftInputMode="adjustResize" but this was moving my bottom navigation above the keyboard too. Setting windowsSoftInputMode to panAdjust was cutting away the toolbar and few items from the list.
Have you got any suggestions about how to achieve similar effect to the one in Todoist? Maybe with a DialogFragment?
Below is my current code (navigation bar implementation is still not completed):
MainActivity:
public class MainActivity extends AppCompatActivity {
private WorkoutsListFragment workoutsListFragment;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar myToolbar = findViewById(R.id.my_toolbar);
setSupportActionBar(myToolbar);
ActionBar actionBar = getSupportActionBar();
this.workoutsListFragment = new WorkoutsListFragment();
getSupportFragmentManager()
.beginTransaction()
.add(R.id.main_layout, workoutsListFragment)
.commit();
}
}
WorkoutListFragment:
public class WorkoutsListFragment extends Fragment
implements View.OnClickListener, WorkoutListViewHolderInterface {
private RecyclerView recyclerView;
private WorkoutListRecycleViewAdapter recycleViewAdapter;
private AddBoxView addBoxView;
private FloatingActionButton actionButton;
private InputMethodManager imm;
private WorkoutViewModel workoutViewModel;
private boolean addBoxStatus;
public WorkoutsListFragment() {}
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.addBoxStatus = false;
//Irrelevant code removed (Dagger 2 DI)
this.workoutViewModel =
ViewModelProviders.of(this, workoutViewModelFactory).get(WorkoutViewModel.class);
workoutViewModel.init(7);
workoutViewModel.getWorkout().observe(this, w -> this.updateWorkoutsList(w, false));
this.imm = (InputMethodManager) getContext().getSystemService(Context.INPUT_METHOD_SERVICE);
}
#Override
public View onCreateView(
#NonNull LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_workouts_list, container, false);
this.recyclerView = view.findViewById(R.id.workoutsList);
// this.recyclerView.setHasFixedSize(true);
RecyclerView.LayoutManager recyclerViewLayoutManager = new LinearLayoutManager(getContext());
this.recyclerView.setLayoutManager(recyclerViewLayoutManager);
this.recycleViewAdapter = new WorkoutListRecycleViewAdapter(this);
this.recyclerView.setAdapter(recycleViewAdapter);
this.actionButton = view.findViewById(R.id.floating_action_add_workout);
this.actionButton.setOnClickListener(this);
this.addBoxView = new AddBoxView(Objects.requireNonNull(getContext()));
FrameLayout.LayoutParams addBoxLayoutParams =
new FrameLayout.LayoutParams(
ViewGroup.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.WRAP_CONTENT,
Gravity.CENTER);
this.addBoxView.setLayoutParams(addBoxLayoutParams);
this.addBoxView.getButton().setOnClickListener(this);
return view;
}
#Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.floating_action_add_workout:
this.addBoxView((FrameLayout) v.getParent(), v);
break;
case R.id.overlay_layout:
removeAddBoxView((FrameLayout) v);
break;
case R.id.button:
this.addNewWorkout(this.addBoxView.getInputText());
break;
}
}
//Irrelevant code removed....
private void addBoxView(FrameLayout viewGroup, View v) {
this.addBoxStatus = true;
viewGroup.setClickable(true);
viewGroup.setOnClickListener(this);
viewGroup.setBackgroundColor(
getResources().getColor(R.color.cardview_shadow_start_color, null));
viewGroup.removeView(v);
viewGroup.addView(this.addBoxView);
this.addBoxView.requestInputFocus();
this.imm.showSoftInput(this.addBoxView.getInput(), InputMethodManager.SHOW_IMPLICIT);
}
private void removeAddBoxView(FrameLayout viewGroup) {
viewGroup.setClickable(false);
this.imm.hideSoftInputFromWindow(this.addBoxView.getInput().getWindowToken(), 0);
viewGroup.removeView(this.addBoxView);
viewGroup.setBackgroundColor(getResources().getColor(R.color.cardview_shadow_end_color, null));
viewGroup.addView(this.actionButton);
this.addBoxStatus = false;
}
}
Fragment_workouts_list layout:
<?xml version="1.0" encoding="utf-8"?>
<FrameLayout 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"
android:background="#e1e1e1"
android:padding="0dp"
tools:context=".Activities.MainActivity.Fragments.Workouts.WorkoutsListFragment"
tools:layout_editor_absoluteY="81dp">
<android.support.v7.widget.RecyclerView
android:id="#+id/workoutsList"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clipChildren="false"
android:clipToPadding="false"
android:elevation="0dp"
android:scrollbars="vertical"
app:layout_constraintBottom_toTopOf="#+id/add_box_view"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">
</android.support.v7.widget.RecyclerView>
<FrameLayout
android:id="#+id/overlay_layout"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:clickable="false"
android:outlineProvider="bounds">
<android.support.design.widget.FloatingActionButton
android:id="#+id/floating_action_add_workout"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="bottom|end"
android:layout_marginBottom="40dp"
android:layout_marginEnd="40dp"
android:clickable="true"
android:src="#android:color/holo_blue_dark" />
</FrameLayout>
</FrameLayout>
MainActivity Layout
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout
android:id="#+id/root_view"
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_height="match_parent"
android:layout_width="match_parent"
android:orientation="vertical">
<android.support.v7.widget.Toolbar
android:id="#+id/my_toolbar"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
android:elevation="4dp"
android:theme="#style/AppTheme.AppBar" />
<FrameLayout
android:id="#+id/main_layout"
android:layout_width="0dp"
android:layout_height="0dp"
app:layout_constraintBottom_toTopOf="#+id/navigation"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="#+id/my_toolbar">
</FrameLayout>
<android.support.design.widget.BottomNavigationView xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/navigation"
android:layout_width="match_parent"
android:layout_height="56dp"
android:background="#color/colorPrimary"
android:foregroundGravity="bottom"
android:visibility="visible"
app:itemTextColor="#color/textColorWhite"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:menu="#menu/my_navigation_items" />
</android.support.constraint.ConstraintLayout>

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?

access the imageview from activity to fragment in android

I have to develop one android application.
Here i have using fragment concept.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="vertical"
android:id="#+id/_listDetails"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal"
android:id="#+id/layout"
>
<ImageView
android:id="#+id/pos"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/neg"
android:src="#drawable/a_pos_off" />
<ImageView
android:id="#+id/neg"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:layout_toLeftOf="#+id/imageView5"
android:src="#drawable/a_neg_off" />
</RelativeLayout>
<LinearLayout
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_below="#+id/layout"
android:orientation="horizontal" >
<fragment
android:id="#+id/activity_main"
android:name="com.xmlparsing.MainActivity"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
<fragment
android:id="#+id/fragment2"
android:name="com.xmlparsing.SubCate"
android:layout_width="0px"
android:layout_height="match_parent"
android:layout_weight="1" />
</LinearLayout>
</RelativeLayout>
Here i have used Two fragments..They are ., MainActivity.java and SubCate.java
public class AndroidListFragmentActivity extends Activity {
/** Called when the activity is first created. */
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.fragment);
}}
Here The positive image is there in fragment.xml file...Here if i have to click these positive image which means the have to increase the text size for fullcontent of second fragement (SubCate.java )...
How can i do ??? pls give me solution for these ...
This is my Second Fragment (SubCate.java) code:
#TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class SubCate extends Fragment
{
int count = 0, i, j;
WebSettings webSettings;
WebView fullcontent;
ImageView positive,negative;
AndroidListFragmentActivity _listDetailsFrag;
View activityView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
}
#Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.subcate,
container, false);
fullcontent = (WebView) view.findViewById(R.id.fullcontent);
Bundle bundle = getArguments();
return view;
}
public void updateDetail(String articlefullcontent) {
_FullContent = articlefullcontent.substring(1);
fullcontent.setWebChromeClient(new WebChromeClient());
webSettings = fullcontent.getSettings();
fullcontent.getSettings();
fullcontent.loadDataWithBaseURL(null, _FullContent, "text/html", "utf-8", null);
positive = ((ImageView )activityView.findViewById(R.id.pos));
positive.setOnClickListener(new OnClickListener() {
public void onClick(View v) {
count++;
if (count == -1)
{
webSettings.setTextSize(WebSettings.TextSize.SMALLER);
positive.setImageResource(R.drawable.a_pos_off);
negative.setImageResource(R.drawable.a_neg_on);
}
else
if (count == 0)
{
webSettings.setTextSize(WebSettings.TextSize.NORMAL);
positive.setImageResource(R.drawable.a_pos_off);
negative.setImageResource(R.drawable.a_neg_off);
}
else
if (count >= 1)
{ count=1;
webSettings.setTextSize(WebSettings.TextSize.LARGER);
positive.setImageResource(R.drawable.a_pos_on);
negative.setImageResource(R.drawable.a_neg_off);
}
}
});
}
}
I have to run the app and click that imageview means the fullcontent text is not increase the value...How can i do ??? please help me and provide me some solution ???
Your fragment can not find the corresponding ImageView. Edit to the next and check:
positive = (ImageView )getActivity().findViewById(R.id.pos);//use getActivity() Method

Categories