I want to have a slider that is open when the app starts. It will be open with buttons and such and when the user closes it, there will be more buttons to access. Is this possible with a sliding drawer? What would I add to the onCreate() method?
Thanks
XML Layout - In a basic LinearLayout:
<SlidingDrawer
android:id="#+id/slide"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:content="#+id/content"
android:handle="#+id/handle"
android:orientation="vertical"
android:scrollbars="vertical" >
<LinearLayout
android:id="#id/handle"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:gravity="center"
android:orientation="horizontal" >
<ImageView
android:id="#+id/handleImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:src="#drawable/ic_tray_expand" />
<Button
android:id="#+id/handleButton"
android:layout_width="wrap_content"
android:layout_height="match_parent"
android:background="#drawable/btn"
android:text="Up me" />
</LinearLayout>
<LinearLayout
android:id="#+id/content"
android:paddingTop="2dp"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#013E53"
android:gravity="center"
android:orientation="vertical" >
<TextView
android:id="#+id/tv_commentDisplay"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:gravity="left"
android:paddingLeft="10dp"
android:textSize="20dp" />
</LinearLayout>
</SlidingDrawer>
And your Activity will looks like this:
public class Home extends Activity implements OnDrawerScrollListener
{
private ImageView handleImage;
private Button handleButton;
private SlidingDrawer slide;
private TextView tv_commentDisplay;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
requestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.main);
tv_commentDisplay = (TextView)this.findViewById(R.id.tv_commentDisplay);
handleImage = (ImageView)this.findViewById(R.id.handleImage);
handleButton = (Button)this.findViewById(R.id.handleButton);
slide = (SlidingDrawer)this.findViewById(R.id.slide);
slide.open(); // not sure
slide.setOnDrawerScrollListener(this);
handleButton = ((Button)this.findViewById(R.id.handleButton));
tv_commentDisplay.setText("Hello World");
}
#Override
public void onScrollEnded() {
}
#Override
public void onScrollStarted() {
if (slide.isOpened())
handleImage.setImageResource(R.drawable.ic_tray_collapse);
else {
handleImage.setImageResource(R.drawable.ic_tray_expand);
}
}
Use open() in your onCreate(), it will open the drawer immediately.
You can take a look at the full API here
Related
Hello good morning in my view i cant handled the setOnItemClickListener
I am trying to create an application which dynamically lists installed applications in a gridview.I am able to display the applications in a gridview form but not able to make these items clickable. Nothing happens when i click these items. The code inside the setOnItemClickListener does not get triggered on click event.
<?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"
tools:context=".Tab2profile">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:ignore="UselessParent">
//my scrollview
<ScrollView
android:layout_width="match_parent"
android:fillViewport="true"
android:layout_height="match_parent">
<RelativeLayout
android:id="#+id/MainRLyout"
android:layout_width="match_parent"
android:orientation="vertical"
android:layout_height="wrap_content">
<RelativeLayout
android:id="#+id/girislayout"
android:layout_alignParentTop="true"
android:layout_width="match_parent"
android:layout_height="wrap_content">
<TextView
android:id="#+id/usernameTv"
android:layout_alignParentStart="true"
android:layout_width="wrap_content"
android:layout_margin="10dp"
android:layout_centerVertical="true"
android:textSize="#dimen/text_size_Orta"
android:textColor="#color/cardview_dark_background"
android:layout_height="wrap_content"
tools:ignore="PrivateResource" />
<Button
android:id="#+id/Login_LogOut_Button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:minHeight="0dp"
android:layout_centerVertical="true"
android:layout_alignParentEnd="true"
android:layout_marginStart="5dp"
android:layout_marginLeft="5dp"
android:layout_marginEnd="5dp"
android:background="#drawable/logout_rounded"
android:drawableRight="#drawable/logout_icon"
android:padding="0dp"
android:text="#string/Logout_Text_str"
android:textColor="#android:color/white"
android:textSize="12sp"
tools:ignore="RelativeOverlap,RtlHardcoded" />
</RelativeLayout>
//in here i cant handled the item click funktşon
<syr13.comapp4.Custum_Adapters.ExpandableHeightGridView
android:id="#+id/Your_Apps_Li"
android:numColumns="3"
android:layout_below="#id/Your_Apps_Tv"
android:horizontalSpacing="6dp"
android:layout_margin="5dp"
android:verticalSpacing="6dp"
android:gravity="center"
android:layout_width="match_parent"
android:layout_height="match_parent">
</syr13.comapp4.Custum_Adapters.ExpandableHeightGridView>
</RelativeLayout>
</ScrollView>
</RelativeLayout>
</FrameLayout>
I think you can Change your Code
from
button.setOnItemClickListener(this);
To
button.setOnClickListener(this);
for example :-
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button button = findViewById(R.id.button);
button.setOnClickListener(this);
}
#Override
public void onClick(View v) {
switch (v.getId())
{
case R.id.button:
//Do Something
break;
}
}
}
I have tried to search in android developers but I had no luck.
My onClick method does not print any message. I want to change my text from ... to ...; however nothing happens when I click it.
JAVA
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
/**
* This method is called when the Cookie button is clicked.
*/
public void eatCookie(View view) {
display("I'm so full");
}
/**
* This method displays the given quantity value on the screen.
*/
private void display(String status) {
TextView statusTextView = (TextView) findViewById(
R.id.status_text_view);
statusTextView.setText("" + status);
}
}
XML
<LinearLayout 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="#B388FF"
android:orientation="vertical"
tools:context=".MainActivity">
<ImageView
android:id="#+id/android_cookie_image_view"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="centerCrop"
android:src="#drawable/before_cookie" />
<TextView
android:id="#+id/status_text_view"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginLeft="16dp"
android:layout_marginRight="16dp"
android:layout_marginTop="16dp"
android:text="I'm so hungry"
android:textColor="#android:color/white"
android:textSize="34sp" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="16dp"
android:text="EAT COOKIE"
android:onClick="eatCookie"/>
</LinearLayout>
Your code is working correct in my device
Try to Clean your project and re run the code again.
Build -> Clean
and then run the code
I am trying to make a custom media player where when ever the music start a loader will start and when the music is paused or ends the the loader goes invisible .This is what i am trying to get when the button is clicked ..How to set the desired output.I am using the following gradel
compile 'com.leo.simplearcloader:simplearcloader:1.0.+'
compile 'de.hdodenhof:circleimageview:2.0.0'
my xml code
<?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:layout_width="match_parent"
android:layout_height="match_parent"
xmlns:custom="http://schemas.android.com/apk/res-auto"
tools:context="com.union.playerfront1.MainActivity"
>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="invisible"
android:id="#+id/load1">
<com.leo.simplearcloader.SimpleArcLoader
android:visibility="visible"
android:id="#+id/loader"
android:layout_centerInParent="true"
android:layout_width="200dp"
android:layout_height="200dp"
custom:arc_style="simple_arc"
custom:arc_speed="medium"
custom:arc_margin="3dp"
>
</com.leo.simplearcloader.SimpleArcLoader>
</RelativeLayout>
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent">
<de.hdodenhof.circleimageview.CircleImageView
xmlns:app="http://schemas.android.com/apk/res-auto"
android:id="#+id/profile_image"
android:layout_width="150dp"
android:layout_height="150dp"
android:src="#drawable/profile"
app:civ_border_width="2dp"
app:civ_border_color="#FF000000"
android:layout_centerVertical="true"
android:layout_centerHorizontal="true" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="New Button"
android:id="#+id/button"
android:layout_marginBottom="44dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true" />
</RelativeLayout>
my java code
public class MainActivity extends AppCompatActivity {
boolean isPressed = false;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
final MediaPlayer mediaPlayer =MediaPlayer.create(this,R.raw.song);
final RelativeLayout real=(RelativeLayout)findViewById(R.id.load1);
final Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (isPressed){
button.setBackgroundResource(R.drawable.profile);
real.setVisibility(View.VISIBLE);
mediaPlayer.start();
}
}
});
}
}
How I can change the visibility of the loader on the base that a song is playing or it is stopped I am able to set the button visibility properly.How i can set the visibility on the basis of song pause,song start,song stop?
Hello.. I have a layout in which I have ImageView and EditText view. I want to add a different layout on click of ImageView of arrow.
It should look like this after click event of ImageView. How can I get this??
Pleas help...
For perfect answer please add your xml file and java file source code.
Example
MainActivity.java
public class MainActivity extends Activity {
LinearLayout linear1,linear2;
ImageView imgArrow;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
linear1 = (LinearLayout) findViewById(R.id.linear1);
linear2 = (LinearLayout) findViewById(R.id.linear2);
imgArrow = (ImageView) findViewById(R.id.imgArrow);
linear1.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if(linear2.getVisibility() == View.GONE){
linear2.setVisibility(View.VISIBLE);
imgArrow.setImageResource(R.drawable.up);
}else{
linear2.setVisibility(View.GONE);
imgArrow.setImageResource(R.drawable.down);
}
}
});
}
}
activity_main.xml
<LinearLayout 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:orientation="vertical" >
<LinearLayout
android:id="#+id/linear1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal" >
<ImageView
android:id="#+id/btnClick"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_weight="0.3"
android:src="#drawable/ic_launcher" />
<TextView
android:id="#+id/tv1"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:layout_weight="0.5"
android:text="Additional Contact" />
<ImageView
android:id="#+id/imgArrow"
android:layout_width="0dp"
android:layout_height="40dp"
android:layout_weight="0.2"
android:src="#drawable/down" />
</LinearLayout>
<LinearLayout
android:id="#+id/linear2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_margin="10dp"
android:visibility="gone"
android:orientation="vertical" >
<TextView
android:id="#+id/txt1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="User Name here" />
<TextView
android:id="#+id/txt2"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Phone number here" />
<Button
android:id="#+id/btnone"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Click Here" />
</LinearLayout>
</LinearLayout>
Here down and up is image file for arrow.
so put that both arrow file in drawable folder.
You could just set an OnClickListener to the ImageView, which contains the arrow. Then assign a method, which handles the click and produces your different layout
I have the following code:
public class MainActivity extends ActionBarActivity {
EditText userfield;
EditText passfield;
Button logbtn;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
userfield = (EditText)findViewById(R.id.editText);
passfield = (EditText)findViewById(R.id.editText2);
logbtn = (Button)findViewById(R.id.button);
GifWebView view = new GifWebView(this, "file:///android_asset/watg.gif");
view.getSettings().setLoadWithOverviewMode(true);
view.getSettings().setUseWideViewPort(true);
setContentView(view);
userfield.bringToFront();
userfield.invalidate();
passfield.bringToFront();
passfield.invalidate();
logbtn.bringToFront();
logbtn.invalidate();
}
As you can see the GifWebView (extends WebView) is created programatically after the onCreate()
The button and EditTexts are made in the Activity.xml but are brought to front programatically in the Java file.
Why doesnt the above code work?
EDIT - Frame Layout Problem
My XML Code
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
<com.example.dencallanan.giftestapp.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="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" 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=".MainActivity">
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText"
android:layout_marginBottom="153dp"
android:layout_alignParentBottom="true"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:hint="USERNAME" />
<EditText
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="#+id/editText2"
android:layout_marginTop="58dp"
android:layout_alignTop="#+id/editText"
android:layout_alignParentRight="true"
android:layout_alignParentEnd="true"
android:layout_alignLeft="#+id/editText"
android:layout_alignStart="#+id/editText"
android:hint="PASSWORD" />
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Log In"
android:id="#+id/button"
android:layout_below="#+id/editText2"
android:layout_centerHorizontal="true"
android:layout_marginTop="25dp" />
</RelativeLayout>
</FrameLayout>
The Error:
You are calling setContentView(R.layout.activity_main) and again later setContentView(view). setContentView will overwrite the layout, this will in effect, replace it with a new layout. I suggest using a FrameLayout in XML file... Webview will be your bottom layer. On top of that Put a Linear/RelativeLayout with the Edittexts and Button... Simple and clean.
You can add your webview in XML like:
<com.package.GifWebView android:id="#+id/GWebView"
android:layout_width="match_parent"
android:layout_height="match_parent"
/>
In code, access this as:
GifWebView gWView = (GifWebView) findViewById(R.id.GWebView);
gWView = new GifWebView(this, "file:///android_asset/watg.gif");