I made a custom view which I called Game.java
public class Game extends View {
public Game(Context context, AttributeSet attrs) {
//here goes class
public void shot(){
}
//method I want to use sometime
}
here's part of my layout file for Activity game.xml:
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/root"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".GameActivity" >
<com.vladdrummer.textmaster.Game
android:id="#+id/game"
android:layout_width="wrap_content"
android:windowSoftInputMode="adjustResize"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<View
android:id="#+id/spareview"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_alignParentLeft="true"
android:layout_centerVertical="true" />
etc..
So, in Activity I simply do
setContentView(R.layout.game);
and I got my custom View class called Game.java as the part of the screen
But how do I address it? if I do :
Game game;
game =(Game) findViewById(R.id.game);
game.shot();
it crashes.
of course , I can do :
Game game=new Game(this)
setContentView(game);
and have access to game so it won't crash, but I need other elements on screen as well
please tell me how to do it right
You can have a RelativeLayout or LinearLayout in your xml and place it anywhere you want. You can have other views in xml also. This is one way
setContentView(R.layout.yourlayout); // infalte layout
RelativeLayout rl =(RelativeLayout) findViewById(R.id.relativeLayout);// initialize
Game game=new Game(this);
rl.addView(game); // ad view to container
But you have the custom view in your layout. You are missing constructors in GameView. Your method also should also work
Read Chapter 4 Creating user Interface by Retro Meier Professional Android Devleopment
Related
Im trying to find a method to freeze the activity state, but without success.
Im creating a game, and I want to add a pause button. When the user clicks, the activity should freeze with the components, and when click again, keep from where it was before. Is that possible to do that?
Please check the solution it can be helpful for u. As u want to freez the screen for sometime than u have to play with View Visibility.
For it u have to create the view like i have use the rlView in my below layout. Visible it when u want to freez the screen.And again when u want to remove it than set the visibility of view is gone..
We can manage the View visiblity like below:-
RelativeLayout rlView= (RelativeLayout) findViewById(R.id.rlView)
YOUR_BUTTON_CLICK.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(rlView.getVisibility()==View.VISIBLE)
{
rlView.setVisibility(View.GONE);
}
else {
rlView.setVisibility(View.VISIBLE);
}
}
});
}
Please check the 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">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
>
HERE IS YOUR WHOLE LAYOUT VIEW
</RelativeLayout>
<RelativeLayout
android:id="#+id/rlView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone"
android:background="#android:color/transparent"
>
</RelativeLayout>
</RelativeLayout>
Im trying to let my textview run from left to right in an infinite way.But somehow it looks really weird I even cant explain it in words so I decided to record and upload it.
Here is the video : https://www.youtube.com/watch?v=gj3FF7fEutk&feature=youtu.be
So thats how I wrote my Textview :
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:singleLine="true"
android:ellipsize="marquee"
android:marqueeRepeatLimit ="marquee_forever"
android:focusable="true"
android:focusableInTouchMode="true"
android:scrollHorizontally="true"
android:textAppearance="?android:attr/textAppearanceMedium"
android:text="Title"
android:layout_marginTop="20dp"
android:textColor="#color/white"
android:id="#+id/textView_title_full"
android:layout_centerHorizontal="true" />
And this TextView is in a RelativeLayout declared like this :
<RelativeLayout
android:layout_width="wrap_content"
android:layout_height="170dp"
android:id="#+id/BELOW_FULL"
android:layout_gravity="bottom"
android:background="#drawable/color_below_player_full"
android:layout_below="#+id/viewpager_albumart_full"
android:layout_alignParentBottom="true">
where the RelativeLayout is also in a Relativelayout like this :
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:weightSum="1"
android:background="#0E0E0E">
I of course used the txt_song_title.setSelected(true); method.
How it looks like what I want to achieve :https://www.youtube.com/watch?v=o4uAA4pik68
Thank you all very much !
EDIT:
All parts where I do sth with my TextView :
public void init_textviews()
{
...
txt_song_title = (TextView)findViewById(R.id.textView_title_full);
txt_song_title.setSelected(true);
}
//init_textviews() is called in my onCreate method
txt_song_title.setText("Some Long text"); // called in onCreate after init_textviews()
And lastly:
public static void update_GUI_full (Context context) //Called when it is a button is clicked
{
...
txt_song_title.setText("longTexthere");
...
}
Working side by side with #Ahmet Kazaman we've found that there seemed to be a problem when combining a marquee TextView with another TextView periodically updated using a Handler.
After some tests we've found that this strange behaviour only happens when the TextViews are iside a RelativeLayout and everything woeks as expected when the enclosing layout is a LinearLayout. Thus, the solution (more a workaround) is to change the enclosing layout to be a Linearlayout.
I have an activity that contains a fragment and the fragment contains a customview. The customview contains an edittext and directly below it a recyclerview with height of match_parent. When the user gets focus on the edittext a softinput keyboard appears.. unfortunately this keyboard hides half the recyclerview underneath it. I want the recyclerview to resize to the remaining visible height of the screen (under the edittext) so that items in the list aren't hidden by the keyboard.
I've tried setting adjustresize for the activity in the manifest but it has no effect:
android:windowSoftInputMode="adjustResize"
I've tried setting my app theme to a base theme (in case something in my custom theme was causing a problem) - no effect:
android:theme="#style/Theme.AppCompat.NoActionBar"
I've checked to make sure there are no framelayouts anywhere down the hierarchy to the view that has to be resized (I read an SO thread that it could be a problem) - there are none.
I've checked the activity, the fragment, the customview and every adapter connected to make sure that I'm not calling getWindow anywhere (I'm not).
The customview is inflated at runtime from a viewstub. Could this have a connection?
Edit:
contents of the parent:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/parent_root"
android:layout_width="match_parent"
android:layout_height="match_parent"
>
...
<ViewStub
android:id="#+id/viewstub_outername"
android:layout="#layout/viewstub_thatIneed"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
</RelativeLayout>
contents of the viewstub:
<RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:id="#+id/viewstub_root"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:background="#android:color/white"
>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview1"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
<android.support.v7.widget.RecyclerView
android:id="#+id/recyclerview2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_marginTop="20dp"
android:visibility="invisible"
/>
</RelativeLayout>
Showing the keyboard thusly:
public static void showSoftKeyboard(Activity activity) {
View view = activity.getCurrentFocus();
InputMethodManager inputManager = (InputMethodManager) activity.getSystemService(Context.INPUT_METHOD_SERVICE);
inputManager.showSoftInput(view, InputMethodManager.SHOW_IMPLICIT);
}
Don't do anything in manifest file. Just use
((LinearLayoutManager)recyclerView.getLayoutManager()).setStackFromEnd(true);
This will take care of recyclerview resizing.
window.decorView.setOnApplyWindowInsetsListener { view, insets ->
val insetsCompat = toWindowInsetsCompat(insets, view)
mainNavigation.isGone = insetsCompat.isVisible(ime())
view.onApplyWindowInsets(insets)
}
The project I have includes a main activity that displays a dialog with a listview that uses an adapter to add each element from an XML file. This includes a seekBar. I want to be able to listen to seekBar events in the activity. For the checkboxes in the listView I was able to add the listener directly in the XML and then just implement it in the activity, but I haven't found a way to do this for the seekBar.
The other obvious option would be set the onSeekBarChanged listener, passing it the activity in the Adapter code and extend onSeekBarChangedListener in the activity. The problem is I can't figure out how to pass the activity to the adapter.
Here's the XML for the list view items
<?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"
android:orientation="vertical" >
<CheckBox
android:id="#+id/checkBox"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="placeholderText"
android:onClick = "layerChecked" />
<SeekBar
android:id="#+id/seekBarOpacity"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="1000" />
</LinearLayout>
I managed to this the second way. I implemented the listener in the activity, grabbed the context from the constructor of the array adapter, stored it in a private class variable and then set the context (cast as a listener) as the seekBarListener.
private Context activity;
public LayersAdapter(Context context, ArrayList<Layer> layers){
super(context, R.layout.item_layout, layers);
activity = context;
}
and then
opacityBar.setOnSeekBarChangeListener((OnSeekBarChangeListener) activity);
at the moment I'm calling and building fragments like this:
if (getSupportFragmentManager().findFragmentById(R.id.fragment_list) == null) {
list = new MyListFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_list, list).commit();
}
But I wonder if this is so called best practice, because this seems to me to be much boilerplate code. Are there better ways?
Use XML layouts and Fragment classes. Here I create a layout with 2 fragments. The class inflates the layout fragment_actionbarcompat.xml (that code isn't shown here but it's a basic layout file). And I create a layout file for the activity that houses the 2 fragments.
The ActionBarCompatFragment class overrides the onCreateView method to inflate it's layout. That gets injected into the fragment tag layoutwise.
In your case normally you don't just add in a plain ListFragment, you extend ListFragment and add your custom code into it. It's a way of better supporting fancy patterns like Model-View-Controller. Fragments are meant to be isolated compartments so you can reuse them between activities if you'd like. In most cases your class will hold the logic to load the data that the fragment needs.
ActionBarCompatFragment.java
#Override
public final View onCreateView(LayoutInflater inflater, ViewGroup root, Bundle savedInstanceState) {
final int layoutId = R.layout.fragment_actionbarcompat;
return inflater.inflate(layoutId, root, false);
}
File: activity_main.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" >
<fragment
android:id="#+id/ActionBarCompatFragment"
android:layout_width="#dimen/ActionBarSize"
android:layout_height="match_parent"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
class="com.packagename.app.ActionBarCompatFragment" >
<!-- Preview: layout=#layout/fragment_actionbarcompat -->
</fragment>
<fragment
android:id="#+id/ComposerFragment"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentTop="true"
android:layout_toRightOf="#id/ActionBarCompatFragment"
class="com.packagename.app.ComposerFragment" >
<!-- Preview: layout=#layout/fragment_composer -->
</fragment>
</RelativeLayout>